From 8cc5e2cb4363c7dbe380df5c7e4021d86da7c428 Mon Sep 17 00:00:00 2001 From: Sarthak Agrawal Date: Mon, 18 Aug 2025 12:24:30 -0700 Subject: [PATCH] Add local nginx dev environment --- dev/nginx/Makefile | 35 +++++++++++++++++++++++ dev/nginx/README.md | 39 +++++++++++++++++++++++++ dev/nginx/nginx.conf | 21 ++++++++++++++ dev/nginx/nginx.yaml | 68 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 163 insertions(+) create mode 100644 dev/nginx/Makefile create mode 100644 dev/nginx/README.md create mode 100644 dev/nginx/nginx.conf create mode 100644 dev/nginx/nginx.yaml diff --git a/dev/nginx/Makefile b/dev/nginx/Makefile new file mode 100644 index 0000000000..a819b9c875 --- /dev/null +++ b/dev/nginx/Makefile @@ -0,0 +1,35 @@ +PORT ?= 8080 + +.PHONY: help deploy cleanup logs update port-forward test + +help: ## Show this help + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}' + @echo "\nVariables:" + @echo " PORT=8080 Port for port-forwarding (default: 8080)" + +deploy: ## Deploy nginx and nginx-hello server to Kubernetes + kubectl create configmap nginx-config --from-file=nginx.conf --dry-run=client -o yaml | kubectl apply -f - + kubectl apply -f nginx.yaml + @echo "Waiting for pods to be ready..." + kubectl wait --for=condition=ready pod -l app=nginx-proxy --timeout=60s + kubectl wait --for=condition=ready pod -l app=nginx-hello --timeout=60s + +cleanup: ## Delete everything from Kubernetes + kubectl delete configmap nginx-config --ignore-not-found=true + kubectl delete -f nginx.yaml --ignore-not-found=true + +logs: ## Show nginx logs + kubectl logs -l app=nginx-proxy -f + +update: ## Update nginx config and restart pods + kubectl create configmap nginx-config --from-file=nginx.conf --dry-run=client -o yaml | kubectl apply -f - + kubectl rollout restart deployment/nginx-proxy + +port-forward: ## Port forward nginx pod to localhost:PORT (default: 8080) + kubectl port-forward deployment/nginx-proxy $(PORT):80 + +test: ## Test the setup via port-forward (run 'make port-forward' first) + @echo "Testing nginx health..." + @curl -s http://localhost:$(PORT)/health + @echo "\nTesting nginx-hello backend..." + @curl -s http://localhost:$(PORT)/ | head -5 diff --git a/dev/nginx/README.md b/dev/nginx/README.md new file mode 100644 index 0000000000..21311736a4 --- /dev/null +++ b/dev/nginx/README.md @@ -0,0 +1,39 @@ +# Deploy NGINX locally for testing + +This guide provides an easy way to deploy and experiment with NGINX configs locally + +## Quick Start + +```bash +# Deploy nginx and nginx-hello server +make deploy + +# Port-forward nginx pod (in separate terminal) +make port-forward + +# Test it works +make test + +# Clean up +make cleanup +``` + +## Available Commands + +- `make deploy` - Deploy nginx and nginx-hello server to Kubernetes +- `make port-forward` - Port forward nginx pod to localhost:8080 +- `make test` - Test the setup via curl (assuming port-forward is running) +- `make update` - Update config and restart pods +- `make logs` - View nginx logs +- `make cleanup` - Delete everything + +## How it works + +1. NGINX pod proxies all requests to nginx-hello server pod +2. Use port-forward to access nginx on localhost:8080 +3. Edit `nginx.conf` directly and run `make update` + +## URLs (after port-forward) + +- http://localhost:8080/health - NGINX health check +- http://localhost:8080/ - Proxied to nginx-hello server diff --git a/dev/nginx/nginx.conf b/dev/nginx/nginx.conf new file mode 100644 index 0000000000..62e595f679 --- /dev/null +++ b/dev/nginx/nginx.conf @@ -0,0 +1,21 @@ +events { + worker_connections 1024; +} + +http { + upstream nginx-hello-backend { + server nginx-hello-service:8080; + } + + server { + listen 80; + + location /health { + return 200 "OK\n"; + } + + location / { + proxy_pass http://nginx-hello-backend; + } + } +} diff --git a/dev/nginx/nginx.yaml b/dev/nginx/nginx.yaml new file mode 100644 index 0000000000..92141294c3 --- /dev/null +++ b/dev/nginx/nginx.yaml @@ -0,0 +1,68 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-proxy + namespace: default + labels: + app: nginx-proxy +spec: + replicas: 1 + selector: + matchLabels: + app: nginx-proxy + template: + metadata: + labels: + app: nginx-proxy + spec: + containers: + - name: nginx + image: nginx:alpine + ports: + - containerPort: 80 + volumeMounts: + - name: nginx-config + mountPath: /etc/nginx/nginx.conf + subPath: nginx.conf + volumes: + - name: nginx-config + configMap: + name: nginx-config + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: nginx-hello + namespace: default + labels: + app: nginx-hello +spec: + replicas: 1 + selector: + matchLabels: + app: nginx-hello + template: + metadata: + labels: + app: nginx-hello + spec: + containers: + - name: nginx-hello + image: nginxdemos/nginx-hello:plain-text + ports: + - containerPort: 8080 + +--- +apiVersion: v1 +kind: Service +metadata: + name: nginx-hello-service + namespace: default +spec: + selector: + app: nginx-hello + ports: + - port: 8080 + targetPort: 8080