如何使用kubectl编辑资源配置?

Chr*_*ski 3 kubernetes kubectl

我创建了一个资源: kubectl create -f example.yaml

如何使用kubectl编辑此资源?据说kubectl edit,但我不确定资源名称,并kubectl edit example返回错误:

the server doesn't have a resource type "example"
Run Code Online (Sandbox Code Playgroud)

sve*_*ltr 8

您可以kubectl edit -f example.yaml直接编辑它.不过我建议在本地编辑文件并执行kubectl apply -f example.yaml,因此它会在Kubernetes上更新.

另外:您的命令失败,因为您必须指定资源类型.实施例类型是pod,service,deployment等.您可以使用普通模型查看所有可能的类型kubectl get.该类型应与kindYAML文件中的值匹配,并且名称应匹配metadata.name.

例如,可以使用以下文件进行编辑 kubectl edit deployment nginx-deployment

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2 # tells deployment to run 2 pods matching the template
  template: # create pods using pod definition in this template
    metadata:
      # unlike pod-nginx.yaml, the name is not included in the meta data as a unique name is
      # generated from the deployment name
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
Run Code Online (Sandbox Code Playgroud)

  • 只是一个注释。使用 `apply` 会给我一个警告:`警告:kubectl apply 应该用于由 kubectl create --save-config 或 kubectl apply` 创建的资源。`kubectl replace -f example.yaml` 似乎在没有警告的情况下实现了上述目标。 (2认同)