Use NodePorts to expose the service nodePort on all nodes in the cluster.
Note
|
Using NodePorts requires additional port resources. |
A node port exposes the service on a static port on the node IP address.
NodePorts are in the 30000-32767 range by default, which means a NodePort is unlikely to match a service’s intended port (for example, 8080 may be exposed as 31020).
The administrator must ensure the external IPs are routed to the nodes and local firewall rules on all nodes allow access to the open port.
NodePorts and external IPs are independent and both can be used concurrently.
You specify a port number for the nodePort when you create or modify a service. If you didn’t manually specify a port, system will allocate one for you.
-
Log into the master node.
-
If the project you want to use does not exist, create a new project for your service:
$ oc new-project <project_name>
For example:
$ oc new-project external-ip
-
Edit the service definition to specify
spec.type:NodePort
and optionally specify a port in the 30000-32767 range.apiVersion: v1 kind: Service metadata: name: mysql labels: name: mysql spec: type: NodePort ports: - port: 3306 nodePort: 30036 name: http selector: name: mysql
-
Run the following command to create the service:
$ oc create -f <file_name>
For example:
$ oc create -f mysql.yaml
-
Run the following command to see that the new service is created:
$ oc get svc NAME CLUSTER_IP EXTERNAL_IP PORT(S) AGE mysql 172.30.89.219 <none> 3306:30036/TCP 2m
Note that the external IP is listed as
<none>
and the node ports are listed.
You should be able to access the service using the <NodeIP>:<NodePort>
address.