IBM Support

How do you create a basic application in a Kubernetes cluster?

Question & Answer


Question

How do you create a basic application in a Kubernetes cluster on IBM Cloud?

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:

  1. Download and install the IBM Cloud CLI.
     
  2. Download the Kubernetes CLI.
     
  3. Install the container service plug-in by using the following command:
    ibmcloud plugin install container-service -r "IBM Cloud"
     
  4. 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.
     
  5. Log in to IBM Cloud by using the ibmcloud login command.
     
  6. Select the account where you created the cluster.
     
  7. 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>
     
  8. 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>
       
  9. Export the environment variables that were retrieved by using the previous command to start using Kubernetes.
     
  10. 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




 

[{"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Product":{"code":"SSJTBP","label":"IBM Cloud Kubernetes Service and Red Hat OpenShift on IBM Cloud"},"Component":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"All Versions","Edition":"","Line of Business":{"code":"LOB21","label":"Public Cloud Platform"}}]

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

Manage My Notification Subscriptions