K8s - kubectl

Every time before start kubectl, remember export admin.config file first which contains server info for connection. The pwd should under path where the config file lies.

1
$ export KUBECONFIG=./admin.conf

then start nodes

1
$ kubectl get node

or

1
$ kubectl get node -n testing

useful cmd

  1. 通过bash获得pod中某个容器的TTY,相当于登录容器

    1
    $ kubectl exec -it <pod-name> -c <container-name> -n <namespace> -- bash

    ref: kubectl 常用命令总结

  2. --
    shell命令前,要加– 号,不然shell命令中的参数,不能识别
    https://www.wandouip.com/t5i281271/

  3. 获取异常容器 (for debug)

    1
    $  kubectl get pods -n kube-system | grep -v Running
  4. get pod description (for debug)

    1
    $ kubectl describe pod <pod-name> -n <namespaces-name>
  5. 查看异常pod的日志 (for debug)

    1
    $ kubectl logs <pod-name> -n <namespaces-name>
  6. 查看相关的config map存不存在 (for debug)
    find the path to configmap file first

    1
    $ ls -l   # if the result is "total 0", means the configmap is not exist
  7. delete pod

    1
    $ kubectl delete pod <pod-name> -n <namespaces-name>

    -n <namespaces-name> can be omitted.

delete pods in batches (e.g. delete all evicted pods)

1
$ kubectl get pods -A| grep Evicted | awk '{print $2}' | xargs kubectl delete pod -n cec
  1. create namespace
    1
    $ kubectl create namespace <name>
  2. check detailed info (especially for ip), add -o wide
    1
    2
    3
    4
    5
    6
    $ kubectl get node -o wide -A
    NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
    pccc-303-7-master-1 Ready master 3h12m v1.18.2 192.168.2.1 <none> CentOS Linux 7 (Core) 3.10.0-957.27.2.el7.x86_64 docker://19.3.5
    pccc-303-7-worker-1 Ready worker 3h10m v1.18.2 11.110.2.2 <none> CentOS Linux 7 (Core) 3.10.0-957.27.2.el7.x86_64 docker://19.3.5
    pccc-303-7-worker-2 Ready worker 3h10m v1.18.2 11.110.2.3 <none> CentOS Linux 7 (Core) 3.10.0-957.27.2.el7.x86_64 docker://19.3.5
    pccc-303-7-worker-3 Ready worker 3h10m v1.18.2 11.110.2.4 <none> CentOS Linux 7 (Core) 3.10.0-957.27.2.el7.x86_64 docker://19.3.5
  3. watch -d: monitor all pods status with certain namespace:
    1
    $ watch -d kubectl get pods -n <namespace>
  4. Tail the system journal log
    1
    $ journalctl -f
  5. List config maps
    1
    2
    3
    4
    5
    6
    7
    $ kubectl get configmap -A
    # Check the configmap name for a specific namespace
    $ kubectl get cm -n <namespace>
    # Check the config in a configmap
    $ kubectl describe cm -n <namespace>
    # Edit a configmap
    $ kubectl edit configmap -n <namespace>
  6. List all services in a namespace, with extended info
    1
    $ kubectl get svc -n <namespace> -o json