Skip to content

Instantly share code, notes, and snippets.

@tjl2
Last active September 11, 2023 16:45
Show Gist options
  • Save tjl2/f215ffa0488aff46e2bfdf8365b5708f to your computer and use it in GitHub Desktop.
Save tjl2/f215ffa0488aff46e2bfdf8365b5708f to your computer and use it in GitHub Desktop.
Nginx Ingress Controller Server Snippets Example
# 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
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
@tjl2
Copy link
Author

tjl2 commented Sep 11, 2023

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

curl http://http-echo.tjl2.uk/blah
{
  "path": "/blah",
  "headers": {
    "host": "http-echo.tjl2.uk",
    "x-request-id": "a57a29506e088896a388ff10c54108ac",
    "x-real-ip": "10.244.0.115",
    "x-forwarded-for": "10.244.0.115",
    "x-forwarded-host": "http-echo.tjl2.uk",
    "x-forwarded-port": "80",
    "x-forwarded-proto": "http",
    "x-forwarded-scheme": "http",
    "x-scheme": "http",
    "user-agent": "curl/8.1.2",
    "accept": "*/*"
  },
  "method": "GET",
  "body": "",
  "fresh": false,
  "hostname": "http-echo.tjl2.uk",
  "ip": "10.244.0.115",
  "ips": [
    "10.244.0.115"
  ],
  "protocol": "http",
  "query": {},
  "subdomains": [
    "http-echo"
  ],
  "xhr": false,
  "os": {
    "hostname": "http-echo1-6447d55685-zwxsl"
  },
  "connection": {}
}

Requests to ingress.tjl2.uk respond with the ingress server-snippet

curl http://ingress.tjl2.uk/blah
Hello, directly from the Ingress!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment