# kubectl set resources 命令详解

资源对象中的 Pod 可以指定计算资源需求(CPU-单位 m、内存-单位 Mi),即使用的最小资源请求(Requests),限制(Limits)的最大资源需求,Pod 将保证使用在设置的资源数量范围。

  • Requests:计算资源最小数量。
  • Limits:资源最大允许数量。

对于每个 Pod 资源,如果指定了 Limits(限制)值,并省略了 Requests(请求),则 Requests 默认为 Limits 的值。

可用资源对象包括(支持大小写):replicationcontroller、deployment、daemonset、job、replicaset。

# 语法

$ resources (-f FILENAME | TYPE NAME) ([--limits=LIMITS & --requests=REQUESTS]

# 示例

将 deployment 的 nginx 容器 cpu 限制为“200m”,将内存设置为“512Mi”

kubectl set resources deployment nginx -c=nginx --limits=cpu=200m,memory=512Mi

为 nginx 中的所有容器设置 Requests 和 Limits

kubectl set resources deployment nginx --limits=cpu=200m,memory=512Mi --requests=cpu=100m,memory=256Mi

删除 nginx 中容器的计算资源值

kubectl set resources deployment nginx --limits=cpu=0,memory=0 --requests=cpu=0,memory=0

打印从本地更新 nginx 容器限制的结果(以 yaml 格式),而不会触发服务器

打印结果(以 yaml 格式),在不影响服务器的情况下,从本地更新 nginx 容器限制

kubectl set resources -f path/to/file.yaml --limits=cpu=200m,memory=512Mi --local -o yaml

# 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.
containers c * The names of containers in the selected pod templates to change, all containers are selected by default - may use wildcards
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 get from a server.
limits The resource requirement requests for this container. For example, 'cpu=100m,memory=256Mi'. Note that server side components may assign requests depending on the server configuration, such as limit ranges.
local false If true, set resources 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').
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.
requests The resource requirement requests for this container. For example, 'cpu=100m,memory=256Mi'. Note that server side components may assign requests depending on the server configuration, such as limit ranges.
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].
Last Updated: 4/15/2023, 8:33:17 PM