***Kubernetes***

***Kubernetes***

Kubernetes Workloads (Deployments, Jobs, CronJobs, etc.)

Kubernetes is an open-source platform that helps automate the deployment, scaling, and management of containerized applications. A workload is any containerized application that runs on Kubernetes, and there are several types of workloads that Kubernetes supports. In this article, we will discuss the various types of Kubernetes workloads, including Deployments, StatefulSets, DaemonSets, Jobs, and CronJobs. We will also show you how to deploy each type of workload in a Kubernetes cluster.

🔹Deployments:

Deployments are used to manage the rollout and scaling of replica sets, which are a set of identical pods. Deployments allow you to update your application with zero downtime by using rolling updates. Rolling updates ensure that your application is updated gradually, pod by pod until the desired state is reached. Deployments also allow you to roll back to a previous version of your application if needed.

To deploy a deployment in a Kubernetes cluster, create a YAML file with the following contents:

COPY

COPY

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

This YAML file creates a deployment named "my-deployment" with three replicas. It uses a selector to match the labels of the pods, which are labelled "app: my-app". The deployment template specifies the container image and port to use for each pod.

🔹StatefulSets:

StatefulSets are used to manage stateful applications, such as databases or other applications that require unique network identities and stable storage. StatefulSets provide stable, unique network identities for each pod, and they maintain the order of pod creation and deletion. This ensures that the pods are created and deleted in the same order each time, which is important for stateful applications.

What is a Kubernetes StatefulSet?

To deploy a StatefulSet in a Kubernetes cluster, create a YAML file with the following contents:

COPY

COPY

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: my-statefulset
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  serviceName: my-statefulset
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-container
          image: my-image:latest
          ports:
            - containerPort: 80
          volumeMounts:
            - name: my-volume
              mountPath: /data
  volumeClaimTemplates:
    - metadata:
        name: my-volume
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 1Gi

This YAML file creates a StatefulSet named "my-statefulset" with three replicas. It uses a selector to match the labels of the pods, which are labelled "app: my-app". The StatefulSet template specifies the container image and port to use for each pod, as well as a volume mount for the data directory. The volumeClaimTemplates section specifies a persistent volume claim template that is used to create a volume for each pod.

🔹DaemonSets:

DaemonSets are used to run a single instance of a pod on each node in a Kubernetes cluster. DaemonSets are often used to run monitoring agents or other system-level services that need to be present on each node in the cluster.

An introduction to Kubernetes DaemonSets

To deploy a DaemonSet in a Kubernetes cluster, create a YAML file with the following contents:

COPY

COPY

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: my-daemonset
spec:
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-container
          image: my-image:latest
          ports:
            - containerPort: 80

This YAML file creates a DaemonSet named "my-daemon set" that runs a single instance of a pod on each node in the Kubernetes cluster. It uses a selector to match the labels of the pods, which are labelled "app: my-app". The DaemonSet template specifies the container image and port to use for each pod.

🔹Jobs:

Jobs are used to run batch or one-off tasks in a Kubernetes cluster. Jobs create one or more pods to run the task, and once the task is complete, the pods are terminated. Jobs are often used for tasks such as data processing or running automated tests.

Kubernetes Batch Jobs: A Comprehensive Guide 101 - Learn | Hevo

To deploy a Job in a Kubernetes cluster, create a YAML file with the following contents:

COPY

COPY

apiVersion: batch/v1
kind: Job
metadata:
  name: my-job
spec:
  completions: 1
  template:
    spec:
      containers:
        - name: my-container
          image: my-image:latest
          command: [ "echo", "Hello, World!" ]
      restartPolicy: Never

This YAML file creates a Job named "my-job" that runs a single pod to execute the "echo" command. The Job specifies that it should run only one time, and it uses the "Never" restart policy, which means that the pod is not restarted if it fails.

🔹CronJobs:

CronJobs are used to run scheduled tasks in a Kubernetes cluster. CronJobs are similar to Jobs, but they allow you to schedule tasks to run at specific times or intervals. CronJobs use the same cron syntax as the Unix cron utility.

Zenesys - Kubernetes Cronjobs - How To Run Cron Job In Kubernetes?


To deploy a CronJob in a Kubernetes cluster, create a YAML file with the following contents:

COPY

COPY

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: my-cronjob
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: my-container
              image: my-image:latest
              command: [ "echo", "Hello, World!" ]
          restartPolicy: Never

This YAML file creates a CronJob named "my-cronjob" that runs a Job every minute. The CronJob uses the "*/1 " cron syntax, which means that it runs every minute. The Job template specifies a single container that runs the "echo" command and uses the "Never" restart policy.

📍Conclusion:

In conclusion, Kubernetes workloads provide a powerful way to deploy, manage, and scale containerized applications. Deployments, StatefulSets, DaemonSets, Jobs, and CronJobs are all useful for different types of applications and use cases. With the YAML examples provided in this article, you can deploy each type of workload in your own Kubernetes cluster.

Thank You! Stay Connected ☁️👩‍💻🌈