// Module included in the following assemblies:
//
// * networking/configuring_ingress_cluster_traffic/configuring-ingress-cluster-traffic-nodeport.adoc

ifeval::["{context}" == "configuring-ingress-cluster-traffic-nodeport"]
:nodeport:
endif::[]

:_mod-docs-content-type: PROCEDURE
[id="nw-exposing-service_{context}"]
= Exposing the service by creating a route

[role="_abstract"]
To enable external access to your application that runs on {product-title}, you can expose the service as a route by using the `oc expose` command.

.Prerequisites

* You logged into {product-title}.

.Procedure

. Log in to the project where the service you want to expose is located:
+
[source,terminal]
----
$ oc project <project_name>
----

ifndef::nodeport[]
. Run the `oc expose service` command to expose the route:
+
[source,terminal]
----
$ oc expose service nodejs-ex
----
+
.Example output
[source,terminal]
----
route.route.openshift.io/nodejs-ex exposed
----

. To verify that the service is exposed, you can use a tool, such as `curl` to check that the service is accessible from outside the cluster.
+
.. To find the hostname of the route, enter the following command:
+
[source,terminal]
----
$ oc get route
----
+
.Example output
[source,terminal]
----
NAME        HOST/PORT                        PATH   SERVICES    PORT       TERMINATION   WILDCARD
nodejs-ex   nodejs-ex-myproject.example.com         nodejs-ex   8080-tcp                 None
----
+
.. To check that the host responds to a GET request, enter the following command:
+
.Example `curl` command
[source,terminal]
----
$ curl --head nodejs-ex-myproject.example.com
----
+
.Example output
[source,terminal]
----
HTTP/1.1 200 OK
...
----

endif::nodeport[]
ifdef::nodeport[]
. To expose a node port for the application, modify the custom resource definition (CRD) of a service by entering the following command:
+
[source,terminal]
----
$ oc edit svc <service_name>
----
+
.Example output
[source,yaml]
----
spec:
  ports:
  - name: 8443-tcp
    nodePort: 30327
    port: 8443
    protocol: TCP
    targetPort: 8443
  sessionAffinity: None
  type: NodePort
----
+
* `nodePort`: Optional parameter. Specifies the node port range for the application. By default, {product-title} selects an available port in the `30000-32767` range.
* `type`: Specifies the service type.

. Optional: To confirm the service is available with a node port exposed, enter the following command:
+
[source,terminal]
----
$ oc get svc -n myproject
----
+
.Example output
[source,terminal]
----
NAME                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
nodejs-ex           ClusterIP   172.30.217.127   <none>        3306/TCP         9m44s
nodejs-ex-ingress   NodePort    172.30.107.72    <none>        3306:31345/TCP   39s
----

. Optional: To remove the service created automatically by the `oc new-app` command, enter the following command:
+
[source,terminal]
----
$ oc delete svc nodejs-ex
----

.Verification

* To check that the service node port is updated with a port in the `30000-32767` range, enter the following command:
+
[source,terminal]
----
$ oc get svc
----
+
In the following example output, the updated port is `30327`:
+
.Example output
[source,terminal]
----
NAME    TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
httpd   NodePort   172.xx.xx.xx    <none>        8443:30327/TCP   109s
----
endif::nodeport[]

ifdef::nodeport[]
:!nodeport:
endif::[]
