The purpose of this project is to deploy a simple node.js application to Kubernetes on IBM Cloud.
-
You are going to work with your command line/terminal. On MacOS, look for Terminal. On Windows, look for Command Prompt (Command line), and if you have Linux, you will surely know :)
-
Install git for Windows, git for Linux, or git for macOS
-
Install Docker of version at least 19.03.13
-
Install IBM Cloud CLI
-
Install IBM Cloud CLI plugin for Kubernetes Service
$ ibmcloud plugin install kubernetes-service
-
Install IBM Cloud CLI for Container Registry
$ ibmcloud plugin install container-registry
-
Clone the repository
Open command line and navigate to your preffered folder/directory.
Examle:
$ cd Desktop/MyProjects
Clone this repository:
$ git clone https://github.com/IBM/CZ-IBM-Cloud-Guide.git
If you are facing any troubles, look at the official documentation for cloning a repository
-
Create IBM Cloud account here. To create an account follow the tutorial in the IBM Cloud Guide in chapter 4.
-
Login to your IBM Cloud account here
-
Create
Kubernetes Service
in IBM Cloud here- As
Pricing plan
chooseFree
- Optionally change
Cluster name
andResource group
- Click
Create
- As
-
Create
namespace
inContainer Registry
here- Before creating the namespace, choose your preffered location
- Optionally change
Resource group
- Fill in
Name
- Click
Create
-
Login to IBM Cloud
$ ibmcloud login -sso
Output:
Paste
y
to get one time password in browser and then copy and paste the one time password. Otherwise open the URL in browser manually.API endpoint: https://cloud.ibm.com Region: eu-de Get a one-time code from https://identity-2.uk-south.iam.cloud.ibm.com/identity/passcode to proceed. Open the URL in the default browser? [Y/n] >
-
Login to the Container Registry
$ ibmcloud cr login
Output:
Logging in to 'registry.eu-de.bluemix.net'... Logged in to 'registry.eu-de.bluemix.net'. Logging in to 'de.icr.io'... Logged in to 'de.icr.io'. OK
The most important issue here is to login to the correct Container Registry. Note, that in the output is
Logged in to 'de.icr.io'
. Thede.icr.io
is specific for a Container Registry located in Frankfurt. For example, Contrainer Registry in London would beuk.icr.io
or Global Container Registry isicr.io
. You will be automatically logged in to the registry in as is in your ibm cloud target. You can see the target by runningibmcloud target
after logging to ibm cloud account. -
Get your Kubernetes cluster context
$ ibmcloud ks cluster config --cluster <your_cluster_id>
In order to be able to deploy an application to your cluster, you need to store the context of the cluster locally, for which you need to obtain a Cluster ID. Therefore, go to the Kubernetes Cluster in the user interface in the browser via the upper left menu, select Resource list, then click on Clusters and select your cluster. Here you can see and copy the Cluster ID.
-
Create Docker Image
Navigate to the root of this project. To make sure you are in the root paste the following command and see if you can see your repository:
For MacOs/Linux
$ ls
For Windows:
$ dir
Output:
Dockerfile README.md app deployment.yml
To create a docker image, run the following command:
$ docker build -t <provider>/<my_namespace>/<my_repo>:<my_tag> .
The
<provider>
is a name of the container registry provider. Depending on your provider choose the correct name. In case of the container registry in IBM Cloud it depends on the location of your registry. Make sure you are using the location, which your namespace is created in. To obtain the correct value, go to images in IBM Cloud, choose your location and click create.The
<my_namespace>
is the namespace you have created in Container Registry. Paste the name of a namespace, that you have created.The
<my_repo>
is a name of a repository in Repositories. The repository will be created automatically, so there is no need to create it. You can use any value for<my_repo>
.The
<my_tag>
can either contain any text or number value. For example, you can paste a version of this image like0.0.1
orlatest
.Make sure, the docker is running.
The tag is now visible by running this command:
$ docker image ls
-
Push Docker Image to your Container Registry
$ docker push <provider>/<my_namespace>/<my_repo>:<my_tag>
The created Docker Image needs to be pushed to the Container Registry in order for Kubernetes Service to retrieve this Docker Image. Your image will now be displayed in the IBM Cloud Container Registry under images.
-
Update the image tag (name) in
deployment.yml
file instead of*
spec: containers: - name: nodejs image: * imagePullPolicy: Always ports: - containerPort: 3000
The image tag(name) is the value, that you used while building and pushing the docker image:
<provider>/<my_namespace>/<my_repo>:<my_tag>
-
Deploy the application into Kubernetes cluster
The file
deployment.yml
in the root of the project contains a script, which processes the deployment automatically. The objective is to apply this deployment template.$ kubectl apply -f deployment.yml
-
Navigate to your
Kubernetes cluster
in IBM Cloud, click onWorker nodes
and copy thePublic IP
.Paste the IP adress to your browser and add nodePort
30080
.The port can be changed in
deployment.yml
file.Example:
http://159.122.177.240:30080
Congratulations! Now you are able to see the application in browser!
You might face issues while working on this guide. Here are the most common ones and the solution.
Do you get a similar error after running docker build
or docker push
?
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
The reason is, that your docker is not running. Try to open the application or follow the official documentation
Do you get this error?
➜ CZ-IBM-Cloud-Guide git:(main) docker push icr.io/some-namespace/some-repo:1.0
The push refers to repository [icr.io/some-namespace/some-repo]
035d751a3458: Preparing
cf0c0a876640: Preparing
c566b09f3beb: Preparing
e62349c14338: Preparing
ca44545b101f: Preparing
b26bc695d405: Waiting
1a058d5342cc: Waiting
unauthorized: The login credentials are not valid, or your IBM Cloud account is not active.
Solution:
-
Make sure, you are logged in to
ibmcloud
by running the following command:ibmcloud target
-
Make sure you have logged in to Container Registry
ibmcloud cr login
-
Check if the Container Registry that you are logged into matches the location, where your namespace is created. For example, if your namespace is located in Frankfurt, you need to login to the Container Registry
de.icr.io
.