Set up traefik ingressroute with prefixstrip middleware to avoid extra network hop

This commit is contained in:
sid palas
2023-02-12 09:35:28 -05:00
parent 44e5f69fd8
commit 1e72d407dc
4 changed files with 116 additions and 48 deletions

View File

@ -0,0 +1,71 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: client-react-nginx
labels:
app: client-react-nginx
spec:
replicas: 1
selector:
matchLabels:
app: client-react-nginx
template:
metadata:
labels:
app: client-react-nginx
spec:
containers:
- image: sidpalas/devops-directive-docker-course-client-react-nginx:5
name: client-react-nginx
ports:
- containerPort: 80
protocol: TCP
readinessProbe:
failureThreshold: 3
httpGet:
path: /ping
port: 80
resources:
limits:
memory: 100Mi
requests:
cpu: 50m
memory: 100Mi
securityContext:
allowPrivilegeEscalation: false
privileged: false
volumeMounts:
- mountPath: /etc/nginx/conf.d
name: nginx-conf
securityContext:
seccompProfile:
type: RuntimeDefault
volumes:
- configMap:
defaultMode: 420
name: nginx-conf
name: nginx-conf
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-conf
data:
default.conf: |-
server {
listen 80;
location /ping {
access_log off;
add_header 'Content-Type' 'text/plain';
return 200 "pong";
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
include /etc/nginx/extra-conf.d/*.conf;
}

View File

@ -0,0 +1,45 @@
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: primary
spec:
entryPoints:
- web
routes:
- kind: Rule
match: Host(`docker-course-kubernetes.devopsdirective.com`)
services:
- kind: Service
name: client-react-nginx
namespace: default
port: 80
scheme: http
- kind: Rule
match: Host(`docker-course-kubernetes.devopsdirective.com`) && PathPrefix(`/api/node`)
middlewares:
- name: strip-api-prefixes
services:
- kind: Service
name: api-node
port: 3000
scheme: http
- kind: Rule
match: Host(`docker-course-kubernetes.devopsdirective.com`) && PathPrefix(`/api/golang`)
middlewares:
- name: strip-api-prefixes
services:
- kind: Service
name: api-golang
port: 8080
scheme: http
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: strip-api-prefixes
spec:
stripPrefix:
forceSlash: false
prefixes:
- /api/node
- /api/golang

View File

@ -1,11 +0,0 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: primary
namespace: default
spec:
defaultBackend:
service:
name: client-react-nginx
port:
number: 80

View File

@ -1,37 +0,0 @@
server {
listen 80;
# Kubernetes dns server
resolver kube-dns.kube-system.svc.cluster.local valid=5s;
location /ping {
access_log off;
add_header 'Content-Type' 'text/plain';
return 200 "pong";
}
location /api/golang/ {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
set $api_golang_upstream api-golang.default.svc.cluster.local:8080/;
proxy_pass http://$api_golang_upstream;
}
location /api/node/ {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
set $api_node_upstream api-node.default.svc.cluster.local:3000/;
proxy_pass http://$api_node_upstream;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
include /etc/nginx/extra-conf.d/*.conf;
}