Kubernetes kubectl create deployment 命令详解

kubectl create deployment

创建具有指定名称的deployment 。

语法

$ deployment NAME --image=image [--dry-run]

示例

创建一个名为my-dep的deployment,运行busybox镜像。

kubectl create deployment my-dep --image=busybox

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.
dry-run false If true, only print the object that would be sent, without sending it.
generator deployment-basic/v1beta1 The name of the API generator to use.
image [] Image name to run.
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').
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'
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].
validate true If true, use a schema to validate the input before sending it
K8S中文社区微信公众号

Kubernetes kubectl create configmap 命令详解

kubectl  create configmap

根据配置文件、目录或指定的literal-value创建configmap 。

configmap 用来保存一个或多个key/value信息。

当基于配置文件创建configmap时,key将默认为文件的基础名称,value默认为文件文本内容。如果基本名称的key无效,则可以指定另一个key。

当基于目录创建configmap时,key还是文件的基础名称,目录中每个配置文件名都被设置为key,文件内容设置为value。

语法

$ configmap NAME [--from-file=[key=]source] [--from-literal=key1=value1] [--dry-run]

示例

根据文件创建一个名为my-config的configmap

kubectl create configmap my-config --from-file=path/to/bar

使用指定的keys创建一个名为my-config的configmap

kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt

使用key1 = config1和key2 = config2创建一个名为my-config的configmap

kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2

从文件中的key = value对创建一个名为my-config的configmap

kubectl create configmap my-config --from-file=path/to/bar

从env文件创建一个名为my-config的configmap

kubectl create configmap my-config --from-env-file=path/to/bar.env

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.
dry-run false If true, only print the object that would be sent, without sending it.
from-env-file Specify the path to a file to read lines of key=val pairs to create a configmap (i.e. a Docker .env file).
from-file [] Key file can be specified using its file path, in which case file basename will be used as configmap key, or optionally with a key and file path, in which case the given key will be used. Specifying a directory will iterate each named file in the directory whose basename is a valid configmap key.
from-literal [] Specify a key and literal value to insert in configmap (i.e. mykey=somevalue)
generator configmap/v1 The name of the API generator to use.
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').
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'
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].
validate true If true, use a schema to validate the input before sending it
K8S中文社区微信公众号

Kubernetes kubectl create clusterrolebinding 命令详解

kubectl create clusterrolebinding

为特定的ClusterRole创建ClusterRoleBinding。

语法

$ clusterrolebinding NAME --clusterrole=NAME [--user=username] [--group=groupname] [--serviceaccount=namespace:serviceaccountname] [--dry-run]

示例

在集群范围将cluster-admin ClusterRole授予用户user1,user2和group1。

kubectl create clusterrolebinding cluster-admin --clusterrole=cluster-admin --user=user1 --user=user2 --group=group1

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.
clusterrole ClusterRole this ClusterRoleBinding should reference
dry-run false If true, only print the object that would be sent, without sending it.
generator clusterrolebinding.rbac.authorization.k8s.io/v1alpha1 The name of the API generator to use.
group [] groups to bind to the role
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').
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'
serviceaccount [] service accounts to bind to the role, in the format :
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].
user [] usernames to bind to the role
validate true If true, use a schema to validate the input before sending it

 

K8S中文社区微信公众号

Kubernetes kubectl create clusterrole 命令详解

kubectl create clusterrole

创建一个ClusterRole。

语法

$ clusterrole NAME --verb=verb --resource=resource.group [--resource-name=resourcename] [--dry-run]

示例

创建一个名为“pod-reader”的ClusterRole,允许用户在pod上执行“get”,“watch”和“list”

kubectl create clusterrole pod-reader --verb=get,list,watch --resource=pods

创建一个名为“pod-reader”的ClusterRole,其中指定了ResourceName

kubectl create clusterrole pod-reader --verb=get,list,watch --resource=pods --resource-name=readablepod --resource-name=anotherpod

在指定的API Group中创建为"foo"的ClusterRole

kubectl create clusterrole foo --verb=get,list,watch --resource=rs.extensions

创建一个名为“foo”的ClusterRole,并指定SubResource

kubectl create clusterrole foo --verb=get,list,watch --resource=pods,pods/status

使用指定的NonResourceURL创建名称“foo”的ClusterRole

kubectl create clusterrole "foo" --verb=get --non-resource-url=/logs/*

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.
dry-run false If true, only print the object that would be sent, without sending it.
no-headers false When using the default or custom-column output format, don't print headers (default print headers).
non-resource-url [] a partial url that user should have access to.
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').
resource [] resource that the rule applies to
resource-name [] resource in the white list that the rule applies to, repeat this flag for multiple items
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'
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].
validate true If true, use a schema to validate the input before sending it
verb [] verb that applies to the resources contained in the rule

 

K8S中文社区微信公众号

Kubernetes kubectl create 命令详解

kubectl create

通过配置文件名或stdin创建一个集群资源对象。

支持JSON和YAML格式的文件。

语法

$ create -f FILENAME

示例

通过pod.json文件创建一个pod。

kubectl create -f ./pod.json

通过stdin的JSON创建一个pod。

cat pod.json | kubectl create -f -

API版本为v1的JSON格式的docker-registry.yaml文件创建资源。

kubectl create -f docker-registry.yaml --edit --output-version=v1 -o json

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.
dry-run false If true, only print the object that would be sent, without sending it.
edit false Edit the API resource before creating
filename f [] Filename, directory, or URL to files to use to create the resource
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').
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'
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].
validate true If true, use a schema to validate the input before sending it
windows-line-endings false Only relevant if --edit=true. Use Windows line-endings (default Unix line-endings)

 

K8S中文社区微信公众号

Kubernetes kubectl convert 命令详解

kubectl convert

转换配置文件为不同的API版本,支持YAML和JSON格式。

该命令将配置文件名,目录或URL作为输入,并将其转换为指定的版本格式,如果目标版本未指定或不支持,则转换为最新版本。

默认输出将以YAML格式打印出来,可以使用- o选项改变输出格式。

语法

$ convert -f FILENAME

示例

将“pod.yaml”转换为最新版本并打印到stdout。

kubectl convert -f pod.yaml

将“pod.yaml”指定的资源的实时状态转换为最新版本#,并以json格式打印到stdout。

kubectl convert -f pod.yaml --local -o json

将当前目录下的所有文件转换为最新版本,并将其全部创建。

kubectl convert -f . | kubectl create -f -

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 to need to get converted.
include-extended-apis true If true, include definitions of new APIs via calls to the API server. [default true]
local true If true, convert will NOT try to 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 Output the formatted object with the given group version (for ex: 'extensions/v1beta1').)
recursive R false Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.
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].
validate true If true, use a schema to validate the input before sending it

 

K8S中文社区微信公众号