推荐工具
kubectx
kubectx:用来切换集群的访问
kubens:用来切换默认的namespace
kubectl-aliases
kubectl命令别名
自动完成
zsh
1
2
source <(kubectl completion zsh) # setup autocomplete in zsh into the current shell
echo "if [ $commands[kubectl] ]; then source <(kubectl completion zsh); fi" >> ~/.zshrc # add autocomplete permanently to your zsh shell
其他的方式见kubectl Cheat Sheet
kubectl常用命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
kubectl api-resources --namespaced=false
kubectl api-resources --namespaced=true
# -R表示递归目录下所有配置
kubectl apply -R -f configs/
kubectl get cs
kubectl get svc --sort-by=.metadata.creationTimestamp
# 查看节点
kubectl get no --sort-by=.metadata.creationTimestamp
kubectl drain <node-name>
kubectl taint nodes node1 key=value:NoSchedule
kubectl get ing pdd --n java
# 不调度
kubectl cluster-info dump
kubectl get po --field-selector spec.nodeName=xxxx
kubectl exec -it ng-57d74c8694-6cqnz sh -n=java
kubectl get pods --all-namespaces --field-selector spec.nodeName=<node> -o wide
kubectl get pods -o wide --all-namespaces
kubectl get po -l app=nginx -w
kubectl delete po -l app=onekey-ali-web -n=$(namespace)
kubectl get po --all-namespaces
# 查看异常pod
kubectl get po --all-namespaces --field-selector 'status.phase!=Running'
集群管理相关命令
应用管理相关
1
2
3
4
5
6
7
8
9
10
11
kubectl top pod
kubectl delete deployment,services -l app=nginx
kubectl scale deployment/nginx-deployment --replicas=2
kubectl get svc --all-namespaces=true
kubectl rollout undo deployment/nginx-deployment --to-revision=2
kubectl autoscale deployment <deployment-name> --min=2 --max=5 --cpu-percent=80
#来watch ReplicaSet的变化。
kubectl get rs -w
kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
# deployment "nginx-deployment" image updated
kubectl set image deploy monitorapi-deployment monitorapi=registry-vpc.cn-shenzhen.aliyuncs.com/amiba/monitorapi:1.2.4 -n=java
强制删除
有时 删除pv/pvc时会有问题,这个使用得加2个命令参数--grace-period=0 --force
删除所有失败的pod
1
2
3
4
5
6
kubectl get po --all-namespaces --field-selector 'status.phase==Failed'
kubectl delete po --field-selector 'status.phase==Failed'
#模糊删除pod
key=
kgpo -n default | grep $key | awk '{print $1}' | xargs kubectl delete po -n1 -n default
事件
1
2
kubectl get events --field-selector involvedObject.kind=Service --sort-by='.metadata.creationTimestamp'
kubectl get event --all-namespaces --field-selector involvedObject.name=$po
参考链接:
Recommended Tools
kubectx
kubectx: Used to switch cluster access
kubens: Used to switch default namespace
kubectl-aliases
kubectl command aliases
Auto-completion
zsh
1
2
source <(kubectl completion zsh) # setup autocomplete in zsh into the current shell
echo "if [ $commands[kubectl] ]; then source <(kubectl completion zsh); fi" >> ~/.zshrc # add autocomplete permanently to your zsh shell
For other methods, see kubectl Cheat Sheet
Common kubectl Commands
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
kubectl api-resources --namespaced=false
kubectl api-resources --namespaced=true
# -R means recursively apply all configs in the directory
kubectl apply -R -f configs/
kubectl get cs
kubectl get svc --sort-by=.metadata.creationTimestamp
# View nodes
kubectl get no --sort-by=.metadata.creationTimestamp
kubectl drain <node-name>
kubectl taint nodes node1 key=value:NoSchedule
kubectl get ing pdd --n java
# Don't schedule
kubectl cluster-info dump
kubectl get po --field-selector spec.nodeName=xxxx
kubectl exec -it ng-57d74c8694-6cqnz sh -n=java
kubectl get pods --all-namespaces --field-selector spec.nodeName=<node> -o wide
kubectl get pods -o wide --all-namespaces
kubectl get po -l app=nginx -w
kubectl delete po -l app=onekey-ali-web -n=$(namespace)
kubectl get po --all-namespaces
# View abnormal pods
kubectl get po --all-namespaces --field-selector 'status.phase!=Running'
Cluster Management Related Commands
Application Management Related
1
2
3
4
5
6
7
8
9
10
11
kubectl top pod
kubectl delete deployment,services -l app=nginx
kubectl scale deployment/nginx-deployment --replicas=2
kubectl get svc --all-namespaces=true
kubectl rollout undo deployment/nginx-deployment --to-revision=2
kubectl autoscale deployment <deployment-name> --min=2 --max=5 --cpu-percent=80
# To watch ReplicaSet changes.
kubectl get rs -w
kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
# deployment "nginx-deployment" image updated
kubectl set image deploy monitorapi-deployment monitorapi=registry-vpc.cn-shenzhen.aliyuncs.com/amiba/monitorapi:1.2.4 -n=java
Force Delete
Sometimes there are problems when deleting pv/pvc. In this case, you need to add 2 command parameters --grace-period=0 --force
Delete All Failed Pods
1
2
3
4
5
6
kubectl get po --all-namespaces --field-selector 'status.phase==Failed'
kubectl delete po --field-selector 'status.phase==Failed'
# Fuzzy delete pods
key=
kgpo -n default | grep $key | awk '{print $1}' | xargs kubectl delete po -n1 -n default
Events
1
2
kubectl get events --field-selector involvedObject.kind=Service --sort-by='.metadata.creationTimestamp'
kubectl get event --all-namespaces --field-selector involvedObject.name=$po
Reference links:
推奨ツール
kubectx
kubectx:クラスターアクセスの切り替えに使用
kubens:デフォルトのnamespaceの切り替えに使用
kubectl-aliases
kubectlコマンドのエイリアス
自動補完
zsh
1
2
source <(kubectl completion zsh) # setup autocomplete in zsh into the current shell
echo "if [ $commands[kubectl] ]; then source <(kubectl completion zsh); fi" >> ~/.zshrc # add autocomplete permanently to your zsh shell
他の方法については、kubectl Cheat Sheetを参照してください。
kubectlのよく使うコマンド
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
kubectl api-resources --namespaced=false
kubectl api-resources --namespaced=true
# -Rはディレクトリ内のすべての設定を再帰的に適用することを意味します
kubectl apply -R -f configs/
kubectl get cs
kubectl get svc --sort-by=.metadata.creationTimestamp
# ノードを表示
kubectl get no --sort-by=.metadata.creationTimestamp
kubectl drain <node-name>
kubectl taint nodes node1 key=value:NoSchedule
kubectl get ing pdd --n java
# スケジュールしない
kubectl cluster-info dump
kubectl get po --field-selector spec.nodeName=xxxx
kubectl exec -it ng-57d74c8694-6cqnz sh -n=java
kubectl get pods --all-namespaces --field-selector spec.nodeName=<node> -o wide
kubectl get pods -o wide --all-namespaces
kubectl get po -l app=nginx -w
kubectl delete po -l app=onekey-ali-web -n=$(namespace)
kubectl get po --all-namespaces
# 異常なpodを表示
kubectl get po --all-namespaces --field-selector 'status.phase!=Running'
クラスター管理関連のコマンド
アプリケーション管理関連
1
2
3
4
5
6
7
8
9
10
11
kubectl top pod
kubectl delete deployment,services -l app=nginx
kubectl scale deployment/nginx-deployment --replicas=2
kubectl get svc --all-namespaces=true
kubectl rollout undo deployment/nginx-deployment --to-revision=2
kubectl autoscale deployment <deployment-name> --min=2 --max=5 --cpu-percent=80
# ReplicaSetの変化を監視するために。
kubectl get rs -w
kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
# deployment "nginx-deployment" image updated
kubectl set image deploy monitorapi-deployment monitorapi=registry-vpc.cn-shenzhen.aliyuncs.com/amiba/monitorapi:1.2.4 -n=java
強制削除
pv/pvcを削除する際に問題が発生することがあります。この場合、2つのコマンドパラメータ--grace-period=0 --force を追加する必要があります。
すべての失敗したPodを削除
1
2
3
4
5
6
kubectl get po --all-namespaces --field-selector 'status.phase==Failed'
kubectl delete po --field-selector 'status.phase==Failed'
# あいまいな削除pod
key=
kgpo -n default | grep $key | awk '{print $1}' | xargs kubectl delete po -n1 -n default
イベント
1
2
kubectl get events --field-selector involvedObject.kind=Service --sort-by='.metadata.creationTimestamp'
kubectl get event --all-namespaces --field-selector involvedObject.name=$po
参考リンク:
Рекомендуемые инструменты
kubectx
kubectx: Используется для переключения доступа к кластеру
kubens: Используется для переключения пространства имен по умолчанию
kubectl-aliases
Псевдонимы команд kubectl
Автодополнение
zsh
1
2
source <(kubectl completion zsh) # setup autocomplete in zsh into the current shell
echo "if [ $commands[kubectl] ]; then source <(kubectl completion zsh); fi" >> ~/.zshrc # add autocomplete permanently to your zsh shell
Для других методов см. kubectl Cheat Sheet
Общие команды kubectl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
kubectl api-resources --namespaced=false
kubectl api-resources --namespaced=true
# -R означает рекурсивно применить все конфигурации в каталоге
kubectl apply -R -f configs/
kubectl get cs
kubectl get svc --sort-by=.metadata.creationTimestamp
# Просмотр узлов
kubectl get no --sort-by=.metadata.creationTimestamp
kubectl drain <node-name>
kubectl taint nodes node1 key=value:NoSchedule
kubectl get ing pdd --n java
# Не планировать
kubectl cluster-info dump
kubectl get po --field-selector spec.nodeName=xxxx
kubectl exec -it ng-57d74c8694-6cqnz sh -n=java
kubectl get pods --all-namespaces --field-selector spec.nodeName=<node> -o wide
kubectl get pods -o wide --all-namespaces
kubectl get po -l app=nginx -w
kubectl delete po -l app=onekey-ali-web -n=$(namespace)
kubectl get po --all-namespaces
# Просмотр аномальных подов
kubectl get po --all-namespaces --field-selector 'status.phase!=Running'
Команды, связанные с управлением кластером
Связанные с управлением приложениями
1
2
3
4
5
6
7
8
9
10
11
kubectl top pod
kubectl delete deployment,services -l app=nginx
kubectl scale deployment/nginx-deployment --replicas=2
kubectl get svc --all-namespaces=true
kubectl rollout undo deployment/nginx-deployment --to-revision=2
kubectl autoscale deployment <deployment-name> --min=2 --max=5 --cpu-percent=80
# Для наблюдения за изменениями ReplicaSet.
kubectl get rs -w
kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
# deployment "nginx-deployment" image updated
kubectl set image deploy monitorapi-deployment monitorapi=registry-vpc.cn-shenzhen.aliyuncs.com/amiba/monitorapi:1.2.4 -n=java
Принудительное удаление
Иногда возникают проблемы при удалении pv/pvc. В этом случае нужно добавить 2 параметра команды --grace-period=0 --force
Удалить все неудачные поды
1
2
3
4
5
6
kubectl get po --all-namespaces --field-selector 'status.phase==Failed'
kubectl delete po --field-selector 'status.phase==Failed'
# Нечеткое удаление подов
key=
kgpo -n default | grep $key | awk '{print $1}' | xargs kubectl delete po -n1 -n default
События
1
2
kubectl get events --field-selector involvedObject.kind=Service --sort-by='.metadata.creationTimestamp'
kubectl get event --all-namespaces --field-selector involvedObject.name=$po
Ссылки: