kubernetes Kubectl错误:对象已被修改;请将您的更改应用到最新版本,然后重试

hrysbysz  于 2023-08-03  发布在  Kubernetes
关注(0)|答案(7)|浏览(71)

我在尝试应用补丁时得到以下错误:

core@dgoutam22-1-coreos-5760 ~ $ kubectl apply -f ads-central-configuration.yaml
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
Error from server (Conflict): error when applying patch:
{"data":{"default":"{\"dedicated_redis_cluster\": {\"nodes\": [{\"host\": \"192.168.1.94\", \"port\": 6379}]}}"},"metadata":{"annotations":{"kubectl.kubernetes.io/last-applied-configuration":"{\"apiVersion\":\"v1\",\"data\":{\"default\":\"{\\\"dedicated_redis_cluster\\\": {\\\"nodes\\\": [{\\\"host\\\": \\\"192.168.1.94\\\", \\\"port\\\": 6379}]}}\"},\"kind\":\"ConfigMap\",\"metadata\":{\"annotations\":{},\"creationTimestamp\":\"2018-06-27T07:19:13Z\",\"labels\":{\"acp-app\":\"acp-discovery-service\",\"version\":\"1\"},\"name\":\"ads-central-configuration\",\"namespace\":\"acp-system\",\"resourceVersion\":\"1109832\",\"selfLink\":\"/api/v1/namespaces/acp-system/configmaps/ads-central-configuration\",\"uid\":\"64901676-79da-11e8-bd65-fa163eaa7a28\"}}\n"},"creationTimestamp":"2018-06-27T07:19:13Z","resourceVersion":"1109832","uid":"64901676-79da-11e8-bd65-fa163eaa7a28"}}
to:
&{0xc4200bb380 0xc420356230 acp-system ads-central-configuration ads-central-configuration.yaml 0xc42000c970 4434 false}
**for: "ads-central-configuration.yaml": Operation cannot be fulfilled on configmaps "ads-central-configuration": the object has been modified; please apply your changes to the latest version and try again**
core@dgoutam22-1-coreos-5760 ~ $

字符串

uoifb46i

uoifb46i1#

您的yaml配置似乎是从生成的内容复制粘贴的,因此包含了creationTimestamp(以及resourceVersionselfLinkuid)等字段,这些字段不属于声明性配置文件。
把你的番薯清理干净。删除特定于示例的内容。你的最终yaml应该足够简单,你可以很容易地理解它。

xt0899hw

xt0899hw2#

从文件中删除以下行:

creationTimestamp:   
  resourceVersion:  
  selfLink:   
  uid:

字符串
然后尝试再次申请。

mctunoxg

mctunoxg3#

给予把最后一个resourceVersion放在更新中,你可以让它运行:

kubectl get deployment <DEPLOYMENT-NAME> -o yaml | grep resourceVersion

字符串

6gpjuf90

6gpjuf904#

您可能已编辑了相同的导出部署文件..
1 -尝试使用以下命令重新导出:

kubectl get deployment <DEPLOYMENT-NAME> -o yaml > deployment-file.yaml

字符串
2 -在“deployment-file.yaml”中进行必要的修改
3 -应用更改:

kubectl apply -f deployment-file.yaml

或:

您可能希望直接编辑展开。用途:
第一个月
如果您不熟悉VI编辑器,请更改默认编辑器:export EDITOR=nano

hec6srdp

hec6srdp5#

我能够在我的测试环境中重现该问题。复制步骤:
1.从Kubernetes Engine > Workloads > Deploy创建部署
1.输入应用程序名称、命名空间、标签
1.选择群集或创建新群集
您可以在这里查看YAML文件,这里是示例:

---
apiVersion: "apps/v1"
kind: "Deployment"
metadata:
  name: "nginx-1"
  namespace: "default"
  labels:
    app: "nginx-1"
spec:
  replicas: 3
  selector:
    matchLabels:
      app: "nginx-1"
  template:
    metadata:
      labels:
        app: "nginx-1"
    spec:
      containers:
      - name: "nginx"
        image: "nginx:latest"
---
apiVersion: "autoscaling/v2beta1"
kind: "HorizontalPodAutoscaler"
metadata:
  name: "nginx-1-hpa"
  namespace: "default"
  labels:
    app: "nginx-1"
spec:
  scaleTargetRef:
    kind: "Deployment"
    name: "nginx-1"
    apiVersion: "apps/v1"
  minReplicas: 1
  maxReplicas: 5
  metrics:
  - type: "Resource"
    resource:
      name: "cpu"
      targetAverageUtilization: 80

字符串
部署后,如果您转到Kubernetes Engine > Workloads > nginx-1(单击它)
a.)您将获得部署详细信息(概述、详细信息、修订历史记录、事件、YAML)
B.)单击YAML并从YAML选项卡复制内容
c.)创建新的YAML文件并粘贴内容并保存文件
d.)现在如果你运行命令$kubectl apply -f newyamlfile.yaml,它会显示下面的错误:

Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
Error from server (Conflict): error when applying patch:
{"metadata":{"annotations":{"kubectl.kubernetes.io/last-applied-configuration":"{\"apiVersion\":\"apps/v1\",\"kind\":\"Deployment\",\"metadata\":{\"annotations\":{\"deployment.kubernetes.io/revision\":\"1\"},\"creationTimestamp\":\"2019-09-17T21:34:39Z\",\"generation\":1,\"labels\":{\"app\":\"nginx-1\"},\"name\":\"nginx-1\",\"namespace\":\"default\",\"resourceVersion\":\"218884\",\"selfLink\":\"/apis/apps/v1/namespaces/default/deployments/nginx-1\",\"uid\":\"f41c5b6f-d992-11e9-9adc-42010a80023b\"},\"spec\":{\"progressDeadlineSeconds\":600,\"replicas\":3,\"revisionHistoryLimit\":10,\"selector\":{\"matchLabels\":{\"app\":\"nginx-1\"}},\"strategy\":{\"rollingUpdate\":{\"maxSurge\":\"25%\",\"maxUnavailable\":\"25%\"},\"type\":\"RollingUpdate\"},\"template\":{\"metadata\":{\"creationTimestamp\":null,\"labels\":{\"app\":\"nginx-1\"}},\"spec\":{\"containers\":[{\"image\":\"nginx:latest\",\"imagePullPolicy\":\"Always\",\"name\":\"nginx\",\"resources\":{},\"terminationMessagePath\":\"/dev/termination-log\",\"terminationMessagePolicy\":\"File\"}],\"dnsPolicy\":\"ClusterFirst\",\"restartPolicy\":\"Always\",\"schedulerName\":\"default-scheduler\",\"securityContext\":{},\"terminationGracePeriodSeconds\":30}}},\"status\":{\"availableReplicas\":3,\"conditions\":[{\"lastTransitionTime\":\"2019-09-17T21:34:47Z\",\"lastUpdateTime\":\"2019-09-17T21:34:47Z\",\"message\":\"Deployment has minimum availability.\",\"reason\":\"MinimumReplicasAvailable\",\"status\":\"True\",\"type\":\"Available\"},{\"lastTransitionTime\":\"2019-09-17T21:34:39Z\",\"lastUpdateTime\":\"2019-09-17T21:34:47Z\",\"message\":\"ReplicaSet \\\"nginx-1-7b4bb7fbf8\\\" has successfully progressed.\",\"reason\":\"NewReplicaSetAvailable\",\"status\":\"True\",\"type\":\"Progressing\"}],\"observedGeneration\":1,\"readyReplicas\":3,\"replicas\":3,\"updatedReplicas\":3}}\n"},"generation":1,"resourceVersion":"218884"},"spec":{"replicas":3},"status":{"availableReplicas":3,"observedGeneration":1,"readyReplicas":3,"replicas":3,"updatedReplicas":3}}
to:
Resource: "apps/v1, Resource=deployments", GroupVersionKind: "apps/v1, Kind=Deployment"
Name: "nginx-1", Namespace: "default"
Object: &{map["apiVersion":"apps/v1" "metadata":map["name":"nginx-1" "namespace":"default" "selfLink":"/apis/apps/v1/namespaces/default/deployments/nginx-1" "uid":"f41c5b6f-d992-11e9-9adc-42010a80023b" "generation":'\x02' "labels":map["app":"nginx-1"] "annotations":map["deployment.kubernetes.io/revision":"1"] "resourceVersion":"219951" "creationTimestamp":"2019-09-17T21:34:39Z"] "spec":map["replicas":'\x01' "selector":map["matchLabels":map["app":"nginx-1"]] "template":map["metadata":map["labels":map["app":"nginx-1"] "creationTimestamp":<nil>] "spec":map["containers":[map["imagePullPolicy":"Always" "name":"nginx" "image":"nginx:latest" "resources":map[] "terminationMessagePath":"/dev/termination-log" "terminationMessagePolicy":"File"]] "restartPolicy":"Always" "terminationGracePeriodSeconds":'\x1e' "dnsPolicy":"ClusterFirst" "securityContext":map[] "schedulerName":"default-scheduler"]] "strategy":map["type":"RollingUpdate" "rollingUpdate":map["maxUnavailable":"25%" "maxSurge":"25%"]] "revisionHistoryLimit":'\n' "progressDeadlineSeconds":'\u0258'] "status":map["observedGeneration":'\x02' "replicas":'\x01' "updatedReplicas":'\x01' "readyReplicas":'\x01' "availableReplicas":'\x01' "conditions":[map["message":"Deployment has minimum availability." "type":"Available" "status":"True" "lastUpdateTime":"2019-09-17T21:34:47Z" "lastTransitionTime":"2019-09-17T21:34:47Z" "reason":"MinimumReplicasAvailable"] map["lastTransitionTime":"2019-09-17T21:34:39Z" "reason":"NewReplicaSetAvailable" "message":"ReplicaSet \"nginx-1-7b4bb7fbf8\" has successfully progressed." "type":"Progressing" "status":"True" "lastUpdateTime":"2019-09-17T21:34:47Z"]]] "kind":"Deployment"]}
for: "test.yaml": Operation cannot be fulfilled on deployments.apps "nginx-1": the object has been modified; please apply your changes to the latest version and try again


要解决这个问题,你需要找到确切的yaml文件,然后根据你的要求编辑它,然后你可以运行$kubectl apply -f nginx-1.yaml
希望这些信息能找到你。

7gs2gvoe

7gs2gvoe6#

这个错误是因为deployment.yaml有一个resourceVersion的条目。删除它,因为它是不需要的,您将能够应用新的配置。

liwlm1x9

liwlm1x97#

我在尝试应用新的Kubernetes secret来覆盖旧Kubernetes集群的现有secret时遇到了这个错误。
当我应用新的Kubernetes秘密时,我得到了下面的警告:
警告:resource secrets/myapp-tls缺少kubectl.kubernetes.io/last-applied-configurationkubectl apply所需的www.example.com注解。kubectl apply只能用于kubectl create --save-config或kubectl apply以声明方式创建的资源。将自动修补缺少的注解。secret/myapp-tls已配置
当我检查Kubernetes集群时,我看到了错误:
更新端点失败无法在已修改对象的端点上完成操作;请将您的更改应用到最新版本,然后重试

这就是我解决的方法

显然,问题是因为秘密文件包含了creationTimestamp(以及resourceVersionselfLinkuid)等字段,这些字段不属于声明性配置文件,就像上面提到的Roman's answer一样。
为了快速修复,我简单地删除了现有的/旧的Kubernetes秘密,并重新应用了新的秘密。这一次它工作得很好。

相关问题