How to Create Deployment in Kubernetes: Simple Guide
To create a deployment in Kubernetes, use the
kubectl create deployment command followed by the deployment name and container image. This command sets up a deployment resource that manages your app's pods automatically.Syntax
The basic syntax to create a deployment is:
kubectl create deployment <deployment-name> --image=<container-image>
Here, <deployment-name> is the name you give your deployment, and <container-image> is the Docker image your pods will run.
bash
kubectl create deployment my-app --image=nginx:1.23.3Example
This example creates a deployment named my-nginx running the nginx:1.23.3 image. Kubernetes will create pods to run this container and manage their lifecycle.
bash
kubectl create deployment my-nginx --image=nginx:1.23.3
kubectl get deployments
kubectl get podsOutput
deployment.apps/my-nginx created
NAME READY UP-TO-DATE AVAILABLE AGE
my-nginx 1/1 1 1 10s
NAME READY STATUS RESTARTS AGE
my-nginx-6d4cf56db6-abcde 1/1 Running 0 10s
Common Pitfalls
Common mistakes when creating deployments include:
- Using an incorrect image name or tag, causing pod failures.
- Not specifying the image, which leads to errors.
- Forgetting to check pod status after creation.
Always verify your deployment and pods with kubectl get deployments and kubectl get pods.
bash
kubectl create deployment my-app # Error: required flag(s) "image" not set # Correct way: kubectl create deployment my-app --image=nginx:1.23.3
Quick Reference
Tips for creating Kubernetes deployments:
- Use
kubectl create deployment <name> --image=<image>to create quickly. - Check deployment status with
kubectl get deployments. - Check pods with
kubectl get pods. - Update deployments with
kubectl set image deployment/<name> <container-name>=<new-image>.
Key Takeaways
Use 'kubectl create deployment --image=' to create a deployment easily.
Always specify a valid container image to avoid pod startup errors.
Verify deployment and pod status with 'kubectl get deployments' and 'kubectl get pods'.
Common errors include missing image flag and wrong image names.
You can update deployments later using 'kubectl set image'.