乐趣区

Kubernatek8s常用命令

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

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

faceless@ttg12:~/tmp$ kubectl get pods
NAME        READY   STATUS    RESTARTS   AGE
nginx-app   1/1     Running   0          9m52s

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

  • 查看指定容器状态kubectl describe

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

faceless@ttg12:~/tmp$ kubectl describe pods nginx-app
Name:         nginx-app
Namespace:    default
Priority:     0
Node:         ttg12/192.168.199.212
Start Time:   Thu, 02 Jul 2020 10:53:06 +0800
Labels:       run=nginx-app
Annotations:  <none>
Status:       Pending
IP:
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      True
Volumes:
  default-token-b79fd:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-b79fd
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  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 aux
kubectl 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
退出移动版