$ kubectl run hello-world --image=hello-world -it --restart=Never
Hello from Docker! This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/
For more examples and ideas, visit: https://docs.docker.com/get-started/
実行できていることがわかる Podの一覧を取得してみると
1 2 3
$ kubectl get pod NAME READY STATUS RESTARTS AGE hello-world 0/1 Completed 0 54s
実行完了したPodが残っているのがわかる
–rmオプション
dockerと同じ(-itも一緒だった)で--rmで実行後に自動削除してくれる
1 2
$ kubectl run hello-world --image=hello-world -it --restart=Never --rm Error from server (AlreadyExists): pods "hello-world" already exists
先ほど起動したPodを削除していなかったので削除する
1 2 3 4
$ kubectl delete pod hello-world pod "hello-world" deleted $ kubectl get pod No resources found in default namespace.
再度実行
1 2 3 4 5 6
$ kubectl run hello-world --image=hello-world -it --restart=Never --rm
Hello from Docker! (省略) $ kubectl get pod No resources found in default namespace.
実行後削除されているのがわかる
バックグラウンド実行
-itなしで実行することでバックグラウンドでの実行となる
1 2
$ kubectl run hello-world --image=hello-world --restart=Never pod/hello-world created
出力(log)を確認する
1 2 3 4 5 6 7 8 9
$ kubectl logs hello-world
Hello from Docker! (省略) $ kubectl get pod NAME READY STATUS RESTARTS AGE hello-world 0/1 Completed 0 59s $ kubectl delete pod hello-world pod "hello-world" deleted
$ kubectl run hello-world --image=hello-world kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead. deployment.apps/hello-world created
DEPRECATEDのメッセージが出てしまったが、deployment.apps/hello-world createdとのことなので実行はできているっぽい Use kubectl run --generator=run-pod/v1 or kubectl create instead.とのことなので今後は kubectl create deployment --image hello-world hello-worldを利用する
1 2 3 4 5 6 7 8 9 10 11 12
$ kubectl get all NAME READY STATUS RESTARTS AGE pod/hello-world-664fd677b8-gmm2f 0/1 Completed 3 62s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10h
NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/hello-world 0/1 1 0 62s
NAME DESIRED CURRENT READY AGE replicaset.apps/hello-world-664fd677b8 1 1 0 62s
試しにPodを削除してみる
1 2
$ kubectl delete pod hello-world-664fd677b8-gmm2f pod "hello-world-664fd677b8-gmm2f" deleted
しかし、ReplicaSetが再度起動する
1 2 3
$ kubectl get pod NAME READY STATUS RESTARTS AGE hello-world-664fd677b8-6n967 0/1 CrashLoopBackOff 2 43s
$ kubectl delete deployment hello-world deployment.apps "hello-world" deleted $ kubectl get all NAME READY STATUS RESTARTS AGE pod/hello-world-664fd677b8-6n967 0/1 Terminating 7 13m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10h
Podは停止に少し時間がかかるが、少し待てば削除される
1 2 3
$ kubectl get all NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10h