如何使用 kubectl 编辑资源配置?
How do I edit a resource configuration with kubectl?
我创建了一个资源:
kubectl create -f example.yaml
如何使用 kubectl 编辑此资源?据说是kubectl edit
,但是我不确定资源名称,kubectl edit example
returns 的错误是:
the server doesn't have a resource type "example"
您可以kubectl edit -f example.yaml
直接编辑它。尽管如此,我还是建议在本地编辑文件并执行 kubectl apply -f example.yaml
,以便它在 Kubernetes 上得到更新。
另外:您的命令失败,因为您必须指定资源类型。示例类型有 pod
、service
、deployment
等。您可以使用普通 kubectl get
查看所有可能的类型。类型应匹配 YAML 文件中的 kind
值,名称应匹配 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
我创建了一个资源:
kubectl create -f example.yaml
如何使用 kubectl 编辑此资源?据说是kubectl edit
,但是我不确定资源名称,kubectl edit example
returns 的错误是:
the server doesn't have a resource type "example"
您可以kubectl edit -f example.yaml
直接编辑它。尽管如此,我还是建议在本地编辑文件并执行 kubectl apply -f example.yaml
,以便它在 Kubernetes 上得到更新。
另外:您的命令失败,因为您必须指定资源类型。示例类型有 pod
、service
、deployment
等。您可以使用普通 kubectl get
查看所有可能的类型。类型应匹配 YAML 文件中的 kind
值,名称应匹配 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