K8s 常用操作备忘
# 查看pod日志
kubectl logs -f --tail 100 admin-center-server-cbd975547-fjlhs -n prod
# 查看pod重启前的日志
kubectl logs -f -p --tail 100 admin-center-server-cbd975547-fjlhs -n prod
# 进入pod中
kubectl exec -it -nnginx cfhy-prod-front-7d4d58fd4d-pj9lg -- bash
# 复制pod内的文件到本地
kubectl -n prod cp pay-center-server-75f46cf465-5cfkt:/usr/software/service/debug_tools/dump.hprof ./dump.hprof
#操作deployment 更新
kubectl rollout pause deployment gateway-server -n prod
kubectl rollout resume deployment gateway-server -n prod
kubectl rollout pause deployment app-gateway -n prod
kubectl rollout resume deployment app-gateway -n prod
# 列出默认命名空间下的所有Deployment
kubectl get deploy
# 通过定义文件更新Deployment
kubectl apply -f test.yaml
# 监控"test"这个Deployment的状态更新
kubectl rollout status deploy/test
# 暂停"test"这个Deployment的更新流程:
kubectl rollout pause deploy/test
# 恢复"test"这个Deployment的更新流程:
kubectl rollout resume deploy/test
# 查看"test"这个Deployment的更新历史:
kubectl rollout history deploy/test
# 回退test最近的更新
kubectl rollout undo deploy/test
# 把test这个Deployment回滚到指定版本
kubectl rollout undo deploy/test --to-revision=1
# 监控服务启动情况
watch "kubectl get pod -nprod | grep -E 'order-center|contract|mess|supp|user|har|risk|hz|stat|insu|admin-center'"
# 监控服务启动情况并监控svc endpoints 变化
watch -d -n 1 "date;kubectl get pod -n prod | grep admin-center-server; echo '=======> service ======> ' ;kubectl describe svc admin-center-server -n prod|grep Endpoints:"
# 查看pod的docker镜像
kubectl get pod cfhy-test-nginx-787d9785fb-22fj2 -n nginx -o jsonpath='{.spec.containers[*].image}'
# k8s 创建文件secret
kubectl -ndev create secret generic bigdata-model-ml --from-file=./model_xgb.pmml
kubectl-ndev create secret generic pabank-config-secret-20230718 --from-file=./config
kubectl -ndev create secret generic pabank-config-secret-20230718 --from-file=./config
kubectl -ndev create secret generic pabank-config-secret-20230718-1 --from-file=./config.properties
kubectl -ndev create secret generic pabank-config-secret-20230904 --from-file=./2000909332.pfx
kubectl -ndev create secret generic bigdata-model-ml --from-file=./model_xgb.pmml
# 编辑secret
kubectl edit secret -nprod 2024-01-15-secret
kubectl edit secret -nprod project-secret
# kubectl 更换镜像
kubectl set image deployment/nginx busybox=busybox:3.2 nginx=nginx:1.19.1
# 集群中运行着的所有镜像
kubectl get pods -A -o=custom-columns='DATA:spec.containers[*].image'
# 列举 default 名字空间中运行的所有镜像,按 Pod 分组
kubectl get pods --namespace default --output=custom-columns="NAME:.metadata.name,IMAGE:.spec.containers[*].image"
# 查看deployment的镜像
kubectl get deployment my-deployment -o=jsonpath='{.spec.template.spec.containers[*].image}'
# 除 "registry.k8s.io/coredns:1.6.2" 之外的所有镜像
kubectl get pods -A -o=custom-columns='DATA:spec.containers[?(@.image!="registry.k8s.io/coredns:1.6.2")].image'
# 输出 metadata 下面的所有字段,无论 Pod 名字为何
kubectl get pods -A -o=custom-columns='DATA:metadata.*'
# 标记 my-node 节点为不可调度
kubectl cordon my-node
# 对 my-node 节点进行清空操作,为节点维护做准备
kubectl drain my-node
# 标记 my-node 节点为可以调度
kubectl uncordon my-node
# 显示给定节点的度量值
kubectl top node my-node
# 显示主控节点和服务的地址
kubectl cluster-info
# 将当前集群状态转储到标准输出
kubectl cluster-info dump
# 将当前集群状态输出到 /path/to/cluster-state
kubectl cluster-info dump --output-directory=/path/to/cluster-state
# 查看当前节点上存在的现有污点
kubectl get nodes -o='custom-columns=NodeName:.metadata.name,TaintKey:.spec.taints[*].key,TaintValue:.spec.taints[*].value,TaintEffect:.spec.taints[*].effect'
# 如果已存在具有指定键和效果的污点,则替换其值为指定值
kubectl taint nodes foo dedicated=special-user:NoSchedule
# 部分更新某节点
kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'
# 更新容器的镜像;spec.containers[*].name 是必需的。因为它是一个合并性质的主键。
kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
# 使用带位置数组的 JSON patch 更新容器的镜像
kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'
# 使用带位置数组的 JSON patch 禁用某 Deployment 的 livenessProbe
kubectl patch deployment valid-deployment --type json -p='[{"op": "remove", "path": "/spec/template/spec/containers/0/livenessProbe"}]'
# 在带位置数组中添加元素
kubectl patch sa default --type='json' -p='[{"op": "add", "path": "/secrets/1", "value": {"name": "whatever" } }]'
# 通过修正 scale 子资源来更新 Deployment 的副本数
kubectl patch deployment nginx-deployment --subresource='scale' --type='merge' -p '{"spec":{"replicas":2}}'