在 kubectl apply -f 中删除不存在的注解

Remove unexisting annotations in kubectl apply -f

当使用 kubectl -f apply 对 kubernetes 资源应用更新时,在应用的配置中,我删除了部署资源中当前存在的一些注释,这些注释没有被删除(但现有注释中的更改正在正确更新)。如何强制删除更新过程中删除的注释?

顺便说一句,我想避免删除并重新创建资源

如@Matt 所述

Did you use kubectl apply to create this data on the resource? apply stores previous state in an annotation. If that annotation doesn't exist then it can't detect what data to delete

更多信息here


您可以使用kubectl edit删除那些注释。

Edit a resource from the default editor.

The edit command allows you to directly edit any API resource you can retrieve via the command line tools. It will open the editor defined by your KUBE_EDITOR, or EDITOR environment variables, or fall back to 'vi' for Linux or 'notepad' for Windows. You can edit multiple objects, although changes are applied one at a time. The command accepts filenames as well as command line arguments, although the files you point to must be previously saved versions of resources.

Editing is done with the API version used to fetch the resource. To edit using a specific API version, fully-qualify the resource, version, and group.

The default format is YAML. To edit in JSON, specify "-o json".

The flag --windows-line-endings can be used to force Windows line endings, otherwise the default for your operating system will be used.

In the event an error occurs while updating, a temporary file will be created on disk that contains your unapplied changes. The most common error when updating a resource is another editor changing the resource on the server. When this occurs, you will have to apply your changes to the newer version of the resource, or update your temporary saved copy to include the latest resource version.


我用 nginx pod 和一些注解做了一个例子

apiVersion: v1
kind: Pod
metadata:
  name: annotations-demo
  annotations:
    delete: without-restart
spec:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80

我用过

  • kubectl describe 查看注解是否添加。

    Annotations: delete: without-restart

  • kubectl edit删除这个注释,现在是空的

    Annotations: