-
Couldn't load subscription status.
- Fork 2k
Custom listen ports document #3715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
359f8d9
Custom listen ports document
jasonwilliams14 5781093
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6136c2d
Merged updated URL
jasonwilliams14 11579c3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f07e170
Add DOC ID
jasonwilliams14 7d7722c
Fix URL link typo
jasonwilliams14 520e22d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 72cd706
Apply suggestions from code review
ADubhlaoich 630031f
Merge branch 'main' into docs/custom-listen-ports
ADubhlaoich b2e41a6
Update custom listen ports document for release
ADubhlaoich 4cbd837
Apply suggestions from code review
ADubhlaoich 9eb89d7
Update docs/content/tutorials/custom-listen-ports.md
jasonwilliams14 4fc0274
Merge branch 'main' into docs/custom-listen-ports
jasonwilliams14 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| --- | ||
| title: Customze ports for NGINX Ingress Controller | ||
| description: | | ||
| Customze ports for NGINX Ingress Controller | ||
| weight: 1800 | ||
| doctypes: ["concept"] | ||
| toc: true | ||
| docs: "DOCS-1191" | ||
| --- | ||
| ## Customizing the `listen` line in NGINX Ingress Controller. | ||
|
|
||
| This document will explain how to change the default ports that NGINX Ingress Controller is configured for, as well as add additional `listen` settings. For more information, please read the [NGINX Listen documentation](http://nginx.org/en/docs/http/ngx_http_core_module.html#listen). | ||
jasonwilliams14 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| ## Changing Default Ports | ||
|
|
||
| By default, NGINX Ingress Controller listens on ports 80 and 443. These ports can be changed easily, but modifying the `listen` ports for your NGINX Ingress resources will require the editing of .tmpl files. | ||
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| If you are using `ingress` resource you will need to modify: | ||
| - `nginx-plus-ingress.tmpl` if using NGINX Plus | ||
| - `nginx-ingress.tmpl` if using NGINX OSS | ||
|
|
||
| If you are using NGINX Ingress Controller CRDs (virtualServer): | ||
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - `nginx-plus-virtualserver.tmpl` for NGINX Plus | ||
| - `nginx-virtualserver.tmpl` if using NGINX OSS | ||
|
|
||
| For this example, we are going to use the `nginx-virtualserver.tmpl` to change the port from 80 to 85. | ||
| Here is a link to the directory for the `.tmpl` files: | ||
|
|
||
| [nginx-virtualserver template files](https://github.com/nginxinc/kubernetes-ingress/tree/main/internal/configs/version2) | ||
jasonwilliams14 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| Here we modify `nginx-virtualserver.tmpl` to change the port setting: | ||
jasonwilliams14 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ``` | ||
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| server { | ||
| listen 80{{ if $s.ProxyProtocol }} proxy_protocol{{ end }}; | ||
|
|
||
| server_name {{ $s.ServerName }}; | ||
|
|
||
| set $resource_type "virtualserver"; | ||
| set $resource_name "{{$s.VSName}}"; | ||
| set $resource_namespace "{{$s.VSNamespace}}"; | ||
| ``` | ||
| To change the listen port from `80` to `85`, we modify the `listen` line at the start of the server configuration block. | ||
|
|
||
| It would then look like this: | ||
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| server { | ||
| listen 85{{ if $s.ProxyProtocol }} proxy_protocol{{ end }}; | ||
|
|
||
| server_name {{ $s.ServerName }}; | ||
|
|
||
| set $resource_type "virtualserver"; | ||
| set $resource_name "{{$s.VSName}}"; | ||
| set $resource_namespace "{{$s.VSNamespace}}"; | ||
| ``` | ||
|
|
||
| Edit the file you need (per the example above). In my case, I edited, `nginx-plus-virtualserver.tmpl`: | ||
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| ## Rebuild your NGINX Ingress controller image | ||
|
|
||
| You will need to build your new NGINX Ingress controller image for the new port settings to take affect. | ||
| Once the image is built and pushed, make sure you update your deployment to point to the new image and deploy. | ||
| Once deployed, create a new `virtualServer` resource and then run `nginx -T` to see if the port takes affect. | ||
jasonwilliams14 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Ensure that your `deployment` and your `service` match up to the new port you configured in the templates. | ||
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Here is simple example of my `deployment` and my `service` matching to the new port that NGINX Ingress controller now listens on. | ||
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ``` | ||
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: nginx-ingress | ||
| namespace: nginx-ingress | ||
| spec: | ||
| replicas: 1 | ||
| selector: | ||
| matchLabels: | ||
| app: nginx-ingress | ||
| template: | ||
| metadata: | ||
| labels: | ||
| app: nginx-ingress | ||
| annotations: | ||
| prometheus.io/scrape: "true" | ||
| prometheus.io/port: "9113" | ||
| prometheus.io/scheme: http | ||
| spec: | ||
| serviceAccountName: nginx-ingress | ||
| containers: | ||
| - image: nginx/nginx-ingress:3.0.2 | ||
| imagePullPolicy: IfNotPresent | ||
| name: nginx-ingress | ||
| ports: | ||
| - name: http | ||
| containerPort: 85 | ||
| - name: https | ||
| containerPort: 443 | ||
| - name: readiness-port | ||
| containerPort: 8081 | ||
| - name: prometheus | ||
| containerPort: 9113 | ||
| readinessProbe: | ||
| httpGet: | ||
| path: /nginx-ready | ||
| port: readiness-port | ||
| periodSeconds: 1 | ||
| securityContext: | ||
| ``` | ||
|
|
||
| Notice that now, my `http` port is set to `85`, which reflects the change I made in the template file. | ||
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Here is my `service` file: | ||
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ``` | ||
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| apiVersion: v1 | ||
| kind: Service | ||
| metadata: | ||
| name: nginx-ingress | ||
| namespace: nginx-ingress | ||
| spec: | ||
| externalTrafficPolicy: Local | ||
| type: LoadBalancer | ||
| ports: | ||
| - port: 80 | ||
| targetPort: 85 | ||
| protocol: TCP | ||
| name: http | ||
| - port: 8443 | ||
| targetPort: 8443 | ||
| protocol: TCP | ||
| name: https | ||
| selector: | ||
| app: nginx-ingress | ||
| ``` | ||
|
|
||
| Since NGINX Ingress controller is now listening on ports 85 and 8443, we modify the `targetPort` in the NGINX Ingress controller service, to match what we have changed in our deployment, to ensure traffic will be sent to the proper port. | ||
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| The key part above is the `targetPort` section. Since I change NGINX Ingress to listen on port 85, I need to match that in the service. That way, requests will be sent to NGINX Ingress controller on port 85 instead of the default value which is port 80. | ||
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| If you view the `NGINX` configuration .conf file using `nginx -T`, you should see the port you defined in the .template file, now is set on the `listen` line. | ||
jasonwilliams14 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Here is an example output of the `NGINX` configuration that is now generated: | ||
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```bash | ||
| k exec -it -n nginx-ingress nginx-ingress-54bffd78d9-v7bns -- nginx -T | ||
ADubhlaoich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| ``` | ||
ADubhlaoich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| server { | ||
| listen 85; | ||
| listen [::]:85; | ||
| listen 8011; | ||
|
|
||
| server_name cafe.example.com; | ||
|
|
||
| set $resource_type "virtualserver"; | ||
| set $resource_name "cafe"; | ||
| set $resource_namespace "default"; | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.