Kubernetes(クバネティス、K8sと略される。8は「ubernete」の8文字を置き換えたもの)は、クラウドプラットフォーム内の複数のホストにあるコンテナ化されたアプリケーションを管理するためのオープンソースシステムです。Kubernetesの目標は、コンテナ化されたアプリケーションのデプロイをシンプルかつ効率的(強力)にすることです。Kubernetesは、アプリケーションのデプロイ、スケーリング、更新、メンテナンスのメカニズムを提供します。
リソース情報の表示
ノード (Nodes)
リソース名: nodes, 略称: no
$ kubectl get no # すべてのノードを表示
$ kubectl get no -o wide # すべてのノードの詳細情報を表示
$ kubectl describe no # ノードの詳細を表示
$ kubectl get no -o yaml # ノードの詳細をYAML形式で表示
$ kubectl get node --selector=[label_name] # 特定のラベルでノードをフィルタリング
$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
# jsonpath式で定義されたフィールド情報を出力
$ kubectl top node [node_name] # ノードのリソース使用状況(CPU/メモリ/ストレージ)を表示
ポッド (Pods)
リソース名: pods, 略称: po
$ kubectl get po # すべてのポッドを表示
$ kubectl get po -o wide
$ kubectl describe po
$ kubectl get po --show-labels # ポッドのラベルを表示
$ kubectl get po -l app=nginx
$ kubectl get po -o yaml
$ kubectl get pod [pod_name] -o yaml --export
$ kubectl get pod [pod_name] -o yaml --export > nameoffile.yaml
# ポッドの情報をYAMLファイルに出力
$ kubectl get pods --field-selector status.phase=Running
# フィールドセレクタを使用してポッド情報をフィルタリング
ネームスペース (Namespaces)
リソース名: namespaces, 略称: ns
$ kubectl get ns
$ kubectl get ns -o yaml
$ kubectl describe ns
デプロイメント (Deployments)
リソース名: deployments, 略稱: deploy
$ kubectl get deploy
$ kubectl describe deploy
$ kubectl get deploy -o wide
$ kubectl get deploy -o yaml
サービス (Services)
リソース名: services, 略称: svc
$ kubectl get svc
$ kubectl describe svc
$ kubectl get svc -o wide
$ kubectl get svc -o yaml
$ kubectl get svc --show-labels
デーモンセット (DaemonSets)
リソース名: daemonsets, 略称: ds
$ kubectl get ds
$ kubectl describe ds --all-namespaces
$ kubectl describe ds [daemonset_name] -n [namespace_name]
$ kubectl get ds [ds_name] -n [ns_name] -o yaml
イベント (Events)
リソース名: events, 略称: ev
$ kubectl get events
$ kubectl get events -n kube-system
$ kubectl get events -w
ログ (Logs)
$ kubectl logs [pod_name]
$ kubectl logs --since=1h [pod_name]
$ kubectl logs --tail=20 [pod_name]
$ kubectl logs -f -c [container_name] [pod_name]
$ kubectl logs [pod_name] > pod.log
サービスアカウント (Service Accounts)
リソース名: serviceaccounts, 略称: sa
$ kubectl get sa
$ kubectl get sa -o yaml
$ kubectl get serviceaccounts default -o yaml >./sa.yaml
$ kubectl replace serviceaccount default -f ./sa.yaml
レプリカセット (ReplicaSets)
リソース名: replicasets, 略称: rs
$ kubectl get rs
$ kubectl describe rs
$ kubectl get rs -o wide
$ kubectl get rs -o yaml
ロール (Roles)
$ kubectl get roles --all-namespaces
$ kubectl get roles --all-namespaces -o yaml
シークレット (Secrets)
$ kubectl get secrets
$ kubectl get secrets --all-namespaces
$ kubectl get secrets -o yaml
コンフィグマップ (ConfigMaps)
リソース名: configmaps, 略称: cm
$ kubectl get cm
$ kubectl get cm --all-namespaces
$ kubectl get cm --all-namespaces -o yaml
イングレス (Ingresses)
リソース名: ingresses, 略称: ing
$ kubectl get ing
$ kubectl get ing --all-namespaces
永続ボリューム (PersistentVolumes)
リソース名: persistentvolumes, 略称: pv
$ kubectl get pv
$ kubectl describe pv
永続ボリューム要求 (PersistentVolumeClaims)
リソース名: persistentvolumeclaims, 略称: pvc
$ kubectl get pvc
$ kubectl describe pvc
ストレージクラス (StorageClasses)
リソース名: storageclasses, 略称: sc
$ kubectl get sc
$ kubectl get sc -o yaml
複数リソース
$ kubectl get svc, po
$ kubectl get deploy, no
$ kubectl get all
$ kubectl get all --all-namespaces
リソース属性の変更
テイント (Taints)
$ kubectl taint [node_name] [taint_name]
ラベル (Labels)
$ kubectl label [node_name] disktype=ssd
$ kubectl label [pod_name] env=prod
コーダン / アンコーダン (Cordon / Uncordon)
$ kubectl cordon [node_name] # ノードの保守(新規ポッドの停止)
$ kubectl uncordon [node_name] # ノードのスケジュール再開
ノードのドレイン (Draining Nodes)
$ kubectl drain [node_name] # ノードを空にする
ノード / ポッド
$ kubectl delete node [node_name]
$ kubectl delete pod [pod_name]
$ kubectl edit node [node_name]
$ kubectl edit pod [pod_name]
デプロイメント / ネームスペース
$ kubectl edit deploy [deploy_name]
$ kubectl delete deploy [deploy_name]
$ kubectl expose deploy [deploy_name] --port=80 --type=NodePort
$ kubectl scale deploy [deploy_name] --replicas=5
$ kubectl delete ns
$ kubectl edit ns [ns_name]
サービス
$ kubectl edit svc [svc_name]
$ kubectl delete svc [svc_name]
デーモンセット
$ kubectl edit ds [ds_name] -n kube-system
$ kubectl delete ds [ds_name]
サービスアカウント
$ kubectl edit sa [sa_name]
$ kubectl delete sa [sa_name]
アノテーション (Annotations)
$ kubectl annotate po [pod_name] [annotation]
$ kubectl annotate no [node_name]
リソースの追加
ポッドの作成
$ kubectl create -f [name_of_file]
$ kubectl apply -f [name_of_file]
$ kubectl run [pod_name] --image=nginx --restart=Never
$ kubectl run [pod_name] --generator=run-pod/v1 --image=nginx
$ kubectl run [pod_name] --image=nginx --restart=Never
サービスの作成
$ kubectl create svc nodeport [svc_name] --tcp=8080:80
デプロイメントの作成
$ kubectl create -f [name_of_file]
$ kubectl apply -f [name_of_file]
$ kubectl create deploy [deploy_name] --image=nginx
コンテナとの対話
$ kubectl run [pod_name] --image=busybox --rm -it --restart=Never -- sh
YAMLファイルの出力
$ kubectl create deploy [deploy_name] --image=nginx --dry-run -o yaml > deploy.yaml
$ kubectl get po [pod_name] -o yaml --export > pod.yaml
ヘルプの表示
$ kubectl -h
$ kubectl create -h
$ kubectl run -h
$ kubectl explain deploy.spec
リクエスト
API呼び出し
$ kubectl get --raw /apis/metrics.k8s.io/
クラスター情報
$ kubectl config
$ kubectl cluster-info
$ kubectl get componentstatus