How to create a deployment in Kubernetes?


What is Deployment?

A Deployment provides declarative updates for Pods and ReplicaSets. You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets or to remove existing Deployments and adopt all their resources with new Deployments.

Use cases:

  1. To maintain high availability.

  2. Easy to scale.

  3. Maintains the desired state of the application.

  4. Easy to update and revert the application.


Machine used in this example:

  • Deployed a Kubernetes cluster on the VM machine.

  • One node cluster (One master node and one worker node)

  • Kubernetes version:- v1.27.2

  • OS:- ubuntu 20.04.6 LTS

  • Container runtime:- containerd


Creating a Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: httpd 
  name: httpd 
spec:
  replicas: 4 #number of pods created after deployment is created
  selector:
    matchLabels:
      app: httpd 
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: httpd 
    spec:
      containers:
      - image: docker.io/httpd #container-image
        name: httpd #container-name
        ports:
        - containerPort: 80 #container-image-port
        resources: {}
status: {}

Save this yaml file on your k8s master.


Create a Deployment using file httpd-dep.yml

kubectl apply -f (file-name)

We can use apply and create to create a kind of resource in k8s.

The major difference between create and apply is as follows:-

  1. Create: The "create" command is used to create new resources in Kubernetes. When you use the "create" command, you provide a YAML or JSON file that describes the desired state of the resource you want to create. The command creates the resource based on the provided file, generating a new Kubernetes object in the cluster. If a resource with the same name already exists, the "create" command will return an error.

  2. Apply: The "apply" command is used to create or update resources in Kubernetes. It allows you to apply changes to existing resources by specifying a YAML or JSON file that contains the desired state of the resource, similar to the "create" command. However, the key difference is that the "apply" command will make the necessary updates to the existing resource, rather than returning an error for an existing resource.

    The "apply" command detects the differences between the desired state described in the file and the current state of the resource in the cluster. It updates the resource to match the desired state, adding or modifying fields as necessary. If the resource doesn't exist, the "apply" command will create it.


Get the deployment details:

kubectl get deployment (deployment-name) -o wide --show-labels

In this deployment, there are four pods running which means the desired state for this deployment is set to four replicas.


Get the pods of the deployment:

kubectl get pods


Delete deployment:

kubectl delete deployment (deployment-name)


Bonus:

How to create a deployment using cli

kubectl create deployment (deployment-name) --image=(image-name) --port=(image-port)

This command will create a deployment as per the user-defined image and the port should be the listening port of the image.

For example:- docker.io/httpd port is 80.

How to save the yaml file of the deployment.

kubectl create deployment (deployment-name) --image=(image-name) --port=(image-port) --dry-run=client -o yaml > filename.yml

By running a dry run, you can check for syntax errors, validate resource configurations, verify the intended changes, and identify potential issues or conflicts that may arise when applying the command. This helps in avoiding unintended consequences or mistakes that could impact your running applications or infrastructure.


0
Subscribe to my newsletter

Read articles from Dinesh Jagdish Verma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dinesh Jagdish Verma
Dinesh Jagdish Verma

Passionate about technology and its endless possibilities, I am a BCA graduate who embarked on a journey to explore the world of IT. With a strong foundation in Linux and a keen interest in cloud computing, I delved into the realms of Docker, Podman, and AWS, honing my skills along the way. As a constant learner, I am currently immersing myself in the intricacies of Kubernetes, eager to unlock its full potential for scalable and efficient application deployment. My goal is to leverage this powerful container orchestration platform to streamline DevOps processes and drive seamless digital transformations. With a solid understanding of system administration, containerization, and cloud infrastructure, I am equipped to tackle complex challenges and deliver innovative solutions. I thrive in collaborative environments, leveraging my communication skills to effectively bridge the gap between technical complexities and business objectives. Driven by curiosity and fueled by a growth mindset, I continuously seek out new technologies and industry trends. I am excited to connect with like-minded professionals, exchange knowledge, and contribute to the ever-evolving tech landscape. If you're looking for a dedicated and adaptable IT professional who can navigate the dynamic world of containers, cloud, and beyond, let's connect and embark on a transformative journey together.