Kubernetes - By Shivansh Vasu
Kubernetes - By Shivansh Vasu
(@theshivanshvasu)
1. Introduction to Kubernetes
What is Kubernetes?
An open-source container orchestration platform.
Developed by Google, now maintained by CNCF (Cloud Native Computing Foundation).
2. Kubernetes Architecture
7. Advanced Topics
Helm
Package manager for Kubernetes.
Use Helm charts to define, install, and upgrade complex Kubernetes applications.
Network Policies
Define rules for Pod communication.
RBAC (Role-Based Access Control)
Manage permissions within the cluster.
RESOURCES
Kubernetes is an open-source container orchestration system for automating software
deployment, scaling, and management. Google originally designed Kubernetes, but the
Cloud Native Computing Foundation now maintains the project.
Resources:
● https://www.youtube.com/watch?v=X48VuDVv0do
● https://www.youtube.com/watch?v=s_o8dwzRlu4
● https://www.youtube.com/watch?v=yznvWW_L7AA
● https://www.youtube.com/watch?v=YzaYqxW0wGs&list=PL34sAs7_26wNBRWM6BD
hnonoA5FMERax0
● https://www.youtube.com/watch?v=l_lWfipUimk&list=PLhW3qG5bs-L8EU_Oocu6Rk
NPpYpaamtXX
● https://www.youtube.com/watch?v=umXEmn3cMWY
● https://www.youtube.com/watch?v=azuwXALfyRg&t=2s
● https://www.youtube.com/watch?v=VnvRFRk_51k&list=PLy7NrYWoggjziYQIDorlXjTv
vwweTYoNC
Links:
● https://kubernetes.io/docs/home/
● https://www.tutorialspoint.com/kubernetes/index.htm
● https://medium.com/free-code-camp/learn-kubernetes-in-under-3-hours-a-detailed-gu
ide-to-orchestrating-containers-114ff420e882
Courses:
● https://kube.academy/
● https://killercoda.com/
● https://kodekloud.com/learning-path-kubernetes/
Getting Started:
● Kubernetes installation tools
● Kubernetes installation with Kind
● Kubernetes Installation with minikube
● Kubectl cheat sheet
● Kubernetes Core concepts
Certifications:
● https://www.cncf.io/certification/ckad/
● https://www.cncf.io/certification/cka/
● https://training.linuxfoundation.org/certification/certified-kubernetes-security-specialist
Kubernetes:
● Create a local kubernetes cluster with minikube or kind
● Install kubectl in your machine to access the kubernetes cluster
● Create a nginx deployment with 4 replicas using kubectl
● Scale the above replicas to 10 and access the nginx via a load balancer or ingress (
for ingress you can use nginx ingress controller and loadbancer use metallb )
● Delete the above deployment and create the above deployment using kubernetes
manifest files
● Append a config map to the above deployment to output hello-world from browser
● Create a secret and mount it to the deployment
● What is the difference between statefulsets vs deployments vs daemonsets
● Create a sidecar and attach to nginx deployment
● Use a init container to make changes to the index.html file for the deployment to
output “hello from <your name>”
● Create 2 different nginx deployments in different namespaces and curl each other to
get the outputs.
● Create a HPA for nginx to automatically scale for huge loads ( simulate the load
using open source tools like locust / j meter / k6 )
● Create resource quota for namespaces
● Limit resources for nginx deployment to use max of 1CPU and 1GB of ram
● Create a network policy to only allow nginx1 to talk to nginx2 which are 2 different
deployments and block communication from any other pods ( you need to install a
CNI for this, you can use cilium, flannel or calico for this )
● Create a multi node kubernetes cluster ( if you have spare laptop attach it as another
node ) if not skip
● Pull a private image from dockerhub by passing the image pull secrets
● Configure affinity and anti-affinity to deployments
● What is Qos in kubernetes
● Install helm and create a basic helm chart
● Install a nginx helm chart
● Learn the templating engine of helm chart and what the .tpl file does in a chart
● Chart dependencies - i.e install nginx1 chart while nginx2 chart is being installed
● Override existing nginx helm chart with values.yaml file
SOLUTION:
# Install Kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s
https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client
● Scale the above replicas to 10 and access the nginx via a load balancer or ingress (
for ingress you can use nginx ingress controller and loadbancer use metallb )
# Scale Nginx Deployment to 10 Replicas
kubectl scale --replicas=10 deployment/nginx-deployment
# Configure Ingress
cat <<EOF > nginx-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
EOF
# Apply the Ingress YAML file
kubectl apply -f nginx-ingress.yaml
● Delete the above deployment and create the above deployment using kubernetes
manifest files
● Append a config map to the above deployment to output hello-world from browser
● Use a init container to make changes to the index.html file for the deployment to
output “hello from <your name>”
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
initContainers: # Add init container section
- name: init-container
image: alpine:latest # Use Alpine Linux image for simplicity
command: ["sh", "-c", "/path/to/init-container-script.sh"]
volumeMounts:
- name: html-volume
mountPath: /usr/share/nginx/html
volumes:
- name: html-volume
emptyDir: {} # Use emptyDir volume for simplicity
● Create 2 different nginx deployments in different namespaces and curl each other to
get the outputs.
# Create namespaces
kubectl create namespace namespace1
kubectl create namespace namespace2
● Create a HPA for nginx to automatically scale for huge loads ( simulate the load
using open source tools like locust / j meter / k6 )
# Create Nginx Deployment YAML file
cat <<EOF > nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1 # Start with a single replica
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
EOF
# Apply HPA
kubectl apply -f nginx-hpa.yaml
● Limit resources for nginx deployment to use max of 1CPU and 1GB of ram
# Create a ResourceQuota YAML file named resource-quota.yaml
cat <<EOF > resource-quota.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
name: nginx-resource-quota # Name for the ResourceQuota
namespace: my-namespace
spec:
hard:
requests.cpu: "1" # Maximum CPU request allowed
requests.memory: "1Gi" # Maximum memory request allowed
limits.cpu: "1" # Maximum CPU limit allowed
limits.memory: "1Gi" # Maximum memory limit allowed
EOF
●
● Create a multi node kubernetes cluster ( if you have spare laptop attach it as
another node ) if not skip
○ Prepare Nodes:
■ Ensure spare laptops or machines meet Kubernetes requirements.
○ Install Docker on each node.
○ Install Kubernetes Tools
■ Install kubeadm, kubelet, and kubectl on all nodes.
○ Initialize Master Node:
■ Choose one node as the master and run kubeadm init to set up control plane.
○ Join Worker Nodes:
■ On other nodes, run kubeadm join to connect them to the master.
○ Set Up Networking:
■ Choose a CNI plugin (e.g., Calico, Flannel) and configure it on the cluster.
○ Verify Cluster Setup:
■ Use kubectl get nodes to ensure all nodes, including master and workers, are visible.
○ Test Functionality:
■ Deploy sample applications (e.g., Nginx, WordPress) to test cluster functionality.
● Pull a private image from dockerhub by passing the image pull secrets
# Create Docker Hub Secret with your credentials
kubectl create secret docker-registry dockerhub-secret \
--docker-username=<username> \
--docker-password=<password> \
--docker-server=https://index.docker.io/v1/ && \
# Apply Pod manifest that uses the created secret for image pulling
kubectl apply -f pod-manifest.yaml
● Configure affinity and anti-affinity to deployments
○ Affinity:
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-deployment
spec:
replicas: 3
selector:
matchLabels:
app: example-app
template:
metadata:
labels:
app: example-app
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: "app"
operator: In
values:
- example-app
topologyKey: "kubernetes.io/hostname"
Anti-Affinity:
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-deployment
spec:
replicas: 3
selector:
matchLabels:
app: example-app
template:
metadata:
labels:
app: example-app
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: "app"
operator: In
values:
- example-app
topologyKey: "kubernetes.io/hostname"
● Learn the templating engine of helm chart and what the .tpl file does in a chart
Helm uses Go templating engine to dynamically generate Kubernetes manifest files in Helm charts. .tpl
files within a Helm chart's templates directory contain Kubernetes manifest definitions with embedded
Go templating syntax, allowing for parameterization and reusability of configurations during chart
installation.
● Chart dependencies - i.e install nginx1 chart while nginx2 chart is being installed
# Create or modify the requirements.yaml file in your Helm chart
directory with the specified dependencies
echo "dependencies:
- name: nginx1
version: \"1.0.0\"
repository: \"https://example.com/charts/nginx1\"" >
requirements.yaml
# Install your Helm chart and its dependencies using the helm install
command
helm install my-nginx-chart ./my-nginx-chart