A simple HTTP server built with Go that provides three endpoints with environment variable integration:
- Environment Variable Integration: Reads from
PORT,USER, andURLenvironment variables - Configurable Port: Server port can be set via
PORTenvironment variable (defaults to 8080) - Health Check Ready: Endpoints designed for Kubernetes health checks
- Secret Management: Demonstrates secure environment variable handling
/or/hello- Returns a hello message/env-echo- Echoes a message containing an environment variable (USER)/secret-echo- Echoes a message containing a secret environment variable (URL)
- Go 1.21 or later installed on your system
-
Navigate to the project directory:
cd my-app-devops -
Run the server:
go run main.go
The server will start on port 8080 by default.
-
To use a different port, set the
PORTenvironment variable:PORT=3000 go run main.go
Once the server is running, you can test the endpoints:
# Test on default port 8080
curl http://localhost:8080/hello
# Test on custom port 3000
PORT=3000 go run main.go &
curl http://localhost:3000/hello
kill %1# Test USER environment variable
USER=John curl http://localhost:8080/env-echo
# Test URL environment variable
URL=secret-data curl http://localhost:8080/secret-echocurl http://localhost:8080/hello
# or
curl http://localhost:8080/curl http://localhost:8080/env-echocurl http://localhost:8080/secret-echoPORT- Server port (default: 8080)USER- User environment variable (used in the env-echo endpoint)URL- Secret environment variable (used in the secret-echo endpoint)
The application is structured with:
- Handler Functions: Separate functions for each endpoint (
helloHandler,envEchoHandler,secretEchoHandler) - Environment Variable Handling: Graceful fallbacks when environment variables are not set
- HTTP Server Setup: Configurable port with environment variable support
- Error Handling: Proper HTTP response writing and error logging
To build an executable:
go build -o server main.go
./serverYour application is deployed using the following Kubernetes components:
- ConfigMap (
config-map.yaml): Stores application configuration (PORT, USER) - Secret (
secret-db.yaml): Stores sensitive database connection string - Deployment (
deployment.yaml): Manages application pods with 2 replicas - Service (
service.yaml): Exposes the application via NodePort
- Replicas: 2 (high availability)
- Resources:
- Requests: 100m CPU, 128Mi Memory
- Limits: 500m CPU, 512Mi Memory
- Port: 8080 (container and service)
- Image:
kanavghai/my-app:latest
- Kubernetes cluster (local: minikube, Docker Desktop, or cloud: GKE, EKS, AKS)
kubectlCLI tool installed and configured- Docker image built and pushed to a registry
First, build and push your Docker image to a registry:
# Build the image
docker build -t kanavghai/my-app:latest .
# Push to registry (replace with your registry)
docker push kanavghai/my-app:latest# Apply all Kubernetes manifests
kubectl apply -f config-map.yaml
kubectl apply -f secret-db.yaml
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml# 1. Create ConfigMap for application configuration
kubectl apply -f config-map.yaml
# 2. Create Secret for database connection
kubectl apply -f secret-db.yaml
# 3. Deploy the application
kubectl apply -f deployment.yaml
# 4. Create service for external access
kubectl apply -f service.yaml# Check deployment status
kubectl get deployments
# Check pods
kubectl get pods
# Check services
kubectl get services
# Check configmaps and secrets
kubectl get configmaps
kubectl get secrets# Get service information
kubectl get service my-app-service
# Get detailed service info
kubectl describe service my-app-service# Forward local port to service
kubectl port-forward service/my-app-service 8080:8080
# Then access: http://localhost:8080# Get minikube IP
minikube ip
# Get NodePort
kubectl get service my-app-service -o jsonpath='{.spec.ports[0].nodePort}'
# Access: http://<minikube-ip>:<nodePort># View pod logs
kubectl logs -l app=my-app
# Follow logs in real-time
kubectl logs -f -l app=my-app
# Describe deployment
kubectl describe deployment my-app-deployment
# Describe pods
kubectl describe pods -l app=my-app# Scale to 5 replicas
kubectl scale deployment my-app-deployment --replicas=5
# Check scaling status
kubectl get deployment my-app-deployment# Update image (trigger rolling update)
kubectl set image deployment/my-app-deployment my-app=kanavghai/my-app:v2
# Or update deployment YAML and reapply
kubectl apply -f deployment.yaml# Delete all resources
kubectl delete -f deployment.yaml
kubectl delete -f service.yaml
kubectl delete -f config-map.yaml
kubectl delete -f secret-db.yaml
# Or delete all at once
kubectl delete -f .# Check if image exists in registry
docker pull kanavghai/my-app:latest
# Verify image name in deployment.yaml matches your registry# Check pod status
kubectl get pods -l app=my-app
# View pod events
kubectl describe pod <pod-name>
# Check pod logs
kubectl logs <pod-name># Verify service is running
kubectl get service my-app-service
# Check endpoints
kubectl get endpoints my-app-service
# Test service connectivity
kubectl run test-pod --image=busybox --rm -it --restart=Never -- wget -O- http://my-app-service:8080/hello# Verify ConfigMap
kubectl get configmap my-app-config -o yaml
# Verify Secret
kubectl get secret db-secret -o yaml
# Check if pods are using the config
kubectl describe pod <pod-name> | grep -A 10 "Environment:"Your deployment includes readiness and liveness probes. Check them with:
# View deployment details
kubectl describe deployment my-app-deployment
# Check probe status in pod description
kubectl describe pod <pod-name># Monitor resource usage
kubectl top pods -l app=my-app
# Monitor resource usage by node
kubectl top nodes-
Build the Docker image:
docker build -t go-http-server . -
Run the container:
docker run -p 8080:8080 go-http-server
-
Run with custom environment variables:
docker run -p 8080:8080 -e USER=custom-user go-http-server
-
Build and run with Docker Compose:
docker-compose up --build
-
Run in background:
docker-compose up -d
-
Stop the service:
docker-compose down
-
View logs:
docker-compose logs -f