Skip to content

Instantly share code, notes, and snippets.

@royki
Last active July 15, 2021 01:48
Show Gist options
  • Save royki/03a417cb80b359f8832f8354b71a15c7 to your computer and use it in GitHub Desktop.
Save royki/03a417cb80b359f8832f8354b71a15c7 to your computer and use it in GitHub Desktop.
Kubernetes Command

kubectl (command in default namespace)

  • kubectl version
  • kubectl api-versions
  • kubectl cluster-info & kubectl cluster-info dump
  • kubectl get namespace
  • kubectl get all
  • kubectl get cs or
  • kubectl get nodes
  • kubectl describe nodes
  • Get the name of the Pod and store it in the POD_NAME environment variable:
    • export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
  • Label a POD
    • kubectl label pod $POD_NAME app=v1
  • Query PODs by the label
    • kubectl get pods -l app=v1
  • kubectl get pods (in default Namespace)
    • get pod details in another namespace
      • kubectl get pods --namespace=kube-system
    • get pod in selector dev environment
      • kubectl get pods --selector env=dev
  • All objects in prod environment
    • kubectl get all --selector env=prod
  • Get POD which is 'prod', part of 'finance' BU and is a 'frontend' tier
    • kubectl get all --selector env=prod,bu=finance,tier=frontend
  • kubectl get pods -o wide
  • kubectl describe pods
  • Get the container variable
    • kubectl exec POD_NAME env
  • Enter into a container of a POD
    • kubectl exec -it POD_NAME bash
  • To know details about one particular pod - kubectl describe pod POD_NAME
  • kubectl get replicasets or kubectl get rs
  • kubectl describe replicasets or kubectl describe rs
  • kubectl get deployments
  • kubectl describe deployments
  • kubectl get services
  • kubectl get sc
  • kubectl get events

Create Pod

  • kubectl create -f pod-definition.yml or kubectl create -f pod-definition.yml --namespace=dev (in dev namespace)
  • Create a Pod with NGINX Image -
    • kubectl run nginx-pod --image=nginx --generator=run-pod/v1
    • kubectl run nginx-pod --image=nginx
  • Generate POD Manifest YAML file (-o yaml). Don't create it(--dry-run)
    • kubectl run --generator=run-pod/v1 nginx-pod --image=nginx --dry-run -o yaml
  • Delete a pod kubectl delete pod POD_NAME

Edit

  • Edit the service named 'docker-registry': kubectl edit svc/docker-registry
  • Use an alternative editor - KUBE_EDITOR="nano" kubectl edit svc/docker-registry
  • Edit the service 'docker-registry' in JSON using the v1 API format:
    • kubectl edit svc/docker-registry --output-version=v1 -o json

Create a Deployment

  • kubectl create deployment --image=nginx nginx-deployment
  • Generate Deployment YAML file (-o yaml). Don't create it(--dry-run)
    • kubectl create deployment --image=nginx nginx-deployment --dry-run -o yaml
  • Generate Deployment YAML file (-o yaml). Don't create it(--dry-run) with 4 Replicas (--replicas=4)
    • kubectl create deployment --image=nginx nginx-deployment --dry-run -o yaml > nginx-deployment.yaml
    • kubectl create deployment does not have a --replicas option. You could first create it and then scale it using the kubectl scale command.

Log View

  • kubectl logs pod pod_name --namespace=kube-system
  • If there are multiple containers in the POD definition file then use the name of the container to view the log of a particular container.
    • kubectl logs pod_name container-1_name

kubectl taint & toleration

  • Get Nodes -> kubectl get nodes
  • Check Taint on Node -> kubectl get nodes node01 | grep -i taint
  • Apply Taint on Node -> kubectl taint nodes node01 spray=mortein:NoSchedule
  • Untained Master Node -> kubectl taint nodes master node-role.kubernetes.io/master:NoSchedule-

Analytics View (need to install plugins like metrics-server)

  • kubectl top node
  • kubectl top pod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment