Skip to content

Lab 6: Deploying on Kubernetes

What am I about to learn?

Today's lab session focuses on Kubernetes! We will build a Kubernetes cluster and deploy a containerized application.

Tip

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla et euismod nulla. Curabitur feugiat, tortor non consequat finibus, justo purus auctor massa, nec semper lorem quam in massa.

Lorem ipsum dolor sit amet, (1) consectetur adipiscing elit.

  1. :man_raising_hand: I'm an annotation! I can contain code, formatted text, images, ... basically anything that can be expressed in Markdown.

The Bonus Lab focuses on how to:

  • Deploy Kubernetes on the Google Kubernetes Engine (GKE) in GCP.
  • Run the basic commands to interact with and push Docker images to Docker Hub.
  • Deploy images from Docker Hub to Kubernetes.
  • Horizontally scale images to support increased traffic using a load-balancing service.

You will need to watch the following video to follow the step-by-step commands.

Take your time and make sure you double-check the commands before you run them.

  • The following video demonstrates the commands used in this tutorial.

Watch the video

You should run this tutorial on your GCP VM and the Google Cloud Shell ✅

  • To run this tutorial, you will need a GCP VM; you must use the Docker VM that we created in Lab 5.

  1. Go to Docker Hub and create a new account. We will use Docker Hub to push our images to deploy on Kubernetes.

  2. Create a new public repository.

    • Add a name and a description; your Docker Hub username is in the top-right corner of your screen.

    dockerhub

  3. Go back to your Docker VM (from Lab 5) and log in to Docker Hub using your Docker user. If you deleted your VM, create a new one and install Docker.

    In the VM, do not forget to switch to docker-user.
    The command is su - docker-user; this ensures that you log in and switch to the docker-user home directory.

  4. Feel free to use the mini-hi repo or create your own.

    • If you create your application, you will need to create a new repo, push your code, and clone it in the VM.
    • In my case, I used the public repo mini-hi.git.
    $ git clone --branch master https://github.com/steliosot/mini-hi.git
    

    If you want, you can install Docker on your own computer.
    * Link: Docker Desktop
    The mini-hi app is a simple Node.js server with a Hello World message.

    const express = require('express')
    const app = express()
    
    app.get('/', (req, res) => {
        res.send('Hello World! Cloud@Birkbeck is fun!')
    })
    
    app.listen(3000)
    
  5. Move into the project directory.

    $ cd mini-hi/
    
  6. The Dockerfile is already there (I created it for you 😊). Examine it:

    FROM alpine
    RUN apk add --update nodejs npm
    COPY . /src
    WORKDIR /src
    EXPOSE 3000
    ENTRYPOINT ["node", "./app.js"]
    
  7. Build your image.

    • Image name format: <DockerHub-username>/<image-name>:<version>
    $ docker image build -t steliosot/mini-hi:1 .
    
  8. Push your image to Docker Hub.

    $ docker push steliosot/mini-hi:1
    

    Refresh your Docker Hub page; your image should now appear.

  9. Search for your image.

    $ sudo docker search steliosot
    
    • 0 stars yet 😅 but it’s a start 😄
  10. Create a Kubernetes cluster (GKE Standard) in Google Cloud.

    • Give it a meaningful name, e.g. my-gke-cluster.
    • By default, we deploy 3 nodes.
    • This may take a few minutes—sit tight!
  11. Activate the Cloud Shell, connect to your cluster, and copy the connect command:

    $ gcloud container clusters get-credentials stelios-cluster13 --zone us-central1-c --project lab-7-270015
    

    The gcloud command is part of the Google Cloud CLI used to manage resources.

  12. Run the following commands to check your connection.

    $ kubectl get nodes
    $ kubectl cluster-info
    
  13. Run a container using the mini-hi image.

    $ kubectl run mini-hi-pod --image=steliosot/mini-hi:1
    
  14. Check if the pod is running.

    $ kubectl get pods
    

    Wait until you see STATUS Running; repeat the command if necessary.

  15. Describe the pod for more details.

    $ kubectl describe pods
    

    The displayed IP is internal to the cluster.

  16. Create a deployment file named mini-hi-deployment.yaml.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: mini-hi-deployment
      labels:
        app: minihi
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: minihi
      template:
        metadata:
          labels:
            app: minihi
        spec:
          containers:
          - name: minihi
            image: steliosot/mini-hi:1
            imagePullPolicy: Always
            ports:
            - containerPort: 3000
    

    YAML is a human-readable format for configuration files.
    It stands for YAML Ain’t Markup Language.

  17. Delete your previous pod.

    $ kubectl delete pods mini-hi-pod
    
  18. Apply the deployment file.

    $ kubectl apply -f mini-hi-deployment.yaml
    
  19. Get pods again.

    $ kubectl get pods
    
  20. Scale the deployment. Edit the file and change replicas: 3replicas: 10.

    $ kubectl apply -f mini-hi-deployment.yaml
    
  21. View pod placement across nodes.

    $ kubectl get pods -o wide
    
  22. Create a service file named mini-hi-service.yaml.

    apiVersion: v1
    kind: Service
    metadata:
      name: mini-hi-service
      labels:
        app: mini-hi-service
    spec:
      type: LoadBalancer
      ports:
      - name: http
        port: 80
        protocol: TCP
        targetPort: 3000
      selector:
        app: minihi
      sessionAffinity: None
    
  23. Apply the service.

    $ kubectl apply -f mini-hi-service.yaml
    
  24. Check services to find your external IP.

    $ kubectl get services
    

    Open that IP in your browser to view the running app.

  25. Update your app (new Hello World message), rebuild and push as version 2.

    $ docker build -t steliosot/mini-hi:2 .
    $ docker push steliosot/mini-hi:2
    
  26. Edit the deployment to use the new image and scale to 15 replicas.

    $ kubectl edit deployment mini-hi-deployment
    

    Change:
    replicas: 15
    image: steliosot/mini-hi:2

  27. Check pods again.

    $ kubectl get pods
    

    You’ll see some pods terminating and new ones starting with the updated image.

  28. Refresh your browser — your updated app is now live! 🎉

  29. Remember to delete your cluster when finished to avoid extra costs.

    $ gcloud container clusters delete my-gke-cluster
    

Result: MkDocs will automatically render proper numbering (1, 2, 3 …) without breaks, while keeping bullets, code blocks, and quotes exactly as expected.