虽然k8s提供了dashboard,对常用命令的理解和正确使用,还是很有必要的。

1. get命令

查看nodes,pod,service,endpoints,secret等等的状态

kubectl get 组件名      

查看节点信息

# kubectl get nodes
NAME         STATUS   ROLES    AGE     VERSION
k8s-master   Ready    master   4d20h   v1.16.2
k8s-node83   Ready    <none>   4d19h   v1.16.2
k8s-node85   Ready    <none>   4d19h   v1.16.2

例如查看pod信息,可以使用 kubectl get pod

kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-86c57db685-g6776   1/1     Running   0          19h
nginx-86c57db685-pbs7v   1/1     Running   0          18h

查看详细信息可以加上-o wide

kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP          NODE         NOMINATED NODE   READINESS GATES
nginx-86c57db685-g6776   1/1     Running   0          19h   10.11.1.3   k8s-node85   <none>           <none>
nginx-86c57db685-pbs7v   1/1     Running   0          18h   10.11.2.2   k8s-node83   <none>           <none>

其他namespace的指定 -n namespace名,为指定时,表示查看默认命名空间

kubectl get pod -n kube-system
NAME                                 READY   STATUS    RESTARTS   AGE
coredns-58cc8c89f4-jdfc7             1/1     Running   0          20h
coredns-58cc8c89f4-z8t26             1/1     Running   0          20h
etcd-k8s-master                      1/1     Running   0          20h
kube-apiserver-k8s-master            1/1     Running   0          20h

查看所有命名空间的pod

kubectl get pod --all-namespaces
NAMESPACE              NAME                                        READY   STATUS    RESTARTS   AGE
default                nginx-86c57db685-g6776                      1/1     Running   0          19h
default                nginx-86c57db685-pbs7v                      1/1     Running   0          18h
kube-system            coredns-58cc8c89f4-jdfc7                    1/1     Running   0          20h
kube-system            coredns-58cc8c89f4-z8t26                    1/1     Running   0          20h

2. apply命令

创建,变更一个yaml文件内资源,也可以是目录,目录内包含一组yaml文件。

kubectl apply -f xxx.yaml

3. delete命令

删除一个yaml文件内资源,也可以是目录,目录内包含一组yaml文件。

kubectl delete -f xxx.yaml

4. describe命令

查看资源状态,比如有一组deployment内的pod没起来,一般用于pod调度过程出现的问题排查

kubectl describe pod pod名

比如查看pod nginx-86c57db685-g6776:

kubectl describe pod nginx-86c57db685-g6776
Name:         nginx-86c57db685-g6776
Namespace:    default
Priority:     0
Node:         k8s-node85/192.168.17.85
Start Time:   Wed, 13 Apr 2022 15:00:54 +0800
Labels:       app=nginx
              pod-template-hash=86c57db685
Annotations:  <none>
Status:       Running
IP:           10.11.1.3
IPs:
  IP:           10.11.1.3
Controlled By:  ReplicaSet/nginx-86c57db685
......

可以看到nginx-86c57db685-g6776资源的相关信息。

5. logs命令

查看pod日志,用于pod状态未就绪的故障排查

kubectl logs pod名

查看nginx-86c57db685-g6776的日志信息。

kubectl logs nginx-86c57db685-g6776    
/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

6. top命令

查看node节点或者是pod资源(cpu,内存资源)使用情况

kubectl top 组件名 

kubectl top 是基础命令,但是需要部署配套的组件才能获取到监控值。

7. 进入pod内部

kubectl exec -ti pod名 /bin/bash 

比如进入ngnix的pod内:

# kubectl exec -ti nginx-86c57db685-g6776 /bin/bash
root@nginx-86c57db685-g6776:/# 
root@nginx-86c57db685-g6776:/# ls
bin   dev                  docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc                   lib   media  opt  root  sbin  sys  usr

Logo

开源、云原生的融合云平台

更多推荐