# kubectl expose 命令详解

将资源暴露为新的 Kubernetes Service。

指定 deployment、service、replica set、replication controller 或 pod,并使用该资源的选择器作为指定端口上新服务的选择器。deployment 或 replica set 只有当其选择器可转换为 service 支持的选择器时,即当选择器仅包含 matchLabels 组件时才会作为暴露新的 Service。

资源包括(不区分大小写):

pod(po),service(svc),replication controller(rc),deployment(deploy),replica set(rs)

# 语法

$ expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP] [--target-port=number-or-name] [--name=name] [--external-ip=external-ip-of-service] [--type=type]

# 示例

为 RC 的 nginx 创建 service,并通过 Service 的 80 端口转发至容器的 8000 端口上。

kubectl expose rc nginx --port=80 --target-port=8000

由“nginx-controller.yaml”中指定的 type 和 name 标识的 RC 创建 Service,并通过 Service 的 80 端口转发至容器的 8000 端口上。

kubectl expose -f nginx-controller.yaml --port=80 --target-port=8000

# 其它详细说明

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.
cluster-ip ClusterIP to be assigned to the service. Leave empty to auto-allocate, or set to 'None' to create a headless service.
container-port Synonym for --target-port
dry-run false If true, only print the object that would be sent, without sending it.
external-ip Additional external IP address (not managed by Kubernetes) to accept for the service. If this IP is routed to a node, the service can be accessed by this IP in addition to its generated service IP.
filename f [] Filename, directory, or URL to files identifying the resource to expose a service
generator service/v2 The name of the API generator to use. There are 2 generators: 'service/v1' and 'service/v2'. The only difference between them is that service port in v1 is named 'default', while it is left unnamed in v2. Default is 'service/v2'.
labels l Labels to apply to the service created by this call.
load-balancer-ip IP to assign to the Load Balancer. If empty, an ephemeral IP will be created and used (cloud-provider specific).
name The name for the newly created object.
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').
overrides An inline JSON override for the generated object. If this is non-empty, it is used to override the generated object. Requires that the object supply a valid apiVersion field.
port The port that the service should serve on. Copied from the resource being exposed, if unspecified
protocol The network protocol for the service to be created. Default is 'TCP'.
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.
selector A label selector to use for this service. Only equality-based selector requirements are supported. If empty (the default) infer the selector from the replication controller or replica set.)
session-affinity If non-empty, set the session affinity for the service to this; legal values: 'None', 'ClientIP'
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.
target-port Name or number for the port on the container that the service should direct traffic to. Optional.
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 Type for this service: ClusterIP, NodePort, or LoadBalancer. Default is 'ClusterIP'.
Last Updated: 4/15/2023, 8:33:17 PM