• 创建一个容器kubectl run
faceless@ttg12:~/tmp$ kubectl run --image=nginx:alpine nginx-app --port=8180pod/nginx-app created
  • 获取当前容器列表kubectl get

类似于docker ps,查询资源列表。

faceless@ttg12:~/tmp$ kubectl get podsNAME        READY   STATUS    RESTARTS   AGEnginx-app   1/1     Running   0          9m52s

如果容器创建后,状态一直Pending(未正常启动至Running状态),那么可以用下面kubectl describe查看具体的原因是什么。

  • 查看指定容器状态kubectl describe

类似于docker inspect,获取资源的详细信息。无论何时,只要遇到pod有问题,都先describe看下pod的状态。

faceless@ttg12:~/tmp$ kubectl describe pods nginx-appName:         nginx-appNamespace:    defaultPriority:     0Node:         ttg12/192.168.199.212Start Time:   Thu, 02 Jul 2020 10:53:06 +0800Labels:       run=nginx-appAnnotations:  <none>Status:       PendingIP:IPs:          <none>Containers:  nginx-app:    Container ID:    Image:          nginx:alpine    Image ID:    Port:           8180/TCP    Host Port:      0/TCP    State:          Waiting      Reason:       ContainerCreating    Ready:          False    Restart Count:  0    Environment:    <none>    Mounts:      /var/run/secrets/kubernetes.io/serviceaccount from default-token-b79fd (ro)Conditions:  Type              Status  Initialized       True  Ready             False  ContainersReady   False  PodScheduled      TrueVolumes:  default-token-b79fd:    Type:        Secret (a volume populated by a Secret)    SecretName:  default-token-b79fd    Optional:    falseQoS Class:       BestEffortNode-Selectors:  <none>Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s                 node.kubernetes.io/unreachable:NoExecute for 300sEvents:  Type     Reason            Age                  From               Message  ----     ------            ----                 ----               -------  Warning  FailedScheduling  59s (x5 over 5m12s)  default-scheduler  0/1 nodes are available: 1 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate.  Normal   Scheduled         20s                  default-scheduler  Successfully assigned default/nginx-app to ttg12  Normal   Pulling           18s                  kubelet, ttg12     Pulling image "nginx:alpine"
  • 在容器内执行一个命令kubectl exec

类似于docker exec,在容器内执行一个命令

weiping@ttg12:~/tmp$ kubectl exec nginx-app ps auxkubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.PID   USER     TIME  COMMAND    1 root      0:00 nginx: master process nginx -g daemon off;   29 nginx     0:00 nginx: worker process   30 nginx     0:00 nginx: worker process   31 nginx     0:00 nginx: worker process   32 nginx     0:00 nginx: worker process   33 root      0:00 ps aux