Last active
September 11, 2023 16:45
-
-
Save tjl2/f215ffa0488aff46e2bfdf8365b5708f to your computer and use it in GitHub Desktop.
Nginx Ingress Controller Server Snippets Example
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
# This configuration is an example of how to get an Ingress to respond directly | |
# to a path, without routing to a service. This setup is being used on a DOKS | |
# service, with Nginx Ingress Controller installed and a Load Balancer in place. | |
# Those pieces can be setup easily with a 1-click install in the DO Marketplace: | |
# https://marketplace.digitalocean.com/apps/nginx-ingress-controller. I then | |
# grabbed the IP adress of the LB and pointed an A record at it. With that in | |
# place, this YAML can be applied with: | |
# kubectl apply -f ingress-nginx-server-snippets-example.yaml | |
# Once this is applied, you can test it with: | |
# curl http://<HOSTNAME>/anything # Will respond with echo from the container | |
# curl http://<HOSTNAME>/direct # Will respond with direct response from the Ingress | |
# Create a dummy deployment of mendhak/http-https-echo:26 to attach ingress to | |
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: http-echo | |
namespace: default | |
spec: | |
replicas: 1 | |
selector: | |
matchLabels: | |
app: http-echo | |
template: | |
metadata: | |
labels: | |
app: http-echo | |
spec: | |
containers: | |
- name: http-echo | |
image: mendhak/http-https-echo:26 | |
ports: | |
- name: http | |
containerPort: 8080 | |
--- | |
# Create a service for the above deployment | |
apiVersion: v1 | |
kind: Service | |
metadata: | |
name: http-echo-svc | |
namespace: default | |
spec: | |
selector: | |
app: http-echo | |
ports: | |
- protocol: TCP | |
port: 80 | |
targetPort: 8080 | |
--- | |
# Create an ingress that will route to the above service, but directly respond to /direct | |
apiVersion: networking.k8s.io/v1 | |
kind: Ingress | |
metadata: | |
name: ingress-direct | |
namespace: default | |
annotations: | |
nginx.ingress.kubernetes.io/server-snippet: | | |
location ~ .* { | |
return 200 'Hello, directly from the Ingress!'; | |
access_log off; | |
} | |
spec: | |
rules: | |
- host: ingress.tjl2.uk | |
http: | |
paths: | |
- path: / | |
pathType: Prefix | |
backend: | |
service: | |
name: http-echo-svc | |
port: | |
number: 80 | |
ingressClassName: nginx |
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
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: http-echo1 | |
namespace: default | |
spec: | |
replicas: 1 | |
selector: | |
matchLabels: | |
app: http-echo1 | |
template: | |
metadata: | |
labels: | |
app: http-echo1 | |
spec: | |
containers: | |
- name: http-echo1 | |
image: mendhak/http-https-echo:26 | |
ports: | |
- name: http | |
containerPort: 8080 | |
--- | |
# Create a service for the above deployment | |
apiVersion: v1 | |
kind: Service | |
metadata: | |
name: http-echo1-svc | |
namespace: default | |
spec: | |
selector: | |
app: http-echo1 | |
ports: | |
- protocol: TCP | |
port: 80 | |
targetPort: 8080 | |
--- | |
# Create an ingress that will route to the above service, but directly respond to /direct | |
apiVersion: networking.k8s.io/v1 | |
kind: Ingress | |
metadata: | |
name: http-echo1-ingress | |
namespace: default | |
spec: | |
rules: | |
- host: http-echo.tjl2.uk | |
http: | |
paths: | |
- path: / | |
pathType: Prefix | |
backend: | |
service: | |
name: http-echo1-svc | |
port: | |
number: 80 | |
ingressClassName: nginx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With the above configurations in place, here's how everything behaves:
Requests to http-echo.tjl2.uk respond with a response from the http-echo deployment
Requests to ingress.tjl2.uk respond with the ingress server-snippet