5 Common Kubernetes Deployment Templates You Need to Know

If you're just getting started with Kubernetes, it can be overwhelming to get your head around all the different deployment templates and configurations available. However, understanding the basics of Kubernetes deployment concepts is essential for building and managing containerized applications.

In this article, we'll cover five of the most common Kubernetes deployment templates you need to know. Buckle up and let's dive in!

1. Deployment

The Deployment template is one of the most common templates for managing application deployments in Kubernetes. It's the primary template for managing updates to containerized applications, such as rolling updates, rollbacks, and scaling up or down.

Deployments provide a declarative way to define the desired state of a deployment and allow Kubernetes to do the heavy lifting to achieve that state. Kubernetes will automatically create or delete resources to maintain the desired state, making it an essential tool for managing containerized applications.

Creating a Deployment

Creating a Deployment in Kubernetes is relatively simple. You start by defining a YAML file that specifies the desired state of your application. For example, consider the following YAML file that deploys a sample application:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

This file tells Kubernetes to create a Deployment called nginx-deployment that will run three replicas of the nginx containers. It also specifies the image and port to use for the container.

To create this Deployment, you can use the kubectl create command as follows:

$ kubectl create -f nginx-deployment.yaml

Once the Deployment is created, you can use kubectl get deployments to see the status of the deployment:

$ kubectl get deployments
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           20s

Updating a Deployment

Updating a Deployment in Kubernetes is straightforward. You can update the YAML file you created earlier with the changes you want to make and then use the kubectl apply command to apply the changes:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 5 # increase the replica count
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.7 # use a newer version of nginx
        ports:
        - containerPort: 80
$ kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx-deployment configured

Rolling Back a Deployment

Another useful feature of the Deployment template is the ability to roll back to a previous version if something goes wrong with an update. Kubernetes keeps track of the previous versions of a deployment, and you can use the kubectl rollout command to roll back to a previous version.

For example, to roll back to the previous version of our nginx-deployment, we can use the following command:

$ kubectl rollout undo deployment/nginx-deployment

Scaling a Deployment

Scaling a Deployment is also effortless in Kubernetes. You can use the kubectl scale command to scale the number of replicas for a deployment:

$ kubectl scale --replicas=5 deployment/nginx-deployment
deployment.apps/nginx-deployment scaled

Deleting a Deployment

To delete a Deployment, you can use the kubectl delete command:

$ kubectl delete deployment/nginx-deployment

2. StatefulSet

A StatefulSet is another Kubernetes template that is used to manage stateful applications that require stable, unique network identities and persistent storage. A StatefulSet creates a set of Pods with unique identities that are created in order and terminated in reverse order.

StatefulSets are useful for applications that require stable and predictable network identities, such as databases or distributed systems. They also provide guarantees for ordered and reliable scaling, upgrading, and deleting of these stateful applications.

Creating a StatefulSet

Creating a StatefulSet is very similar to creating a Deployment. Here's an example YAML file:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: zookeeper
spec:
  replicas: 3
  selector:
    matchLabels:
      app: zookeeper
  template:
    metadata:
      labels:
        app: zookeeper
    spec:
      containers:
      - name: zookeeper
        image: zookeeper:3.6.2
        ports:
        - containerPort: 2181
        - containerPort: 2888
        - containerPort: 3888
      volumes:
      - name: datadir
        persistentVolumeClaim:
          claimName: zookeeper-datadir
  volumeClaimTemplates:
  - metadata:
      name: datadir
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi

This YAML file defines a StatefulSet called zookeeper. It includes replicas, which specifies the number of Pods to create, and other properties such as the template to use for the Pod and the volumes to use for the container.

Updating a StatefulSet

Updating a StatefulSet is similar to updating a Deployment. However, you need to use the kubectl apply or kubectl replace command instead of kubectl apply.

$ kubectl replace -f zookeeper.yaml

Deleting a StatefulSet

To delete a StatefulSet, you need to use the kubectl delete statefulset command:

$ kubectl delete statefulset zookeeper

3. DaemonSet

A DaemonSet ensures that a particular Pod is running on each node in the Kubernetes cluster. This template is useful when you need to run a background process on every node of the cluster, such as a log collection agent, a monitoring agent, or a network proxy.

Creating a DaemonSet

Creating a DaemonSet involves creating a YAML file that specifies the desired state of the Pod, including the template to use for the Pod and the selector to use for the nodes:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
spec:
  selector:
    matchLabels:
      app: fluentd-elasticsearch
  template:
    metadata:
      labels:
        app: fluentd-elasticsearch
    spec:
      containers:
      - name: fluentd-elasticsearch
        image: fluent/fluentd-kubernetes-daemonset:v1.11.1-debian-elasticsearch7-1.0

This YAML file creates a DaemonSet called fluentd-elasticsearch, which runs a single fluentd-elasticsearch container on every node in the cluster.

Updating a DaemonSet

Updating a DaemonSet is similar to updating a Deployment or StatefulSet. You can use the kubectl apply or kubectl replace command to update the YAML file and apply the changes to the cluster:

$ kubectl replace -f fluentd-elasticsearch.yaml

Deleting a DaemonSet

You can delete a DaemonSet using the kubectl delete command:

$ kubectl delete daemonset fluentd-elasticsearch

4. Job

A Job in Kubernetes is a template that is used to create one or more Pods that run to completion. Jobs are useful when you have a batch or a one-time task that needs to be executed in a container.

Creating a Job

Creating a Job is very similar to creating a Deployment. Here's an example YAML file:

apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    metadata:
      name: pi
    spec:
      containers:
      - name: pi
        image: perl
        command: ["perl",  "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never

This YAML file defines a Job called pi. It specifies a template to create a Pod that runs the perl command to calculate the value of pi.

Checking the Status of a Job

Once a Job is created, you can check the status of the Job using the kubectl describe command:

$ kubectl describe job pi
Name:         pi
Namespace:    default
Selector:     controller-uid=9209bdba-0c70-416d-bf68-ba6975293f57
Labels:       controller-uid=9209bdba-0c70-416d-bf68-ba6975293f57
              job-name=pi
Annotations:  <none>
Parallelism:  1
Completions:  1
Start Time:   Thu, 04 Feb 2021 19:19:35 -0700
Pods Statuses:	0 Running / 1 Succeeded / 0 Failed
Pod Template:
  Labels:  controller-uid=9209bdba-0c70-416d-bf68-ba6975293f57
           job-name=pi
  Containers:
   pi:
    Image:      perl
    Port:       <none>
    Host Port:  <none>
    Command:
      perl
      -Mbignum=bpi
      -wle
      print bpi(2000)
  Environment:  <none>
  Mounts:       <none>

Deleting a Job

Once a Job completes, Kubernetes automatically terminates the associated container. You can delete a completed Job using the kubectl delete command:

$ kubectl delete job pi

5. CronJob

A CronJob in Kubernetes is a template that runs a job on a scheduled basis. It's used to run jobs at specific intervals or at specific times of the day, week, or month.

Creating a CronJob

Creating a CronJob is similar to creating a Job. Here's an example YAML file:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

This YAML file creates a CronJob called hello. It specifies a schedule to run the job every minute and a jobTemplate to create a Pod with a busybox container that prints the current date and time and displays a greeting.

Checking the Status of a CronJob

To check the status of a CronJob, use the kubectl get cronjob command:

$ kubectl get cronjob
NAME    SCHEDULE    SUSPEND   ACTIVE   LAST SCHEDULE   AGE
hello   */1 * * * *   False     0        <none>          4m30s

Deleting a CronJob

You can delete a CronJob using the kubectl delete command:

$ kubectl delete cronjob hello

Conclusion

In this article, we covered five of the most common Kubernetes deployment templates: Deployment, StatefulSet, DaemonSet, Job, and CronJob. Each template has its unique features and use cases, and it's essential to understand their differences when managing containerized applications.

By mastering these templates, you'll be able to build and deploy containerized applications with confidence and ensure that your applications are scalable, reliable, and up-to-date. We hope you found this article helpful in your Kubernetes journey!

Editor Recommended Sites

AI and Tech News
Best Online AI Courses
Classic Writing Analysis
Tears of the Kingdom Roleplay
Learn Devops: Devops philosphy and framework implementation. Devops organization best practice
Personal Knowledge Management: Learn to manage your notes, calendar, data with obsidian, roam and freeplane
Learning Path Video: Computer science, software engineering and machine learning learning path videos and courses
ML Ethics: Machine learning ethics: Guides on managing ML model bias, explanability for medical and insurance use cases, dangers of ML model bias in gender, orientation and dismorphia terms
Learn Typescript: Learn typescript programming language, course by an ex google engineer