Low Orbit Flux Logo 2 F

Kubernetes - Kubectl - Tasks and Commands

Read or at least skim over the Kubernetes Concepts Document first.

Common Commands

kubectl get list resources
kubectl describe show resource info
kubectl logs print container logs in a pod
kubectl exec run command on container in a pod

More common commands:

kubectl version  
kubectl cluster-info  
kubectl cluster-info dump  
kubectl get nodes  
kubectl get deployments  
kubectl describe deployment  
kubectl get pod  
kubectl get pods  
kubectl get pods –output=wide more info, including IPs
kubectl describe pods  
kubectl get events  
kubectl config view  
kubectl get svc  
kubectl get services  
kubectl describe services/nginx  
kubectl get pod,svc -n kube-system view pod,svc for kube-system
kubectl delete service hello-node  
kubectl delete deployment hello-node  


kubectl logs my_pod1  
kubectl exec my_pod1 – env run command on pod
kubectl exec -ti my_pod1 – bash get a shell on the pod’s container

Creating Deployments / Exposing Services

Expose a service using NodePort:

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --type=NodePort --port=8080

Expose a service using LoadBalancer:

kubectl create deployment nginx2 --image=nginx
kubectl expose deployment nginx2 --type=LoadBalancer --port=8080

Show containers names:

kubectl get pods nginx-85b98978db-w587v -o jsonpath='{.spec.containers[*].name}'
kubectl get pods -o jsonpath='{.items[*].spec.containers[*].name}'

Create a deployment using this image:

kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1

Connection from host to cluster:

kubectl proxy    
curl http://localhost:8001/version

Access pod through API

export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')

curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/

Route to pod API:

curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/proxy/

export NODE_PORT=$(kubectl get services/nginx -o go-template='{{(index .spec.ports 0).nodePort}}')

curl localhost:$NODE_PORT


Get pods and services with a label:

kubectl get pods -l app=kubernetes-bootcamp
kubectl get services -l app=kubernetes-bootcamp

Get pod name:

export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')

Label a pod:

kubectl label pods $POD_NAME version=v1

kubectl describe pods $POD_NAME

kubectl get pods -l version=v1


Delete service by label:

kubectl delete service -l app=nginx

Access application from within container:

kubectl exec -ti $POD_NAME -- curl localhost:8080


Show replica set ( desired vs current ):

kubectl get rs     

Scale it up:

kubectl scale deployments/kubernetes-bootcamp --replicas=4

Check it:

kubectl get deployments
kubectl get pods -o wide
kubectl describe deployments/kubernetes-bootcamp
kubectl describe services/kubernetes-bootcamp

Check, see a different node each time:

export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
curl localhost:$NODE_PORT

Scale down:

kubectl scale deployments/kubernetes-bootcamp --replicas=2

Updates / Rollouts

Update the image used, will be done as rolling update:

kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2

kubectl rollout status deployments/kubernetes-bootcamp  # check status of rollout

kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10    # another roll out

Check status of rollout:

kubectl get deployments
kubectl get pods
kubectl describe pods

Rollback / undo a rollout

kubectl rollout undo deployments/kubernetes-bootcamp  

Even More

These are some notes from where I left of testing.

Describe pod:

kubectl describe pod nginx2-85bf7b8976-5xvp8

Output snippet:

Node:         swan2/   # node IP
IP:            # pod / container IP

Cluster IP but no external IP:

kubectl get svc

Output snippet:

NAME         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
nginx2       NodePort    \<none\>        8080:31870/TCP   62m

Describe service:

kubectl describe svc nginx2

Output snippet:

IP:                      # cluster IP
Endpoints:            # pod container IP


Left off here with testing: