|NO.Z.00019|——————————|^^ 标准 ^^|——|KuberNetes&标准.V18|——|常用操作.V04|更新资源|删除资源|
一、更新资源
### --- 更新资源
[root@k8s-master01 ~]# kubectl set image deployment/frontend www=image:v2 // 滚动更新 "frontend" Deployment 的 "www" 容器镜像
[root@k8s-master01 ~]# kubectl rollout history deployment/frontend // 检查 Deployment 的历史记录,包括版本
[root@k8s-master01 ~]# kubectl rollout undo deployment/frontend // 回滚到上次部署版本
[root@k8s-master01 ~]# kubectl rollout undo deployment/frontend --to-revision=2 // 回滚到特定部署版本
[root@k8s-master01 ~]# kubectl rollout status -w deployment/frontend // 监视 "frontend" Deployment 的滚动升级状态直到完成
[root@k8s-master01 ~]# kubectl rollout restart deployment/frontend // 轮替重启 "frontend" Deployment
[root@k8s-master01 ~]# cat pod.json | kubectl replace -f - // 通过传入到标准输入的 JSON 来替换 Pod
~~~ # 强制替换,删除后重建资源。会导致服务不可用。
[root@k8s-master01 ~]# kubectl replace --force -f ./pod.json
~~~ # 为多副本的 nginx 创建服务,使用 80 端口提供服务,连接到容器的 8000 端口。
[root@k8s-master01 ~]# kubectl expose rc nginx --port=80 --target-port=8000
~~~ # 将某单容器 Pod 的镜像版本(标签)更新到 v4
[root@k8s-master01 ~]# kubectl get pod mypod -o yaml | sed 's/\(image: myimage\):.*$/\1:v4/' | kubectl replace -f -
[root@k8s-master01 ~]# kubectl label pods my-pod new-label=awesome // 添加标签
[root@k8s-master01 ~]# kubectl annotate pods my-pod icon-url=http://goo.gl/XXBTWq // 添加注解
[root@k8s-master01 ~]# kubectl autoscale deployment foo --min=2 --max=10 // 对 "foo" Deployment 自动伸缩容
二、部分更新资源
### --- 部分更新资源
~~~ # 部分更新某节点
[root@k8s-master01 ~]# kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'
~~~ # 更新容器的镜像;spec.containers[*].name 是必须的。因为它是一个合并性质的主键。
[root@k8s-master01 ~]# kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
~~~ # 使用带位置数组的 JSON patch 更新容器的镜像
[root@k8s-master01 ~]# kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'
~~~ # 使用带位置数组的 JSON patch 禁用某 Deployment 的 livenessProbe
[root@k8s-master01 ~]# kubectl patch deployment valid-deployment --type json -p='[{"op": "remove", "path": "/spec/template/spec/containers/0/livenessProbe"}]'
~~~ # 在带位置数组中添加元素
[root@k8s-master01 ~]# kubectl patch sa default --type='json' -p='[{"op": "add", "path": "/secrets/1", "value": {"name": "whatever" } }]'
三、删除资源
### --- 删除资源
[root@k8s-master01 ~]# kubectl delete -f ./pod.json // 删除在 pod.json 中指定的类型和名称的 Pod
[root@k8s-master01 ~]# kubectl delete pod,service baz foo // 删除名称为 "baz" 和 "foo" 的 Pod 和服务
[root@k8s-master01 ~]# kubectl delete pods,services -l name=myLabel // 删除包含 name=myLabel 标签的 pods 和服务
[root@k8s-master01 ~]# kubectl delete pods,services -l name=myLabel --include-uninitialized // 删除包含 label name=myLabel 标签的 Pods 和服务
[root@k8s-master01 ~]# kubectl -n my-ns delete po,svc --all // 删除在 my-ns 名字空间中全部的 Pods 和服务
### --- 删除所有与 pattern1 或 pattern2 awk 模式匹配的 Pods
[root@k8s-master01 ~]# kubectl get pods -n mynamespace --no-headers=true | awk '/pattern1|pattern2/{print $1}' | xargs kubectl delete -n mynamespace pod
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
浙公网安备 33010602011771号