[K8s] Command cheatsheet

  • View config
kubectl config view
  • Show cluster info
kubectl cluster-info

# Example output
Kubernetes control plane is running at https://10.0.0.9:8443
KubeDNS is running at https://10.0.0.9:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
  • Get component info
kubectl get xxx -o wide -A
kubectl describe xxx

# Use -l $LABEL to filter using labels
# USe --show-labels to show labels

# xxx can be nodes, pods, deployments,
# events, services, replicaset, etc.
# To specify a specific object: xxx/name. For example: deloy/my-deploy

# For full list and the short names, do 
kubectl api-resources
  • Create/Edit/Delete any object
# Create
kubectl create -f config.yaml

# Edit. Can be used to rollout a new version when editing deployments
kubectl edit xxx/$NAME

# Delete
kubectl delete xxx/$NAME
kubectl delete -f config.yaml
  • Create a deployment which manages a Pod
kubectl create deployment hello-node --image=k8s.gcr.io/echoserver:1.4
  • Expose a deployment as a service
# 8080 here is the container port
# NodePort exposes the ports on each node running related pods
kubectl expose deployment/hello-node --type="NodePort" --port 8080
  • Scale a deployment
kubectl scale deployments/kubernetes-bootcamp --replicas=4
  • Update a deployment
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2 --record=true

# Check the rollout status
kubectl rollout status deployments/kubernetes-bootcamp

# Undo the rollout
# This rollback to previous replicaset
kubectl rollout undo deployments/kubernetes-bootcamp
  • Undo a rollout
# Find the replicaset you want to restore
# (default 10 history replicasets are saved)
kubectl get rs -o wide
kubectl rollout history $DEPLOY

# Get the revision number of the desired replicaset
kubectl describe rs kubernetes-bootcamp-fb5c67579

# Rollback to the specific replicaset
kubectl rollout undo deployments/kubernetes-bootcamp --to-revision=2
  • Logging
kubectl logs $POD_NAME
  • Execute a command on a container in a Pod
kubectl exec $POD_NAME -- $COMMAND


# Start a bash session. If no container specified, kubectl.kubernetes.io/default-container will be used
kubectl exec -it $POD_NAME -c $CONTAINER_NAME -- bash
  • Access a Pod’s port locally w/o services (for debugging)
# Method 1. Port-forwarding
kubectl port-forward POD_NAME HOST_PORT:POD_PORT
# Then you can access the endpoint at 
# localhost:HOST_PORT

# Method 2. Proxy
kubectl proxy
# Then you can access the endpoint at
# http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME:$POD_PORT/proxy/
  • Labeling
# Attach a new label
kubectl label pods $POD_NAME version=v1

# View labels of a Pod
kubectl describe pods $POD_NAME
  • Secrets
    Ways to use secrets:
    • As container envs (secretKeyRef)
    • As volumes mounted to containers
    • Save to a docker image and provide access inside the cluster
# Create a secret
kubectl create secret generic $SECRET [--from-file file_name] [--from-literal]

# Show a secret
kubectl get secret/xxx -o json
  • ConfigMap
kubectl create configmap $CONFIGMAP [--from-file] [--from-literal]
  • Drain a Node
# Drain a Node to move all Pods to other Nodes
kubectl drain $NODE

# Undo draining
kubectl uncordon $NODE to revert
  • Clean up
kubectl delete service hello-node
kubectl delete deployment hello-node
minikube stop
minikube delete

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *