• 帮忙
kubectl help
  • 版本信息
[root@host ~]# kubectl versionClient Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40", GitTreeState:"clean", BuildDate:"2020-05-20T12:52:00Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40", GitTreeState:"clean", BuildDate:"2020-05-20T12:43:34Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
  • 显示集群信息(查看 master 节点)
root@host ~]# kubectl cluster-info Kubernetes master is running at https://192.168.31.27:6443KubeDNS is running at https://192.168.31.27:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
  • 查看节点列表
[root@host68 ~]# kubectl get nodesNAME      STATUS   ROLES    AGE    VERSIONhost68   Ready    master   131d   v1.18.3host69   Ready    <none>   70d    v1.18.6host70   Ready    <none>   38d    v1.18.3host71   Ready    <none>   70d    v1.18.6host72   Ready    <none>   66d    v1.18.3host73   Ready    <none>   66d    v1.18.3host74   Ready    <none>   126d   v1.18.3
  • 查看所有 pod 和 namespace
[root@vip-068 ~]# kubectl get pods --all-namespacesNAMESPACE    NAME                  READY   STATUS    RESTARTS   AGEdefault      gitlab-86hgh          1/1     Running   0          124ddefault      mysql-qqghr           1/1     Running   0          129des           es-46tml              1/1     Running   0          37des           es-5twv5              1/1     Running   1          37des           es-g4tnp              1/1     Running   0          29des           es-vx9r9              1/1     Running   0          30des           es-xj8kk              1/1     Running   0          27d
  • 列出所有 node 和 pod
kubectl get pod -o=custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName --all-namespaces
  • 查看某个 pod 外面的容器
[root@host68 ~]# kubectl -n es get pod es-46tml -o jsonpath="{.spec['containers','initContainers'][*].name}"es# 或者kubectl -n es describe pod es-46tml
  • 列出某个容器外面的目录
# 命令kubectl -n <namespace> -c <container> exec <pod_name> -- ls -la /# 示例kubectl -n es -c es exec es-46tml -- ls -la /usr/share/elasticsearch/config[root@host68 ~]# kubectl -n es -c es exec es-46tml -- ls -la /usr/share/elasticsearch/configtotal 1360drwxrwxr-x 1 elasticsearch root    4096 Oct 12 13:28 .drwxrwxr-x 1 elasticsearch root    4096 Sep  5 06:03 ..-rw-rw---- 1 elasticsearch root     199 Sep  5 06:03 elasticsearch.keystore-rw-rw---- 1 elasticsearch root      53 May 28 16:33 elasticsearch.yml-rw-rw---- 1 elasticsearch root    2301 May 28 16:28 jvm.optionsdrwxrwxr-x 2 elasticsearch root    4096 May 28 16:31 jvm.options.d-rw-rw---- 1 elasticsearch root    7860 May 28 16:33 log4j2.properties-rw-rw---- 1 elasticsearch root     473 May 28 16:32 role_mapping.yml-rw-rw---- 1 elasticsearch root     197 May 28 16:32 roles.yml-rw-r--r-- 1 root          root 1345763 Oct 12 13:28 synonym.txt-rw-rw---- 1 elasticsearch root       0 May 28 16:32 users-rw-rw---- 1 elasticsearch root       0 May 28 16:32 users_roles
  • 删除容器外面的文件
kubectl -n es -c es exec es-46tml -- rm /usr/share/elasticsearch/config/synonym.txt
  • 创立目录
kubectl -n es -c es exec es-46tml -- mkdir /usr/share/elasticsearch/config/analysis
  • 拷贝文件到容器目录
# 拷贝kubectl -n es -c es cp synonym.txt es-46tml:/usr/share/elasticsearch/config/analysis# 查看[root@host68 test]# kubectl -n es -c es exec es-46tml -- ls -la /usr/share/elasticsearch/config/analysistotal 1324drwxr-xr-x 2 root          root    4096 Oct 13 05:57 .drwxrwxr-x 1 elasticsearch root    4096 Oct 13 05:56 ..-rw-r--r-- 1 root          root 1345763 Oct 13 05:57 synonym.txt
qbit snap