Kubernetes Notes
Kubernetes Notes
Architecture of Kubernetes
Overview of Kubernetes Objects
Structure of Kubernetes yaml
Kubernetes Objects in detail
* Pod
* ReplicaSet
* Deployment
* Service
Installation of Kubernetes
Kubernetes environment varibles
kubernetes volumes
kubernetes secrets
kubernetes networking
Kubernetes namespaces
===================================================================================
===================================
Kubernetes: Kubernetes is container orchestration tool or container management tool
developed by google.
Kubernetes is used for managing all containerization application.
Kubernetes provides self healing mechanism in case machine interrupts
or restarts due to network or resource issues.
Kubernetes has agents to monitor all the containers to keep the
application 24/7.
Kubernetes maintains zero downtime in case of rollback and ugrading
application.
Kubernetes manages all sensitive information via kubernetes secrets in
encrypted format.
Kubernetes provides an wrapper or layer to Docker containers to monitor
applicaton and containers to make sure application available always.
===================================================================================
====================================
Why Kubernetes?
===================================================================================
====================================
Docker -> Building images(OS file and application) and create/running container ->
Push images to dockerhub.com
eg: 1 host machine -> docker service -> 1 container -> Access application/website
docker run -> manually -> whenever machine is down -> docker service will go
down and if docker service goes down -> all running container will be down and
after machine is up and running still docker container will down.
Disadvantages:
1. Downtime for application whenever we have network issue or machine restart due
to unknown reason.
2. Time consumping activity to bring up an container.
3. We dont any agent to monitor all the docker container.
4. No self healing mechanism for container to recovery automatically.
5. No proper memory and cpu management on container, as it uses complete resources
from base machine OS.
6. IN case of rollback or upgrading the application, downtime is required as
container needs to be deleted and recreated with new application.
7. In Docker, sensitive information like user and password are stored in plain text
format
===================================================================================
===================================
Architecture of Kubernetes:
===================================================================================
===================================
Master Node: Control plane component:
1. API Server
2. Controller
3. Scheduler
4. ETCD
Worker Node:
1. Kubelet
2. Docker
3. Kube proxy
kubectl
===================================================================================
=======================================
Kubernetes Objects:
===================================================================================
=======================================
1. Pod : Pod is smallest object of kubernetes for managing the containers and
application.
2. ReplicaSet: Replicaset is used for scaling up and down the pods depends up the
load on application.
4. Service: Service is used for exposing the application running inside the
container to outside world.
===================================================================================
=======================================
Structure of Kubernetes yaml:
===================================================================================
=======================================
apiVersion: v1,apps/v1
v1 => Pod, Service
apps/v1 => Replicaset, Deployment
apiVersion is used for mentioning the version of kubernetes objects.
kind: Pod/ReplicaSet/Deployment/Service
Kind is used for indentify , what type of object kubernetes apiVersion is
going to use.
metadata:
name: mypod
Information about the kubernetes object like name, unique identity as label.
spec:
containers:
image: nginx:latest
name: mypod
Specification about the kubernetes objects which contains information like
image details, container details, secrets, volumes, etc
===================================================================================
=======================================
vi sample-pod-def.yml
===================================================================================
======================================
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
image: nginx:latest
name: nginx-container
===================================================================================
==========================================Pod : Pod is smallest object of
kubernetes for managing the containers and application.
Pod contains the containers.
Pod contains the single unique instance of container.
Inside a pod we can multiple unique container like helper container(DB).
Pod has its own IP Address
No Duplicate containers are created inside a pod
commands: kubectl run my-pod --image nginx:latest // adhoc command for creating
an pod
kubectl get pods // list all the running pods
kubectl describe pod my-pod // Detailed information about
the pod
kubectl delete pod my-pod // delete a pod
kubectl get pods -o wide // list all the running pods
and displays the Node Ip and Pod IP
kubectl exec -it my-pod bash
========================
vi sample-pod-def.yml
========================
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx-container
image: nginx:latest
======================
============================================
How to edit the pod details ?
============================================
1. Edit running pod via kubectl edit command:
kubectl edit pod pod_name -> vi editor -> make change -> :wq save
2. vi sample-pod-def.yml -> vi editor -> make change -> :wq save
kubectl apply -f sample-pod-def.yml
===================================================================================
================================
ReplicaSet: Replicaset is used for scaling up and down the pods depends up the
load on application.
====================================
vi replicaset-def.yml
====================================
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset
spec:
template:
metadata:
name: nginx-pod
labels:
name: mypod
tier: frontend
spec:
containers:
- name: nginx-container
image: nginx:latest
replicas: 3
selector:
matchLabels:
name: mypod
tier: frontend
===================================================================================
===============================
Labels and Selector:
===================================================================================
================================
With Labels and Selector, we can an unique identity for pods.
Using Labels and Selector, replicaset will identify the respective pods which needs
to monitored.
===================================================================================
=================================
commands: kubectl create -f replicaset-def.yml // create an replicaset
kubectl get replicaset // list all replicaset
kubectl get pods // list all the running pods
kubectl describe replicaset nginx-replicaset // Detailed information
about the replicaset
kubectl delete replicaset nginx-replicaset // delete a replicaset
========================================
deployment-def.yml
========================================
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-recreate
spec:
template:
metadata:
name: nginx-pod
labels:
name: mypod
tier: frontend
spec:
containers:
- name: nginx-container
image: nginx:latest
replicas: 3
selector:
matchLabels:
name: mypod
tier: frontend
=============================================================
How to upgrade or rollback application ?
=============================================================
1. Edit running deployment via kubectl edit command:
kubectl edit deployment deployment_name -> vi editor -> make change -> :wq save
deployment-rollingupdate.yml
========================================
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-rollingupdate
labels:
name: mypod
tier: frontend
spec:
template:
metadata:
name: nginx-pod
labels:
name: mypod
tier: frontend
spec:
containers:
- name: nginx-container
image: nginx:latest
replicas: 30
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1 // how many pods we want to upgrade at a time
maxUnavailable: 1 // Keep one pod untill the rollback or upgrade is completed
to keep existing application up.
selector:
matchLabels:
name: mypod
tier: frontend
=============================================================
How to upgrade or rollback application ?
=============================================================
1. Edit running deployment via kubectl edit command:
kubectl edit deployment deployment_name -> vi editor -> make change -> :wq save
==========================
service-def.yml
==========================
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: LoadBalancer
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
externalIPs:
- Public-IP-workernode/Loadbalancerurl
selector:
name: mypod
tier: frontend
===================================================================================
==============
commands: kubectl create -f service-def.yml // create an service
kubectl get service // list all service
kubectl describe service service_name // Detailed information about
the deployment
kubectl delete service service_name // delete a service
Prerequisite:
2 Instances : 1 master , 1 worker
Operating system: Amazon AMI 2 (Amazon Linux 2 Kernel 5.10 AMI 2.0.20230418.0
x86_64 HVM gp2)
Instance Type: t2. medium (2 core CPU and 4GB RAM)
Security group: Port 22 ssh
all traffic 0.0.0.0/0
HardDisk : 10 GB
===================================================================================
====================================
Step 2: Add Kubernetes repo to both Master and Worker Nodes.
vi /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch
enabled=1
gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
===================================================================================
====================================
Step 3: Turn of the swap space and check if selinux in disabled on master and
worker
swapoff --all
sestatus //checking selinux security should be in disabled mode
===================================================================================
====================================
Step 4: Install kubelet, kubeadm , kubectl & start and enable service on both
master and worker
kubeadm init
Now Run the command below only on master node, same commands
will be display above output from kubeadm init command.
Preserve the output in Notepad for join more worker nodes in future.
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
===================================================================================
====================================
Step 6: Run Kubeadm Join command from output of kubeadm init only on Worker Nodes
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
[root@ip-172-31-29-163 ~]#
===================================================================================
====================================
Step 7: Set the path using export command.
Master Node:
vi /etc/profile.d/k8s-master.sh
export KUBECONFIG=/etc/kubernetes/admin.conf
swapoff --all
Worker Node:
vi /etc/profile.d/k8s-worker.sh
export KUBECONFIG=/etc/kubernetes/kubelet.conf
swapoff --all
curl https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/
calico.yaml -O
eg:
=================================
sample-volumes.yml
=================================
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod-volume
spec:
containers:
- name: nginx-container
image: nginx:latest
volumeMounts:
- name: host-vol
mountPath: /apps
volumes:
- name: host-vol
hostPath:
path: /apps
================================
How to create an secret :
================================
vi secret-def.yml
================================
apiVersion: v1
kind: Secret
metadata:
name: nginx-secret
type: Opaque
data:
user: bmdpbngtdXNlcg==
password: bmdpbngtcGFzc3dvcmQ=
======================================================
kubectl create -f secret-def.yml
kubectl get secrets
kubectl describe secret secret_name
kubectl delete secret secret_name
===============================================
How to encrypt and decode the sensitive data?
================================================
bydefault - base64 encryption and decryption mechansim
=============================
vi env-variables.yml
=============================
eg:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod-secret
spec:
containers:
- name: nginx-container
image: nginx:latest
env:
- name: USER
value: john
- name: PASSWORD
value: test123
- name: server
value: db-server-01
eg:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod-secret
namespace: qa-team
spec:
containers:
- name: nginx-container
image: nginx:latest
=====================================
How to create an namespace by user:
=====================================
kubectl create namespace ds-team
kubectl get namespace/ns
kubectl describe namespace namespace_name
kubectl delete namespace namespace_name
===================================================================================
====================================
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
export KUBECONFIG=/etc/kubernetes/admin.conf
Then you can join any number of worker nodes by running the following on each as
root: