Skip to content

Instantly share code, notes, and snippets.

@vishal-kvn
Created March 28, 2019 19:19
Show Gist options
  • Select an option

  • Save vishal-kvn/c8c920a2f6658bafc49d7c2b8bd09d24 to your computer and use it in GitHub Desktop.

Select an option

Save vishal-kvn/c8c920a2f6658bafc49d7c2b8bd09d24 to your computer and use it in GitHub Desktop.
Kubernetes cheat sheet
minikube version
minikube start
kubectl version
kubectl cluster-info
kubectl get nodes
# Deploying app example 1
kubectl run kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 --port=8080
kubectl get deployments
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
kubectl proxy
curl http://localhost:8001/version
curl http://localhost:8001/api/v1/namespaces
# Deploying app example 2
kubectl create deployment hello-node --image=gcr.io/hello-minikube-zero-install/hello-node
kubectl get deployments
kubectl get pods
kubectl get events
kubectl config view
# By default, pod is only accessible by it's internal IP within the cluster. In order to make the container accessible
# from outside the k8s virtual network, the pod has to be exposed as a k8s service.
kubectl expose deployment hello-node --type=LoadBalancer --port=8080 # creates a service
kubectl get services
minikube service hello-node
# Troubleshoting with kubectl
kubectl get # list resources
kubectl describe # show detailed information about a resource
kubectl logs # print logs from container in a pod
kubectl exec # execute a command on a container in a pod
kubectl get pods
kubectl describe pods
# Troubleshooting using kube proxy
kubectl proxy # run this in a new terminal
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/proxy/
kubectl logs $POD_NAME
# Executing commands on the container
kubectl exec $POD_NAME env
kubectl exec -ti $POD_NAME bash
cat server.js
curl localhost:8000
exit
# Each pod has it's own uniqyue IP even if it is on the same node
# A service groups are the related pods using labelSelector
# Creating a new service
kubectl get pods
kubectl get services
kubectl kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080 # this creates a new service with name: kubernetes-bootcamp
kubectl describe services/kubernetes-bootcamp
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
curl $(minikube ip):$NODE_PORT
# Using labels
kubectl describe deployment
kubectl get pods -l run=kubernetes-bootcamp # -l is the label name from the kubectl describe deployment command
kubectl get services -l run=kubernetes-bootcamp
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
kubectl label pod $POD_NAME app=v1 # labeling the pod
kubectl describe pod $POD_NAME # the new label app=v1 should appear in the Labels column of the pod
kubectl get pods -l app=v1 # this allows for pod lookup using the label app=v1
# Deleting a service
kubectl delete service -l run=kubernetes-bootcamp
kubectl get services
curl $(minikube ip):$NODE_PORT # this should no longer be reachable outside the cluster
kubectl exec -ti $POD_NAME curl localhost:8080 # app should still be running inside the container
# Scaling a deployment
kubectl get pods
kubectl scale deployments/kubernetes-bootcamp --replicas=4
kubectl get pods # this should increase the number of pods to 4
kubectl get pods -o wide
kubectl describe services/kubernetes-bootcamp
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
$ echo NODE_PORT=$NODE_PORT
curl $(minikube ip):$NODE_PORT
kubectl scale deployments/kubernetes-bootcamp --replicas=2 # scaling down deploys
kubectl get deployments
kubectl get pods -o wide
# Update the version of the app aka rolling updates
kubectl get deployments
kubectl get pods
kubectl describe pods
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2 # set image deploys the new container image of the app
# Verify an update
kubectl describe services/kubernetes-bootcamp
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
curl $(minikube ip):$NODE_PORT
kubectl rollout status deployments/kubernetes-bootcamp
kubectl describe pods
# Rollback an update
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10
kubectl get deployments
kubectl get pods
kubectl describe pods
kubectl rollout undo deployments/kubernetes-bootcamp ## main command to rollback - `rollout undo`
kubectl get pods
kubectl decribe pods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment