kubectl scale 使用最新图像删除 pods 并保留旧图像
kubectl scale removing pods with latest image and keeping old ones
所以我有一个部署工作 运行s 一些 kubectl 命令。
一个配置文件修改为使用最新的docker镜像SHA,我运行这些命令将它部署出来:
kubectl apply -f myconfig-file.yaml
#we have to scale up temporarily due to reasons beyond the purview of this question
kubectl scale -f myconfig-file.yaml --replicas=4
#* wait a minute or so *
sleep 60
kubectl scale -f myconfig-file.yaml --replicas=2
Apply 正确更新了 Google Cloud 上的 replicationcontroller 定义,指向最新的镜像,但原始容器仍然存在。放大确实会创建具有正确图像的容器,但是一旦我缩小,它就会删除最新的容器,留下带有旧图像的旧容器。
我已经确认:
- 带有新图像的新容器按预期工作。
- 我最终手动进行了部署并手动删除了旧容器(并且 k8s 使用最新图像正确地创建了两个新容器)并且当我缩小规模时,带有新图像的新容器仍然存在。我的应用程序按预期运行。
我的 yaml 文件有问题:
apiVersion: v1
kind: ReplicationController
metadata:
name: my-app-cluster
spec:
replicas: 2
template:
metadata:
labels:
run: my-app-cluster
spec:
containers:
- image: mySuperCoolImage@sha256:TheLatestShaHere
imagePullPolicy: Always
name: my-app-cluster
ports:
- containerPort: 8008
protocol: TCP
terminationGracePeriodSeconds: 30
我正在使用 Google Cloud K8s FWIW。我需要在 YAML 文件中做些什么来指示 k8s 销毁旧实例吗?
当您使用新的映像标签更新部署定义时,您不需要缩放 up/down。部署应该开始新 pods 并自行删除 pods 和旧图像,使用缩放只会使过程复杂化,而且正如您所注意到的,它在决定哪个 pods 删除。
所以看起来我的大部分问题都源于我使用的是 ReplicationController
而不是更受支持的 Deployment
或 ReplicaSet
。不幸的是,此时我需要与我的团队协商迁移到该格式的最佳方式,因为我们有一些考虑因素。
与此同时,我用这个小技巧解决了这个问题。
oldPods=($(kubectl get pods | grep myPodType | perl -wnE'say /.*-myPodType-\w*/g'))
#do the apply and scale thing like listed above
#then delete
for item in ${oldPods[@]}; do kubectl delete pod $item; done
所以我有一个部署工作 运行s 一些 kubectl 命令。 一个配置文件修改为使用最新的docker镜像SHA,我运行这些命令将它部署出来:
kubectl apply -f myconfig-file.yaml
#we have to scale up temporarily due to reasons beyond the purview of this question
kubectl scale -f myconfig-file.yaml --replicas=4
#* wait a minute or so *
sleep 60
kubectl scale -f myconfig-file.yaml --replicas=2
Apply 正确更新了 Google Cloud 上的 replicationcontroller 定义,指向最新的镜像,但原始容器仍然存在。放大确实会创建具有正确图像的容器,但是一旦我缩小,它就会删除最新的容器,留下带有旧图像的旧容器。
我已经确认:
- 带有新图像的新容器按预期工作。
- 我最终手动进行了部署并手动删除了旧容器(并且 k8s 使用最新图像正确地创建了两个新容器)并且当我缩小规模时,带有新图像的新容器仍然存在。我的应用程序按预期运行。
我的 yaml 文件有问题:
apiVersion: v1
kind: ReplicationController
metadata:
name: my-app-cluster
spec:
replicas: 2
template:
metadata:
labels:
run: my-app-cluster
spec:
containers:
- image: mySuperCoolImage@sha256:TheLatestShaHere
imagePullPolicy: Always
name: my-app-cluster
ports:
- containerPort: 8008
protocol: TCP
terminationGracePeriodSeconds: 30
我正在使用 Google Cloud K8s FWIW。我需要在 YAML 文件中做些什么来指示 k8s 销毁旧实例吗?
当您使用新的映像标签更新部署定义时,您不需要缩放 up/down。部署应该开始新 pods 并自行删除 pods 和旧图像,使用缩放只会使过程复杂化,而且正如您所注意到的,它在决定哪个 pods 删除。
所以看起来我的大部分问题都源于我使用的是 ReplicationController
而不是更受支持的 Deployment
或 ReplicaSet
。不幸的是,此时我需要与我的团队协商迁移到该格式的最佳方式,因为我们有一些考虑因素。
与此同时,我用这个小技巧解决了这个问题。
oldPods=($(kubectl get pods | grep myPodType | perl -wnE'say /.*-myPodType-\w*/g'))
#do the apply and scale thing like listed above
#then delete
for item in ${oldPods[@]}; do kubectl delete pod $item; done