Skip to content

Commit 018c2be

Browse files
author
Sarthak Agrawal
committed
add local nginx dev env
1 parent 235a4b2 commit 018c2be

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-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 echo 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=echo-server --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 echo 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+
# Simple NGINX Kubernetes Environment
2+
3+
Easy way to test NGINX proxying to a backend pod in Kubernetes.
4+
5+
## Quick Start
6+
7+
```bash
8+
# Deploy nginx and echo 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 echo 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 echo 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 echo 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 echo-backend {
7+
server echo-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://echo-backend;
19+
}
20+
}
21+
}

dev/nginx/nginx.yaml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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: echo-server
38+
namespace: default
39+
labels:
40+
app: echo-server
41+
spec:
42+
replicas: 1
43+
selector:
44+
matchLabels:
45+
app: echo-server
46+
template:
47+
metadata:
48+
labels:
49+
app: echo-server
50+
spec:
51+
containers:
52+
- name: echo
53+
image: ealen/echo-server:latest
54+
ports:
55+
- containerPort: 8080
56+
env:
57+
- name: PORT
58+
value: "8080"
59+
60+
---
61+
apiVersion: v1
62+
kind: Service
63+
metadata:
64+
name: echo-service
65+
namespace: default
66+
spec:
67+
selector:
68+
app: echo-server
69+
ports:
70+
- port: 8080
71+
targetPort: 8080

0 commit comments

Comments
 (0)