Question & Answer
Question
Answer
The steps below describe how to create a basic application in a docker container that is deployed in a Kubernetes Cluster. This information is a starting point for more complex scenarios. For a more detailed example, review the tutorial Creating Kubernetes clusters.
Steps:
- Download and install the IBM Cloud CLI.
- Download the Kubernetes CLI.
- Install the container service plug-in by using the following command:
ibmcloud plugin install container-service -r "IBM Cloud"
- Create the cluster by using the process on the Deploy, scale, and manage your containerized application workloads page. You can create a free cluster for testing purposes.
- Log in to IBM Cloud by using the ibmcloud login command.
- Select the account where you created the cluster.
- Use the ibmcloud cr namespace-list command to verify that you have already set a namespace for the IBM Docker images registry. If you do not have any namespace, you can create one by using the following command: ibmcloud cr namespace-add <NAMESPACE>
- Build a Docker image in IBM Cloud, or use a default image. You can retrieve a Dockerfile example called dWTVSimpleContainerApp from GitHub. The example uses the node docker image and a basic node.js application. To build a docker image starting from a Dockerfile in the IBM Cloud registry, complete the following steps:
- Go in the directory where you have the Dockerfile. If you are using the previous Dockerfile example from the GitHub repository, go in dWTVSimpleContainerApp directory.
- Run the following command:
ibmcloud cr build -t <region>.icr.io/<YourNamespace>/<imageName>:<tag>
ibmcloud cs cluster-config <yourClusterName>
- Go in the directory where you have the Dockerfile. If you are using the previous Dockerfile example from the GitHub repository, go in dWTVSimpleContainerApp directory.
- Export the environment variables that were retrieved by using the previous command to start using Kubernetes.
- Run the following commands to use kubectl to run the image in a cluster(POD):
kubectl run myapplication --image=<region>.icr.io/<YourNamespace>/<imageName>:<tag>
kubectl expose deployment/myapplication --type=NodePort --name=myapplication-service --port=<EXPOSED_PORT>
Note: The EXPOSED_PORT variable is the port that you are exposing externally, which is defined in the Dockerfile. For example, if you have EXPOSE 6006 in the docker file, put 6006 as --port parameter value.
You can access to your application with <public_ip>:<port>
You can retrieve the Public IP with the ibmcloud cs workers command.
The output is similar to the information in the following table.
ID | Public IP | Private IP | Machine Type | State | Status | Zone | Version |
kube-mil01-pac294be99dd854f038267d7c4496fea19-w1 | 159.122.183.170 | 10.144.186.73 | free | normal | Ready | mil01 | 1.9.7_1512 |
You can retrieve the port with the kubectl describe service myapplication-service command.
The output will be similar to the following response:
Name: myapplication-service
Namespace: default
Labels: run=myapplication
Annotations: <none>
Selector: run=myapplication
Type: NodePort
IP: 172.21.246.9
Port: <unset> 6006/TCP
NodePort: <unset> 30910/TCP
Endpoints: 172.30.245.71:6006
Session Affinity: None
Events: <none>
The field that you need to copy from the output of the command is NodePort. The value is a randomly assigned port number that Kubernetes gives to this service.
In the example, you can access to the application with 159.122.183.170:30910
Was this topic helpful?
Document Information
More support for:
IBM Cloud Kubernetes Service and Red Hat OpenShift on IBM Cloud
Software version:
All Versions
Document number:
963472
Modified date:
22 November 2019
UID
ibm1KB0011007