In this lesson, we will deploy a simple application on Kubernetes and understand the deployment process. By the end of this lesson, you will have a hands-on understanding of how to create a deployment, manage replicas, and expose your application.
A Deployment in Kubernetes is a resource that provides declarative updates to applications. It manages the creation and scaling of a set of Pods, ensuring that the desired state of the application is maintained.
To create a Deployment, you need to define it in a YAML file. Here’s a simple example of a Deployment that runs an Nginx web server:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
To deploy the application, use the kubectl command-line tool:
kubectl apply -f nginx-deployment.yaml
To check the status of your Deployment, run:
kubectl get deployments
To see the Pods created by the Deployment, use:
kubectl get pods
To access your application from outside the cluster, you need to expose it using a Service. Here’s how to create a Service for the Nginx Deployment:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
selector:
app: nginx
ports:
- port: 80
targetPort: 80
nodePort: 30001
Deploy the Service with:
kubectl apply -f nginx-service.yaml
You can access your Nginx application by navigating to http://<Node_IP>:30001 in your web browser. Replace <Node_IP> with the IP address of your Kubernetes node.
Best Practice: Always specify resource requests and limits for your containers to ensure they have enough resources to run effectively.
Common Mistake: Forgetting to apply the YAML file after making changes. Always run
kubectl apply -f <filename>to apply updates.
kubectl apply to deploy and manage your application.kubectl get pods.kubectl commands.