Skip to main content
โšก Calmops

Kubernetes Basics and Go Applications

Kubernetes Basics and Go Applications

Introduction

Kubernetes orchestrates containerized applications at scale. This guide covers deploying Go applications to Kubernetes with best practices.

Core Concepts

Kubernetes Objects

  • Pod: Smallest deployable unit
  • Deployment: Manages pod replicas
  • Service: Exposes pods to network
  • ConfigMap: Configuration data
  • Secret: Sensitive data

Good: Kubernetes Manifests

Deployment Manifest

# โœ… GOOD: Kubernetes Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-app
  labels:
    app: go-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: go-app
  template:
    metadata:
      labels:
        app: go-app
    spec:
      containers:
      - name: go-app
        image: myregistry/go-app:1.0
        ports:
        - containerPort: 8080
        env:
        - name: LOG_LEVEL
          value: "info"
        - name: DATABASE_URL
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: url
        resources:
          requests:
            memory: "64Mi"
            cpu: "250m"
          limits:
            memory: "128Mi"
            cpu: "500m"
        livenessProbe:
          httpGet:
            path: /live
            port: 8080
          initialDelaySeconds: 10
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /ready
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 5

Service Manifest

# โœ… GOOD: Kubernetes Service
apiVersion: v1
kind: Service
metadata:
  name: go-app-service
spec:
  selector:
    app: go-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080
  type: LoadBalancer

ConfigMap and Secret

# โœ… GOOD: ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  LOG_LEVEL: "info"
  ENVIRONMENT: "production"

---
# โœ… GOOD: Secret
apiVersion: v1
kind: Secret
metadata:
  name: db-secret
type: Opaque
stringData:
  url: "postgres://user:password@db:5432/mydb"

Good: Kubectl Commands

Common Commands

# โœ… GOOD: Apply manifests
kubectl apply -f deployment.yaml

# โœ… GOOD: View deployments
kubectl get deployments

# โœ… GOOD: View pods
kubectl get pods

# โœ… GOOD: View services
kubectl get services

# โœ… GOOD: View logs
kubectl logs <pod-name>

# โœ… GOOD: Port forward
kubectl port-forward <pod-name> 8080:8080

# โœ… GOOD: Scale deployment
kubectl scale deployment go-app --replicas=5

# โœ… GOOD: Update image
kubectl set image deployment/go-app go-app=myregistry/go-app:2.0

# โœ… GOOD: Delete deployment
kubectl delete deployment go-app

Advanced Patterns

Horizontal Pod Autoscaling

# โœ… GOOD: HPA configuration
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: go-app-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: go-app
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70

Ingress Configuration

# โœ… GOOD: Ingress for routing
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: go-app-ingress
spec:
  rules:
  - host: api.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: go-app-service
            port:
              number: 80

Best Practices

1. Set Resource Limits

# โœ… GOOD: Resource limits
resources:
  requests:
    memory: "64Mi"
    cpu: "250m"
  limits:
    memory: "128Mi"
    cpu: "500m"

# โŒ BAD: No limits

2. Use Health Checks

# โœ… GOOD: Liveness and readiness probes
livenessProbe:
  httpGet:
    path: /live
    port: 8080
readinessProbe:
  httpGet:
    path: /ready
    port: 8080

# โŒ BAD: No probes

3. Use Secrets for Sensitive Data

# โœ… GOOD: Use secrets
env:
- name: DATABASE_URL
  valueFrom:
    secretKeyRef:
      name: db-secret
      key: url

# โŒ BAD: Hardcoded secrets
env:
- name: DATABASE_URL
  value: "postgres://user:password@db:5432/mydb"

Resources

Summary

Kubernetes provides powerful orchestration for Go applications. Use deployments for managing replicas, services for networking, and proper resource limits for efficiency. Health checks and autoscaling ensure reliability and optimal resource usage.

Comments