Deploy a Sample Application
In this step, you'll learn how to create and deploy a simple Kubernetes application using YAML manifests. We'll create both a Pod and a Deployment to demonstrate different ways of deploying applications.
First, create a directory for your Kubernetes manifests:
mkdir -p ~/project/k8s-manifests
cd ~/project/k8s-manifests
Create a simple NGINX Pod manifest:
nano nginx-pod.yaml
Add the following content:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Press Ctrl+X
, then Y
, and Enter
to save and exit.
Now, create a Deployment manifest:
nano nginx-deployment.yaml
Add the following content:
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:latest
ports:
- containerPort: 80
Apply the manifests to create the resources:
kubectl apply -f nginx-pod.yaml
kubectl apply -f nginx-deployment.yaml
Example output:
pod/nginx-pod created
deployment.apps/nginx-deployment created
Verify the created resources:
kubectl get pods
kubectl get deployments
Example output:
NAME READY STATUS RESTARTS AGE
nginx-pod 1/1 Running 0 1m
nginx-deployment-xxx-yyy 1/1 Running 0 1m
nginx-deployment-xxx-zzz 1/1 Running 0 1m
nginx-deployment-xxx-www 1/1 Running 0 1m
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 1m
Wait for the deployment to create the replicas. The READY
column should show 3/3
when all replicas are ready.
Key differences between Pod and Deployment:
- Pod: Single instance of an application
- Deployment: Manages multiple replicas and provides self-healing