All-Things-Docker-and-Kubernetes

Kubernetes Commands

Pods and Parameters

Pods Description  
  kubectl completion --help To search for “enabling completion”
  source <(kubectl completion bash) To enable completion for kubectl
  kubectl api-resources To list out shortname for resources
  kubectl get pods Get information about running pods in current namespace
  kubectl describe pod </code> Describe one pod
  kubectl expose pod --port=444 --name=frontend </code> Expose the port of a pod (creates a new service)
  kubectl port-forward 8080 </code> Port forward the exposed pod port to your local machine
  kubectl attach -i </code> Attach to the pod
  kubectl exec -- command </code> Execute a command on the pod
  kubectl exec -it pod-name bash SSH onto the pod
  kubectl label pods mylabel=awesome </code> Add a new label to a pod
  kubectl run -i --tty busybox --image=busybox --restart=Never -- sh Run a shell in a pod - very useful for debugging
Parameters Description  
  --all-namespaces To display resource in all namespaces
  -l LABEL-NAME To filter for a specific LABEL-NAME
  -l LABEL-NAME1,LABEL-NAME2 For filtering pods that has specific label names
  --sort-by=metadata.creationTimestamp To sort the Pods by age
  --output=yaml To dusplay output in YAML format
  --o yaml To dusplay output in YAML format, can also use JSON
  --wide To dusplay more information in the output
  --show-labels show labels attached to those pods

kubectl create vs. kubectl apply

kubectl create is what we call Imperative Management. On this approach you tell the Kubernetes API what you want to create, replace or delete, not how you want your K8s cluster world to look like.

Example:

kubectl create deployment my-deployment --image=nginx

kubectl apply is part of the Declarative Management approach, where changes that you may have applied to a live object (i.e. through scale) are “maintained” even if you apply other changes to the object.

Example:

kubectl apply -f my-manifest.yaml 

The main difference is that if the resource exists, kubectl create will error out and kubectl apply will not error out.

Deployments

Deployments Description  
  kubectl get deployments Get information on current deployments
  kubectl get rs Get information about the replica sets
  kubectl rollout status deployment/helloworld-deployment Get deployment status
  kubectl set image deployment/helloworld-deployment k8s-demo=k8s-demo Run k8s-demo with the image label version 2
  kubectl edit deployment/helloworld-deployment Edit the deployment object
  kubectl rollout status deployment/helloworld-deployment Get the status of the rollout
  kubectl rollout history deployment/helloworld-deployment Get the rollout history
  kubectl rollout undo deployment/helloworld-deployment Rollback to previous version
  kubectl rollout undo deployment/helloworld-deployment --to-revision=n Rollback to any version version


Back to first page