Skip to content

rtcms/go-lang-k8s

Repository files navigation

Go Client-Server Kubernetes Application

A sample Go-based client-server application designed to demonstrate Kubernetes deployment patterns, load balancing, and horizontal pod autoscaling (HPA). This project showcases a simple but effective microservices architecture with a Go server and client components.

🏗️ Architecture Overview

The application consists of two main components:

  • Go Server: A TCP server that handles client connections and returns the server's hostname
  • Go Client: A shell script that continuously sends HTTP requests to the server

The architecture is deployed on Kubernetes using Helm charts with the following features:

  • Horizontal Pod Autoscaling (HPA) for automatic scaling
  • Service discovery and load balancing
  • Containerized deployment with Docker

Architecture Diagram

📁 Project Structure

go-lang-k8s/
├── charts/                    # Helm charts for Kubernetes deployment
│   └── go-web/
│       ├── Chart.yaml         # Main chart configuration
│       ├── client/            # Client subchart
│       └── server/            # Server subchart
├── client-src/
│   └── client.sh             # Client script that sends requests
├── server-src/
│   └── main.go               # Go server implementation
├── Dockerfile.client         # Client container definition
├── Dockerfile.server         # Server container definition
├── Makefile                  # Build and deployment automation
└── README.md                 # This file

🚀 Prerequisites

Before running this application, ensure you have the following installed:

  • Docker: For building container images
  • Minikube: For local Kubernetes cluster
  • Helm: For deploying the application
  • kubectl: For Kubernetes cluster management
  • Go: For building the server (optional, as we use Docker)

🛠️ Installation & Deployment

1. Clone the Repository

git clone https://github.com/rtcms/go-lang-k8s.git
cd go-lang-k8s

2. Start Minikube (if not already running)

minikube start

3. Deploy the Application

make run

This command will:

  • Build Docker images for both client and server
  • Install Helm dependencies
  • Deploy the application to Kubernetes

📋 Available Commands

The project includes a Makefile with the following commands:

Command Description
make build Build Docker images and prepare Helm charts
make run Build and deploy the application
make hpa Monitor Horizontal Pod Autoscaler status
make destroy Clean up Docker images and uninstall Helm release

🔍 Monitoring & Observability

Check Application Status

# View all resources
kubectl get all

# Check pod status
kubectl get pods

# View service endpoints
kubectl get endpoints

# Monitor HPA
kubectl get hpa

View Logs

# Server logs
kubectl logs -l app=go-web-server

# Client logs
kubectl logs -l app=go-web-client

Access the Application

# Port forward to access the service locally
kubectl port-forward svc/go-web-service 8080:8080

# Then access via browser or curl
curl http://localhost:8080

🔧 Application Components

Go Server (server-src/main.go)

  • Port: 8080
  • Protocol: TCP/HTTP
  • Functionality:
    • Accepts client connections
    • Returns server hostname in HTTP response
    • Implements mutex-based request handling
    • Simulates 5-second processing time per request

Go Client (client-src/client.sh)

  • Functionality:
    • Continuously sends HTTP requests to the server
    • Runs in background with 2-second intervals
    • Logs request counter and status

Kubernetes Resources

  • Deployments: Separate deployments for client and server
  • Services: ClusterIP service for internal communication
  • HPA: Horizontal Pod Autoscaler for automatic scaling
  • ConfigMaps/Secrets: For configuration management (if needed)

🐛 Troubleshooting

Common Issues

  1. Docker Registry Issues

    Sometimes the docker server of minikube might not point to the local docker registry.
    

    Solution: Ensure Docker is pointing to the correct registry or use eval $(minikube docker-env)

  2. Port Conflicts

    • Ensure port 8080 is not already in use
    • Check if other services are running on the same port
  3. Helm Installation Issues

    # Check Helm status
    helm list
    
    # Delete and reinstall if needed
    helm uninstall go-web
    make run
  4. Pod Startup Issues

    # Check pod events
    kubectl describe pod <pod-name>
    
    # Check pod logs
    kubectl logs <pod-name>

Debugging Commands

# Check cluster status
kubectl cluster-info

# Verify nodes
kubectl get nodes

# Check all resources
kubectl get all -A

# Describe specific resource
kubectl describe <resource-type> <resource-name>

🔄 Scaling

The application includes Horizontal Pod Autoscaling (HPA) configured to automatically scale the server based on CPU usage:

# Monitor HPA
kubectl get hpa --watch

# Manual scaling (if needed)
kubectl scale deployment go-web-server --replicas=3

🧹 Cleanup

To completely remove the application and clean up resources:

make destroy

This will:

  • Uninstall the Helm release
  • Remove Docker images
  • Clean up Kubernetes resources

📄 License

This project is open source and available under the MIT License.

About

Go Lang based client and server application that can be deployed on K8s using Helm charts

Topics

Resources

Stars

Watchers

Forks