IT-LIFEブログ

主にIT系のことや時事ネタや、たまに雑記

【Kubernetes】Podに入ってコマンド実行する方法

以下のpod.ymlを作成します。

apiVersion: v1
kind: Pod
metadata:
    name: debug
    namespace: default
    labels:
        env: study 
spec:
    containers:
    - name: debug
      image: centos:7
      command:
      - "sh"
      - "-c"
      args:
      - |
        while true
        do
          sleep ${DELAY}
        done
      env:
      - name: "DELAY"
        value: "5"

---
apiVersion: v1
kind: Pod
metadata:
    name: nginx
    namespace: default
    labels:
        env: study 
spec:
    containers:
    - name: nginx
      image: nginx:1.17.2-alpine

まずはマニフェストファイルからPodを立ち上げる

kubectl apply -f pod.yml 

以下のコマンドで起動しているPodを確認

kubectl get pod -o wide

f:id:nok-0930-ss:20200502163448p:plain

CentOS:7イメージで作成されたコンテナがあるPodに入る

kubectl exec -it debug sh

CentOS:7PodからnginxPodに対してcurlコマンドを実行してみましょう

curl http://172.17.0.5

実行結果 f:id:nok-0930-ss:20200502163700p:plain

以下のコマンドでPodを抜けて終了です。

exit

不要なPodは以下のコマンドで削除できます。

kubectl delete -f pod.yml