Kubernetes会自动删除不再需要的资源。

huangapple go评论65阅读模式
英文:

Kubernetes automatically remove resources no longer required

问题

在Kubernetes中是否有相当于AWS CloudFormation的功能,可以只提供一个更新后的配置文件,然后Kubernetes会自动将其与先前版本进行比较,并删除不再需要的资源?

英文:

Using AWS CloudFormation, I can create a stack based on a template that includes all required resources. I can then create a new template, adding some resources, removing some, and changing description of others. I can then update the CloudFormation stack with the new template. CloudFormation will automatically remove any resources that are no longer in the template, add the new ones, and update modified resources. In addition, the update will roll back if any of the operations fails.

Is there an equivalent to this in Kubernetes, where I can just provide an updated configuration file, and have Kubernetes automatically compare that to the previous version and remove any resources that should no longer be there?

答案1

得分: 3

对于单一资源(例如单个 Pod 或 Deployment),Kubernetes 将自动协调状态。所以它在这个意义上的工作方式类似于 CloudFormation。如果您更改一个部署并从中移除一个 Pod,Kubernetes 将自动移除资源。

如果您想将多个资源视为单个对象,您可以考虑使用类似 Helm 的工具,它简化了将多个 Kubernetes 资源打包在一起的过程。

英文:

For single resources (e.g. a single Pod or Deployment) Kubernetes will automatically reconcile the state. So it works in a similar manner as CloudFormation in that sense. If you change a deployment and remove a pod from it, Kubernetes will automatically remove the resources.

If you want to treat multiple resources as a single object, you can look at something like Helm, which simplifies packaging multiple Kubernetes resources together.

答案2

得分: 0

使用部署模板将满足您的需求,可以随时回滚部署。

当使用正确的标志,如 "status/history/undo",结合 "rollout" 命令时,应该可以帮助您控制堆栈资源的滚动或回滚。

kubectl rollout status deployment nginx

检查滚动历史记录

kubectl rollout history deployment nginx

回滚到以前的版本

kubectl rollout undo deployment nginx

在下面的示例中,我使用 deployment_v1.yaml 文件创建了一个部署,其中有两个 pod,每个 pod 中有 2 个容器(nginx/redis)。

kubectl create -f deployment_v1.yaml --record=true

deployment_v1.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    run: multi-container-deploy
  name: multi-container-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: multi-container
  template:
    metadata:
      labels:
        app: multi-container
    spec:
      containers:
      - image: nginx
        name: nginx-1
      - image: redis
        name: redis-2

在滚动过程中检查状态

$ kubectl rollout status deployment multi-container-deploy
等待部署 "multi-container-deploy" 完成滚动: 1 个已更新的副本可用...
部署 "multi-container-deploy" 成功滚动完成

滚动历史记录

$ kubectl rollout history deployment multi-container-deploy
deployment.apps/multi-container-deploy
REVISION  CHANGE-CAUSE
1         kubectl create --filename=deployment_v1.yaml --record=true
$ kubectl get all
NAME                                          READY   STATUS    RESTARTS   AGE
pod/multi-container-deploy-5fc8944c58-r4dt4   2/2     Running   0          60s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   32d

NAME                                     READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/multi-container-deploy   1/1     1            1           60s

NAME                                                DESIRED   CURRENT   READY   AGE
replicaset.apps/multi-container-deploy-5fc8944c58   1         1         1       60s

现在,假设我们通过 kubectl edit 命令从原始部署中删除了 redis pod。

kubectl edit deployments multi-container-deploy

在编辑后,检查新的滚动状态如下

$ kubectl rollout status deployment multi-container-deploy
等待部署 "multi-container-deploy" 完成滚动: 1 个旧副本正在等待终止...
等待部署 "multi-container-deploy" 完成滚动: 1 个旧副本正在等待终止...
部署 "multi-container-deploy" 成功滚动完成

检查新的滚动历史记录,我们会看到以下更新的列表(直接编辑的缺点是我们不会有关于第2步操作的详细信息)。

$ kubectl rollout history deployment multi-container-deploy
deployment.apps/multi-container-deploy
REVISION  CHANGE-CAUSE
1         kubectl apply --filename=deployment_v1.yaml --record=true
2         kubectl apply --filename=deployment_v1.yaml --record=true

我们还可以检查资源是否成功移除,现在只有一个包含一个容器的 pod 运行。

$ kubectl get all
NAME                                         READY   STATUS    RESTARTS   AGE
pod/multi-container-deploy-7cdb9cbf4-jr9nc   1/1     Running   0          4m36s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   32d

NAME                                     READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/multi-container-deploy   1/1     1            1           13m

NAME                                                DESIRED   CURRENT   READY   AGE
replicaset.apps/multi-container-deploy-5fc8944c58   0         0         0       13m
replicaset.apps/multi-container-deploy-7cdb9cbf4    1         1         1       4m36s

我们可以通过运行以下命令撤消上述编辑部署的更改

$ kubectl rollout undo deployment multi-container-deploy
deployment.apps/multi-container-deploy 已回滚

如果我们再次检查,我们将看到 pod 重新运行,再次包含两个容器。

$ kubectl get all
NAME                                          READY   STATUS    RESTARTS   AGE
pod/multi-container-deploy-5fc8944c58-xn4mz   2/2     Running   0          40s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   32d

NAME                                     READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/multi-container-deploy   1/1     1            1           15m

NAME                                                DESIRED   CURRENT   READY   AGE
replicaset.apps/multi-container-deploy-5fc8944c58   1         1         1       15m
replicaset.apps/multi-container-deploy-7cdb9cbf4    0         0         0       6m59s

并且滚动历史记录将更新如下

$ kubectl rollout history deployment multi-container-deploy
deployment.apps/multi-container-deploy
REVISION  CHANGE-CAUSE
2         kubectl apply --filename=deployment_v2.yaml --record=true
3         kubectl apply --filename=deployment_v2.yaml --record=true
英文:

Using deployment template will suffice your need, a deployment can be rollback at any time needed.

Rollout command when used with correct flags like "status/history/undo" should help you control the stack resource rollout or rollback..

kubectl rollout status deployment nginx

Check rollout History

kubectl rollout history deployment nginx

Rolling Back to a Previous Revision

kubectl rollout undo deployment nginx

In below example i created a deployment with two pods using deployment_v1.yaml file which has 2 containers inside a pod (nginx/redis)

kubectl create -f deployment_v1.yaml --record=true

deployment_v1.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    run: multi-container-deploy
  name: multi-container-deploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: multi-container
  template:
    metadata:
      labels:
        app: multi-container
    spec:
      containers:
      - image: nginx
        name: nginx-1
      - image: redis
        name: redis-2

Checking Status during rollout

$ kubectl rollout status deployment multi-container-deploy
Waiting for deployment &quot;multi-container-deploy&quot; rollout to finish: 0 of 1 updated replicas are available...
deployment &quot;multi-container-deploy&quot; successfully rolled out

Rollout history

$ kubectl rollout history deployment multi-container-deploy
deployment.apps/multi-container-deploy
REVISION  CHANGE-CAUSE
1         kubectl create --filename=deployment_v1.yaml --record=true
$ kubectl get all
NAME                                          READY   STATUS    RESTARTS   AGE
pod/multi-container-deploy-5fc8944c58-r4dt4   2/2     Running   0          60s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    &lt;none&gt;        443/TCP   32d

NAME                                     READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/multi-container-deploy   1/1     1            1           60s

NAME                                                DESIRED   CURRENT   READY   AGE
replicaset.apps/multi-container-deploy-5fc8944c58   1         1         1       60s

Now say we remove the redis pod from the original deployment by say kubectl edit command

kubectl edit deployments multi-container-deploy

Check new rollout status after edit as below

$ kubectl rollout status deployment multi-container-deploy
Waiting for deployment &quot;multi-container-deploy&quot; rollout to finish: 1 old replicas are pending termination...
Waiting for deployment &quot;multi-container-deploy&quot; rollout to finish: 1 old replicas are pending termination...
deployment &quot;multi-container-deploy&quot; successfully rolled out

Check new rollout history and we will see list updated as below (disadvantage of direct edit we will not have much info on what was done on step2)

$ kubectl rollout history deployment multi-container-deploy
deployment.apps/multi-container-deploy
REVISION  CHANGE-CAUSE
1         kubectl apply --filename=deployment_v1.yaml --record=true
2         kubectl apply --filename=deployment_v1.yaml --record=true

We can also check that the resource was successful removed and we only have pod running with one container.

$ kubectl get all
NAME                                         READY   STATUS    RESTARTS   AGE
pod/multi-container-deploy-7cdb9cbf4-jr9nc   1/1     Running   0          4m36s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    &lt;none&gt;        443/TCP   32d

NAME                                     READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/multi-container-deploy   1/1     1            1           13m

NAME                                                DESIRED   CURRENT   READY   AGE
replicaset.apps/multi-container-deploy-5fc8944c58   0         0         0       13m
replicaset.apps/multi-container-deploy-7cdb9cbf4    1         1         1       4m36s

We can Undo above edit on deployment just by running below command

$ kubectl rollout undo deployment multi-container-deploy
deployment.apps/multi-container-deploy rolled back

If we check back we have the pod running back with two containers again.

$ kubectl get all
NAME                                          READY   STATUS    RESTARTS   AGE
pod/multi-container-deploy-5fc8944c58-xn4mz   2/2     Running   0          40s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    &lt;none&gt;        443/TCP   32d

NAME                                     READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/multi-container-deploy   1/1     1            1           15m

NAME                                                DESIRED   CURRENT   READY   AGE
replicaset.apps/multi-container-deploy-5fc8944c58   1         1         1       15m
replicaset.apps/multi-container-deploy-7cdb9cbf4    0         0         0       6m59s

And rollout history will be updated as below

$ kubectl rollout history deployment multi-container-deploy
deployment.apps/multi-container-deploy
REVISION  CHANGE-CAUSE
2         kubectl apply --filename=deployment_v2.yaml --record=true
3         kubectl apply --filename=deployment_v2.yaml --record=true

huangapple
  • 本文由 发表于 2020年1月6日 23:11:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定