Kubectl expose: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(23 intermediate revisions by the same user not shown)
Line 2: Line 2:


* [[Kubectl#Commands|kubectl]]
* [[Kubectl#Commands|kubectl]]
* [[Kubernetes Service Operations]]


=Overview=
=Overview=


<tt>expose</tt> dynamically exposes a resource (pod, service, replicationcontroller, deployment, replicaset) as a new service. The default service type is [[Kubernetes_Service_Concepts#ClusterIP|ClusterIP]], other type ([[Kubernetes_Service_Concepts#NodePort|NodePort]], [[Kubernetes_Service_Concepts#LoadBalancer|LoadBalancer]], or [[Kubernetes_Service_Concepts#ExternalName|ExternalName]]) can be specified with --type.
<code>expose</code> dynamically exposes a resource, which can be a [[Kubernetes_Pod_and_Container_Concepts#Pod|pod]], another [[Kubernetes_Service_Concepts#Service|service]], a [[Kubernetes_Deployments|deployment]], [[Kubernetes_ReplicaSet|replica set]] or a [[Kubernetes_ReplicationController|replication controller]] as a new service. If no service type is explicitly specified, [[Kubernetes_Service_Concepts#ClusterIP|ClusterIP]] is assumed. Specific service types ([[Kubernetes_Service_Concepts#NodePort|NodePort]], [[Kubernetes_Service_Concepts#LoadBalancer|LoadBalancer]], or [[Kubernetes_Service_Concepts#ExternalName|ExternalName]]) can be requested with <code>--type</code>.


kubectl [-n default] expose pod pg-0 --port=5432 --target-port=5432 --name=postgres --type=NodePort
=Expose a Pod=


<font color=darkgray>TODO: I don't seem to see the service with kubectl get services, but attempting to connect to the database works.</font>
If a pod is used, the command reads the pod's labels and creates a service with a matching selector. By default, if no type is specified, ClusterIP is assumed.
 
<syntaxhighlight lang='bash'>
kubectl [-n <namespace>] expose pod <pod-name> \
  --name=<service-name> --port=<port> --target-port=<target-port> \
  [--type=ClusterIP|NodePort|...]
</syntaxhighlight>
 
<syntaxhighlight lang='bash'>
kubectl expose pod httpd --name=httpd-svc --port=9898 --target-port=80
</syntaxhighlight>
 
=Expose a Higher Level Controller=
 
If a replication controller, deployment or replicaset are used, the command <font color=darkgray>reads their selector and re-uses it for the service being created.</font>
 
=Deployment=
 
<syntaxhighlight lang='text'>
kubectl expose deployment my-deployment --name=my-service --type=ClusterIP
</syntaxhighlight>
 
<syntaxhighlight lang='bash'>
kubectl expose deployment http-server --port=9999 --type=NodePort --name=http-service --overrides '{ "apiVersion": "v1","spec":{"ports": [{"port":9999,"protocol":"TCP","targetPort":9999,"nodePort":30999}]}}'
</syntaxhighlight>
 
=Expose a Service=
<font color=darkkhaki>TODO
</font>

Latest revision as of 18:42, 6 December 2023

Internal

Overview

expose dynamically exposes a resource, which can be a pod, another service, a deployment, replica set or a replication controller as a new service. If no service type is explicitly specified, ClusterIP is assumed. Specific service types (NodePort, LoadBalancer, or ExternalName) can be requested with --type.

Expose a Pod

If a pod is used, the command reads the pod's labels and creates a service with a matching selector. By default, if no type is specified, ClusterIP is assumed.

kubectl [-n <namespace>] expose pod <pod-name> \
  --name=<service-name> --port=<port> --target-port=<target-port> \
  [--type=ClusterIP|NodePort|...]
kubectl expose pod httpd --name=httpd-svc --port=9898 --target-port=80

Expose a Higher Level Controller

If a replication controller, deployment or replicaset are used, the command reads their selector and re-uses it for the service being created.

Deployment

kubectl expose deployment my-deployment --name=my-service --type=ClusterIP
kubectl expose deployment http-server --port=9999 --type=NodePort --name=http-service --overrides '{ "apiVersion": "v1","spec":{"ports": [{"port":9999,"protocol":"TCP","targetPort":9999,"nodePort":30999}]}}'

Expose a Service

TODO