Session 03
Kubernetes Documentation
Getting Started with Kubernetes
Kubernetes (K8s) is an open-source system for automating deployment, scaling, and management of containerized applications.
🚀 Docker vs Kubernetes – Quick Comparison
| Feature | Docker | Kubernetes |
|---|---|---|
| Purpose | Build and run containers | Container orchestration |
| Function | Packages applications | Manages containers |
| Architecture | Runs on single nodes | Runs on clusters |
| Scaling | Manual scaling | Automatic scaling |
Summary:
- Docker is used for creating and running containers.
- Kubernetes is used for managing those containers at scale across multiple nodes.
🚀 Prerequisites
- Basic knowledge of containers (e.g., Docker)
- A running Kubernetes cluster (Minikube, Kind, or a managed service like GKE, EKS, AKS)
kubectlinstalled and configured- Enable kuberntes from Docker Desktop to get started.
🛠 Install kubectl command to manage kubernetes
# Install kubectl
curl -LO "https://dl.k8s.io/release/$(curl -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl && mv kubectl /usr/local/bin/For Windows Download link
- Enable Kubernetes on Docker Desktop
- Understand the use of kubeconfig file
- Run some basic commands
🧠 Kubernetes Components (Brief Overview)
Kubernetes consists of a control plane and worker nodes, each with several key components. Below is a summary of both.
🛠️ Control Plane Components
These components manage the overall cluster state.
1. kube-apiserver
- The front-end of the Kubernetes control plane.
- Exposes the Kubernetes API.
- All internal/external communication happens through it.
2. etcd
- A key-value store for all cluster data.
- Stores configuration, state, and metadata.
- Highly available and consistent.
3. kube-scheduler
- Watches for new Pods without assigned nodes.
- Selects the best node to run the Pod based on constraints and policies.
4. kube-controller-manager
- Runs various controllers (e.g., ReplicationController, NodeController).
- Ensures desired state matches the actual cluster state.
5. cloud-controller-manager (optional)
- Manages cloud-specific resources (e.g., load balancers, IPs).
- Used only in cloud environments (e.g., AWS, Azure, GCP).
🚀 Node Components
These run on each worker node and are responsible for running Pods.
1. kubelet
- Agent that runs on every node.
- Registers node with the cluster.
- Ensures containers are running in the desired state.
2. kube-proxy
- Maintains network rules for Pods.
- Enables communication within the cluster using Services.
- Handles routing and load balancing.
3. container runtime
- Software that runs containers (e.g., containerd, CRI-O, Docker).
- Kubernetes uses it to run application containers.
🔍 Add-ons (Common but not mandatory)
1. CoreDNS
- Provides DNS-based service discovery.
- Resolves service names to IPs.
2. Metrics Server
- Collects resource metrics (CPU/memory).
- Enables autoscaling and monitoring.
3. Ingress Controller
- Manages external HTTP/HTTPS access to services.
- Works with Ingress resources.
✅ Summary Table
| Component | Role | Runs On |
|---|---|---|
| kube-apiserver | API access point | Control Plane |
| etcd | Cluster data store | Control Plane |
| kube-scheduler | Pod scheduling | Control Plane |
| kube-controller-manager | Runs controllers | Control Plane |
| cloud-controller-manager | Manages cloud resources | Control Plane |
| kubelet | Node agent | Worker Node |
| kube-proxy | Network proxy | Worker Node |
| container runtime | Runs containers | Worker Node |
📝 Tip: Run
kubectl get componentstatusesto see health of control plane components.
AKS Deployment Guide
Steps to deploy Azure Kubernetes Service (AKS) using Azure CLI.
📌 Prerequisites
- An active Azure subscription
- Azure CLI installed (
az --versionto check) - Logged into Azure:
az login --use-device-code- Check your azure subscription
az account list - table- Check login status
az account show- Create a Resource Group
az group create --name myResourceGroup --location centralindia- Create AKS Cluster
az aks create \
--resource-group myResourceGroup \
--name myAKSCluster \
--location centralindia \
--node-count 1 \
--node-vm-size Standard_B2s \
--generate-ssh-keys- Register the Missing Resource Provider if get an error
az provider register --namespace Microsoft.OperationalInsights
az provider show --namespace Microsoft.OperationalInsights --query "registrationState"- Register all required add on for AKS
for ns in Microsoft.ContainerService Microsoft.OperationalInsights Microsoft.Insights Microsoft.Network Microsoft.Compute Microsoft.Storage; do
az provider register --namespace $ns
done- Get AKS Credentials (Kubeconfig)
az aks get-credentials --resource-group myResourceGroup --name myAKSCluster- Verify Cluster Access
kubectl get nodes✅ Jenkins Credential ID for AKS kubeconfig
When you upload your kubeconfig file as a Secret file in Jenkins, you choose an ID for it. Examples:
aks-kubeconfigkubeconfigprod-aks- (Any custom name you prefer)
This ID is used in the Jenkins pipeline to reference the kubeconfig file.
✅ Example: Upload & Use aks-kubeconfig
📌 In Jenkins:
- Navigate to Manage Jenkins → Credentials
- Choose the appropriate scope (e.g., Global, Folder, etc.)
- Click “Add Credentials”
- Choose “Secret file”
- Upload your
kubeconfigfile - Set the ID to:
aks-kubeconfig
📋 Jenkins Pipeline (Declarative) Example
pipeline {
agent any
environment {
KUBECONFIG = "${WORKSPACE}/kubeconfig"
}
stages {
stage('Use AKS Kubeconfig') {
steps {
withCredentials([file(credentialsId: 'aks-kubeconfig', variable: 'KUBECONFIG_FILE')]) {
sh '''
cp $KUBECONFIG_FILE $KUBECONFIG
chmod 600 $KUBECONFIG
kubectl get nodes
'''
}
}
}
}
}🛠️ Kompose - Convert Docker Compose to Kubernetes YAML
Kompose is a tool that helps you convert your docker-compose.yml into Kubernetes resource manifests.
✅ What Kompose Does
Kompose automatically converts:
- Docker Compose services ➡️ Kubernetes Deployments
- Exposed ports ➡️ Kubernetes Services
- Volumes ➡️ PersistentVolumeClaims
- Environment variables ➡️ ConfigMaps or inline in Pods
📦 Install Kompose
🔧 On Linux / macOS
curl -L https://github.com/kubernetes/kompose/releases/download/v1.30.0/kompose-linux-amd64 -o kompose
chmod +x kompose
sudo mv kompose /usr/local/bin/🔄 Convert Docker Compose to Kubernetes Manifests
- Navigate to the folder where your docker-compose.yml is located:
- Run:
kompose convert- To deploy directly to your cluster:
kompose up- To remove
kompose downYou may have to take care of the labels and ports assigned to svc and pods .
📘 Kubernetes Pods Assignment
Complete the following tasks to strengthen your understanding of Pods in Kubernetes.
📝 Tasks 01
- Create a Pod named
nginx-podusing thenginximage. - Describe the
nginx-podand review its details. - View logs of the
nginx-pod. - Execute an interactive shell inside the
nginx-pod. - Write a YAML manifest to create a Pod named
busybox-podrunning thebusyboximage. - Create a Pod that runs for a long time using
sleepcommand. - Create a Pod with specific resource
requestsandlimitsfor CPU and memory. - Create a Pod that sets an environment variable
MY_ENV_VAR=HelloK8s. - Create a Pod that mounts a
hostPathvolume to a container. - Delete a Pod using
kubectl deleteand confirm it’s removed.
📘 Kubernetes Deployments Assignment
Work through the following tasks to gain practical experience with Deployments in Kubernetes.
📝 Tasks 02
- Create a Deployment named
web-deploywith 2 replicas using thenginximage. - Verify that the Deployment created the correct number of Pods.
- Scale the Deployment
web-deployto 5 replicas. - Update the Deployment image to
nginx:1.21. - Roll back the Deployment to the previous version.
- Write a YAML manifest to create a Deployment named
httpd-deployusing thehttpdimage. - Add a label
env=prodto all Pods created byweb-deploy. - Use
kubectl rollout statusto monitor the status of a rolling update. - Delete the Deployment and verify that all Pods are also deleted.
📘 Kubernetes Services Assignment
Complete the following tasks to understand and work with Services in Kubernetes.
📝 Tasks 03
- Create a Deployment named
nginx-deploywith 3 replicas using thenginximage. - Expose the Deployment with a ClusterIP Service named
nginx-clusteripon port 80. - Verify that the Service correctly routes traffic to all 3 Pods.
- Use
kubectl get endpointsto confirm endpoints are assigned to the Service. - Create a NodePort Service named
nginx-nodeportto expose the same Deployment on a node port. - Access the application in a browser using
<NodeIP>:<NodePort>. - Create a YAML file to define a LoadBalancer Service (only works in cloud environments).
- Delete the
nginx-clusteripandnginx-nodeportservices. - Create a headless Service using
clusterIP: Noneand verify DNS resolution inside a Pod. - Use
kubectl port-forwardto expose a Pod without a Service and access it locally.