Lab 3: Deploy your first application

Learn how to deploy an application to a Kubernetes cluster.

Once your client is configured, you are ready to deploy your first application, k8sdemo.

The frontend example application

In this part of the lab we will deploy an application called k8sdemo that has already been built and uploaded to DockerHub under the name niklaushirt/k8sdemo.

We will use the following yaml:

kind: Deployment
metadata:
  name: k8sdemo
  namespace: default
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: k8sdemo
    spec:
      containers:
      - name: k8sdemo
        image: niklaushirt/k8sdemo:1.0.0
  	...
          env:   
          - name: PORT
            value : "3000"
          - name: APPLICATION_NAME
            value: k8sdemo
          - name: BACKEND_URL
            value: http://k8sdemo-backend-service.default.svc:3000/api

It defines the Container image to deploy (with 1 instances/replicas) and an environment variable BACKEND_URL that points to the backend servcie we will be deploying in the next section.

If you wish you can have a look at the complete yaml file:

gedit ~/training/deployment/demoapp.yaml 

Deploy the frontend application

  1. Start by running k8sdemo

    kubectl create -f ~/training/deployment/demoapp.yaml
    

    ``

    This action will take a bit of time. To check the status of the running application, you can use kubectl get pods.

    You should see output similar to the following:

 kubectl get pods -n default 
 
 > NAME                       READY     STATUS    RESTARTS   AGE
 > k8sdemo-7d46f69d68-bd2cw   0/1       Running   0          17s  
  1. Eventually, the status should show up as 1/1 Running.

     kubectl get pods -n default 
        
     > NAME                          READY     STATUS              RESTARTS   AGE
     > k8sdemo-7d46f69d68-bd2cw      1/1       Running             0          5m
    

    The end result of the run command is to create a Deployment resource that manages the lifecycle of those pods.

  2. Once the status reads Running, we need to expose that deployment as a service so we can access it through the IP of the worker nodes. The k8sdemo application listens on port 3000.

    Run:

    kubectl expose deployment k8sdemo --name k8sdemo-service -n default --type="NodePort" --port=3000
          
    > service "k8sdemo-service" exposed
    

    ``

  3. To find the port used on that worker node, examine your new service:

     kubectl get service -n default k8sdemo-service
        
     > NAME              TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
    > k8sdemo-service   NodePort   10.109.30.227   <none>        3000:30931/TCP   11m
    

    ``

We can see that our in this example<nodeport> is 31208. We can see in the output the port mapping from 3000 inside the pod exposed to the cluster on port 30931. This port in the 31000 range is automatically chosen, and will probably be different for you.

  1. k8sdemo is now running on your cluster, and exposed to the internet.

    Training VM

    If you are using the training VM, you can open the webpage directly by typing:

    minikube service k8sdemo-service
    

    where k8sdemo-service is the name of the exposed kubernetes service.


    Cloud/Standalone

    If you are NOT using the training VM, you can open the webpage by forwarding a local port to the service in your Cluster:

    kubectl port-forward --namespace default $(kubectl get po -n default -l app=k8sdemo | grep k8sdemo | \awk '{print $1;}') 3000:3000 >> /dev/null &
    

    And then navigating to http://localhost:3000/ in your browser.

  2. Your k8sdemo should now be open in the your default browser. However it will show an error, because we have not yet deployed the backend.

    Testing DEMO_API STATUS: ERROR Trying to reach backend ….

Deploy the application backend

In this part of the lab we will deploy the application backend called k8sdemo-backend that has already been built and uploaded to DockerHub under the name niklaushirt/k8sdemo-backend.

  1. Start by running k8sdemo-backend

    kubectl create -f ~/training/deployment/demoapp-backend.yaml
    

    ``

    This action will take a bit of time. To check the status of the running application, you can use kubectl get pods.

    You should see output similar to the following:

 kubectl get pods -n default
 
 > NAME                              READY     STATUS    RESTARTS   AGE
  > k8sdemo-7d46f69d68-xcgcw          0/1       Running   0          13m
  > k8sdemo-backend-9c777544b-cp59q   0/1       Running   0          1m
  > k8sdemo-backend-9c777544b-jqjz9   0/1       Running   0          1m  

Eventually, the status should show up as 1/1 Running.

 kubectl get pods -n default
 
 > NAME                              READY     STATUS    RESTARTS   AGE
  > k8sdemo-7d46f69d68-xcgcw          1/1       Running   0          13m
  > k8sdemo-backend-9c777544b-cp59q   1/1       Running   0          6m35s
  > k8sdemo-backend-9c777544b-jqjz9   1/1       Running   0          6m35s

The end result of the run command is to create a Deployment resource that manages the lifecycle of those pods.

  1. Once the status reads Running, we need to expose that deployment as a service so we can access it through the IP of the worker nodes. The k8sdemo application listens on port 3000. Run:
 kubectl expose deployment k8sdemo-backend --name k8sdemo-backend-service -n default --type="NodePort" --port=3000
 
 > service "k8sdemo-service" exposed
  1. Now reload the webpage and verify, that it shows

    Testing DEMO_API STATUS: OK ….

  2. If you reload the webpage several times, you should see, tht the IP Address of the backend API Pod is changing between the two Pods that have been spun up.


**Congratulations!!! This concludes Lab 3 on deploying a web application to Kubernetes ** We will be using this deployment in the following Labs.