kubectlコマンド一覧
Podのリソースを取得
code:bash
% kubectl get pod
NAME READY STATUS RESTARTS AGE
myapp 1/1 Running 0 20m
Podのリソースを取得(IPアドレスやNode情報を取得)
code:bash
% kubectl get pod --output wide --namespace default
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp 1/1 Running 0 22m 10.244.0.5 kind-control-plane <none> <none>
YAML形式でリソース情報の取得
code:bash
% kubectl get pod myapp --output yaml --namespace default (git)-main apiVersion: v1
kind: Pod
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"labels":{"app":"myapp"},"name":"myapp","namespace":"default"},"spec":{"containers":[{"image":"blux2/hello-server:1.0","name":"hello-server","ports":{"containerPort":8080}}]}} creationTimestamp: "2024-06-23T05:51:25Z"
labels:
app: myapp
name: myapp
namespace: default
resourceVersion: "623"
uid: 04da3cba-2fc8-4a87-8c4c-685eedf9f462
spec:
containers:
- image: blux2/hello-server:1.0
imagePullPolicy: IfNotPresent
name: hello-server
ports:
- containerPort: 8080
protocol: TCP
...
リソースの詳細を取得
code:bash
% kubectl describe pod myapp --namespace default
Name: myapp
Namespace: default
Priority: 0
Service Account: default
Node: kind-control-plane/172.20.0.2
Start Time: Sun, 23 Jun 2024 14:51:25 +0900
Labels: app=myapp
Annotations: <none>
Status: Running
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 25m default-scheduler Successfully assigned default/myapp to kind-control-plane
Normal Pulling 25m kubelet Pulling image "blux2/hello-server:1.0"
Normal Pulled 25m kubelet Successfully pulled image "blux2/hello-server:1.0" in 5.304s (5.304s including waiting)
Normal Created 25m kubelet Created container hello-server
Normal Started 25m kubelet Started container hello-server
コンテナのログを取得
code:bash
% kubectl logs myapp --namespace default
2024/06/23 05:51:31 Starting server on port 8080
# 複数コンテナがPod内に存在する場合は,--container(-c)オプションを利用して,コンテナを指定
% kubectl logs myapp --container hello-server --namespace default 2024/06/23 05:51:31 Starting server on port 8080
特定のDeploymentに紐づくPodのログを参照
code:bash
% kubectl logs deploy/hello-server
デバック用のサイドカーコンテナを立ち上げる
code:bash
% kubectl debug --stdin --tty myapp --image=curlimages/curl:8.4.0 --target=hello-server --namespace default -- sh
Targeting container "hello-server". If you don't see processes from this container it may be because the container runtime doesn't support this feature.
Defaulting debug container name to debugger-dxvcd.
If you don't see a command prompt, try pressing enter.
~ $ curl localhost:8080
Hello, world!~ $
コンテナを即座に実行,コンテナにログイン
code:bash
% kubectl --namespace default run curlpod --image=curlimages/curl:8.4.0 --command -- /bin/sh -c "while true; do sleep inifigy; done;"
pod/curlpod created
% kubectl get pod
NAME READY STATUS RESTARTS AGE
curlpod 1/1 Running 0 36s
myapp 1/1 Running 0 55m
myapp3 1/1 Running 0 23m
% kubectl get pod myapp --output wide -n default
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp 1/1 Running 0 56m 10.244.0.5 kind-control-plane <none> <none>
% kubectl -n default exec --stdin --tty curlpod -- /bin/sh
~ $ curl 10.244.0.5:8080
Hello, world!~
port-forward
code:bash
% kubectl port-forward myapp 5555:8080 -n default
Forwarding from 127.0.0.1:5555 -> 8080
Forwarding from ::1:5555 -> 8080 % curl localhost:5555
Hello, world!%
edit(マニュフェストとリソースの整合性が取れなくなるので,あまり推奨されていない)
code:bash
% kubectl edit pod myapp -n default
...
pod/myapp edited
リソースを削除
kubectlには,Podを再起動するというコマンドはない
そのため,kubectl delete pod podAで特定のPodを削除し,Deploymentにより再度作成されることで,再起動と同等の処理を行う
Deployment全体を再起動したいときは,kubectl rollout restartを利用する
code:bash
% kubectl get pod
NAME READY STATUS RESTARTS AGE
curlpod 1/1 Running 0 13m
myapp 1/1 Running 0 68m
myapp3 1/1 Running 0 36m
% kubectl delete pod myapp -n default
pod "myapp" deleted
% kubectl get pod
NAME READY STATUS RESTARTS AGE
curlpod 1/1 Running 0 14m
myapp3 1/1 Running 0 37m
kubectl cheat sheet