Day 3: Kubernetes Workloads and Scheduling

Gaurav Bharane
3 min readAug 17, 2024

Welcome to Day 3 of the #KubeWeek Challenge!
We’ll cover key concepts like Pods, Deployments, DaemonSets, Jobs, CronJobs, and how Kubernetes schedules them efficiently across nodes.

Table of Contents

Understanding Kubernetes Workloads

  • Pods
  • Deployments
  • DaemonSets
  • Jobs and CronJobs

Kubernetes Scheduling

  • Scheduler Overview
  • Node Selection
  • Taints and Tolerations
  • Affinity and Anti-Affinity Rules

Monitoring and Managing Workloads

  • Scaling Deployments
  • Rolling Updates and Rollbacks

Conclusion

Understanding Kubernetes Workloads

Kubernetes workloads are the objects that represent your applications and services. These workloads are managed by Kubernetes controllers, which ensure that the desired state of your application is maintained.

Pods

Pods are the smallest deployable units in Kubernetes. A pod can contain one or more containers, which share the same network namespace and storage volumes. Pods are ephemeral, meaning they can be created, destroyed, and replaced frequently.

Deployments

Deployments are used to manage stateless applications. They define the desired state of your application, such as the number of replicas, and ensure that the application is always running in the desired state. Deployments are ideal for applications that require scaling and rolling updates.

Example Deployment YAML:

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: nginx

DaemonSets

DaemonSets ensure that a copy of a specific pod runs on every node (or a subset of nodes) in the cluster. They are commonly used for running background tasks like log collection or monitoring agents.

Jobs and CronJobs

Jobs are used to run a task to completion. Once the task is completed, the job will terminate. CronJobs, on the other hand, run jobs on a scheduled basis, similar to cron jobs in Linux.

Example CronJob YAML:

apiVersion: batch/v1
kind: CronJob
metadata:
name: my-cronjob
spec:
schedule: "*/5 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: my-container
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from Kubernetes
restartPolicy: OnFailure

Kubernetes Scheduling

The Kubernetes scheduler is responsible for placing pods on nodes within the cluster based on resource availability and constraints.

Scheduler Overview

The scheduler considers various factors like CPU, memory, and affinity rules to determine the best node for each pod.

Node Selection

Kubernetes selects nodes for pods based on resource requirements, taints, tolerations, and affinity/anti-affinity rules.

Taints and Tolerations

Taints allow you to repel certain pods from being scheduled on specific nodes. Tolerations are applied to pods to allow them to be scheduled on nodes with matching taints.

Affinity and Anti-Affinity Rules

Affinity rules allow you to specify that certain pods should be scheduled together, while anti-affinity rules ensure that pods are spread across different nodes for availability.

Monitoring and Managing Workloads

Once your workloads are running, it’s crucial to monitor and manage them effectively.

Scaling Deployments

Kubernetes allows you to scale your deployments up or down based on demand. This can be done manually or automatically using the Horizontal Pod Autoscaler.

kubectl scale deployment my-deployment --replicas=5

Rolling Updates and Rollbacks

Kubernetes supports rolling updates, allowing you to update your application without downtime. If something goes wrong, you can easily roll back to a previous version.

kubectl rollout undo deployment my-deployment

Conclusion

we covered the basics of pods, deployments, and other workload types, along with how the Kubernetes scheduler optimizes resource usage across the cluster. As you progress through KubeWeek, you’ll gain more confidence in managing these workloads and deploying applications at scale.

Stay tuned for tomorrow’s session, where we’ll dive into storage and persistence in Kubernetes!

#TrainWithShubham #Kubernetes #KubeWeek #DevOps #k8s

--

--

Gaurav Bharane
Gaurav Bharane

Written by Gaurav Bharane

I am a qualified and passionate Developer with a experience in Redhat Linux administration and Computer Programming. Strong creative and analytical skills.

No responses yet