kubernetes operations command summary

When we use the Kubernetes platform,has many commands is important:

The k command is alias to kubectl

  • label for nodes kubectl label nodes host02 disktype=ssd

  • view local config for kubernetes context k config get-contexts

  • switch context’s namespaces kubectl config set-context my-vsphere-cluster --namespace=helm-test

  • force delete pod, sometimes the pod still terminating.

    k delete pods <pod> -n <namespace> --grace-period=0 --force

  • get all resource in current kubernetes cluster

    k get all --all-namespaces

  • view job logs

    k logs $(k get pods --selector=job-name=job-1598792400 --output=jsonpath={.items[*].metadata.name})

  • taint the master nodes no execute and no schedule

    kubectl taint nodes <node-name> node-role.kubernetes.io/master=:NoExecute

    kubectl taint nodes <node-name> node-role.kubernetes.io/master=:NoSchedule

  • no schedule on nodes: SchedulingDisabled

    k get nodes

    k cordon <node name>

    k uncordon <node name> # cancel cordon back to normal

  • when shutdown the nodes first exec

    k drain <node name>

  • port forward kubectl port-forward service/redis-master 7000:6379

  • Arbitrary proxy Kubernetes internal Service kubectl proxy then browse the internal service for example: dashboard and metrics

    http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics:http-metrics/proxy/metrics http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/.

  • operate all resources k delete all --all --namespace=kubesphere-logging-system k get all --all-namespaces

  • Change the current context default namespace kubectl config set-context --current --namespace monitoring

  • deployments an echoserver to kubernetes

1# create deployment
2kubectl create deployment hello-minikube --image=registry.cn-hangzhou.aliyuncs.com/google_containers/echoserver:1.10
3# expose the service
4kubectl expose deployment hello-minikube --type=NodePort --port=8080
  • tag the nodes
1kubectl tag nodes kube-centos-ceph1 role=storage-node
2kubectl get nodes --show-labels
  • terminating pvc when delete pv stuck kubectl patch pvc db-pv-claim -p '{"metadata":{"finalizers":null}}'

    kubectl patch pod db-74755f6698-8td72 -p '{"metadata":{"finalizers":null}}'

  • Helm download charts source code archive format tgz helm pull –repo {repo-url} {chart-name} –version {chart-version} helm pull --repo https://charts.bitnami.com/bitnami metrics-server --version 4.1.4

  • taints the node the effects: “NoSchedule,PreferNoSchedule,NoExecute” kubectl taint node master node.kubernetes.io/unschedulable:NoSchedule kubectl taint nodes node1 key=value:NoSchedule kubectl taint node -l myLabel=X dedicated=foo:PreferNoSchedule

  • remove taints kubectl taint nodes foo dedicated-

Other usefuly commands

1# Create secrets from literal string pair
2kubectl create secret generic mysql-pass --from-literal=password=xxxxxxx -n wp