このセクションでは、Kubernetes クラスタを操作するためのコマンドについて紹介します。

docker イメージの確認

docker 上のコンテナを動かすためのイメージを確認する場合は、以下コマンドを実行します。

[root@kube-work2 ~]# docker images
REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE
k8s.gcr.io/kubernetes-dashboard-amd64   v1.10.1             f9aed6605b81        2 weeks ago         122MB
k8s.gcr.io/kube-proxy                   v1.13.1             fdb321fd30a0        3 weeks ago         80.2MB
k8s.gcr.io/coredns                      1.2.6               f59dcacceff4        2 months ago        40MB
quay.io/coreos/flannel                  v0.10.0-amd64       f0fad859c909        11 months ago       44.6MB
k8s.gcr.io/pause                        3.1                 da86e6ba6ca1        12 months ago       742kB
[root@kube-work2 ~]# 

docker イメージを削除

docker 上のコンテナを動かすためのイメージを手動で削除する場合は、以下コマンドを実行します。

[root@kube-work2 ~]# docker rmi f9aed6605b81
Untagged: k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1
Untagged: k8s.gcr.io/kubernetes-dashboard-amd64@sha256:0ae6b69432e78069c5ce2bcde0fe409c5c4d6f0f4d9cd50a17974fea38898747
Deleted: sha256:f9aed6605b814b69e92dece6a50ed1e4e730144eb1cc971389dde9cb3820d124
Deleted: sha256:fbdfe08b001c6861c50073c98ed175d54e2d6440df7b797e52be97df0065098c
[root@kube-work2 ~]# 

docker 上で稼働しているコンテナを確認

docker 上で稼働しているコンテナを確認する場合は、以下コマンドを実行します。

[root@kube-master ~]# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED              STATUS              PORTS                    NAMES
3ad21eb237b9        rancher/rancher:v2.0.2   "rancher --http-list…"   About a minute ago   Up About a minute   0.0.0.0:8080->8080/tcp   unruffled_tereshkova
e8cf77f4ae07        f0fad859c909             "/opt/bin/flanneld -…"   2 hours ago          Up 2 hours                                   k8s_kube-flannel_kube-flannel-ds-amd64-t4bx9_kube-system_5a937115-1040-11e9-98a4-5254008e2308_0
249e4b6fc12a        k8s.gcr.io/pause:3.1     "/pause"                 2 hours ago          Up 2 hours                                   k8s_POD_kube-flannel-ds-amd64-t4bx9_kube-system_5a937115-1040-11e9-98a4-5254008e2308_0
e42cd0a0cb5f        fdb321fd30a0             "/usr/local/bin/kube…"   3 hours ago          Up 3 hours                                   k8s_kube-proxy_kube-proxy-jt4jq_kube-system_62b0b772-103e-11e9-98a4-5254008e2308_0
dd8fee5ace02        k8s.gcr.io/pause:3.1     "/pause"                 3 hours ago          Up 3 hours                                   k8s_POD_kube-proxy-jt4jq_kube-system_62b0b772-103e-11e9-98a4-5254008e2308_0
205df14953dc        ab81d7360408             "kube-scheduler --ad…"   3 hours ago          Up 3 hours                                   k8s_kube-scheduler_kube-scheduler-kube-master_kube-system_44b569a35761491825f4e7253fbf0543_0
13ea998f00ac        40a63db91ef8             "kube-apiserver --au…"   3 hours ago          Up 3 hours                                   k8s_kube-apiserver_kube-apiserver-kube-master_kube-system_bcc6952e3e0c1f16dbe3b81f2570faef_0
3e4d5435a126        3cab8e1b9802             "etcd --advertise-cl…"   3 hours ago          Up 3 hours                                   k8s_etcd_etcd-kube-master_kube-system_6d84a28122b1e72c921e9dec54a60ebf_0
990a37fa3cac        26e6f1db2a52             "kube-controller-man…"   3 hours ago          Up 3 hours                                   k8s_kube-controller-manager_kube-controller-manager-kube-master_kube-system_d4ff37ee76fe761a28f11175fd1c384e_0
4985dadd1670        k8s.gcr.io/pause:3.1     "/pause"                 3 hours ago          Up 3 hours                                   k8s_POD_kube-scheduler-kube-master_kube-system_44b569a35761491825f4e7253fbf0543_0
063c91f55594        k8s.gcr.io/pause:3.1     "/pause"                 3 hours ago          Up 3 hours                                   k8s_POD_kube-controller-manager-kube-master_kube-system_d4ff37ee76fe761a28f11175fd1c384e_0
221859f6e75b        k8s.gcr.io/pause:3.1     "/pause"                 3 hours ago          Up 3 hours                                   k8s_POD_kube-apiserver-kube-master_kube-system_bcc6952e3e0c1f16dbe3b81f2570faef_0
abbb7847c596        k8s.gcr.io/pause:3.1     "/pause"                 3 hours ago          Up 3 hours                                   k8s_POD_etcd-kube-master_kube-system_6d84a28122b1e72c921e9dec54a60ebf_0
[root@kube-master ~]# 

docker 上で停止しているコンテナを確認

docker 上で停止しているコンテナを確認する場合は、以下コマンドを実行します。

[root@kube-master ~]# docker ps -a
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                        PORTS               NAMES
b5a93cbe07ea        rancher/rancher:latest   "entrypoint.sh"          27 minutes ago      Exited (137) 21 minutes ago                       stupefied_sinoussi
e8cf77f4ae07        f0fad859c909             "/opt/bin/flanneld -…"   2 hours ago         Up 2 hours                                        k8s_kube-flannel_kube-flannel-ds-amd64-t4bx9_kube-system_5a937115-1040-11e9-98a4-5254008e2308_0
c3d62dde97b3        f0fad859c909             "cp -f /etc/kube-fla…"   2 hours ago         Exited (0) 2 hours ago                            k8s_install-cni_kube-flannel-ds-amd64-t4bx9_kube-system_5a937115-1040-11e9-98a4-5254008e2308_0
249e4b6fc12a        k8s.gcr.io/pause:3.1     "/pause"                 2 hours ago         Up 2 hours                                        k8s_POD_kube-flannel-ds-amd64-t4bx9_kube-system_5a937115-1040-11e9-98a4-5254008e2308_0
e42cd0a0cb5f        fdb321fd30a0             "/usr/local/bin/kube…"   3 hours ago         Up 3 hours                                        k8s_kube-proxy_kube-proxy-jt4jq_kube-system_62b0b772-103e-11e9-98a4-5254008e2308_0
dd8fee5ace02        k8s.gcr.io/pause:3.1     "/pause"                 3 hours ago         Up 3 hours                                        k8s_POD_kube-proxy-jt4jq_kube-system_62b0b772-103e-11e9-98a4-5254008e2308_0
205df14953dc        ab81d7360408             "kube-scheduler --ad…"   3 hours ago         Up 3 hours                                        k8s_kube-scheduler_kube-scheduler-kube-master_kube-system_44b569a35761491825f4e7253fbf0543_0
13ea998f00ac        40a63db91ef8             "kube-apiserver --au…"   3 hours ago         Up 3 hours                                        k8s_kube-apiserver_kube-apiserver-kube-master_kube-system_bcc6952e3e0c1f16dbe3b81f2570faef_0
3e4d5435a126        3cab8e1b9802             "etcd --advertise-cl…"   3 hours ago         Up 3 hours                                        k8s_etcd_etcd-kube-master_kube-system_6d84a28122b1e72c921e9dec54a60ebf_0
990a37fa3cac        26e6f1db2a52             "kube-controller-man…"   3 hours ago         Up 3 hours                                        k8s_kube-controller-manager_kube-controller-manager-kube-master_kube-system_d4ff37ee76fe761a28f11175fd1c384e_0
4985dadd1670        k8s.gcr.io/pause:3.1     "/pause"                 3 hours ago         Up 3 hours                                        k8s_POD_kube-scheduler-kube-master_kube-system_44b569a35761491825f4e7253fbf0543_0
063c91f55594        k8s.gcr.io/pause:3.1     "/pause"                 3 hours ago         Up 3 hours                                        k8s_POD_kube-controller-manager-kube-master_kube-system_d4ff37ee76fe761a28f11175fd1c384e_0
221859f6e75b        k8s.gcr.io/pause:3.1     "/pause"                 3 hours ago         Up 3 hours                                        k8s_POD_kube-apiserver-kube-master_kube-system_bcc6952e3e0c1f16dbe3b81f2570faef_0
abbb7847c596        k8s.gcr.io/pause:3.1     "/pause"                 3 hours ago         Up 3 hours                                        k8s_POD_etcd-kube-master_kube-system_6d84a28122b1e72c921e9dec54a60ebf_0
[root@kube-master ~]#

docker 上で稼働しているコンテナを停止

docker 上のコンテナを手動で停止する場合は、以下コマンドを実行します。

[root@kube-master ~]# docker stop 3ad21eb237b9
3ad21eb237b9
[root@kube-master ~]# 

Pod の詳細を一覧表示

Pod の詳細を一覧で表示する場合には、以下コマンドを実行します。詳細を一覧表示することで、 Pod の IPアドレスと Pod が稼働している Node を一覧で確認することができます。

[root@kube-master sample-replicaset]# kubectl get pods -o wide
NAME              READY   STATUS    RESTARTS   AGE     IP             NODE         NOMINATED NODE   READINESS GATES
sample-rs-46pv5   1/1     Running   0          4m52s   10.244.1.107   kube-work1   <none>           <none>
sample-rs-p4zqm   1/1     Running   0          4m52s   10.244.2.26    kube-work2   <none>           <none>
sample-rs-tq7j8   1/1     Running   0          2m54s   10.244.1.108   kube-work1   <none>           <none>
[root@kube-master sample-replicaset]# 

YAML 形式で Pod のリストを出力

[root@kube-master manifest]# kubectl get pods -o yaml
apiVersion: v1
items:
- apiVersion: v1
  kind: Pod
  metadata:
    annotations:
      kubectl.kubernetes.io/last-applied-configuration: |
        {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"sample-pod","namespace":"default"},"spec":{"containers":[{"image":"nginx:1.13","name":"nginx-container"}]}}
    creationTimestamp: "2019-01-07T13:41:56Z"
    name: sample-pod
    namespace: default
    resourceVersion: "301062"
    selfLink: /api/v1/namespaces/default/pods/sample-pod
    uid: ffbdc4b7-1281-11e9-94ad-525400bc2a02
  spec:
    containers:
    - image: nginx:1.13
      imagePullPolicy: IfNotPresent
      name: nginx-container
      resources: {}
      terminationMessagePath: /dev/termination-log
      terminationMessagePolicy: File
      volumeMounts:
      - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
        name: default-token-75dfq
        readOnly: true
    dnsPolicy: ClusterFirst
    enableServiceLinks: true
    nodeName: kube-work2
    priority: 0
    restartPolicy: Always
    schedulerName: default-scheduler
    securityContext: {}
    serviceAccount: default
    serviceAccountName: default
    terminationGracePeriodSeconds: 30
    tolerations:
    - effect: NoExecute
      key: node.kubernetes.io/not-ready
      operator: Exists
      tolerationSeconds: 300
    - effect: NoExecute
      key: node.kubernetes.io/unreachable
      operator: Exists
      tolerationSeconds: 300
    volumes:
    - name: default-token-75dfq
      secret:
        defaultMode: 420
        secretName: default-token-75dfq
  status:
    conditions:
    - lastProbeTime: null
      lastTransitionTime: "2019-01-07T13:41:56Z"
      status: "True"
      type: Initialized
    - lastProbeTime: null
      lastTransitionTime: "2019-01-07T13:41:57Z"
      status: "True"
      type: Ready
    - lastProbeTime: null
      lastTransitionTime: "2019-01-07T13:41:57Z"
      status: "True"
      type: ContainersReady
    - lastProbeTime: null
      lastTransitionTime: "2019-01-07T13:41:56Z"
      status: "True"
      type: PodScheduled
    containerStatuses:
    - containerID: docker://bb426ed56436f4b31512812445eaefca65f88291da3bb4915a015b442ac52ef0
      image: nginx:1.13
      imageID: docker-pullable://nginx@sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35
      lastState: {}
      name: nginx-container
      ready: true
      restartCount: 0
      state:
        running:
          startedAt: "2019-01-07T13:41:56Z"
    hostIP: 192.168.25.102
    phase: Running
    podIP: 10.244.2.36
    qosClass: BestEffort
    startTime: "2019-01-07T13:41:56Z"
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
[root@kube-master manifest]# 

特定のネームスペースの Pod を確認

特定のネームスペースの Pod リソースを確認する場合には、以下コマンドを実行します。ここでは、 Kubernetes クラスタのコンポーネントやアドオンがデプロイされる kube-system のネットワークスペースの Pod を確認しています。

[root@kube-master ~]# kubectl --namespace=kube-system get all
NAME                                      READY   STATUS    RESTARTS   AGE
pod/coredns-86c58d9df4-c58n7              1/1     Running   0          55m
pod/coredns-86c58d9df4-xdzp9              1/1     Running   0          55m
pod/etcd-kube-master                      1/1     Running   0          54m
pod/kube-apiserver-kube-master            1/1     Running   0          53m
pod/kube-controller-manager-kube-master   1/1     Running   0          54m
pod/kube-flannel-ds-amd64-fghxk           1/1     Running   0          52m
pod/kube-flannel-ds-amd64-sxf7h           1/1     Running   0          54m
pod/kube-flannel-ds-amd64-xdn8d           1/1     Running   0          53m
pod/kube-proxy-gj7d9                      1/1     Running   0          55m
pod/kube-proxy-tdqwj                      1/1     Running   0          53m
pod/kube-proxy-vg2b4                      1/1     Running   0          52m
pod/kube-scheduler-kube-master            1/1     Running   0          54m

NAME               TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)         AGE
service/kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP   55m

NAME                                     DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR                     AGE
daemonset.apps/kube-flannel-ds-amd64     3         3         3       3            3           beta.kubernetes.io/arch=amd64     54m
daemonset.apps/kube-flannel-ds-arm       0         0         0       0            0           beta.kubernetes.io/arch=arm       54m
daemonset.apps/kube-flannel-ds-arm64     0         0         0       0            0           beta.kubernetes.io/arch=arm64     54m
daemonset.apps/kube-flannel-ds-ppc64le   0         0         0       0            0           beta.kubernetes.io/arch=ppc64le   54m
daemonset.apps/kube-flannel-ds-s390x     0         0         0       0            0           beta.kubernetes.io/arch=s390x     54m
daemonset.apps/kube-proxy                3         3         3       3            3           <none>                            55m

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/coredns   2/2     2            2           55m

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/coredns-86c58d9df4   2         2         2       55m
[root@kube-master ~]# 

特定のネームスペースの 特定のPod を確認

特定のネームスペースに登録されている特定の Pod リソースの詳細情報を確認したい場合には、以下コマンドを実行します。ここでは、kube-system ネームスペース の kube-scheduler-kube-master というPod リソースの詳細情報を確認しています。

[root@kube-master ~]# kubectl --namespace=kube-system describe pod kube-scheduler-kube-master
Name:               kube-scheduler-kube-master
Namespace:          kube-system
Priority:           2000000000
PriorityClassName:  system-cluster-critical
Node:               kube-master/192.168.25.100
Start Time:         Sat, 05 Jan 2019 10:21:40 +0900
Labels:             component=kube-scheduler
                    tier=control-plane
Annotations:        kubernetes.io/config.hash: 44b569a35761491825f4e7253fbf0543
                    kubernetes.io/config.mirror: 44b569a35761491825f4e7253fbf0543
                    kubernetes.io/config.seen: 2019-01-05T10:21:40.654558122+09:00
                    kubernetes.io/config.source: file
                    scheduler.alpha.kubernetes.io/critical-pod: 
Status:             Running
IP:                 192.168.25.100
Containers:
  kube-scheduler:
    Container ID:  docker://ac31ca7f12fc252d6b7847639853f53ecc70e65198874e06203b2c1fe764d432
    Image:         k8s.gcr.io/kube-scheduler:v1.13.1
    Image ID:      docker-pullable://k8s.gcr.io/kube-scheduler@sha256:4165e5f0d569b5b5e3bd90d78c30c5408b2c938d719939490299ab4cee9a9c0f
    Port:          <none>
    Host Port:     <none>
    Command:
      kube-scheduler
      --address=127.0.0.1
      --kubeconfig=/etc/kubernetes/scheduler.conf
      --leader-elect=true
    State:          Running
      Started:      Sat, 05 Jan 2019 10:21:42 +0900
    Ready:          True
    Restart Count:  0
    Requests:
      cpu:        100m
    Liveness:     http-get http://127.0.0.1:10251/healthz delay=15s timeout=15s period=10s #success=1 #failure=8
    Environment:  <none>
    Mounts:
      /etc/kubernetes/scheduler.conf from kubeconfig (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kubeconfig:
    Type:          HostPath (bare host directory volume)
    Path:          /etc/kubernetes/scheduler.conf
    HostPathType:  FileOrCreate
QoS Class:         Burstable
Node-Selectors:    <none>
Tolerations:       :NoExecute
Events:            <none>
[root@kube-master ~]# 

特定のネームスペースの特定の Pod のログ確認

特定のネームスペースに登録されている特定の Pod リソースのログ情報を確認する場合には、以下コマンドを実行します。ここでは、kube-system ネームスペース の kube-scheduler-kube-master というPod リソースのログ情報を確認しています。

root@kube-master ~]# kubectl --namespace=kube-system logs kube-scheduler-kube-master
I0105 01:21:42.414863       1 serving.go:318] Generated self-signed cert in-memory
W0105 01:21:42.664463       1 authentication.go:249] No authentication-kubeconfig provided in order to lookup client-ca-file in configmap/extension-apiserver-authentication in kube-system, so client certificate authentication won't work.
W0105 01:21:42.664476       1 authentication.go:252] No authentication-kubeconfig provided in order to lookup requestheader-client-ca-file in configmap/extension-apiserver-authentication in kube-system, so request-header client certificate authentication won't work.
W0105 01:21:42.664488       1 authorization.go:146] No authorization-kubeconfig provided, so SubjectAccessReview of authorization tokens won't work.
I0105 01:21:42.667968       1 server.go:150] Version: v1.13.1
I0105 01:21:42.667995       1 defaults.go:210] TaintNodesByCondition is enabled, PodToleratesNodeTaints predicate is mandatory
W0105 01:21:42.668500       1 authorization.go:47] Authorization is disabled
W0105 01:21:42.668506       1 authentication.go:55] Authentication is disabled
I0105 01:21:42.668514       1 deprecated_insecure_serving.go:49] Serving healthz insecurely on 127.0.0.1:10251
I0105 01:21:42.669086       1 secure_serving.go:116] Serving securely on [::]:10259
E0105 01:21:46.498197       1 reflector.go:134] k8s.io/kubernetes/cmd/kube-scheduler/app/server.go:232: Failed to list *v1.Pod: pods is forbidden: User "system:kube-scheduler" cannot list resource "pods" in API group "" at the cluster scope
[root@kube-master ~]# 

全てのリソースの一覧を確認

Pod や Node などのリソース種別を個別に指定せず、全てのリソースの一覧を取得する場合には、以下コマンドを実行します。ここでは、Pod リソースと Service リソースの一覧が取得されています。

[root@kube-master manifest]# kubectl get all
NAME             READY   STATUS    RESTARTS   AGE
pod/sample-pod   1/1     Running   0          5m34s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   45h
[root@kube-master manifest]# 

リソースの詳細情報を取得

あるリソースの詳細情報を取得する場合には、以下のようなコマンドを実行します。ここでは、sample-pod という名前の Pod リソースの詳細情報を取得しています。

[root@kube-master manifest]# kubectl describe pod sample-pod
Name:               sample-pod
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               kube-work2/192.168.25.102
Start Time:         Mon, 07 Jan 2019 22:41:56 +0900
Labels:             <none>
Annotations:        kubectl.kubernetes.io/last-applied-configuration:
                      {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"sample-pod","namespace":"default"},"spec":{"containers":[{"image":"ng...
Status:             Running
IP:                 10.244.2.36
Containers:
  nginx-container:
    Container ID:   docker://bb426ed56436f4b31512812445eaefca65f88291da3bb4915a015b442ac52ef0
    Image:          nginx:1.13
    Image ID:       docker-pullable://nginx@sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 07 Jan 2019 22:41:56 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-75dfq (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-75dfq:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-75dfq
    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
  ----    ------     ----   ----                 -------
  Normal  Scheduled  9m31s  default-scheduler    Successfully assigned default/sample-pod to kube-work2
  Normal  Pulled     9m31s  kubelet, kube-work2  Container image "nginx:1.13" already present on machine
  Normal  Created    9m31s  kubelet, kube-work2  Created container
  Normal  Started    9m31s  kubelet, kube-work2  Started container
[root@kube-master manifest]# 

リソースの使用状況や使用率を確認

あるリソースの使用状況やリソースの使用率を確認する場合には、以下のようなコマンドを実行します。ここでは、kube-work2 という名前の Node の使用状況やリソースの使用率を確認しています。

[root@kube-master manifest]# kubectl describe node kube-work2
Name:               kube-work2
Roles:              <none>
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/hostname=kube-work2
Annotations:        flannel.alpha.coreos.com/backend-data: {"VtepMAC":"ae:63:1c:71:3f:c4"}
                    flannel.alpha.coreos.com/backend-type: vxlan
                    flannel.alpha.coreos.com/kube-subnet-manager: true
                    flannel.alpha.coreos.com/public-ip: 192.168.25.102
                    kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock
                    node.alpha.kubernetes.io/ttl: 0
                    volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp:  Sat, 05 Jan 2019 11:45:38 +0900
Taints:             <none>
Unschedulable:      false
Conditions:
  Type             Status    LastHeartbeatTime                 LastTransitionTime                Reason                       Message
  ----             ------    -----------------                 ------------------                ------                       -------
  MemoryPressure   False     Mon, 07 Jan 2019 22:55:11 +0900   Sun, 06 Jan 2019 23:31:09 +0900   KubeletHasSufficientMemory   kubelet has sufficient memory available
  DiskPressure     False     Mon, 07 Jan 2019 22:55:11 +0900   Sun, 06 Jan 2019 23:31:09 +0900   KubeletHasNoDiskPressure     kubelet has no disk pressure
  PIDPressure      False     Mon, 07 Jan 2019 22:55:11 +0900   Sun, 06 Jan 2019 23:31:09 +0900   KubeletHasSufficientPID      kubelet has sufficient PID available
  Ready            True      Mon, 07 Jan 2019 22:55:11 +0900   Sun, 06 Jan 2019 23:31:09 +0900   KubeletReady                 kubelet is posting ready status
  OutOfDisk        Unknown   Sat, 05 Jan 2019 11:45:38 +0900   Sat, 05 Jan 2019 23:53:13 +0900   NodeStatusNeverUpdated       Kubelet never posted node status.
Addresses:
  InternalIP:  192.168.25.102
  Hostname:    kube-work2
Capacity:
 cpu:                2
 ephemeral-storage:  38770180Ki
 hugepages-1Gi:      0
 hugepages-2Mi:      0
 memory:             1882188Ki
 pods:               110
Allocatable:
 cpu:                2
 ephemeral-storage:  35730597829
 hugepages-1Gi:      0
 hugepages-2Mi:      0
 memory:             1779788Ki
 pods:               110
System Info:
 Machine ID:                 cf773ef721dcef2ed5b47fce59a84182
 System UUID:                CF773EF7-21DC-EF2E-D5B4-7FCE59A84182
 Boot ID:                    c1ba0154-9a21-4bb6-8460-c218352b2c62
 Kernel Version:             3.10.0-957.1.3.el7.x86_64
 OS Image:                   CentOS Linux 7 (Core)
 Operating System:           linux
 Architecture:               amd64
 Container Runtime Version:  docker://18.9.0
 Kubelet Version:            v1.13.1
 Kube-Proxy Version:         v1.13.1
PodCIDR:                     10.244.2.0/24
Non-terminated Pods:         (3 in total)
  Namespace                  Name                           CPU Requests  CPU Limits  Memory Requests  Memory Limits  AGE
  ---------                  ----                           ------------  ----------  ---------------  -------------  ---
  default                    sample-pod                     0 (0%)        0 (0%)      0 (0%)           0 (0%)         13m
  kube-system                kube-flannel-ds-amd64-w64mv    100m (5%)     100m (5%)   50Mi (2%)        50Mi (2%)      2d11h
  kube-system                kube-proxy-k76gv               0 (0%)        0 (0%)      0 (0%)           0 (0%)         2d11h
Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  Resource           Requests   Limits
  --------           --------   ------
  cpu                100m (5%)  100m (5%)
  memory             50Mi (2%)  50Mi (2%)
  ephemeral-storage  0 (0%)     0 (0%)
Events:              <none>
[root@kube-master manifest]# 

Pod 上でのコマンドの実行

Pod 上で特定のコマンドを実行したい場合には、以下コマンドを実行します。 /bin/sh などのシェルを実行することで、あたかもコンテナに対してログインしているような状態を実行可能となります。

[root@kube-master manifest]# kubectl exec -it sample-pod /bin/sh
# 
# ls -la 
total 8
drwxr-xr-x.   1 root root   28 Jan  7 13:41 .
drwxr-xr-x.   1 root root   28 Jan  7 13:41 ..
-rwxr-xr-x.   1 root root    0 Jan  7 13:41 .dockerenv
drwxr-xr-x.   2 root root 4096 Apr 26  2018 bin
drwxr-xr-x.   2 root root    6 Feb 23  2018 boot
drwxr-xr-x.   5 root root  360 Jan  7 13:41 dev
drwxr-xr-x.   1 root root   66 Jan  7 13:41 etc
drwxr-xr-x.   2 root root    6 Feb 23  2018 home
drwxr-xr-x.   1 root root   45 Apr 26  2018 lib
drwxr-xr-x.   2 root root   34 Apr 26  2018 lib64
drwxr-xr-x.   2 root root    6 Apr 26  2018 media
drwxr-xr-x.   2 root root    6 Apr 26  2018 mnt
drwxr-xr-x.   2 root root    6 Apr 26  2018 opt
dr-xr-xr-x. 122 root root    0 Jan  7 13:41 proc
drwx------.   2 root root   37 Apr 26  2018 root
drwxr-xr-x.   1 root root   38 Jan  7 13:41 run
drwxr-xr-x.   2 root root 4096 Apr 26  2018 sbin
drwxr-xr-x.   2 root root    6 Apr 26  2018 srv
dr-xr-xr-x.  13 root root    0 Jan  6 14:30 sys
drwxrwxrwt.   1 root root    6 Apr 30  2018 tmp
drwxr-xr-x.   1 root root   66 Apr 26  2018 usr
drwxr-xr-x.   1 root root   19 Apr 26  2018 var
# 

exit で 終了します。

# exit
[root@kube-master manifest]# 

コンテナ内のファイルをローカルにコピー

Pod (コンテナ)内のファイルをローカルへコピーしたい場合には、以下コマンドを実行します。 ここでは、sample-pod という名前のPod内にある v1.7.1.yaml ファイルをローカルにコピーしています。

[root@kube-master ~]# kubectl cp sample-pod:/etc/hostname v1.7.1.yaml 
tar: Removing leading `/' from member names
[root@kube-master ~]# 

ローカルマシンのディレクトリを参照します。 v1.7.1.yaml ファイルがコピーされています。

[root@kube-master ~]# ls -la
合計 80
〜(省略)〜
-rw-r--r--.  1 root root    11  1月 13 19:44 pod-nginx.yaml
-rw-r--r--.  1 root root    11  1月 13 19:44 v1.7.1.yaml
[root@kube-master ~]#

v1.7.1.yaml ファイルの中身を確認します。

[root@kube-master ~]# cat v1.7.1.yaml 
sample-pod
[root@kube-master ~]# 

ローカルファイルをコンテナ内にコピー

ローカルのファイルを Pod (コンテナ)内へコピーしたい場合には、以下コマンドを実行します。 ここでは、ローカルにある hostname というファイルを sample-pod という名前の Pod へコピーしています。

[root@kube-master ~]# kubectl cp hostname sample-pod:/tmp/
[root@kube-master ~]# 

Pod(コンテナ)内へコピーしたファイルを確認します。

[root@kube-master ~]# kubectl exec -it sample-pod ls /tmp/
hostname
[root@kube-master ~]# 

kubectl におけるデバッグ

kubectl を使って Kubernetes 上のデバッグログを出力する場合には、以下コマンドを実行します。Kubernetes 上で何かしらエラーがあった場合には、API との通信や kubectl の設定に問題があるケースが多く、その際のトラブルシューティングに活用できます。ここでは、 -v オプションを指定して ログレベル 8 のデバッグログを出力しています。

[root@kube-master ~]# kubectl -v=8 get pod
I0113 20:09:24.387309   28268 loader.go:359] Config loaded from file /etc/kubernetes/admin.conf
I0113 20:09:24.387828   28268 loader.go:359] Config loaded from file /etc/kubernetes/admin.conf
I0113 20:09:24.390550   28268 loader.go:359] Config loaded from file /etc/kubernetes/admin.conf
I0113 20:09:24.397245   28268 loader.go:359] Config loaded from file /etc/kubernetes/admin.conf
I0113 20:09:24.397683   28268 round_trippers.go:416] GET https://192.168.25.100:6443/api/v1/namespaces/default/pods?limit=500
I0113 20:09:24.397694   28268 round_trippers.go:423] Request Headers:
I0113 20:09:24.397700   28268 round_trippers.go:426]     Accept: application/json;as=Table;v=v1beta1;g=meta.k8s.io, application/json
I0113 20:09:24.397707   28268 round_trippers.go:426]     User-Agent: kubectl/v1.13.1 (linux/amd64) kubernetes/eec55b9
I0113 20:09:24.405241   28268 round_trippers.go:441] Response Status: 200 OK in 7 milliseconds
I0113 20:09:24.405276   28268 round_trippers.go:444] Response Headers:
I0113 20:09:24.405282   28268 round_trippers.go:447]     Content-Type: application/json
I0113 20:09:24.405288   28268 round_trippers.go:447]     Content-Length: 3634
I0113 20:09:24.405293   28268 round_trippers.go:447]     Date: Sun, 13 Jan 2019 11:09:24 GMT
I0113 20:09:24.405382   28268 request.go:942] Response Body: {"kind":"Table","apiVersion":"meta.k8s.io/v1beta1","metadata":{"selfLink":"/api/v1/namespaces/default/pods","resourceVersion":"1014347"},"columnDefinitions":[{"name":"Name","type":"string","format":"name","description":"Name must be unique within a namespace. Is required when creating resources, although some resources may allow a client to request the generation of an appropriate name automatically. Name is primarily intended for creation idempotence and configuration definition. Cannot be updated. More info: http://kubernetes.io/docs/user-guide/identifiers#names","priority":0},{"name":"Ready","type":"string","format":"","description":"The aggregate readiness state of this pod for accepting traffic.","priority":0},{"name":"Status","type":"string","format":"","description":"The aggregate status of the containers in this pod.","priority":0},{"name":"Restarts","type":"integer","format":"","description":"The number of times the containers in this pod have been restarted.","priority":0},{"name":"Age","type":"stri [truncated 2610 chars]
I0113 20:09:24.406206   28268 get.go:563] no kind is registered for the type v1beta1.Table in scheme "k8s.io/kubernetes/pkg/api/legacyscheme/scheme.go:29"
NAME         READY   STATUS    RESTARTS   AGE
sample-pod   1/1     Running   0          5d21h
[root@kube-master ~]#