In this note i will show how to create a Deployment from the command line using the kubectl
command.
Below you will find the examples of how to deploy an Nginx Docker image on a Kubernetes cluster and how to update and scale it using the kubectl
command only (without YAML configs).
I will also show how to create a Service to expose the created Deployment outside the Kubernetes cluster.
Cool Tip: Get Pod’s logs using the kubectl
command! Read more →
Create Deployment
Deploy the nginx:1.19
Docker image on a Kubernetes cluster, by creating a Deployment using the kubectl
command:
$ kubectl create deployment nginx-depl --image=nginx:1.19 deployment.apps/nginx-depl created
To ensure the Deployment is created and the Pod is running, execute:
$ kubectl get deployment -o wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR nginx-depl 1/1 1 1 16s nginx nginx:1.19 app=nginx-depl $ kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-depl-5fd5bfb4cf-m9s4z 1/1 Running 0 31s 172.17.0.2 minikube
Update Deployment
To update the Deployment, e.g. update the Nginx image version to 1.21
, execute:
$ kubectl set image deployment/nginx-depl nginx=nginx:1.21 deployment.apps/nginx-depl image updated $ kubectl get deployment -o wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR nginx-depl 1/1 1 1 4m50s nginx nginx:1.21 app=nginx-depl
Scale Deployment
To scale the Deployment, e.g. create one more replica Pod:
$ kubectl scale --replicas=2 deployment/nginx-depl deployment.apps/nginx-depl scaled $ kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-depl-68fc9c8dfb-9sf5z 1/1 Running 0 20m 172.17.0.3 minikube nginx-depl-68fc9c8dfb-nvsbw 1/1 Running 0 10s 172.17.0.2 minikube
Cool Tip: List & Change Namespaces in Kubernetes! Read more →
Create Service
Create a Service to expose the Deployment outside the cluster:
$ kubectl create service nodeport nginx-depl --tcp=80:80 service/nginx-depl created
The command above exposes the nginx
Service on each Node’s IP (NodeIP
) at a static port (NodePort
) in the range 30000-32768
, by default:
$ kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4m6s nginx-depl NodePort 10.100.217.168 <none> 80:30915/TCP 6s
To find out the NodeIP
, execute:
$ kubectl get nodes -o wide NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP minikube Ready control-plane,master 12d v1.20.2 192.168.99.102 <none>
To access the nginx
Service, from outside the cluster, open the <NodeIP>:<NodePort>
in a web-browser or simply call it using curl
:
$ curl 192.168.99.102:30915 ... <h1>Welcome to nginx!</h1> ...
CleanUp
Delete the Deployment (also deletes the Pods) and the Service:
$ kubectl delete deployment nginx-depl deployment.apps "nginx-depl" deleted $ kubectl delete service nginx-depl service "nginx-depl" deleted