|
| 1 | +// Module included in the following assemblies: |
| 2 | +// |
| 3 | +// * networking/configuring_ingress_cluster_traffic/nw-configuring-ingress-controller-endpoint-publishing-strategy.adoc |
| 4 | + |
| 5 | +:_mod-docs-content-type: PROCEDURE |
| 6 | +[id="nw-ingress-controller-nodeportservice-projects_{context}"] |
| 7 | += Adding a single NodePort service to an Ingress Controller |
| 8 | + |
| 9 | +Instead of creating a `NodePort`-type `Service` for each project, you can create a custom Ingress Controller to use the `NodePortService` endpoint publishing strategy. To prevent port conflicts, consider this configuration for your Ingress Controller when you want to apply a set of routes, through Ingress sharding, to nodes that might already have a `HostNetwork` Ingress Controller. |
| 10 | + |
| 11 | +Before you set a `NodePort`-type `Service` for each project, read the following considerations: |
| 12 | + |
| 13 | +* You must create a wildcard DNS record for the Nodeport Ingress Controller domain. A Nodeport Ingress Controller route can be reached from the address of a worker node. For more information about the required DNS records for routes, see "User-provisioned DNS requirements". |
| 14 | +* You must expose a route for your service and specify the `--hostname` argument for your custom Ingress Controller domain. |
| 15 | +* You must append the port that is assigned to the `NodePort`-type `Service` in the route so that you can access application pods. |
| 16 | +
|
| 17 | +.Prerequisites |
| 18 | + |
| 19 | +* You installed the {oc-first}. |
| 20 | +* Logged in as a user with `cluster-admin` privileges. |
| 21 | +* You created a wildcard DNS record. |
| 22 | +// https://docs.openshift.com/container-platform/4.16/networking/ingress-controller-dnsmgt.html (does not detail how to create the DNS) |
| 23 | +
|
| 24 | +.Procedure |
| 25 | + |
| 26 | +. Create a custom resource (CR) file for the Ingress Controller: |
| 27 | ++ |
| 28 | +.Example of a CR file that defines information for the `IngressController` object |
| 29 | +[source,yaml] |
| 30 | +---- |
| 31 | +apiVersion: v1 |
| 32 | +items: |
| 33 | +- apiVersion: operator.openshift.io/v1 |
| 34 | + kind: IngressController |
| 35 | + metadata: |
| 36 | + name: <custom_ic_name> <1> |
| 37 | + namespace: openshift-ingress-operator |
| 38 | + spec: |
| 39 | + replicas: 1 |
| 40 | + domain: <custom_ic_domain_name> <2> |
| 41 | + nodePlacement: |
| 42 | + nodeSelector: |
| 43 | + matchLabels: |
| 44 | + <key>: <value> <3> |
| 45 | + namespaceSelector: |
| 46 | + matchLabels: |
| 47 | + <key>: <value> <4> |
| 48 | + endpointPublishingStrategy: |
| 49 | + type: NodePortService |
| 50 | +# ... |
| 51 | +---- |
| 52 | +<1> Specify the a custom `name` for the `IngressController` CR. |
| 53 | +<2> The DNS name that the Ingress Controller services. As an example, the default ingresscontroller domain is `apps.ipi-cluster.example.com`, so you would specify the `<custom_ic_domain_name>` as `nodeportsvc.ipi-cluster.example.com`. |
| 54 | +<3> Specify the label for the nodes that include the custom Ingress Controller. |
| 55 | +<4> Specify the label for a set of namespaces. Substitute `<key>:<value>` with a map of key-value pairs where `<key>` is a unique name for the new label and `<value>` is its value. For example: `ingresscontroller: custom-ic`. |
| 56 | + |
| 57 | +. Add a label to a node by using the `oc label node` command: |
| 58 | ++ |
| 59 | +[source,terminal] |
| 60 | +---- |
| 61 | +$ oc label node <node_name> <key>=<value> <1> |
| 62 | +---- |
| 63 | +<1> Where `<value>` must match the key-value pair specified in the `nodePlacement` section of your `IngressController` CR. |
| 64 | + |
| 65 | +. Create the `IngressController` object: |
| 66 | ++ |
| 67 | +[source,terminal] |
| 68 | +---- |
| 69 | +$ oc create -f <ingress_controller_cr>.yaml |
| 70 | +---- |
| 71 | + |
| 72 | +. Find the port for the service created for the `IngressController` CR: |
| 73 | ++ |
| 74 | +[source,terminal] |
| 75 | +---- |
| 76 | +$ oc get svc -n openshift-ingress |
| 77 | +---- |
| 78 | ++ |
| 79 | +.Example output that shows port `80:32432/TCP` for the `router-nodeport-custom-ic3` service |
| 80 | +[source,terminal] |
| 81 | +---- |
| 82 | +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| 83 | +router-internal-default ClusterIP 172.30.195.74 <none> 80/TCP,443/TCP,1936/TCP 223d |
| 84 | +router-nodeport-custom-ic3 NodePort 172.30.109.219 <none> 80:32432/TCP,443:31366/TCP,1936:30499/TCP 155m |
| 85 | +---- |
| 86 | + |
| 87 | +. To create a new project, enter the following command: |
| 88 | ++ |
| 89 | +[source,terminal] |
| 90 | +---- |
| 91 | +$ oc new-project <project_name> |
| 92 | +---- |
| 93 | + |
| 94 | +. To label the new namespace, enter the following command: |
| 95 | ++ |
| 96 | +[source,terminal] |
| 97 | +---- |
| 98 | +$ oc label namespace <project_name> <key>=<value> <1> |
| 99 | +---- |
| 100 | +<1> Where `<key>=<value>` must match the value in the `namespaceSelector` section of your Ingress Controller CR. |
| 101 | + |
| 102 | +. Create a new application in your cluster: |
| 103 | ++ |
| 104 | +[source,terminal] |
| 105 | +---- |
| 106 | +$ oc new-app --image=<image_name> <1> |
| 107 | +---- |
| 108 | +<1> An example of `<image_name>` is `quay.io/openshifttest/hello-openshift:multiarch`. |
| 109 | + |
| 110 | +. Create a `Route` object for a service, so that the pod can use the service to expose the application external to the cluster. |
| 111 | ++ |
| 112 | +[source,terminal] |
| 113 | +---- |
| 114 | +$ oc expose svc/<service_name> --hostname=<svc_name>-<project_name>.<custom_ic_domain_name> <1> |
| 115 | +---- |
| 116 | ++ |
| 117 | +[NOTE] |
| 118 | +==== |
| 119 | +You must specify the domain name of your custom Ingress Controller in the `--hostname` argument. If you do not do this, the Ingress Operator uses the default Ingress Controller to serve all the routes for your cluster. |
| 120 | +==== |
| 121 | + |
| 122 | +. Check that the route has the `Admitted` status and that it includes metadata for the custom Ingress Controller: |
| 123 | ++ |
| 124 | +[source,terminal,subs="quotes,attributes"] |
| 125 | +---- |
| 126 | +$ oc get route/hello-openshift -o json | jq '.status.ingress' |
| 127 | +---- |
| 128 | ++ |
| 129 | +.Example output |
| 130 | +[source,terminal] |
| 131 | +---- |
| 132 | +# ... |
| 133 | +{ |
| 134 | + "conditions": [ |
| 135 | + { |
| 136 | + "lastTransitionTime": "2024-05-17T18:25:41Z", |
| 137 | + "status": "True", |
| 138 | + "type": "Admitted" |
| 139 | + } |
| 140 | + ], |
| 141 | + [ |
| 142 | + { |
| 143 | + "host": "hello-openshift.nodeportsvc.ipi-cluster.example.com", |
| 144 | + "routerCanonicalHostname": "router-nodeportsvc.nodeportsvc.ipi-cluster.example.com", |
| 145 | + "routerName": "nodeportsvc", "wildcardPolicy": "None" |
| 146 | + } |
| 147 | + ], |
| 148 | +} |
| 149 | +---- |
| 150 | + |
| 151 | +. Update the default `IngressController` CR to prevent the default Ingress Controller from managing the `NodePort`-type `Service`. The default Ingress Controller will continue to monitor all other cluster traffic. |
| 152 | ++ |
| 153 | +[source,terminal] |
| 154 | +---- |
| 155 | +$ oc patch --type=merge -n openshift-ingress-operator ingresscontroller/default --patch '{"spec":{"namespaceSelector":{"matchExpressions":[{"key":"<key>","operator":"NotIn","values":["<value>]}]}}}' |
| 156 | +---- |
| 157 | + |
| 158 | +.Verification |
| 159 | + |
| 160 | +. Verify that the DNS entry can route inside and outside of your cluster by entering the following command. The command outputs the IP address of the node that received the label from running the `oc label node` command earlier in the procedure. |
| 161 | ++ |
| 162 | +[source,terminal] |
| 163 | +---- |
| 164 | +$ dig +short <svc_name>-<project_name>.<custom_ic_domain_name> |
| 165 | +---- |
| 166 | + |
| 167 | +. To verify that your cluster uses the IP addresses from external DNS servers for DNS resolution, check the connection of your cluster by entering the following command: |
| 168 | ++ |
| 169 | +[source,terminal] |
| 170 | +---- |
| 171 | +$ curl <svc_name>-<project_name>.<custom_ic_domain_name>:<port> <1> |
| 172 | +---- |
| 173 | +<1> Where `<port>` is the node port from the `NodePort`-type `Service`. Based on example output from the `oc get svc -n openshift-ingress` command, the `80:32432/TCP` HTTP route means that `32432` is the node port. |
| 174 | ++ |
| 175 | +.Output example |
| 176 | ++ |
| 177 | +[source,terminal] |
| 178 | +---- |
| 179 | +Hello OpenShift! |
| 180 | +---- |
0 commit comments