Alibaba Cloud k8s-common commands (Kubernetes)

View node information

kubectl get nodes

Query namespace list

kubectl get ns

Query pods list

kubectl get pods -n mdm-dev
kubectl get pods -n mdm-dev -o wide #can show which node pods are deployed on

View pods information (you can view pods startup status)

kubectl describe pods xxxxx -n mdm-dev # where xxx is the name of the first column of pods queried above (kubectl get pods -n mdm-dev);

View pods logs

kubectl logs -f —tail 100 xxxx -n mdm-dev

Enter the pods container

kubectl exec -it xxxx -n mdm-dev — bash

query service

kubectl get svc -n mdm-dev

Query pv/pvc

kubectl get pv
kubectl get pvc -n mdm-dev

query secret

kubectl get secret -n mdm-dev

query configmap

kubectl get cm -n mdm-dev

deploy a resource

kubectl create -f xxxx.yaml

Update a resource after modifying the configuration

kubectl apply -f xxxx.yaml

delete a resource

kubectl delete -f xxxx.yaml

query deployment

kubectl get deploy -n mdm-dev

query statefulset

kubectl get statefulset -n mdm-dev

query daemonset

kubectl get ds -n mdm-dev

Export a current resource configuration to a file

kubectl get pods XXXX -n mdm-dev -o yaml > xxxx.yaml

View the node label

kubectl get nodes —show-labels

View node information

kubectl describe node k8s-worker01

illustrate

  1. -n mdm-dev in the above command refers to resources under the mdm-dev namespace, and mdm-dev is replaced according to the actual situation;

  2. If you need to view resources in all namespaces, you can replace -n mdm-dev with -A or --all-namespaces;

  3. Some of the above commands are abbreviations, such as ns, svc, pv, pvc, cm, etc. For specific supported abbreviations, please refer to the following k8s official website;

  4. The above only lists the commonly used commands. For more commands, please refer to the k8s official website: https://kubernetes.io/docs/reference/kubectl/overview/ ;

Tips

Because the kubectl command operation is relatively long, we can simplify the command through the alias command renaming function of linux; the
specific operation is as follows:

  1. Ssh login can operate k8s cluster nodes through kubectl command;

  2. Execute kubectl get nodes to check the node information and confirm whether the k8s cluster can be connected normally;

  3. Edit ~/.bash_profile, add the following content at the end of the file (where mdm-dev should be modified according to the actual situation), then save and exit;

alias kg='kubectl get'
alias kc='kubectl create'
alias kb='kubectl describe'
alias kl='kubectl logs'
alias ka='kubectl apply'
alias kd='kubectl delete'
alias ke='kubectl exec -it'
alias kgm='kubectl get -n mdm-dev'
alias kcm='kubectl create -n mdm-dev'
alias kbm='kubectl describe -n mdm-dev'
alias klm='kubectl logs -n mdm-dev'
alias kam='kubectl apply -n mdm-dev'
alias kdm='kubectl delete -n mdm-dev'
alias kem='kubectl exec -it -n mdm-dev'
alias kgs='kubectl get -n kube-system'
alias kcs='kubectl create -n kube-system'
alias kbs='kubectl describe -n kube-system'
alias kls='kubectl logs -n kube-system'
alias kas='kubectl apply -n kube-system'
alias kds='kubectl delete -n kube-system'
alias kes='kubectl exec -it -n kube-system'
alias kga='kubectl get -A'
alias kca='kubectl create -A'
alias kba='kubectl describe -A'
alias kla='kubectl logs -A'
alias kaa='kubectl apply -A'
alias kda='kubectl delete -A'
alias kea='kubectl exec -it -A'
  1. Then execute source ~/.base_profile to make the newly added configuration take effect; add kg, kgm, kgs and other commands; then execute:
    kg nodes to see if it works;
  2. After logging in to the server through ssh, there is no need to execute source ~/.base_profile, and the shell terminal will automatically execute the file after logging in;

Guess you like

Origin blog.csdn.net/weixin_43460193/article/details/125145320