Kubernetes kubectl replace 命令详解

kubectl replace

使用配置文件或stdin来替换资源。

支持JSON和YAML格式。如果替换当前资源,则必须提供完整的资源规范。可以通过以下命令获取:

$ kubectl get TYPE NAME -o yaml

请参考https://htmlpreview.github.io/?https://github.com/kubernetes/kubernetes/blob/HEAD/docs/api-reference/v1/definitions.html中的模型,查找字段是否为可变的。

语法

$ replace -f FILENAME

示例

使用pod.json中的数据替换pod。

kubectl replace -f ./pod.json

根据传入的JSON替换pod。

cat pod.json | kubectl replace -f -

更新镜像版本(tag)到v4

kubectl get pod mypod -o yaml | sed 's/\(image: myimage\):.*$/\1:v4/' | kubectl replace -f -

强制替换,删除原有资源,然后重新创建资源

kubectl replace --force -f ./pod.json

Flags

Name Shorthand Default Usage
cascade false Only relevant during a force replace. If true, cascade the deletion of the resources managed by this resource (e.g. Pods created by a ReplicationController).
filename f [] Filename, directory, or URL to files to use to replace the resource.
force false Delete and re-create the specified resource
grace-period -1 Only relevant during a force replace. Period of time in seconds given to the old resource to terminate gracefully. Ignored if negative.
include-extended-apis true If true, include definitions of new APIs via calls to the API server. [default true]
output o Output mode. Use "-o name" for shorter output (resource/name).
record false Record current kubectl command in the resource annotation. If set to false, do not record the command. If set to true, record the command. If not set, default to updating the existing annotation value only if one already exists.
recursive R false Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.
save-config false If true, the configuration of current object will be saved in its annotation. Otherwise, the annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future.
schema-cache-dir ~/.kube/schema If non-empty, load/store cached API schemas in this directory, default is '$HOME/.kube/schema'
timeout 0s Only relevant during a force replace. The length of time to wait before giving up on a delete of the old resource, zero means determine a timeout from the size of the object. Any other values should contain a corresponding time unit (e.g. 1s, 2m, 3h).
validate true If true, use a schema to validate the input before sending it

 

译者

Vincent

业精于勤 荒于嬉;行成于思 毁于随;