Lesson 06
Deploying applications
~4 min read
Kubernetes manifests describe resources such as Deployments, Pods, and Services. In this lesson, you'll apply a Deployment manifest and inspect the Pods it creates.
To deploy an application in Kubernetes, you typically create a deployment resource. A deployment specifies the desired number of replicas of your application and manages their lifecycle. Kubernetes ensures that the specified number of replicas are running at all times, and if a replica goes down, it automatically creates a new one. Here is an example of a deployment resource:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app.kubernetes.io/name: my-nginx
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: my-nginx
template:
metadata:
labels:
app.kubernetes.io/name: my-nginx
spec:
containers:
- name: nginx
image: nginx:1.26.2
ports:
- containerPort: 80If you examine this deployment you see that it has a few interesting fields. It has the spec or specification which is our way of telling Kubernetes what we want out of our deployment such as the redundancy we want to achieve with a number of replicas (in this case only one for now) and the pod template for the nginx app.
The pod template has its own specification or spec and its main purpose is to tell Kubernetes which containers should run inside our pods. In some cases and maybe most cases pods only run a single container. In this template it is also possible to define initContainers. They are as the name might indicate containers which run when the pod initializes. This is useful for some preprocessing tasks such as database migrations.
Deployment → ReplicaSet → Pods
Deployment: nginx
ReplicaSet: nginx-7f8b6c
P1
P2
P3
In summary, deploying applications in Kubernetes offers scalability, resilience, and ease of management. By leveraging Kubernetes features and resources, you can ensure that your applications run smoothly in a containerized environment.
The sandbox above already has the manifest stored as nginx-deployment.yaml. Let's create a resource from it by running:
kubectl apply -f ./nginx-deployment.yaml
You should see a message saying deployment.apps/nginx-deployment created. Now verify that the deployment was created along with its pod:
kubectl get deploymentsand
kubectl get podsNote that this deployment was done in the default namespace and that is why we do not need to specify the namespace when we query. You can also inspect the deployment in detail with the describe command:
kubectl describe deployment nginx-deploymentTry running these commands in the sandbox above to explore the deployment you just created.
The Deployment now has running Pods. Next, create a Service so other applications can reach them through a stable address.
Self-healing: pod replacement
ReplicaSet: nginx-deployment-7f8b6c (desired: 3)
Click a pod to delete it
Try clicking a pod above to see Kubernetes' self-healing in action. When a pod dies, the ReplicaSet notices and creates a replacement automatically.