容器編排技術 -- Kubernetes kubectl expose命令詳解
發布於2019-08-26 12:07:03
容器編排技術 -- Kubernetes kubectl expose命令詳解
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
$ kubectl run myapp --image=nginx --restart=Always --replicas=2 --port=80
[root@node1 ~]# kubectl edit service myapp
得到的結果
# Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 kind: Service metadata: annotations: field.cattle.io/publicEndpoints: '[{"addresses":["10.60.188.141"],"port":31206,"protocol":"TCP","serviceName":"default:myapp","allNodes":true}]' creationTimestamp: "2020-04-11T14:17:49Z" labels: run: myapp name: myapp namespace: default resourceVersion: "27403024" selfLink: /api/v1/namespaces/default/services/myapp uid: 390d603c-7bff-11ea-be2e-fa92dd475100 spec: clusterIP: 10.43.96.137 externalTrafficPolicy: Cluster ports: - nodePort: 31206 port: 80 protocol: TCP targetPort: 80 selector: run: myapp sessionAffinity: None type: NodePort status: loadBalancer: {} ~ ~
其它詳細說明
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'. |