Kubernetes Objects — Pods, ReplicaSets, and Deployments
Pods
- Kubernete's ultimate goal is to deploy the application using containers on a set of machines they are configured as a Worker node.
- Kubernetes does not deploy containers directly to the worker node.
- The container is encapsulated in the Kubernetes object known as POD.
- Docker is the most common container runtime used in a Kubernetes Pod, but Pods support other container runtimes as well
- Each Pod is meant to run a single instance of a given application.
- If you want to scale your application horizontally, you should use multiple Pods, one for each instance.
- Each pod has it’s own Ip address.
How to create a pod
- Create a YAML file for pod
- vim <filename.yaml>
sample nginx.yaml file
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
name: nginx
spec:
containers:
- image: nginx
name: nginx
3. create a pod using below comment
kubectl create -f <filename.yaml>
4. To check pod is created or not.
kubectl get pod
5. To delete the exiting pod
kubectl delete pod <podname>
ReplicaSets
- A ReplicaSet’s purpose is to maintain a stable set of replica Pods running at any given time.
2. In the replica sets, we used the selector.
3. The selector is used to identifying the Pods it can acquire.
4. a number of replicas indicating how many Pods it should be maintaining.
5. pod template specifying the data of new Pods it should create to meet the number of replicas criteria, When a ReplicaSet needs to create new Pods, it uses its Pod template.
6. using replica set you can easily run multiple pods of given instances.
How to create a replica set
- Create a replica set yaml file.
- vim <filename.yaml>
E.X : nginxreplicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend
labels:
app: nginx
tier: frontend
spec:
# modify replicas according to your case
replicas: 3
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: nginxreplicaset
image: nginx
*here we set replicas = 3, so 3 pods must running at every time
2. create a replica set
kubectl create -f <filename.yaml>
3. You can then get the current ReplicaSets deployed:
kubectl get rs
4. To scale up the replica set.
kubectl scale --replicas=5 -f <filename.yaml>
5. To see you pods
kubectl get pod
Deployments
- A Deployment provides declarative updates for Pods and Replicaset.
- Deployments wrap up Pods and ReplicaSets into a nice package that is capable of deploying your applications.
- A deployment is an object in Kubernetes that lets you manage a set of identical pods.
- Without a deployment, you’d need to create, update, and delete a bunch of pods manually.
- With a deployment, you declare a single object in a YAML file. This object is responsible for creating the pods, making sure they stay up to date and ensuring there are enough of them running
- You can also easily autoscale your applications using a Kubernetes deployment.
- we also update the image in the pod and after updating the pod if the image is failing then we able to roll out it to the old image, as called as ”Rolling Back a Deployment ”
- using deployment we can undo deployment, pause deployment, resume deployment.
How to create a Deployment.
- create a deployment.yaml file
We will create a Deployment configuration file that defines the same state as our ReplicaSet example. Actually, you will notice that the only change between declaring a Deployment and a ReplicaSet is the kind
* sample Nginx deployment YAML
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
- First, the
replicas
key sets the number of instances of the pod that the deployment should keep-alive - Next, we use a label selector to tell the deployment in which pods are part of the deployment. This essentially says “all the pods matching these labels are grouped in our deployment.”
- pod template jammed inside our deployment spec. When the deployment creates pods, it will create them using this template.
2. Create the Deployment by running the following command:
kubectl create -f <filename.yaml>
3. Run kubectl get deployment
to check if the Deployment was created.
4. To see the Deployment rollout status, run
kubectl rollout status deployment <deploymentname>
5. Updating the deployment
e.x: Let’s update the Nginx Pods to use the nginx:1.16.1
image instead of the nginx:1.14.2
image.
kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1 --record
The output is similar to this:
deployment.apps/nginx-deployment image updated
6. Rolling Back a Deployment
1.when the Deployment is not stable, such as crash looping, you may want to rollback a Deployment.
2. Suppose that you made a typo while updating the Deployment, by putting the image name as nginx:1.161
instead of nginx:1.16.1
and your deployment getting crash or failed.
kubectl set image deployment.v1.apps/nginx-deployment nginx=nginx:1.161 --record=true
The output is similar to this:
deployment.apps/nginx-deployment image updated
*and after this, your deployment is failed because you select wrong image version.
- To fix this, you need to roll back to a previous revision of Deployment that is stable.
rolling back to the previous version.
kubectl rollout undo deployment.v1.apps/nginx-deployment
Alternatively, you can rollback to a specific revision by specifying it with --to-revision
:
kubectl rollout undo deployment.v1.apps/nginx-deployment --to-revision=2
7. Checking Rollout History of a Deployment
kubectl rollout history deployment.v1.apps/nginx-deployment
8. Scaling a deployment.
kubectl scale deployment.v1.apps/nginx-deployment --replicas=10
9. Pausing and Resuming a Deployment
kubectl rollout pause deployment.v1.apps/nginx-deployment
**After a pause the deployment, you can make many updates as u wish no new rollout started:
Resume the Deployment
kubectl rollout resume deployment.v1.apps/nginx-deployment
Thank you, if you have any doubt reach out to me
LinkedIn : linkedin.com/in/harshal-kathar-b2a19b118