kubectl 帮助命令

  1. [root@qwe ~]# kubectl
  2. kubectl controls the Kubernetes cluster manager.
  3. Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/
  4. Basic Commands (Beginner):
  5. create Create a resource from a file or from stdin
  6. expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes service
  7. run Run a particular image on the cluster
  8. set Set specific features on objects
  9. Basic Commands (Intermediate):
  10. explain Get documentation for a resource
  11. get Display one or many resources
  12. edit Edit a resource on the server
  13. delete Delete resources by file names, stdin, resources and names, or by resources and label selector
  14. Deploy Commands:
  15. rollout Manage the rollout of a resource
  16. scale Set a new size for a deployment, replica set, or replication controller
  17. autoscale Auto-scale a deployment, replica set, stateful set, or replication controller
  18. Cluster Management Commands:
  19. certificate Modify certificate resources.
  20. cluster-info Display cluster information
  21. top Display resource (CPU/memory) usage
  22. cordon Mark node as unschedulable
  23. uncordon Mark node as schedulable
  24. drain Drain node in preparation for maintenance
  25. taint Update the taints on one or more nodes
  26. Troubleshooting and Debugging Commands:
  27. describe Show details of a specific resource or group of resources
  28. logs Print the logs for a container in a pod
  29. attach Attach to a running container
  30. exec Execute a command in a container
  31. port-forward Forward one or more local ports to a pod
  32. proxy Run a proxy to the Kubernetes API server
  33. cp Copy files and directories to and from containers
  34. auth Inspect authorization
  35. debug Create debugging sessions for troubleshooting workloads and nodes
  36. Advanced Commands:
  37. diff Diff the live version against a would-be applied version
  38. apply Apply a configuration to a resource by file name or stdin
  39. patch Update fields of a resource
  40. replace Replace a resource by file name or stdin
  41. wait Experimental: Wait for a specific condition on one or many resources
  42. kustomize Build a kustomization target from a directory or URL.
  43. Settings Commands:
  44. label Update the labels on a resource
  45. annotate Update the annotations on a resource
  46. completion Output shell completion code for the specified shell (bash or zsh)
  47. Other Commands:
  48. api-resources Print the supported API resources on the server
  49. api-versions Print the supported API versions on the server, in the form of "group/version"
  50. config Modify kubeconfig files
  51. plugin Provides utilities for interacting with plugins
  52. version Print the client and server version information
  53. Usage:
  54. kubectl [flags] [options]
  55. Use "kubectl <command> --help" for more information about a given command.
  56. Use "kubectl options" for a list of global command-line options (applies to all commands).

查看组件状态

kubectl get componentstatus

[root@master01 ~]# kubectl get componentstatus
NAME                 STATUS    MESSAGE             ERROR
controller-manager   Healthy   ok                  
scheduler            Healthy   ok                  
etcd-0               Healthy   {"health":"true"}

查看所有命名空间下的应用

kubectl get deployment —all-namespaces

[root@qwe ~]# kubectl get deployment --all-namespaces
NAMESPACE     NAME                        READY   UP-TO-DATE   AVAILABLE   AGE
default       nginx                       3/3     3            3           2m35s
kube-system   coredns                     1/1     1            1           5h58m
kube-system   dashboard-metrics-scraper   1/1     1            1           5h57m
kube-system   kubernetes-dashboard        1/1     1            1           5h57m
kube-system   metrics-server              1/1     1            1           5h58m

创建资源

kubectl create -f xxx.yaml

kubectl create -f deployment-nginx.yaml

查看节点信息

kubectl describe nodes 节点名或者节点ip

[root@qwe ~]# kubectl describe nodes 172.16.83.19 
Name:               172.16.83.19
Roles:              master
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/arch=amd64
                    ...
                    ...

查看集群中部署的应用列表

kubectl get deployment

[root@qwe ~]# kubectl get deployment
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/3     3            1           21s

查看集群节点

kubectl get nodes

[root@qwe ~]# kubectl get nodes
NAME           STATUS   ROLES    AGE     VERSION
172.16.83.19   Ready    master   5h47m   v1.22.2

查看集群节点详情

kubectl get nodes -o wide

节点打标签

kubectl label nodes <节点名称> labelName=<标签名称>

查看节点标签

kubectl get node —show-labels

删除节点标签

kubectl label node <节点名称> labelName

查看节点标签

kubectl get nodes —show-labels

[root@master01 ~]# kubectl get nodes --show-labels
NAME           STATUS   ROLES    AGE    VERSION   LABELS
172.16.83.30   Ready    master   107m   v1.18.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.83.30,kubernetes.io/os=linux,kubernetes.io/role=master
172.16.83.31   Ready    master   107m   v1.18.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.83.31,kubernetes.io/os=linux,kubernetes.io/role=master
172.16.83.32   Ready    node     106m   v1.18.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=172.16.83.32,kubernetes.io/os=linux,kubernetes.io/role=node

设置节点不被调用/恢复

kubectl cordon <节点名>
kubectl uncordon <节点名>

#设置172.16.83.30节点不被调用,并查看节点发现节点状态显示 schdulingDisable
[root@master01 ~]# kubectl get nodes
NAME           STATUS   ROLES    AGE     VERSION
172.16.83.30   Ready    master   3h39m   v1.18.3
172.16.83.31   Ready    master   3h39m   v1.18.3
172.16.83.32   Ready    node     3h38m   v1.18.3
[root@master01 ~]# kubectl cordon 172.16.83.30
node/172.16.83.30 cordoned
[root@master01 ~]# kubectl get nodes
NAME           STATUS                     ROLES    AGE     VERSION
172.16.83.30   Ready,SchedulingDisabled   master   3h39m   v1.18.3
172.16.83.31   Ready                      master   3h39m   v1.18.3
172.16.83.32   Ready                      node     3h39m   v1.18.3
#恢复节点状态使可以被调用
[root@master01 ~]# kubectl uncordon 172.16.83.30
node/172.16.83.30 uncordoned
[root@master01 ~]# kubectl get nodes
NAME           STATUS   ROLES    AGE     VERSION
172.16.83.30   Ready    master   3h44m   v1.18.3
172.16.83.31   Ready    master   3h44m   v1.18.3
172.16.83.32   Ready    node     3h43m   v1.18.3
[root@master01 ~]#

pod

查看pod节点

kubectl get pod

查看pod标签

kubectl get pods —show-labels

[root@master01 ~]# kubectl get pods --show-labels
NAME       READY   STATUS    RESTARTS   AGE    LABELS
comm-pod   2/2     Running   0          108s   run=comm-pod
pod-demo   1/1     Running   0          65m    run=pod-demo

通过标签过滤

kubectl get pods -l run=pod-demo

[root@master01 ~]# kubectl get pods -l run=pod-demo
NAME       READY   STATUS    RESTARTS   AGE
pod-demo   1/1     Running   0          66m

查看所有pod节点

kubectl pods -A

查看pod节点详情

kubectl get pod -o wide

[root@master01 ~]# kubectl get pods -o wide
NAME        READY   STATUS    RESTARTS   AGE     IP           NODE           NOMINATED NODE   READINESS GATES
podcommon   1/1     Running   0          4m18s   172.20.2.3   172.16.83.32   <none>           <none>

查看所有名称空间下的pod

kubectl get pod —all-namespaces

根据yaml文件创建pod

kubectl apply -f <文件名称>

创建pod时指定运行命令,替换镜像镜像CMD的命令

kubectl run com-pod —image=nginx —image-pull-policy=IfNotPresent —dry-run -o yaml — “echo helloworld”

--dry-run -o yaml         #将命令的输出为yaml文件,此次不执行
[root@master01 ~]# kubectl run com-pod --image=nginx --image-pull-policy=IfNotPresent --dry-run -o yaml -- "echo helloworld"
W0408 14:01:28.807254    4847 helpers.go:535] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: com-pod
  name: com-pod
spec:
  containers:
  - args:
    - echo helloworld
    image: nginx
    imagePullPolicy: IfNotPresent
    name: com-pod
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
[root@master01 ~]#

根据yaml文件删除pod

kubectl delete -f <文件名称>

删除pod

kubectl delete pod -n <名称空间>

通过删除yaml文件删除pod

kubectl delete -f comm-pod.yaml

删除多个pod

kubectl delete pod pod-demo1 pod-demo2

[root@master01 ~]# kubectl delete pod pod-demo1 pod-demo2
pod "pod-demo1" deleted
pod "pod-demo2" deleted

查看异常的pod节点

kubectl get pods -n <名称空间> | grep -v Running

查看异常pod节点日志/详细信息

kubetcl describe pod -n <名称空间>

查看pod节点日志

kubectl logs comm-pod -c comm-pod1

commpod里面有两个容器,需要使用 -c 来指定容器
[root@master01 ~]# kubectl get pods
NAME       READY   STATUS    RESTARTS   AGE
comm-pod   2/2     Running   0          36m
pod-demo   1/1     Running   0          99m
[root@master01 ~]# kubectl logs comm-pod -c comm-pod1
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/04/08 07:00:18 [notice] 1#1: using the "epoll" event method
2022/04/08 07:00:18 [notice] 1#1: nginx/1.21.5
2022/04/08 07:00:18 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/04/08 07:00:18 [notice] 1#1: OS: Linux 3.10.0-957.21.3.el7.x86_64
2022/04/08 07:00:18 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 65536:65536
2022/04/08 07:00:18 [notice] 1#1: start worker processes
2022/04/08 07:00:18 [notice] 1#1: start worker process 30
2022/04/08 07:00:18 [notice] 1#1: start worker process 31

进去pod节点

kubectl exec -it —/bin/bash

kubectl exec -it pod-demo — bash

[root@master01 ~]# kubectl exec -it pod-demo -- bash
root@pod-demo:/# ls
bin   docker-entrypoint.d   home   media  proc  sbin  tmp
boot  docker-entrypoint.sh  lib    mnt    root  srv   usr
dev   etc                   lib64  opt    run   sys   var

在pod里执行命令

kubectl exec -it pod-demo — ls

[root@master01 ~]# kubectl exec -it pod-demo -- ls
bin   docker-entrypoint.d   home   media  proc  sbin  tmp
boot  docker-entrypoint.sh  lib    mnt    root  srv   usr
dev   etc                   lib64  opt    run   sys   var

复制文件到pod内

kubectl cp <文件> :<目的路径>

#在当前路径下创建一个index文件并写入hello world,复制到pod-demo的/usr/share/nginx/html/路径下替换index.html文件,并查看
[root@master01 ~]# vim index.html
[root@master01 ~]# kubectl cp index.html pod-demo:/usr/share/nginx/html/
[root@master01 ~]# curl 172.16.83.32:35814
hello world

普通方式创建pod

kubectl run —image=<镜像名称>

指定镜像拉取策略和lable创建pod

kubectl run podcommon —image=nginx —image-pull-policy=TfNotPresent —labels=”name=todd” —env=”name=todd”

监控pod(一秒钟更新一次命令)

watch -n 1 kubectl get pod

kubectl cluster-info

deployment

deployment部署pod(具有自愈能力,宕机自动拉起)

kubectl create deployment —image=<镜像名称>

deployment 部署pod(多副本)

kubectl create deployment —image=<镜像名称> —replicas=3

查看deployment部署

kubectl get deploy

删除deployment部署

kubectl delete deploy

deployment扩容\缩容pod

kubectl scale deploy —replicas=<5>

deployment扩容\缩容pod

kubectl edit deploy

deployment滚动更新pod

kubectl set image deploy/ <容器名称>=<镜像名称:版本号> —record

deployment查看pod回退版本

kubetcl roollout history deploy/

deployment查看pod回退版本详情

kubectl rollout history deploy/ —revision=1

deployment回退pod到上一个版本

kubectl rollout undo deploy/

deployment回退pod到指定版本

kubectl rollout undo deploy/ —to-revision=1

depolyment暴露pod 集群内存访问(ClusterIP)

kubectl expose deploy —port=8080 —target-port=80 —type=ClusterIP

deployment暴露pod外网访问(NodePort)

kubectl expose deploy —port=8080 —target-port=80 —type=NodePort

SVC

查看服务

kubectl get svc

查看服务详情

kubectl get svc -o wide

查看所有名称空间下的服务

kubectl get svc —all-namesapces

namespace

查看名称空间

kubectl get namespace

查看名称空间

kubectl get ns

创建名称空间

kubectl create ns <名称>

删除名称空间

kubectl delete ns <名称>

查看集群信息

[root@qwe ~]# kubectl cluster-info
Kubernetes control plane is running at https://127.0.0.1:6443
CoreDNS is running at https://127.0.0.1:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
KubeDNSUpstream is running at https://127.0.0.1:6443/api/v1/namespaces/kube-system/services/kube-dns-upstream:dns/proxy
kubernetes-dashboard is running at https://127.0.0.1:6443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy

创建命名空间

kubectl create namespace test01

[root@qwe ~]# kubectl create namespace test01
namespace/test01 created

查看命名空间

kubectl get namespaces

[root@qwe ~]# kubectl get namespaces
NAME              STATUS   AGE
default           Active   42m
kube-node-lease   Active   42m
kube-public       Active   42m
kube-system       Active   42m
test01            Active   103s

运行.yaml文件

[root@qwe ~]# kubectl apply -f memory-request-limit.yaml --namespace=test01 
pod/memory-demo created

查看pods

kubectl get pods

kubectl get pods —namespace=test01

[root@qwe ~]# kubectl get pods --namespace=test01
NAME          READY   STATUS    RESTARTS   AGE
memory-demo   1/1     Running   0          59s

查看pods详细信息

kubectl describe pods

查看服务(Servcice)信息

kubectl get services

[root@qwe ~]# kubectl get services
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.68.0.1    <none>        443/TCP   6h29m

查看部署应用的副本设置列表

kubectl get replicasets

[root@qwe ~]# kubectl get replicaset
NAME               DESIRED   CURRENT   READY   AGE
nginx-6d88d86746   3         3         3       33m

查看副本控制器(ReplicationController)的列表

kubectl get rc

对部署的应用进行滚动升级

对Nginx镜像版本版本进行升级,则使用如下命令:
[root@qwe ~]# kubectl set image deployment/nginx nginx=nginx:1.10
deployment.apps/nginx image updated
[root@qwe ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
memory-demo              1/1     Running   0          41m
nginx-6d88d86746-dctnq   1/1     Running   0          39m
nginx-6d88d86746-qjbk5   1/1     Running   0          39m
nginx-6d88d86746-swc8s   1/1     Running   0          39m
nginx-789fbccfd9-6l47b   0/1     Pending   0          9s