k8's deployment
k8's deployment
A Deployment in Kubernetes is a way to manage and scale applications automatically. It ensures that the
desired number of application instances are running, updates them safely, and manages their lifecycle.
Definition of Kubernetes
It acts as an orchestrator, ensuring your applications run efficiently and reliably across
multiple environments, whether on-premises or in the cloud.
Deployment YAML
A Deployment manages ReplicaSets and ensures the desired number of Pods are running.
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-deployment
labels:
app: example-app
spec:
replicas: 3
selector:
matchLabels:
app: example-app
template:
metadata:
labels:
app: example-app
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 80
How It Works:
at
selector: Specifies the label used to identify the Pods managed by this Deployment.
template: The Pod specification, which includes metadata and container details
Ak
Example Output:
REVISION CHANGE-CAUSE
1 kubectl create
2 kubectl set image deployment/example-deployment nginx=nginx:1.22
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-deployment
annotations:
kubernetes.io/change-cause: "Updated to nginx version 1.22"
spec:
replicas: 3
selector:
matchLabels:
app: example-app
template:
metadata:
labels:
app: example-app
spec:
containers:
- name: nginx
image: nginx:1.22
Key Commands:
Kubernetes Deployments are crucial for managing modern containerized applications. Below
are practical use cases that demonstrate their utility:
1. Scaling Applications R
Scenario: An e-commerce site experiences high traffic during sales events.
N
Solution: Use a Deployment to scale the number of application Pods dynamically
ha
based on traffic.
Benefit: Ensures high availability and smooth user experience.
at
Command:
kubectl scale deployment ecommerce-app --replicas=10
sh
Ak
2. Rolling Updates
Solution: Use Deployment to perform a rolling update, gradually replacing old Pods
with new ones.
Benefit: Avoids service interruptions.
Command:
kubectl set image deployment/api-service api-container=v2.0.1
3. Rollbacks
5. Canary Deployments
Scenario: Test a new feature with a small group of users before full rollout.
Solution: Create a separate Deployment for the new version with limited replicas.
Benefit: Allows safe testing and feedback collection.
environments.
sh
If your deployment is stuck trying to deploy its request ReplicaSet without even
completing, here are some steps and points to troubleshoot and resolve the issue:
Troubleshooting Steps:
to resource constraints.
9. Rolling Back and Re-deploying:
sh
Review cluster-level logs and metrics to get insights into overall cluster health. Tools
like kubectl top and monitoring dashboards like Grafana can be useful.
Here’s an example of a Kubernetes Deployment YAML file for an Nginx server. This
Deployment will create multiple replicas of an Nginx container and expose it on a
specific port.
cpu: "100m"
memory: "128Mi"
limits:
at
cpu: "500m"
memory: "256Mi"
sh
Ak
1. apiVersion: Defines the API version of the Kubernetes resource (apps/v1 for
Deployments).
2. kind: Specifies the type of Kubernetes resource (Deployment in this case).
3. metadata:
o name: The name of the Deployment.
o labels: Key-value pairs used for identifying resources in selectors (app: nginx).
4. spec:
o replicas: The number of desired replicas of the Deployment. Setting it to 3 creates
three instances of the Nginx container.
o selector: A label selector to identify which pods belong to this Deployment.
o template: The pod specification that determines what a pod looks like when it is
created.
metadata under template: Inherited from the Deployment, used to
identify pods.
spec under template:
containers: Array of container specifications in the pod.
name: The name of the container.
image: The Nginx Docker image.
ports: Defines which ports the container will expose
(containerPort: 80 makes the Nginx server accessible on port
80).
resources: Defines resource requests and limits for CPU and
memory for the container.
To deploy an Nginx application using the YAML provided, you will need to follow a series
of commands to create and manage the Kubernetes Deployment. Here's a step-by-step guide
along with explanations for each command:
YAML Content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
R
labels:
app: nginx
N
spec:
replicas: 3 # Number of desired replicas
ha
selector:
matchLabels:
at
app: nginx
template: # The pod template to be created by the Deployment
sh
metadata:
labels:
app: nginx
Ak
spec:
containers:
- name: nginx
image: nginx:latest # Nginx container image and tag
ports:
- containerPort: 80 # Port that the container exposes
resources: # CPU and memory resource requests and limits
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "256Mi"
o Use the kubectl apply command to create the Deployment in your Kubernetes
cluster. This command reads the YAML file and applies it as a Deployment resource.
kubectl apply -f nginx-deployment.yaml
Explanation:
Output Example:
Explanation:
o To get more detailed information about the Deployment, including its status,
sh
conditions, and the pods it manages, use the kubectl describe deployment
command.
Ak
Explanation:
Explanation:
o To view the logs of the Nginx pods, use the kubectl logs command. Replace
<pod-name> with the actual name of the pod.
Explanation:
o To change the number of replicas (increase or decrease), use the kubectl scale
command.
o If you need to update the Nginx Deployment (e.g., change the image or
Ak
Explanation:
Replication Controller
Below is an example of a Replication Controller YAML file that creates and manages
multiple replicas of a sample Nginx container.
Replication Controller YAML Example:
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx-rc
labels:
app: nginx
spec:
replicas: 3 # Number of desired replicas
selector:
app: nginx
template: # The pod template to be created by the Replication Controller
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest # Nginx container image and tag
ports:
- containerPort: 80 # Port that the container exposes
resources: # CPU and memory resource requests and limits
requests:
cpu: "100m"
memory: "128Mi"
limits:
R
cpu: "500m"
N
memory: "256Mi"
ha
1. apiVersion: Defines the API version (v1 for core resources in Kubernetes).
sh
Once the Replication Controller is created, you can manage it using the following commands:
Output Example:
l app=nginx.
sh
Pods Not Starting: Check the logs of individual pods using kubectl logs <pod-name> to
identify why they are not starting.
Resource Constraints: Ensure that the resources (CPU, memory) allocated to the pods are
not exceeded.
Network Issues: Verify network connectivity for the pods to communicate with each other
and with external services.
By using Replication Controllers, Kubernetes ensures that your applications remain highly
available, scalable, and resilient to failures.
ReplicaSet
Creating a ReplicaSet R
Below is an example of a ReplicaSet YAML file for creating and managing multiple replicas
N
of a sample Nginx container.
ha
apiVersion: apps/v1
kind: ReplicaSet
sh
metadata:
name: nginx-rs
Ak
labels:
app: nginx
spec:
replicas: 3 # Number of desired replicas
selector:
matchLabels:
app: nginx
template: # The pod template to be created by the ReplicaSet
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest # Nginx container image and tag
ports:
- containerPort: 80 # Port that the container exposes
resources: # CPU and memory resource requests and limits
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "256Mi"
Explanation of the ReplicaSet YAML:
Managing a ReplicaSet:
R
N
Once the ReplicaSet is created, you can manage it using the following commands:
ha
Output Example:
Pods Not Starting: Check the logs of individual pods using kubectl logs <pod-name> to
understand why they are not starting.
Resource Constraints: Ensure that the resources (CPU, memory) allocated to the pods are
not exceeded.
Network Issues: Verify network connectivity for the pods to communicate with each other
and with external services.
By using ReplicaSets, Kubernetes ensures that your applications remain highly available,
R
scalable, and resilient to failures, similar to the functionality of Replication Controllers but
with more flexibility and ease of use.
N
replicas are running at all times. pods, with more flexibility and automation.
Ak
Deployment Primarily used for static Suited for dynamic deployments, including
Strategy deployments rolling updates and scaling.
Summary:
Replication Controller is suitable for simpler, static applications that do not require
advanced scaling or automated updates. It uses basic label selectors and manual scaling.
ReplicaSet is designed for more complex applications that require automation, rolling
updates, and self-healing capabilities. It supports more advanced label selectors and scaling
R
features, making it more flexible for dynamic deployments.
N
Here are some interview questions that you might encounter regarding Kubernetes and its
components like Deployments, Replication Controllers, and ReplicaSets:
ha
o Discuss their purpose, how they manage the lifecycle of pods, and the advantages
and disadvantages of each.
o Talk about the benefits of using ReplicaSets over Replication Controllers, such as
scalability, ease of use, and more advanced features.
2. What is a Deployment in Kubernetes? How does it work?
o Describe what a Deployment is used for.
o Discuss how Deployments manage the rollout and rollback of applications.
o Explain how you can monitor the status and health of a Deployment.
3. Describe a scenario where you might choose to use a ReplicaSet over a
Replication Controller.
o Discuss specific use cases where ReplicaSets offer more flexibility or are better
suited.
4. How do you troubleshoot issues with a ReplicaSet or a Deployment in
Kubernetes?
o Walk through the steps you would take to diagnose and resolve common issues such
as pods not starting, deployment rollback, or resource constraints.
o Mention commands like kubectl describe, kubectl logs, kubectl get
events, and kubectl rollout status.
5. What are the key benefits of using ReplicaSets in Kubernetes?
o Discuss their role in maintaining a stable state, scaling, and replacing failed pods
automatically.
o Talk about how ReplicaSets can simplify management over individual pod
management.
6. How would you scale a Deployment or ReplicaSet in Kubernetes?
o Explain the commands you would use (kubectl scale, kubectl set image)
and the implications of scaling up or down.
o Discuss considerations around resource availability and pod placement.
7. What are the typical problems you may face when dealing with deployments in
Kubernetes?
o Discuss common issues like pods in CrashLoopBackOff, insufficient resources,
networking problems, and how to resolve them.
8. Explain how Kubernetes ensures high availability for applications.
o Discuss the role of Deployments, Replication Controllers, and ReplicaSets in ensuring
high availability.
o Mention pod distribution, load balancing, and how Kubernetes manages failover.
9. How do you monitor the health of a Kubernetes cluster and its resources?
o Talk about tools like kubectl top, Prometheus, and Grafana.
o Explain how monitoring can help in scaling applications and troubleshooting issues.
10. Explain the kubectl rollout status command and how it is used.
o Discuss its purpose in monitoring the deployment status.
o Describe how you would handle a stuck rollout and what kubectl rollout undo
R
does.
N
ha
at
sh
Ak