Launching your Kubernetes Cluster with Deployment

2 min read

Cover Image for Launching your Kubernetes Cluster with Deployment

What is Deployment in k8s

A Deployment provides a configuration for updates for Pods and ReplicaSets.

Task-1:

Create one Deployment file to deploy a sample todo-app on K8s using the "Auto-healing" and "Auto-Scaling" features.

  • First, we have to make a deployment Yaml file. We can use deployment.yaml file provided

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: todo-app
        labels:
          app: todo
      spec:
        replicas: 2
        selector:
          matchLabels:
            app: todo
        template:
          metadata:
            labels:
              app: todo
          spec:
            containers:
            - name: todo
              image: rishikeshops/todo-app
              ports:
              - containerPort: 3000
    

    or we can build an image first with docker and push the image in docker Hub and write a new YAML file.

    Let's create it from scratch.

    • Fork the repo from GitHub

      https://github.com/sri766/django-todo-cicd

    • Clone it to the local system

      git clone <repo_url>

    • build the image with the docker

        docker build -t <docker_hub_username>/<name>:<tag> .
      
        #eg: docker build -t sri766/django-todo:latest .
        #note: name must be in lower_case
      

    • now run the docker run command to up the container

        docker run -d -p 8000:8000 sri766/django-todo:latest
      

    • and push the image to DockerHub docker push <image_name>:<tag>

    • now, cd to k8s, and check the image to your repo image. eg: sri766/django-todo:latest

    • now we have to create a pod for eg: (we don't have to create another pod.yaml file it already created in k8s folder). just run kubectl apply -f pod.yaml

        #template to create a pod 
        apiVersion: v1
        kind: Pod
        metadata:
          name: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.14.2
            ports:
            - containerPort: 80
      

      Now, we have to create a development. Its main feature is auto-scaling and auto-healing.

      • just write kubectl apply -f development.yaml you will see 3 containers running simultaneously.

      • we can scale it with replica set

        kubectl scale --replicas=3 deployment/my-deployment

      • auto-healing: if you delete a pod deployment create a new pod automatically. As you can see in this example:

Thanks !! for your patient reading ๐Ÿ’•

Happy learning !