初めまして、井村と申します。
担当している案件にてkubernetesを扱っており、備忘の為、kubectlコマンド(linuxコマンドとの組み合わせ)を記載します。
kubectlコマンドのhelpは例とオプションが記載されているのわかりやすい。
$ kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
10.0.0.10 126m 6% 3728Mi 58%
10.0.0.11 53m 2% 4474Mi 69%
10.0.0.12 475m 25% 4998Mi 37%
# 現在使用しているメモリの量を大きい順に並替え
]$ kubectl top node --sort-by=memory
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
10.0.0.12 475m 25% 4998Mi 37%
10.0.0.11 53m 2% 4474Mi 69%
10.0.0.10 126m 6% 3728Mi 58%
# ヘルプ
$ kubectl top node --help
Display resource (CPU/memory) usage of nodes.
The top-node command allows you to see the resource consumption of nodes.
Aliases:
node, nodes, no
Examples:
# Show metrics for all nodes
kubectl top node
# Show metrics for a given node
kubectl top node NODE_NAME
Options:
--no-headers=false:
If present, print output without headers
-l, --selector='':
Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2). Matching
objects must satisfy all of the specified label constraints.
--show-capacity=false:
Print node resources based on Capacity instead of Allocatable(default) of the nodes.
--sort-by='':
If non-empty, sort nodes list using specified field. The field can be either 'cpu' or 'memory'.
--use-protocol-buffers=true:
Enables using protocol-buffers to access Metrics API.
Usage:
kubectl top node [NAME | -l label] [options]
Use "kubectl options" for a list of global command-line options (applies to all commands).
以下のマニュフェストファイルを用いてポッド関連のコマンドも記載。
$ cat nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
$ kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
grepコマンドはよく使う
$ kubectl get all -n default | grep nginx
pod/nginx-deployment-576c6b7b6-7mtk6 1/1 Running 0 78s
pod/nginx-deployment-576c6b7b6-82tv7 1/1 Running 0 78s
pod/nginx-deployment-576c6b7b6-cwp79 1/1 Running 0 78s
deployment.apps/nginx-deployment 3/3 3 3 78s
replicaset.apps/nginx-deployment-576c6b7b6 3 3 3 78s
ポッド内で起動しているコンテナ名について
# 一覧が表示
$ kubectl get pod nginx-deployment-576c6b7b6-7mtk6 -o json
# コンテナ名を取得
$ kubectl get pod nginx-deployment-576c6b7b6-7mtk6 -o jsonpath="{.spec.containers[*].name}"
nginx
# xargsも良く使う
kubectl get pod nginx-deployment-576c6b7b6-7mtk6 -o jsonpath="{.spec.containers[*].name}" | xargs -I {} echo "コンテナ名は {} だよ"
コンテナ名は nginx だよ
# コンテナに対して命令する
$ kubectl exec -it nginx-deployment-576c6b7b6-7mtk6 -c nginx -- curl http://localhost
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
無限ループ処理
$ kubectl exec nginx-deployment-576c6b7b6-7mtk6 -c nginx -- sh -c 'while true; do curl http://localhost > /dev/null 2>&1; done' &
[1] 32636
$ ps aux | grep 'kubectl exec'
hogehoge 32636 0.4 0.3 5472544 28020 pts/0 Sl 05:46 0:00 kubectl exec nginx-deployment-576c6b7b6-7mtk6 -c nginx -- sh -c while true; do curl http://localhost > /dev/null 2>&1; done
hogehoge 32644 0.0 0.0 112808 964 pts/0 S+ 05:46 0:00 grep --color=auto kubectl exec
$ kill 32636
(pkill -f 'kubectl exec')
[1]+ Terminated kubectl exec nginx-deployment-576c6b7b6-7mtk6 -c nginx -- sh -c 'while true; do curl http://localhost > /dev/null 2>&1; done'
ポッド名のみ取得
# 一覧
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-576c6b7b6-7mtk6 1/1 Running 0 14m
nginx-deployment-576c6b7b6-82tv7 1/1 Running 0 14m
nginx-deployment-576c6b7b6-cwp79 1/1 Running 0 14m
# 例1
$ kubectl get pods -o jsonpath="{.items[*].metadata.name}"
nginx-deployment-576c6b7b6-7mtk6 nginx-deployment-576c6b7b6-82tv7 nginx-deployment-576c6b7b6-cwp79
# 例2
$ kubectl get pods -o jsonpath="{.items[*].metadata.name}" | tr ' ' '\n'
nginx-deployment-576c6b7b6-7mtk6
nginx-deployment-576c6b7b6-82tv7
nginx-deployment-576c6b7b6-cwp79
# 例3
$ kubectl get pod --no-headers | awk '{print $1}'
nginx-deployment-576c6b7b6-7mtk6
nginx-deployment-576c6b7b6-82tv7
nginx-deployment-576c6b7b6-cwp79
ポッドが何百台といるときの起動確認
# デプロイされているポッド数とRunning状態のPポッド数が同じであることの確認
$ kubectl get pod -o wide | grep -v -e "NAME" | wc -l
3
$ kubectl get pod -o wide | grep "Running" | wc -l
3
ログについて
# ログ取得
$ kubectl logs nginx-deployment-576c6b7b6-7mtk6 -c nginx
# オプションのtail
$ kubectl logs nginx-deployment-576c6b7b6-7mtk6 -c nginx --tail=3
2025/02/07 04:10:08 [notice] 1#1: start worker process 29
2025/02/07 04:10:08 [notice] 1#1: start worker process 30
127.0.0.1 - - [07/Feb/2025:04:36:25 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.88.1" "-"
# プロンプト画面に常に最新のログ出力
$ kubectl logs nginx-deployment-576c6b7b6-7mtk6 -c nginx -f
# 上記で紹介したlinuxコマンドをまぜたり
$ kubectl get pod --no-headers | awk '{print $1}' | xargs -I {} sh -c 'echo "ポッド名は {} だよ~"; kubectl logs {} --tail=3; echo "--------------------"'
ポッド名は nginx-deployment-576c6b7b6-7mtk6 だよ~
2025/02/07 04:10:08 [notice] 1#1: start worker process 29
2025/02/07 04:10:08 [notice] 1#1: start worker process 30
127.0.0.1 - - [07/Feb/2025:04:36:25 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.88.1" "-"
--------------------
ポッド名は nginx-deployment-576c6b7b6-82tv7 だよ~
2025/02/07 04:10:06 [notice] 1#1: start worker processes
2025/02/07 04:10:06 [notice] 1#1: start worker process 28
2025/02/07 04:10:06 [notice] 1#1: start worker process 29
--------------------
ポッド名は nginx-deployment-576c6b7b6-cwp79 だよ~
2025/02/07 04:10:07 [notice] 1#1: start worker processes
2025/02/07 04:10:07 [notice] 1#1: start worker process 28
2025/02/07 04:10:07 [notice] 1#1: start worker process 29
--------------------
マニュフェストファイル修正、テキスト処理の際にsedコマンドも使います
# nginxコンテナからバージョンを確認、取得
$ kubectl exec -it nginx-deployment-576c6b7b6-7mtk6 -- nginx -v > nginx_version.txt ; cat nginx_version.txt
nginx version: nginx/1.27.4
# ファイル修正
$ sed -i 's/nginx\/1.27.4/nginx\/1.25.0/' nginx_version.txt ; cat nginx_version.txt
nginx version: nginx/1.25.0
最後は忘れずにポッドを削除しましょう。
$ kubectl delete -f nginx-deployment.yaml
deployment.apps "nginx-deployment" deleted