kubectl.
Table of Contents
- Verify Kubernetes Version
- Create a Deployment
- Expose the Deployment as a Service
- Display the Machine Name in the Web App
- Build and Tag a New Docker Image
- Redeploy Using Version 2
- Test the Load Balancer
- Command Reference Table
- Links and References
1. Verify Kubernetes Version
Before deploying, confirm thekubectl client and the local cluster version:
Keeping your
kubectl client in sync with the server helps avoid compatibility issues.2. Create a Deployment
Create a Deployment namedkodekloudapp using the kodekloudapp:v1 image with 2 replicas:
3. Expose the Deployment as a Service
Expose the Deployment on port 8080 (host) → 80 (container). On a local cluster this defaults to aNodePort service, but in cloud environments it provisions a load balancer.
On local clusters,
LoadBalancer maps to a NodePort. When you move to AKS, this will create a cloud load balancer.4. Display the Machine Name in the Web App
To confirm traffic distribution across replicas, update the Razor page to display the machine name. Edit Pages/Index.cshtml:5. Build and Tag a New Docker Image
After updating the code, build a new image taggedkodekloudapp:v2:
v1 and v2 tags:
6. Redeploy Using Version 2
First, clean up existing resources:7. Test the Load Balancer
Visit http://localhost:8080 in two different browser sessions. You’ll see the Machine field alternate between replica hosts, confirming load balancing across pods.8. Command Reference Table
| Command | Purpose |
|---|---|
kubectl version --short | Check client/server versions |
kubectl create deployment ... --replicas=2 | Deploy the application with 2 replicas |
kubectl get deployment | Verify Deployment status |
kubectl expose deployment ... --type=LoadBalancer | Expose Deployment as a Service |
kubectl get service | List Services and their endpoints |
kubectl delete service <name>kubectl delete deployment <name> | Clean up resources before redeployment |