Kubernetes Workloads

Kubernetes Workloads

·

7 min read

k8s workloads

In Kubernetes, workloads are applications running inside pods, which are groups of containers. Pods have defined lifecycles, and if a pod encounters a fault on the node it's running on, all pods on that node fail, necessitating new pods for recovery, even if the node becomes healthy later.

To simplify management, you don't need to directly handle each pod. Instead, Kubernetes provides workload resources, such as controllers, that manage sets of pods on your behalf. These controllers ensure the right number of pods with the correct configurations are running to match your desired state. By using workload resources, you can focus on the high-level application management without getting bogged down in individual pod details. This abstraction allows for efficient application deployment, scaling, and fault tolerance within the Kubernetes cluster.

Deployments

Deployments are used to manage replica sets and rolling updates. They make sure that the application is updated gradually pod by pod and similarly it makes it easy for the rolling back to the previous version of the application

It automates the deployment, scaling, and management of containerized applications. It ensures high availability, load balancing, and rolling updates for seamless application operations in a cluster environment.

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

The above deployment name is nginx-deployment with app:nginx as the label creates 3 replicas of nginx on port no 80 by downloading an image nginx:1.14.2

Stateful sets

A Kubernetes StatefulSet, often abbreviated as K8s StatefulSet, is a valuable resource within the Kubernetes ecosystem that caters specifically to the management of stateful applications. Unlike stateless applications that can be easily replicated and scaled, stateful applications, such as databases, require careful orchestration to maintain data consistency and identity.

StatefulSets provide a mechanism for deploying and maintaining stateful applications in a Kubernetes cluster while ensuring unique network identities and persistent storage for each instance. This is crucial for applications that rely on ordered scaling, stable network identifiers, and data persistence.

StatefulSets offer a range of benefits, such as:

  1. Ordered Scaling: StatefulSets ensure that pods are scaled up or down in a predictable, sequential manner. This is particularly useful for databases, where the order of scaling operations can impact data integrity.

  2. Stable Hostnames: Each pod managed by a StatefulSet is assigned a stable, unique hostname. This simplifies communication between pods and external services, enabling seamless integration within the application.

  3. Persistent Storage: StatefulSets automatically provision persistent storage volumes for each pod, ensuring that data is preserved even if the pod is rescheduled or restarted.

  4. Headless Service: StatefulSets can also create a "headless" service, allowing direct communication with individual pods, and facilitating tasks like data synchronization and configuration propagation.

  5. Update Strategies: StatefulSets offer update strategies like rolling updates, allowing controlled and predictable updates without compromising data integrity.

  6. Stateful Application Patterns: Many stateful applications follow specific deployment patterns. StatefulSets enable these patterns by managing the stateful application's lifecycle and providing mechanisms for initialization and termination.

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: nginx # has to match .spec.template.metadata.labels
  serviceName: "nginx"
  replicas: 3 # by default is 1
  minReadySeconds: 10 # by default is 0
  template:
    metadata:
      labels:
        app: nginx # has to match .spec.selector.matchLabels
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: nginx
        image: registry.k8s.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "my-storage-class"
      resources:
        requests:
          storage: 1Gi

This YAML sets up a headless NGINX Service on port 80, linked to StatefulSet "web" managing 3 pods. Pods have NGINX containers with a 10s termination grace period, using "registry.k8s.io/nginx-slim:0.8" image, mounted with a 1Gi "ReadWriteOnce" volume named "www" via "my-storage-class" storage class.

DaemonSet

Kubernetes DaemonSets ensure that a specific pod runs on every node within a cluster. They are used to deploy system-level daemons or monitoring agents that should run on each node to provide cluster-wide functionality. When a new node is added to the cluster, a matching pod is automatically scheduled on it. Similarly, when a node is removed, the pod running on it is terminated. DaemonSets are helpful for tasks like log collection, monitoring, and network management, where it is essential to have a consistent presence on every node.

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
  namespace: kube-system
  labels:
    k8s-app: fluentd-logging
spec:
  selector:
    matchLabels:
      name: fluentd-elasticsearch
  template:
    metadata:
      labels:
        name: fluentd-elasticsearch
    spec:
      tolerations:
      # these tolerations are to have the daemonset runnable on control plane nodes
      # remove them if your control plane nodes should not run pods
      - key: node-role.kubernetes.io/control-plane
        operator: Exists
        effect: NoSchedule
      - key: node-role.kubernetes.io/master
        operator: Exists
        effect: NoSchedule
      containers:
      - name: fluentd-elasticsearch
        image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi
        volumeMounts:
        - name: varlog
          mountPath: /var/log
      terminationGracePeriodSeconds: 30
      volumes:
      - name: varlog
        hostPath:
          path: /var/log

The provided YAML creates a Kubernetes DaemonSet named "fluentd-elasticsearch" in the "kube-system" namespace. It ensures a pod with a container using the "fluentd-elasticsearch" image is deployed on every node. The container has specified CPU and memory resources, and it mounts the host's "/var/log" directory. Tolerations allow it to run on control plane and master nodes. A termination grace period of 30 seconds is set. This DaemonSet facilitates distributed logging by deploying a Fluentd container on each node to collect and send logs to Elasticsearch.

Create a DaemonSet based on the YAML file:

kubectl apply -f https://k8s.io/examples/controllers/daemonset.yaml

Jobs and CronJobs

In Kubernetes, both "Job" and "CronJob" are resource types that facilitate the execution of tasks at specific intervals or ensure a task is completed successfully.

  1. Job: A Kubernetes Job creates one or more pods and ensures they run to completion. It's commonly used for tasks that are expected to run once, such as batch processing, data migration, or backups. The Job resource guarantees that a certain number of pods complete successfully before considering the task done. If a pod fails, a new one is automatically created to replace it until the desired number of successful completions is achieved.

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

    The above Kubernetes Job, named "pi," uses a pod template with a Perl container (version 5.34.0) to calculate and print Pi with 2000 decimal places. The Job retries up to 4 times on failure and doesn't automatically restart the pod after termination. It's designed for one-time computation tasks.

    You can run the job with this command:

     kubectl apply -f https://kubernetes.io/examples/controllers/job.yaml
    
  2. CronJob: A Kubernetes CronJob is used to schedule and automate recurring tasks based on a cron-like schedule. It creates pods at specified time intervals, making it suitable for repetitive or periodic tasks, such as data synchronization, report generation, and regular cleanups. CronJobs ensure tasks are executed at the specified intervals, and each pod runs to completion before being terminated.

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

    the above Kubernetes CronJob named "hello" is scheduled to run every minute. It creates a Job with a pod template that contains a BusyBox container. This container executes a command to display the current date and a greeting message. The pod's restart policy ensures it restarts only on failure, making it suitable for recurring tasks like logging messages at regular intervals.

    schedule: "* * * * *": Sets the schedule using a cron-like syntax. In this case, it is set to run every minute, indicated by the five asterisks.

Summary

In summary, Kubernetes offers versatile workload resources. Deployments ensure app availability and scaling with updates. StatefulSets handle stateful apps, offering ordered scaling and data persistence. DaemonSets guarantee specific pods on each node for tasks like logging. Jobs manage one-off tasks, ensuring completion. CronJobs automate recurring tasks on a schedule. These resources optimize app control, scalability, and automation. Deployments suit stateless apps, StatefulSets manage stateful ones, DaemonSets handle node-level tasks, Jobs oversee short-term processes, and CronJobs automate repetition. Kubernetes empowers efficient app management, resilience, and automated operations, catering comprehensively to diverse app demands.