Kubernetes kubectl rollout history 命令详解

kubectl history

查看之前推出的版本(历史版本)。

语法

$ history (TYPE NAME | TYPE/NAME) [flags]

示例

查看deployment的历史记录

kubectl rollout history deployment/abc

查看daemonset修订版3的详细信息

kubectl rollout history daemonset/abc --revision=3

Flags

Name Shorthand Default Usage
filename f [] Filename, directory, or URL to files identifying the resource to get from a server.
recursive R false Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.
revision 0 See the details, including podTemplate of the revision specified

 

K8S中文社区微信公众号

Kubernetes kubectl rollout 命令详解

kubectl rollout

对资源进行管理

可用资源包括:

  • deployments
  • daemonsets

子命令

语法

$ rollout SUBCOMMAND

示例

回滚到之前的deployment

kubectl rollout undo deployment/abc

查看daemonet的状态

kubectl rollout status daemonset/foo
K8S中文社区微信公众号

Kubernetes kubectl rolling-update 命令详解

kubectl rolling-update

执行指定ReplicationController的滚动更新。

该命令创建了一个新的RC, 然后一次更新一个pod方式逐步使用新的PodTemplate,最终实现Pod滚动更新,new-controller.json需要与之前RC在相同的namespace下。

http://kubernetes.io/images/docs/kubectl_rollingupdate.svg

语法

$ rolling-update OLD_CONTROLLER_NAME ([NEW_CONTROLLER_NAME] --image=NEW_CONTAINER_IMAGE | -f NEW_CONTROLLER_SPEC)

示例

使用frontend-v2.json中的新RC数据更新frontend-v1的pod。

kubectl rolling-update frontend-v1 -f frontend-v2.json

使用JSON数据更新frontend-v1的pod。

cat frontend-v2.json | kubectl rolling-update frontend-v1 -f -

其他

kubectl rolling-update frontend-v1 frontend-v2 --image=image:v2

kubectl rolling-update frontend --image=image:v2

kubectl rolling-update frontend-v1 frontend-v2 --rollback

Flags

Name Shorthand Default Usage
allow-missing-template-keys true If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
container Container name which will have its image upgraded. Only relevant when --image is specified, ignored otherwise. Required when using --image on a multi-container pod
deployment-label-key deployment The key to use to differentiate between two different controllers, default 'deployment'. Only relevant when --image is specified, ignored otherwise
dry-run false If true, only print the object that would be sent, without sending it.
filename f [] Filename or URL to file to use to create the new replication controller.
image Image to use for upgrading the replication controller. Must be distinct from the existing image (either new image or new image tag). Can not be used with --filename/-f
image-pull-policy Explicit policy for when to pull container images. Required when --image is same as existing image, ignored otherwise.
include-extended-apis true If true, include definitions of new APIs via calls to the API server. [default true]
no-headers false When using the default or custom-column output format, don't print headers (default print headers).
output o Output format. One of: json|yaml|wide|name|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=... See custom columns [http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns], golang template [http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].
output-version DEPRECATED: To use a specific API version, fully-qualify the resource, version, and group (for example: 'jobs.v1.batch/myjob').
poll-interval 3s Time delay between polling for replication controller status after the update. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
rollback false If true, this is a request to abort an existing rollout that is partially rolled out. It effectively reverses current and next and runs a rollout
schema-cache-dir ~/.kube/schema If non-empty, load/store cached API schemas in this directory, default is '$HOME/.kube/schema'
show-all a false When printing, show all resources (default hide terminated pods.)
show-labels false When printing, show all labels as the last column (default hide labels column)
sort-by If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. '{.metadata.name}'). The field in the API resource specified by this JSONPath expression must be an integer or a string.
template Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
timeout 5m0s Max time to wait for a replication controller to update before giving up. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
update-period 1m0s Time to wait between updating pods. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
validate true If true, use a schema to validate the input before sending it

 

K8S中文社区微信公众号

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

 

K8S中文社区微信公众号

Kubernetes kubectl patch 命令详解

kubectl patch

使用(patch)补丁修改、更新资源的字段。

支持JSON和YAML格式。

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

语法

$ patch (-f FILENAME | TYPE NAME) -p PATCH

示例

使用patch更新Node节点。

kubectl patch node k8s-node-1 -p '{"spec":{"unschedulable":true}}'

使用patch更新由“node.json”文件中指定的类型和名称标识的节点

kubectl patch -f node.json -p '{"spec":{"unschedulable":true}}'

更新容器的镜像

kubectl patch pod valid-pod -p '{"spec":{"containers":[{"name":"kubernetes-serve-hostname","image":"new image"}]}}'
kubectl patch pod valid-pod --type='json' -p='[{"op": "replace", "path": "/spec/containers/0/image", "value":"new image"}]'

Flags

Name Shorthand Default Usage
allow-missing-template-keys true If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
filename f [] Filename, directory, or URL to files identifying the resource to update
include-extended-apis true If true, include definitions of new APIs via calls to the API server. [default true]
local false If true, patch will operate on the content of the file, not the server-side resource.
no-headers false When using the default or custom-column output format, don't print headers (default print headers).
output o Output format. One of: json|yaml|wide|name|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=... See custom columns [http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns], golang template [http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].
output-version DEPRECATED: To use a specific API version, fully-qualify the resource, version, and group (for example: 'jobs.v1.batch/myjob').
patch p The patch to be applied to the resource JSON file.
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.
show-all a false When printing, show all resources (default hide terminated pods.)
show-labels false When printing, show all labels as the last column (default hide labels column)
sort-by If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. '{.metadata.name}'). The field in the API resource specified by this JSONPath expression must be an integer or a string.
template Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].
type strategic The type of patch being provided; one of [json merge strategic]

 

K8S中文社区微信公众号

Kubernetes kubectl label 命令详解

kubectl label

更新(增加、修改或删除)资源上的 label(标签)。

  • label 必须以字母或数字开头,可以使用字母、数字、连字符、点和下划线,最长63个字符。
  • 如果--overwrite 为 true,则可以覆盖已有的 label,否则尝试覆盖 label 将会报错。
  • 如果指定了--resource-version,则更新将使用此资源版本,否则将使用现有的资源版本。

语法

$ label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version]

示例

给名为foo的Pod添加label unhealthy=true。

kubectl label pods foo unhealthy=true

给名为foo的Pod修改label 为 'status' / value 'unhealthy',且覆盖现有的value。

kubectl label --overwrite pods foo status=unhealthy

给 namespace 中的所有 pod 添加 label

kubectl label pods --all status=unhealthy

仅当resource-version=1时才更新 名为foo的Pod上的label。

kubectl label pods foo status=unhealthy --resource-version=1

删除名为“bar”的label 。(使用“ - ”减号相连)

kubectl label pods foo bar-

Flags

Name Shorthand Default Usage
all false select all resources in the namespace of the specified resource types
allow-missing-template-keys true If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.
dry-run false If true, only print the object that would be sent, without sending it.
filename f [] Filename, directory, or URL to files identifying the resource to update the labels
include-extended-apis true If true, include definitions of new APIs via calls to the API server. [default true]
local false If true, label will NOT contact api-server but run locally.
no-headers false When using the default or custom-column output format, don't print headers (default print headers).
output o Output format. One of: json|yaml|wide|name|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=... See custom columns [http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns], golang template [http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].
output-version DEPRECATED: To use a specific API version, fully-qualify the resource, version, and group (for example: 'jobs.v1.batch/myjob').
overwrite false If true, allow labels to be overwritten, otherwise reject label updates that overwrite existing labels.
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.
resource-version If non-empty, the labels update will only succeed if this is the current resource-version for the object. Only valid when specifying a single resource.
selector l Selector (label query) to filter on, supports '=', '==', and '!='.
show-all a false When printing, show all resources (default hide terminated pods.)
show-labels false When printing, show all labels as the last column (default hide labels column)
sort-by If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. '{.metadata.name}'). The field in the API resource specified by this JSONPath expression must be an integer or a string.
template Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].

 

K8S中文社区微信公众号