Note:
All 3 templates are same as the liveness probe except:
use readinessProbe:
instead of livenessProbe:
When working with a tool like kubernetes it is very important to make a good friendship with its documentation link
to setup kubernetes we will need two main components
First we need to install kubectl through which we will manage kubernetes.
if you are using linux just copy and paste these commands one by one.
sudo apt-get install curl
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
To test run below command
kubectl version --client
Now we will install minikube, when using minikube 8Gb RAM is recommended.
if you are using linux then just copy and paste these commands one by one.
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
chmod +x minikube
sudo mkdir -p /usr/local/bin/
sudo install minikube /usr/local/bin/
In this step we will install virtual box which is optional, if you want to use virtual box then make sure your machine supports virtualization
To check that your system supports virtualization or not run the command below
grep -E --color 'vmx|svm' /proc/cpuinfo
sudo apt-get update
sudo apt-get upgrade
wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add -
wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O- | sudo apt-key add -
sudo add-apt-repository "deb http://download.virtualbox.org/virtualbox/debian xenial contrib"
sudo apt-get update
sudo apt-get install virtualbox
Lets check that we installed all 3 or 2 main tools correctly or not.
Run the commands that is based on your criteria.
if one fails because of any error use the command below and then try next command.
minikube delete
minikube start
above command will start minikube using virtual box
minikube start --driver=none
if you have not installed virtual box then use the above command, if it also not works the try the below command.
minikube start --driver=docker
above command will start minikube using docker
If none of the above command worked for you then try adding --force flag to command
example :
minikube start --driver=docker --force
If minikube starts then you can type the below command to get its status
minikube status
this command will return result similler to
host: Running
kubelet: Running
apiserver: Running
Now its all set to us kubernetes :-)
it is a resource of Kubernetes that isolates container or group of containers, every pod has its own ip address and each container in it has different port number.
kind: Pod
apiVersion: v1
metadata:
name: <pod-name>
spec:
containers:
- name: <container-name>
image: <image>
ports:
- containerPort: 80
kubectl run <name of pod> --image=<name of the image from registry>
kubectl port-forward <pod-name> <desired-port>:<pod-exposed-port>
kubectl get pods --show-labels // display all pods with their labels
kubectl get pods -L <label key> // display pods with the spicfied label key as heading
kubectl get pods -l <label key>=<value> // display pods with the spicfied label key & value
kubectl get pods -l '<label key>in<(val1,val2)>' --show-labels // display pods with the spicfied label key & value could be either of them
it is a resource of kubernetes that maintain the number of currently running copies of a pod.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend
labels:
app: guestbook
spec:
# modify replicas according to your case
replicas: 3
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: <container-name>
image: <image>
ports:
- containerPort: 80
kubectl create -f <name of yaml file>
kubectl get rs
it will increase or decrease the number of pods that are managed by a ReplicaSet
kubectl scale rs <ReplicaSet-name> --replicas=<desired-number>
This command will delete replicaset without deleting pods generated by that replica set
kubectl delete rs <Replicaset-name> --cascade=false
it is a resource of kubernetes that creates a pod that does a specific task and gets terminated.
apiVersion: batch/v1
kind: Job
metadata:
name: <job-name>
spec:
template:
spec:
containers:
- name: <container-name>
image: <image>
command: ["echo", "Hello world"]
restartPolicy: Never
backoffLimit: 4
it is a resource of kubernetes that creates a job that can repeat after certain time.
first it creats a job then a pod that does the specified work.
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: <cron job name>
spec:
schedule: "*/1 * * * *"
jobTemplate: # job
spec:
template: # pod template
spec:
containers:
- name: <container-name>
image: <image>
command: ["echo", "Hello world"]
restartPolicy: OnFailure
Note : here ***** reprisents minute | hour | day | month | year
it is a resource of kubernetes that allows us to manage routing.
There are mainly 2 type of services
apiVersion: v1
kind: Service
metadata:
name: <service-name>
spec:
selector:
<label-key>: <label-value>
ports:
- port: <Pod-port>
targetPort: <container-exposed-port>
type: LoadBalancer
For this you should have a ReplicaSet managing one or more pods.
kubectl expose rs <ReplicaSet-name> --name=<Service-name> --selector=<label-key>=<label-value> --port=<Pod-port> --target-port=<container-exposed-port> --type=LoadBalancer
Probe means to inspect,probe checks our application again and again after specific period of time to check if it is working correctly.
link to official documentation of probes link
There are 2 types of Probes
liveness probe checks our application again and again after a spicific period of time to check if it is working correctly
If it fails it restarts the container.
There are 3 types of liveness probe
apiVersion: v1
kind: Pod
metadata:
labels:
<key>: <value>
name: <Pod-name>
spec:
containers:
- name: <container-name>
image: <container-image>
livenessProbe:
httpGet:
path: / #root
port: <Pod-port or container-port>
initialDelaySeconds: <delay before first check>
periodSeconds: <time to wait after each check>
apiVersion: v1
kind: Pod
metadata:
name: <pod-name>
spec:
containers:
- name: <container-name>
image: <container-image>
ports:
- containerPort: 8080
livenessProbe:
tcpSocket:
port: <container-port>
initialDelaySeconds: <delay before first check>
periodSeconds: <time to wait after each check>
apiVersion: v1
kind: Pod
metadata:
name: <pod-name>
spec:
containers:
- name: <container-name>
image: <container-image>
livenessProbe:
exec:
command:
- <command eg: ls>
initialDelaySeconds: <delay before first check>
periodSeconds: <time to wait after each check>
Readiness checks our application again and again after a spicific period of time to check if its is working correctly
If it fails it restricts access to container.
There are 3 types of readiness probe
All 3 templates are same as the liveness probe except:
use readinessProbe:
instead of livenessProbe:
actually it is not a resource of kubernetes it is mostly written or defined in the spec: of Pods.
it actually helps us to share data between containers or whole cluster, not only that but also allow us to retain data if the pod or the container is deleted. you can say that it store & share data on the pod level
there are many types of volumes.
it is usually used to share data b/w two or more containers in a single pod, as it makes a directory shared b/w containers.
apiVersion: v1
kind: Pod
metadata:
name: <pod-name>
spec:
volumes:
- name: <volume-name>
emptyDir: {}
containers:
- image: <container-image>
name: <container-name>
volumeMounts:
- mountPath: <path> #path of dir to share
name: <volume-name> #that is mentioned above
- image: <container-image>
name: <container-name>
volumeMounts:
- mountPath: <path> #path of dir to share
name: <volume-name> #that is mentioned above
presistant volumes are a little different than other volumes as the store & share data on the cluster level
it also works a little differently. Below are the steps to use and work with persistent volume (pv).
you can access host file system by minikube ssh
command
apiVersion: v1
kind: PersistentVolume
metadata:
name: <pv-name>
spec:
capacity:
storage: <storage size eg: 5Gi>
hostPath:
path: <path> #pth where to store data on cluster
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: <Recycle or Retain or Delete>
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: <pvc-name>
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: <storage size eg: 100M>
storageClassName: ""
apiVersion: v1
kind: Pod
metadata:
name: <pod-name>
spec:
volumes:
- name: <volume-name>
persistentVolumeClaim:
claimName: <pvc-name> #use pvc name that you have created
containers:
- name: <container-name>
image: <container-image>
volumeMounts:
- mountPath: <path> #path of dir to share
name: <volume-name> #that is mentioned above
In this section we will see tools for configuration and deployments provided by kubernetes.
It is a resource that can hold some configurations of your application so that you don't have to mess with application code to make some configuration changes
There are two types of config map.
kubectl create configmap <resource-name> --from-literal=<key1>=<val1> --from-literal=<key2>=<val2>
kubectl create cm <resource-name> from-file=<file-name.txt>
kind: Pod
apiVersion: v1
metadata:
name: <pod-name>
spec:
volumes:
- name: <volume-name>
configMap:
name: <confifMap-name>
containers:
- name: <container-name>
image: <image>
ports:
- containerPort: 80
volumeMounts:
- name: <volume-name>
mountPath: /path/to/mount
kubectl create cm <resource-name> from-env-file=<file-name.env>
kind: Pod
apiVersion: v1
metadata:
name: <pod-name>
spec:
containers:
- name: <container-name>
image: <image>
ports:
- containerPort: 80
envFrom:
- configMapRef:
name: <configMap-name>
It is a resource that can hold some secret configurations that your app needs to run such as api keys, passwords, access tokens etc
There are two types of secets.
kubectl create secret generic <resource-name> --from-literal=<key1>=<val1> --from-literal=<key2>=<val2>
Method to write and create secreat env or txt file are same as configMap.
except:
secretRef:
instead of configMapRef:
when creating Pod through env file.kubectl create secret generic <resource-name> from-env-file=<file-name.env>
command when creating through env file.secret:
instead of configMap:
& secretName:
instead of name:
when creating Pod through txt file.kubectl create secret generic <resource-name> from-file=<file-name.txt>
command when creating through txt file.
It is a resource of kuberenetes that helps us to deploy update application very easily by providing some strategies.
basically it makes a replica-set and manages that. it does not interact with any pod directly. link to documentation
apiVersion: apps/v1
kind: Deployment
metadata:
name: <deployment-name>
labels: #optional
<key>: <value>
spec:
replicas: <number-of-pods>
selector: #optional
matchLabels:
<key>: <value>
template:
metadata:
labels: #optional
<key>: <value>
spec:
containers:
- name: <container-name>
image: <image>
ports:
- containerPort: 80
strategy:
type: <Recreate> or <RollingUpdate>
It is the default stratergy of kubernetes deployment, according to it when we update any thing one old pod is deleted and one updated pod is created this
process is repeated until all pods are replaced by newer version.
we need to define some properties when using it. Those properties are listed below.
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: <any-number>
maxUnavailable: <any-number>
In this strategy all pods all deleted at once and then newwer pods are created.
in short your application have to face some down-time.
In the last it should look like:
strategy:
type: Recreate
Properties of kubernetes resources that can be added to any resource.
labels are key:value pair that help us to group different resources, we can perform different opperations on those resources using labels.
labels are defined under metadata of a resource. There can be one or more labels of a resource.
e.g type: frontend
kubectl label <resource-type> <resource-name> <key>=<value>
kubectl label <resource-type> <resource-name> <key>-
Annotations are also key:value pair but it is used to give a describe a resource or give any information about the resource.
annotations are also defined under metadata of a resource. There can be one or more labels of a resource.
e.g purpose: "this resource can do _____ work"
kubectl annotate <resource-type> <resource-name> <key>="<value>"
kubectl annotate <resource-type> <resource-name> <key>-
This command helps to get information about any resource in kubernetes in json or yaml format.
it is mostly used to debug a resource.
kubectl get <resource-type> <resource-name> -o <formet json or yaml>
kubectl describe <resource-type> <resource-name>
This command helps to change or edit configuration or properties of a resource while it is running.
kubectl edit <resource-type> <resource-name>
This command deletes a resource.
kubectl delete <resource-type> <resource-name>