svg
Post Image
By Daniel Tanque3 de Novembro, 2023In Sem categoria

Kubernetes: Pods, Replicas and Deployments

Kubernetes is a container orchestration platform that allows you to manage and scale containerized applications. To understand Kubernetes Pods, Replicas, and Deployments, it’s essential to know their roles and how they work together. In this article you will learn more about them.

Pods

In terms of pods you know that they are the ones to host the containers.

To know how many pods there are you run:

kubectl get pods 

To create a new pod with nginx image:

kubectl run nginx --image=nginx
kubectl describe pod <pod-name>

To get in which node they are operating on:

kubectl get pod -o wide <pod-name>

On that we also can see the ready (Running Containers/Total Containers)

To delete a pod you run:

kubectl delete pod <pod-name>

To generate a manifest file with a specific image you run:

kubectl run redis --image=<image name> --dry-run=client -o yaml > <yml file name>

To create the pod or whatever is define in the yaml file you have:

kubectl create -f <name of the yaml file>

Replicas

In older versions it was used ReplicaController now it’s more used ReplicaSet.

To see all the replicas you run:

kubectl get replicaset

Now why do you need labels and selectors? Because we have 3 pods we want 3 replicas running at any point and any time, so when one fails immediately changes. A ReplicaSet has the goal to monitor the pods and if any fails deploy a new one.

So to keep the track on the pods which to focus on and what to do, that’s why labels can have tier or other data at the selector, matchLabels and tier. It connects with metadata, labels and tier.

How to scale

Option 1

kubectl replace -f <replica file yml>

Option 2

kubectl scale --replicas=<number> -f <yml>
kubectl scale --replicas=<number> replicaset <app name>

Note: but this doesn’t change the settings on the file.

Deploying

The structure of a replicaset is almost equal to the structure of a deployment. It mainly changes the kind to “Deployment”.

kind: Deployment

An the D has to be uppercase letter.

So when there’s a rollout there are two possible strategies:

  • recreate strategy (scale all to 0 and than scale all to the limit)
  • rolling strategy (scale one pod at a time)

The commands here used are:

kubectl create -f  <yml>
kubectl get deployments
kubectl apply -f <yml>
kubectl set image <name of the image> <image>=<image version>
kubectl rollout status
kubectl rollout history
kubectl rollout undo

Conclusion

And now you know how to handle pods, how to create replicas and keep your system running and how to deploy.

svgYAML in Kubernetes
svg
svgKubernetes Services

Leave a reply