Skip to content

Commit 4e366e8

Browse files
authored
Add local nginx dev env (#3752)
Problem: Developers found it difficult and annoying to test local nginx configuration.. Solution: I created a solution that creates a simple nginx pod with conf in a ConfigMap and a Makefile for easy use. Testing: Manually tested Closes #1502
1 parent 50597ae commit 4e366e8

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

dev/nginx/Makefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
PORT ?= 8080
2+
3+
.PHONY: help deploy cleanup logs update port-forward test
4+
5+
help: ## Show this help
6+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'
7+
@echo "\nVariables:"
8+
@echo " PORT=8080 Port for port-forwarding (default: 8080)"
9+
10+
deploy: ## Deploy nginx and nginx-hello server to Kubernetes
11+
kubectl create configmap nginx-config --from-file=nginx.conf --dry-run=client -o yaml | kubectl apply -f -
12+
kubectl apply -f nginx.yaml
13+
@echo "Waiting for pods to be ready..."
14+
kubectl wait --for=condition=ready pod -l app=nginx-proxy --timeout=60s
15+
kubectl wait --for=condition=ready pod -l app=nginx-hello --timeout=60s
16+
17+
cleanup: ## Delete everything from Kubernetes
18+
kubectl delete configmap nginx-config --ignore-not-found=true
19+
kubectl delete -f nginx.yaml --ignore-not-found=true
20+
21+
logs: ## Show nginx logs
22+
kubectl logs -l app=nginx-proxy -f
23+
24+
update: ## Update nginx config and restart pods
25+
kubectl create configmap nginx-config --from-file=nginx.conf --dry-run=client -o yaml | kubectl apply -f -
26+
kubectl rollout restart deployment/nginx-proxy
27+
28+
port-forward: ## Port forward nginx pod to localhost:PORT (default: 8080)
29+
kubectl port-forward deployment/nginx-proxy $(PORT):80
30+
31+
test: ## Test the setup via port-forward (run 'make port-forward' first)
32+
@echo "Testing nginx health..."
33+
@curl -s http://localhost:$(PORT)/health
34+
@echo "\nTesting nginx-hello backend..."
35+
@curl -s http://localhost:$(PORT)/ | head -5

dev/nginx/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Deploy NGINX locally for testing
2+
3+
This guide provides an easy way to deploy and experiment with NGINX configs locally
4+
5+
## Quick Start
6+
7+
```bash
8+
# Deploy nginx and nginx-hello server
9+
make deploy
10+
11+
# Port-forward nginx pod (in separate terminal)
12+
make port-forward
13+
14+
# Test it works
15+
make test
16+
17+
# Clean up
18+
make cleanup
19+
```
20+
21+
## Available Commands
22+
23+
- `make deploy` - Deploy nginx and nginx-hello server to Kubernetes
24+
- `make port-forward` - Port forward nginx pod to localhost:8080
25+
- `make test` - Test the setup via curl (assuming port-forward is running)
26+
- `make update` - Update config and restart pods
27+
- `make logs` - View nginx logs
28+
- `make cleanup` - Delete everything
29+
30+
## How it works
31+
32+
1. NGINX pod proxies all requests to nginx-hello server pod
33+
2. Use port-forward to access nginx on localhost:8080
34+
3. Edit `nginx.conf` directly and run `make update`
35+
36+
## URLs (after port-forward)
37+
38+
- http://localhost:8080/health - NGINX health check
39+
- http://localhost:8080/ - Proxied to nginx-hello server

dev/nginx/nginx.conf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
events {
2+
worker_connections 1024;
3+
}
4+
5+
http {
6+
upstream nginx-hello-backend {
7+
server nginx-hello-service:8080;
8+
}
9+
10+
server {
11+
listen 80;
12+
13+
location /health {
14+
return 200 "OK\n";
15+
}
16+
17+
location / {
18+
proxy_pass http://nginx-hello-backend;
19+
}
20+
}
21+
}

dev/nginx/nginx.yaml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
apiVersion: apps/v1
3+
kind: Deployment
4+
metadata:
5+
name: nginx-proxy
6+
namespace: default
7+
labels:
8+
app: nginx-proxy
9+
spec:
10+
replicas: 1
11+
selector:
12+
matchLabels:
13+
app: nginx-proxy
14+
template:
15+
metadata:
16+
labels:
17+
app: nginx-proxy
18+
spec:
19+
containers:
20+
- name: nginx
21+
image: nginx:alpine
22+
ports:
23+
- containerPort: 80
24+
volumeMounts:
25+
- name: nginx-config
26+
mountPath: /etc/nginx/nginx.conf
27+
subPath: nginx.conf
28+
volumes:
29+
- name: nginx-config
30+
configMap:
31+
name: nginx-config
32+
33+
---
34+
apiVersion: apps/v1
35+
kind: Deployment
36+
metadata:
37+
name: nginx-hello
38+
namespace: default
39+
labels:
40+
app: nginx-hello
41+
spec:
42+
replicas: 1
43+
selector:
44+
matchLabels:
45+
app: nginx-hello
46+
template:
47+
metadata:
48+
labels:
49+
app: nginx-hello
50+
spec:
51+
containers:
52+
- name: nginx-hello
53+
image: nginxdemos/nginx-hello:plain-text
54+
ports:
55+
- containerPort: 8080
56+
57+
---
58+
apiVersion: v1
59+
kind: Service
60+
metadata:
61+
name: nginx-hello-service
62+
namespace: default
63+
spec:
64+
selector:
65+
app: nginx-hello
66+
ports:
67+
- port: 8080
68+
targetPort: 8080

0 commit comments

Comments
 (0)