|
| 1 | +# Custom IPv4 and IPv6 Address Listeners |
| 2 | + |
| 3 | +In this example, we will configure a VirtualServer resource with custom IPv4 and IPv6 Address using HTTP/HTTPS listeners. |
| 4 | +This will allow IPv4 and/or IPv6 address using HTTP and/or HTTPS based requests to be made on non-default ports using separate IPs. |
| 5 | + |
| 6 | +## Prerequisites |
| 7 | + |
| 8 | +1. Follow the [installation](https://docs.nginx.com/nginx-ingress-controller/installation/installation-with-manifests/) |
| 9 | + instructions to deploy the Ingress Controller with custom resources enabled. |
| 10 | +2. Ensure the Ingress Controller is configured with the `-global-configuration` argument: |
| 11 | + |
| 12 | + ```console |
| 13 | + args: |
| 14 | + - -global-configuration=$(POD_NAMESPACE)/nginx-configuration |
| 15 | + ``` |
| 16 | + |
| 17 | +3. If you have a NodePort or Loadbalancer service deployed, ensure they are updated to include the custom listener ports. |
| 18 | +Example YAML for a LoadBalancer: |
| 19 | + |
| 20 | + ```yaml |
| 21 | + apiVersion: v1 |
| 22 | + kind: Service |
| 23 | + metadata: |
| 24 | + name: nginx-ingress |
| 25 | + namespace: nginx-ingress |
| 26 | + spec: |
| 27 | + type: LoadBalancer |
| 28 | + ports: |
| 29 | + - port: 8083 |
| 30 | + targetPort: 8083 |
| 31 | + protocol: TCP |
| 32 | + name: ip-listener-1-http |
| 33 | + - port: 8443 |
| 34 | + targetPort: 8443 |
| 35 | + protocol: TCP |
| 36 | + name: ip-listener-2-https |
| 37 | + selector: |
| 38 | + app: nginx-ingress |
| 39 | + ``` |
| 40 | +
|
| 41 | +**Note:** |
| 42 | +
|
| 43 | +- **No Updates for GC:** If a GlobalConfiguration resource already exists, delete the previous one before applying the new configuration. |
| 44 | +- **Single Replica:** Only one replica is allowed when using this configuration. |
| 45 | +
|
| 46 | +## Step 1 - Deploy the GlobalConfiguration resource |
| 47 | +
|
| 48 | +Similar to how listeners are configured in our [custom-listeners](../../custom-listeners) examples, |
| 49 | +here we deploy a GlobalConfiguration resource with the listeners we want to use in our VirtualServer. |
| 50 | +
|
| 51 | + ```yaml |
| 52 | +apiVersion: k8s.nginx.org/v1 |
| 53 | +kind: GlobalConfiguration |
| 54 | +metadata: |
| 55 | + name: nginx-configuration |
| 56 | + namespace: nginx-ingress |
| 57 | +spec: |
| 58 | + listeners: |
| 59 | + - name: ip-listener-1-http |
| 60 | + port: 8083 |
| 61 | + protocol: HTTP |
| 62 | + ipv4: 127.0.0.1 |
| 63 | + - name: ip-listener-2-https |
| 64 | + port: 8443 |
| 65 | + protocol: HTTP |
| 66 | + ipv4: 127.0.0.2 |
| 67 | + ipv6: ::1 |
| 68 | + ssl: true |
| 69 | + ``` |
| 70 | +
|
| 71 | + ```console |
| 72 | + kubectl create -f global-configuration.yaml |
| 73 | + ``` |
| 74 | + |
| 75 | +## Step 2 - Deploy the Cafe Application |
| 76 | + |
| 77 | +Create the coffee and the tea deployments and services: |
| 78 | + |
| 79 | + ```console |
| 80 | + kubectl create -f cafe.yaml |
| 81 | + ``` |
| 82 | + |
| 83 | +## Step 3 - Deploy the VirtualServer with custom listeners |
| 84 | + |
| 85 | +The VirtualServer in this example is set to use the listeners defined in the GlobalConfiguration resource |
| 86 | +that was deployed in Step 1. Below is the yaml of this example VirtualServer: |
| 87 | + |
| 88 | + ```yaml |
| 89 | + apiVersion: k8s.nginx.org/v1 |
| 90 | + kind: VirtualServer |
| 91 | + metadata: |
| 92 | + name: cafe |
| 93 | + spec: |
| 94 | + listener: |
| 95 | + http: ip-listener-1-http |
| 96 | + https: ip-listener-2-https |
| 97 | + host: cafe.example.com |
| 98 | + tls: |
| 99 | + secret: cafe-secret |
| 100 | + upstreams: |
| 101 | + - name: tea |
| 102 | + service: tea-svc |
| 103 | + port: 80 |
| 104 | + - name: coffee |
| 105 | + service: coffee-svc |
| 106 | + port: 80 |
| 107 | + routes: |
| 108 | + - path: /tea |
| 109 | + action: |
| 110 | + pass: tea |
| 111 | + - path: /coffee |
| 112 | + action: |
| 113 | + pass: coffee |
| 114 | + ``` |
| 115 | +
|
| 116 | +1. Create the secret with the TLS certificate and key: |
| 117 | +
|
| 118 | + ```console |
| 119 | + kubectl create -f cafe-secret.yaml |
| 120 | + ``` |
| 121 | + |
| 122 | +2. Create the VirtualServer resource: |
| 123 | + |
| 124 | + ```console |
| 125 | + kubectl create -f cafe-virtual-server.yaml |
| 126 | + ``` |
| 127 | + |
| 128 | +## Step 4 - Test the Configuration |
| 129 | + |
| 130 | +1. Check that the configuration has been successfully applied by inspecting the events of the VirtualServer and the GlobalConfiguration: |
| 131 | + |
| 132 | + ```console |
| 133 | + kubectl describe virtualserver cafe |
| 134 | + ``` |
| 135 | + |
| 136 | + Below you will see the events as well as the new `Listeners` field |
| 137 | + |
| 138 | + ```console |
| 139 | + . . . |
| 140 | + Spec: |
| 141 | + Host: cafe.example.com |
| 142 | + Listener: |
| 143 | + Http: ip-listener-1-http |
| 144 | + Https: ip-listener-2-https |
| 145 | + . . . |
| 146 | + Routes: |
| 147 | + . . . |
| 148 | + Events: |
| 149 | + Type Reason Age From Message |
| 150 | + ---- ------ ---- ---- ------- |
| 151 | + Normal AddedOrUpdated 2s nginx-ingress-controller Configuration for default/cafe was added or updated |
| 152 | + ``` |
| 153 | + |
| 154 | + ```console |
| 155 | + kubectl describe globalconfiguration nginx-configuration -n nginx-ingress |
| 156 | + ``` |
| 157 | + |
| 158 | + ```console |
| 159 | + . . . |
| 160 | + Spec: |
| 161 | + Listeners: |
| 162 | + ipv4: 127.0.0.1 |
| 163 | + Name: ip-listener-1-http |
| 164 | + Port: 8083 |
| 165 | + Protocol: HTTP |
| 166 | + ipv4: 127.0.0.2 |
| 167 | + ipv6: ::1 |
| 168 |
1004E
+ Name: ip-listener-2-https |
| 169 | + Port: 8443 |
| 170 | + Protocol: HTTP |
| 171 | + Ssl: true |
| 172 | + Events: |
| 173 | + Type Reason Age From Message |
| 174 | + ---- ------ ---- ---- ------- |
| 175 | + Normal Updated 14s nginx-ingress-controller GlobalConfiguration nginx-ingress/nginx-configuration was added or updated |
| 176 | + ``` |
| 177 | + |
| 178 | +2. Since the deployed VirtualServer is using ports `8083` and `8443` in this example. you can see that the specific ips and ports |
| 179 | +are set and listening by using the below commands: |
| 180 | + |
| 181 | + Access the NGINX Pod: |
| 182 | + |
| 183 | + ```console |
| 184 | + kubectl get pods -n nginx-ingress |
| 185 | + ``` |
| 186 | + |
| 187 | + ```text |
| 188 | + NAME READY STATUS RESTARTS AGE |
| 189 | + nginx-ingress-65cd79bb8f-crst4 1/1 Running 0 97s |
| 190 | + ``` |
| 191 | + |
| 192 | + ```console |
| 193 | + kubectl debug -it nginx-ingress-65cd79bb8f-crst4 --image=busybox:1.28 --target=nginx-ingress |
| 194 | + ``` |
| 195 | + |
| 196 | + ```console |
| 197 | + / # netstat -tulpn |
| 198 | + Active Internet connections (only servers) |
| 199 | + Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name |
| 200 | + tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN - |
| 201 | + tcp 0 0 127.0.0.1:8083 0.0.0.0:* LISTEN - |
| 202 | + tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN - |
| 203 | + tcp 0 0 127.0.0.2:8443 0.0.0.0:* LISTEN - |
| 204 | + tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN - |
| 205 | + tcp 0 0 :::8081 :::* LISTEN - |
| 206 | + tcp 0 0 :::8080 :::* LISTEN - |
| 207 | + tcp 0 0 :::8083 :::* LISTEN - |
| 208 | + tcp 0 0 ::1:8443 :::* LISTEN - |
| 209 | + tcp 0 0 :::443 :::* LISTEN - |
| 210 | + tcp 0 0 :::80 :::* LISTEN - |
| 211 | + tcp 0 0 :::9113 :::* LISTEN - |
| 212 | + ``` |
| 213 | + |
| 214 | + We can see here that the two IPv4s (`127.0.0.1:8083` and `127.0.0.2:8443`) and the one IPv6 (`::1:8443`) that are set and listening. |
| 215 | + |
| 216 | +3. Examine the NGINX config using the following command: |
| 217 | + |
| 218 | + ```console |
| 219 | + kubectl exec -it nginx-ingress-65cd79bb8f-crst4 -n nginx-ingress -- cat /etc/nginx/conf.d/vs_default_cafe.conf |
| 220 | + ``` |
| 221 | + |
| 222 | + ```console |
| 223 | + ... |
| 224 | + server { |
| 225 | + listen 127.0.0.1:8083; |
| 226 | + listen [::]:8083; |
| 227 | +
|
| 228 | +
|
| 229 | + server_name cafe.example.com; |
| 230 | +
|
| 231 | + set $resource_type "virtualserver"; |
| 232 | + set $resource_name "cafe"; |
| 233 | + set $resource_namespace "default"; |
| 234 | + listen 127.0.0.2:8443 ssl; |
| 235 | + listen [::1]:8443 ssl; |
| 236 | + ... |
| 237 | + ``` |
0 commit comments