Kubernetes Notes
Kubernetes Notes
KUBERNETES
=====================================
Menions: This is an individual node used in Kubernetes Combination of these
minions is called as Kubernetes cluster
Slaves are the nodes that accept the work load from the master and handle
activites load balancing,autoscalling,high availability etc
2 Service: This is used for port mapping and network load balancing
7 Deployment: This used for perfroming all activites that a Replicaset can do
it can also handle rolling update
8 PersistantVolume: Used to specify the section of storage that should be used for
volumes
10 Statefulsets: These are used to handle stateful application like data bases
where consistency in read write operations has to be maintained.
1|Page
KUBERNETES MADHAV
Kubernetes Architecture
Master Componentes
apiServer: Users interact with the apiServer using some clinet like ui,command
line tool like kubelet.It is the apiServer which is the gateway to the cluster
✓ It works as a gatekeeper for authentication and it validates if a specific
user is having permissions to execute a specific command.Example if we want
to deploy a pod or a deployment first apiServers validates if the user is
authorised to perform that action and if so it passes to the next process
ie the "Scheduler"
Scheduler: This process accepts the instructions from apiServer after validation
and starts an application on a sepcific node or set of nodes.
✓ It estimates how much amount of h/w is required for an application and then
checks which slave have the necessary h/w resources and instructs the kubelet
to deploy the application
kubelet: This is the actual process that takes the orders from scheduler and
deploy an application on a slave.This kubelet is present on both master and slave
controller manager: This check if the desired state of the cluster is always
maintained.If a pod dies it recreates that pod to maintain the desired state
Worker components
containerrun time: Docker or some other container technology
kubelet: This process interacts with container run time and the node
and it start a pod with a container in it
1 Kops
2 Kubeadm
3 Kind
2|Page
KUBERNETES MADHAV
1 EKS (AWS)
2 GKE (GCP)
4. Install kubectl
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s
https://storage.googleapis.com/kubernetes-
release/release/stable.txt)/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
vi ~/.bashrc
Add following content into .bashrc, you can choose any arbitary name for cluster
and make sure buck name matches the one you created in previous step.
export KOPS_CLUSTER_NAME=project.in
export KOPS_STATE_STORE=s3://project.in.k8s
3|Page
KUBERNETES MADHAV
Then running command to reflect variables added to .bashrc
source ~/.bashrc
ssh-keygen
Above command may take some time to create the required infrastructure resources on
AWS. Execute the validate command to check its status and wait until the cluster
becomes ready
✓ For the above above command, you might see validation failed error initially
when you create cluster and it is expected behaviour, you have to wait for
some more time and check again.
2 Install Kubectl
chmod +x ./kubectl
3 Install KIND
4|Page
KUBERNETES MADHAV
chmod +x ./kind
vim config.yml
# three node (two workers) cluster config
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
Kubernetes on kubeadm
Kubeadm installation-This is a manaul setup fo Kuberentes and it works on both
cloud and on premise
===================================================================================
Disable SELINUX
setenforce 0
sed -i --follow-symlinks 's/^SELINUX=enforcing/SELINUX=disabled/'
/etc/sysconfig/selinux
===================================================================================
Disable SWAP
===================================================================================
Update sysctl settings for Kubernetes networking
5|Page
KUBERNETES MADHAV
cat >>/etc/sysctl.d/kubernetes.conf<<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
===================================================================================
Add Kubernetes to yum repository
cat >>/etc/yum.repos.d/kubernetes.repo<<EOF
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
===================================================================================
Install Kubernetes
yum install -y kubeadm-1.19.1 kubelet-1.19.1 kubectl-1.19.1
==================================================================================
Enable and start Kubernetes service
On Master
===========
Initilise the Kubernetes cluster
-----------------------------------------
===================================================================================
➔ To be able to use kubectl command to connect and interact with the cluster,
the user needs kube config file.
mkdir /home/ec2-user/.kube
cp /etc/kubernetes/admin.conf /home/ec2-user/.kube/config
chown -R ec2-user:ec2-user /home/ec2-user/.kube
===================================================================================
Deploy calico network
kubectl apply -f https://docs.projectcalico.org/v3.9/manifests/calico.yaml
===================================================================================
For slaves to join the cluster
kubeadm token create --print-join-command
6|Page
KUBERNETES MADHAV
2 Create an IAM with admin roles and assign to the EKS server
3 Install Kubectl
chmod +x ./kubectl
4 Install eksctl
Download the eksctl
curl --silent --location
"https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -
s)_amd64.tar.gz" | tar xz -C /tmp
Check if it is instlled
eksctl version
6 Click on Create
7|Page
KUBERNETES MADHAV
2 To get info about the nodes along with ipaddress and docker version etc
kubectl get nodes -o wide
==============================================================================
Create nginx as a pod and name it webserver
kubectl run --image nginx webserver
================================================================================
Create a mysql pod and also pass the necessary environment variables
kubectl run --image mysql:5 db --env MYSQL_ROOT_PASSWORD=intelliqit
=============================================================================
Kubernetes objects are created using definition/manifest files
These files containe mainly four components
---
apiVersion:
kind:
metadata:
spec:
...
kind : apiversion
Pod v1
Service v1
Namespace v1
Secret v1
ReplicationController v1
ReplicaSet apps/v1
Deployment apps/v1
StatefulSet apps/v1
DaemonSet apps/v1
PersistantVolume v1
PersistantVolumeClaim v1
HorrizontalPodAutoscaller v1
8|Page
KUBERNETES MADHAV
1 vim pod-definition1.yml
--
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
namespace: test-ns
labels:
author: intelliqit
type: proxy
cat: rat
spec:
containers:
- name: mynginx
image: nginx
...
1 vim pod-definition.yml
---
apiVersion: v1
kind: Pod
metadata:
name: postgres-pod
labels:
type: db
author: intelliqit
spec:
containers:
- name: mydb
image: postgres
env:
- name: POSTGRES_PASSWORD
value: intelliqit
- name: POSTGRES_DB
value: mydb
- name: POSTGRES_USER
value: myuser
9|Page
KUBERNETES MADHAV
1 vim pod-definition3.yml
---
apiVersion: v1
kind: Pod
metadata:
name: jenkins-pod
labels:
type: ci-cd
author: intelliqit
spec:
containers:
- name: myjenkins
image: jenkins/jenkins
ports:
- containerPort: 8080
hostPort: 8080
vim pod-definition4.yml
---
apiVersion: v1
kind: Pod
metadata:
name: httpd-pod
labels:
type: webserver
author: intelliqit
spec:
containers:
- name: myhttpd
image: httpd
ports:
- containerPort: 80
hostPort: 8080
...
vim namespace.yml
---
apiVersion: v1
kind: Namespace
metadata:
name: test-ns
...
Create a definitition file to create wordpress and launch it on the above namespace
vim pod-definition5.yml
---
apiVersion: v1
kind: Pod
metadata:
name: wordpress-pod
namespace: test-ns
labels:
type: CMS
author: intelliqit
spec:
containers:
- name: mywordpress
image: wordpress
ports:
- containerPort: 80
hostPort: 8080
...
ReplicationController
vim replication-controller.yml
---
apiVersion: v1
kind: ReplicationController
metadata:
name: httpd-rc
labels:
type: websrver
author: intelliqit
spec:
replicas: 3
template:
metadata:
name: httpd-pod
labels:
type: webserver
spec:
containers:
- name: myhttpd
image: httpd
ports:
- containerPort: 80
hostPort: 8080
...
11 | P a g e
KUBERNETES MADHAV
ReplicaSet
vim replicas-set.yml
---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: tomcat-rs
labels:
type: appserver
author: intelliqit
spec:
replicas: 3
selector:
matchLabels:
type: appserver
template:
metadata:
name: tomcat-pod
labels:
type: appserver
spec:
containers:
- name: mytomcat
image: tomee
ports:
- containerPort: 8080
hostPort: 9090
...
To scale the replicas set we can change the no of replicas in the definition file
and
kubectl replace -f replicas-set.yml
12 | P a g e
KUBERNETES MADHAV
Deployment
Create a deployment definition file for nginx
vim deployment1.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
type: proxy
author: intelliqit
spec:
replicas: 3
selector:
matchLabels:
type: proxy
template:
metadata:
name: nginx-pod
labels:
type: proxy
spec:
containers:
- name: mynginx
image: nginx
ports:
- containerPort: 80
hostPort: 9090
vim deployment2.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
labels:
type: db
spec:
replicas: 2
selector:
matchLabels:
type: db
template:
metadata:
name: mysql-pod
labels:
13 | P a g e
KUBERNETES MADHAV
type: db
spec:
containers:
- name: mydb
image: mysql:5
env:
- name: MYSQL_ROOT_PASSWORD
value: intelliqit
...
DaemonSet
DaemonSet: This is to run pods on every salve and only one slave per node.
vim daemonset.yml
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: nginx-daemon
labels:
type: proxy
spec:
selector:
matchLabels:
type: proxy
template:
metadata:
name: nginx-pod
labels:
type: proxy
spec:
containers:
- name: mynginx
image: nginx
ports:
- containerPort: 80
hostPort: 8080
...
===================================================================================
=======
Service Objects
=======================
1 NodePort:This is used to perform network load balancing
2 LoadBalancer: This will create an ip for the entire cluster and it works only on
managed kubernetes service
3 Clusterip: This is used to fro pods to communicate with other pods in the clsuter
but not with outside world
14 | P a g e
KUBERNETES MADHAV
Create a service definition file fro node port object and apply it on pod -
definition1.yml
vim service1.yml
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
author: intelliqit
spec:
type: NodePort
ports:
- targetPort: 80
port: 80
nodePort: 30008
selector:
type: proxy
vim service2.yml
---
apiVersion: v1
kind: Service
metadata:
name: jenkins-service
labels:
author: intelliqit
spec:
type: LoadBalancer
ports:
- targetPort: 8080
port: 8080
nodePort: 30009
selector:
type: ci-cd
author: intelliqit
...
vim service3.yml
---
apiVersion: v1
kind: Service
metadata:
name: postgres-service
labels:
author: intelliqit
spec:
ports:
- targetPort: 5432
port: 5432
selector:
type: db
author: intelliqit
Kompose
This is used to conver t a docker compose file to Kubernetes definition files
https://www.digitalocean.com/community/tutorials/how-to-migrate-a-docker-compose-
workflow-to-kubernetes
wordpress:
image: wordpress
ports:
- 8888:80
deploy:
replicas: 3
...
16 | P a g e
KUBERNETES MADHAV
Kubernetes Project
➔ This is a voting app created using python,this app is exposed to the customers
and they can cast their vote
➔ This info will be registered in a in memory db(temporary db) that we setup
using redis From here we have a .net application that filers the data and
stores it permenantly in a postgres db and the results can be viewed on an app
created using nodejs
Create 5 deployment definition files for all the above object and 4 service
definition file
vim voting-app-deployment.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: voting-app-deployment
labels:
name: voting-app
author: intelliqit
spec:
replicas: 2
selector:
matchLabels:
name: voting-app
template:
metadata:
name: voting-app-pod
labels:
name: voting-app
spec:
containers:
- name: voting-app
image: dockersamples/examplevotingapp_vote
...
vim result-app-deployment.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: result-app-deployment
labels:
name: result-app
author: intelliqit
spec:
replicas: 2
selector:
matchLabels:
name: result-app
template:
metadata:
name: result-app-pod
labels:
name: result-app
spec:
containers:
17 | P a g e
KUBERNETES MADHAV
- name: result-app
image: dockersamples/examplevotingapp_result
...
vim redis-app-deployment.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-app-deployment
labels:
name: redis-app
author: intelliqit
spec:
selector:
matchLabels:
name: redis-app
template:
metadata:
name: redis-app-pod
labels:
name: redis-app
spec:
containers:
- name: redis-app
image: redis
...
vim postgres-app-deployment.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres-app-deployment
labels:
name: postgres-app
author: intelliqit
spec:
selector:
matchLabels:
name: postgres-app
template:
metadata:
name: postgres-app-pod
labels:
name: postgres-app
spec:
containers:
- name: postgres-app
image: postgres
env:
- name: POSTGRES_PASSWORD
value: intelliiqt
- name: POSTGRES_USER
value: myuser
- name: POSTGRES_DB
value: mydb
...
18 | P a g e
KUBERNETES MADHAV
vim worker-app-deployment.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: worker-app-deployment
labels:
name: worker-app
author: intelliqit
spec:
selector:
matchLabels:
name: worker-app
template:
metadata:
name: worker-app-pod
labels:
name: worker-app
spec:
containers:
- name: worker-app
image: dockersamples/examplevotingapp_worker
...
vim voting-app-service.yml
---
apiVersion: v1
kind: Service
metadata:
name: voting-app-service
labels:
author: intelliqit
spec:
type: LoadBalancer
ports:
- targetPort: 80
port: 80
nodePort: 30008
selector:
name: voting-app
vim result-app-service.yml
---
apiVersion: v1
kind: Service
metadata:
name: result-app-service
labels:
author: intelliqit
spec:
type: LoadBalancer
ports:
- targetPort: 80
port: 80
nodePort: 30009
selector:
name: result-app
19 | P a g e
KUBERNETES MADHAV
vim redis-app-service.yml
---
apiVersion: v1
kind: Service
metadata:
name: redis-app-service
labels:
author: intelliqit
spec:
ports:
- targetPort: 6379
port: 6379
selector:
name: redis-app
...
vim postgres-app-service.yml
---
apiVersion: v1
kind: Service
metadata:
name: postgres-app-service
labels:
author: intelliqit
spec:
ports:
- targetPort: 5432
port: 5432
selector:
name: postgres-app
...
20 | P a g e
KUBERNETES MADHAV
KUBERNETES
PROMETHEUS
Prometheus is an open-source systems monitoring and alerting toolkit originally built at
SoundCloud. Since its inception in 2012, many companies and organizations have adopted
Prometheus, and the project has a very active developer and user community. Let’s dive into its
key features and architecture:
1. Features:
o Multi-dimensional Data Model: Prometheus uses a time series data model with
metrics identified by metric names and key-value pairs (labels).
o PromQL: A flexible query language that leverages this dimensionality for querying
and analysis.
o No Reliance on Distributed Storage: Prometheus operates with single server nodes,
making them autonomous.
o Pull Model for Time Series Collection: Metrics are collected via HTTP pulls.
o Service Discovery or Static Configuration: Targets (services) are discovered
dynamically or configured statically.
o Graphing and Dashboarding Support: Multiple modes for visualizing data.
3. Components:
o The Prometheus ecosystem includes:
▪ The main Prometheus server, responsible for scraping and storing time series
data.
▪ Client libraries for instrumenting application code.
▪ A push gateway for short-lived jobs.
▪ Special-purpose exporters for services like HAProxy, StatsD, and Graphite.
▪ An alertmanager to handle alerts.
4. Architecture:
o Prometheus scrapes metrics from instrumented jobs directly or via an intermediary
push gateway.
o It stores all scraped samples locally and runs rules to aggregate data or generate
alerts.
21 | P a g e
KUBERNETES MADHAV
GRAFANA
Grafana is an open-source platform for visualizing and analyzing data. It’s used for a wide range
of purposes, including performance analysis, business intelligence, and DevOps
dmonitoring. Organizations of all sizes, from small startups to large enterprises, utilize Grafana to
gain insights into their data and make informed decisions 12.
Here are some key points about Grafana:
• Visualization and Analysis: Grafana allows users to see their data via charts and
graphs that are unified into one dashboard (or multiple dashboards!) for easier
interpretation and understanding2.
• Data Integration: It provides integrated support for over 15 popular databases and
monitoring solutions, making it a versatile choice for data analytics3.
22 | P a g e