学习资源
Deployment 配置要点
1 2 3 4 5 6 7 8 9 10 11 12
| spec: selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec: containers: - name: app
|
常用命令速查
资源清理
1 2 3 4 5 6 7
| kubectl api-resources --verbs=list --namespaced -o name | \ xargs -n 1 kubectl get --show-kind --ignore-not-found -n <ns>
kubectl get ingress -n <ns> | grep <name> | awk '{print $1}' | \ xargs kubectl delete ingress -n <ns>
|
资源监控
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| kubectl describe node | \ grep -E '((Name|Roles):\s{6,})|(\s+(memory|cpu)\s+[0-9]+\w{0,2}.+%\))'
NAME_SPACE=default NODE=node1 kubectl get pods -n $NAME_SPACE -o wide | \ awk '{if(NF>9){print $1,$9}else{print $1,$7}}' | \ grep $NODE | while read name node; do echo -n "$name $node " kubectl top pod $name -n $NAME_SPACE | grep -v NAME | \ tail -1 | awk '{print $2,$3,$5}' done | sort -nrk 3
|
容器操作
1 2 3 4 5 6 7 8
| kubectl exec <pod> -c <container> -n <ns> -- <command>
kubectl get pod <pod> -n <ns> -o jsonpath={.spec.containers[*].name}
kubectl get pod <pod> -n <ns> -o yaml | kubectl replace --force -f -
|
标签管理
1 2 3 4 5
| kubectl describe node <node> | grep Labels
kubectl label nodes <node> <label-key>-
|
replace vs apply
1 2 3 4 5
| kubectl replace -f config.yaml
kubectl apply -f config.yaml
|
Pod 重调度机制
默认配置
当节点故障时,Pod需要5分钟以上才能重新调度,原因:
kubelet 配置:
node-status-update-frequency: 10s(状态上报频率)
controller-manager 配置:
node-monitor-period: 5s(状态同步周期)node-monitor-grace-period: 40s(认定不健康时间)pod-eviction-timeout: 5m0s(驱逐超时)
计算: 40s(grace) + 5m(eviction) ≈ 5分40秒
优化方案
调整容忍时间(v1.13+):
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| apiVersion: v1 kind: Pod metadata: name: myapp spec: tolerations: - key: "node.kubernetes.io/not-ready" operator: "Exists" effect: "NoExecute" tolerationSeconds: 30 - key: "node.kubernetes.io/unreachable" operator: "Exists" effect: "NoExecute" tolerationSeconds: 30
|
参考: K3s示例配置
更多资料: