🍣

CKA(Kubernetes) 学習メモ(コマンド)

2023/05/13に公開

Alias 設定

alias k='kubectl'
alias kubectl='k3s kubectl'

Kubernetes Cheat 自動補完

source <(kubectl completion bash) # set up autocomplete in bash into the current shell, bash-completion package should be installed first.
echo "source <(kubectl completion bash)" >> ~/.bashrc # add autocomplete permanently to your bash shell.
alias k=kubectl
complete -o default -F __start_kubectl k

kubectl get

Podの状況確認

kubectl get pods --watch

▼実行結果
controlplane ~ ➜ kubectl get pods --watch
NAME READY STATUS RESTARTS AGE
static-busybox-controlplane 1/1 Running 0 7m46s
static-greenbox-node01 1/1 Running 0 4m30s
static-greenbox-node01 0/1 Error 0 4m41s
static-greenbox-node01 0/1 Terminating 0 4m42s

Podについてもう少し情報が欲しい時

kubectl get nodes -o wide

▼実行結果
controlplane ~ ➜ k get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
frontend-6fb456676-r8pt6 1/1 Running 0 2m25s 10.42.0.13 controlplane <none> <none>
frontend-6fb456676-8f575 1/1 Running 0 2m26s 10.42.0.12 controlplane <none> <none>
frontend-6fb456676-t8dvg 1/1 Running 0 2m26s 10.42.0.11 controlplane <none> <none>
frontend-6fb456676-7sz4b 1/1 Running 0 2m26s 10.42.0.10 controlplane <none> <none>

Namespaceをのリストを取得

kubectl get namaspace

▼実行結果
controlplane ~ ➜ k get ns
NAME STATUS AGE
apx-x9984574 Active 94s
default Active 94m
kube-flannel Active 94m
kube-node-lease Active 94m
kube-public Active 94m
kube-system Active 94m

Namespace関係なく全てのPodを確認

kubectl get pods -A

▼実行結果
controlplane ~ ➜ kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-flannel kube-flannel-ds-f44xh 1/1 Running 0 9m13s
kube-system coredns-787d4945fb-g2cwk 1/1 Running 0 9m13s
kube-system coredns-787d4945fb-glrj9 1/1 Running 0 9m13s
kube-system etcd-controlplane 1/1 Running 0 9m28s

kubectl describe

特定のポッドのネームスペースを指定し、Image情報だけを取得

kubectl describe pod kube-scheduler-controlplane -n kube-system | grep Image

▼実行結果
controlplane ~ ➜ kubectl describe pod kube-scheduler-controlplane -n kube-system | grep Image
Image: registry.k8s.io/kube-scheduler:v1.26.0
Image ID: registry.k8s.io/kube-scheduler@sha256:34a142549f94312b41d4a6cd98e7fddabff484767a199333acb7503bf46d7410

NodeのリストをJSON形式にし、特定のパスにリダイレクトする

kubectl get nodes -o json > /xxx/xxx

▼実行結果
ontrolplane ~ ➜ k get nodes -o json > /opt/outputs/nodes-z3444kd9.json

controlplane ~ ➜ cat /opt/outputs/nodes-z3444kd9.json
{
"apiVersion": "v1",
"items": [
{

kubectl create/replace/run

yamlファイルの定義に沿ったPodを作成する

kubectl create -f ⚪⚪⚪.yaml

▼実行結果
controlplane ~ ➜ kubectl create -f nginx-pod.yaml
pod/nginx created

Static Podを Dry runオプションでYAMLファイルに出力する

kubectl run <static pod name> --dry-run=client -o yaml --command -- sleep xx > xxx.yaml 

▼実行結果
controlplane ~ ✖ k run static-busybox --image=busybox --dry-run=client -o yaml --command -- sleep 1000 > static-busybox.yaml

controlplane ~ ✖ cat static-busybox.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: static-busybox
name: static-busybox
spec:
containers:

指定のNamespaceにPodを作成する

kubectl run <pod name> -n <namespace>

▼実行結果
controlplane ~ ➜ k run temp-bus --image=redis:alpine -n finance
pod/temp-bus created

controlplane ~ ➜ k get pod -n finance
NAME READY STATUS RESTARTS AGE
temp-bus 1/1 Running 0 29s

Podの再作成

kubectl replace --force -f /tmp/xxx.yaml

▼実行結果
controlplane ~ ➜ k replace --force -f /tmp/kubectl-edit-4058511448.yaml
pod "ubuntu-sleeper-3" deleted
pod/ubuntu-sleeper-3 replaced

imageを指定してPodを作成した場合に、YML定義ファイルがどうなるかの出力を行う

kubectl run <pod name> --image=<image name> --dry-run=client -o yaml

▼実行結果
controlplane ~/webapp-color-3 ➜ k run webapp-green --image=kodekloud/webapp-color --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: webapp-green
name: webapp-green
spec:
containers:

  • image: kodekloud/webapp-color
    name: webapp-green
    resources: {}
    dnsPolicy: ClusterFirst
    restartPolicy: Always
    status: {}

Podを晒す

kubectl expose pod <pod name> --port=<port> --name=<service name>

▼実行結果
controlplane ~ ✖ k expose pod messaging --port=6379 --name=messaging-service
service/messaging-service exposed

Deploymentを作成する

kubectl create deployment <deployment name> --<options>

▼実行結果
controlplane ~ ➜ k create deploy hr-web-app --image=kodekloud/webapp-color --replicas=2
deployment.apps/hr-web-app created

Logging & Monitoring

Node

kubectl top node

▼実行結果
controlplane kubernetes-metrics-server on  master ➜ kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
controlplane 290m 0% 1040Mi 0%
node01 33m 0% 301Mi 0%

Pod

kubectl top pod

▼実行結果
controlplane kubernetes-metrics-server on  master ➜ kubectl top pod
NAME CPU(cores) MEMORY(bytes)
elephant 20m 32Mi
lion 1m 18Mi
rabbit 130m 42Mi

Podについてのログ

kubectl logs <pod>

▼実行結果
controlplane ~ ➜ kubectl logs webapp-1
[2023-05-06 07:13:46,031] INFO in event-simulator: USER1 is viewing page2
[2023-05-06 07:13:47,032] INFO in event-simulator: USER4 is viewing page2
[2023-05-06 07:13:48,052] INFO in event-simulator: USER3 is viewing page2
[2023-05-06 07:13:49,053] INFO in event-simulator: USER3 is viewing page1
[2023-05-06 07:13:50,054] INFO in event-simulator: USER1 is viewing page2
[2023-05-06 07:13:51,056] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.

Query

jsonpathクエリを用いて、ノードに関する特定の情報を出力する

kubectl get nodes -o jsonpath='{xxx}'

▼実行結果
controlplane ~ ➜ k get nodes -o jsonpath='{.items[].status.nodeInfo.osImage}'
Ubuntu 20.04.5 LTS
controlplane ~ ➜ k get nodes -o jsonpath='{.items[
].status.nodeInfo.osImage}' > /opt/outputs/nodes_os_x43kj56.txt

Discussion