# kubectl replace 命令详解

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

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

$ kubectl get TYPE NAME -o yaml

# 语法

$ 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
Last Updated: 6/17/2023, 6:57:19 PM