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
- Kubernetes Documentation: https://kubernetes.io/docs/
- Kubectl Cheatsheet: https://kubernetes.io/docs/reference/kubectl/cheatsheet/
- Go on Kubernetes: https://kubernetes.io/docs/setup/production-environment/container-runtimes/
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