Kubernetes untuk Developer Go
Sebagai developer Go yang sudah beberapa kali deploy ke Kubernetes, saya bisa bilang bahwa Go dan Kubernetes itu combination yang sangat natural. Go menghasilkan binary statis yang kecil, Kubernetes butuh container yang efisien, dan keduanya dirancang untuk cloud-native applications.
Di panduan ini, saya akan cover dari containerization sampai deployment ke production cluster. Semua berdasarkan pengalaman langsung di beberapa project di Bandung.
Dockerfile yang Optimal untuk Go
Go adalah bahasa yang sangat cocok untuk Docker karena bisa menghasilkan binary statis. Multi-stage build membuat image kita sangat kecil:
# Build stage
FROM golang:1.22-alpine AS builder
WORKDIR /app
# Cache dependencies
COPY go.mod go.sum ./
RUN go mod download
# Copy source code
COPY . .
# Build binary
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
go build -ldflags="-w -s" -o /app/server ./cmd/server
# Runtime stage
FROM scratch
# Copy CA certificates for HTTPS
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# Copy binary
COPY --from=builder /app/server /server
# Copy config
COPY --from=builder /app/config /config
EXPOSE 8080
ENTRYPOINT ["/server"]Dengan FROM scratch, image kita bisa sekecil 10-15MB. Bandingkan dengan image berbasis Ubuntu yang bisa 200MB lebih.
Kubernetes Manifest
Berikut deployment manifest yang production-ready:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
namespace: production
labels:
app: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: ghcr.io/myorg/myapp:latest
ports:
- containerPort: 8080
# Resource limits
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "500m"
# Health checks
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 10
periodSeconds: 15
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
# Environment variables
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: db-secrets
key: url
- name: LOG_LEVEL
value: "info"
# Graceful shutdown
lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "sleep 5"]
terminationGracePeriodSeconds: 30HPA untuk Auto-Scaling
Horizontal Pod Autoscaler memungkinkan pod di-scale otomatis berdasarkan metrics:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: myapp-hpa
namespace: production
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myapp
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80Service dan Ingress
Kita butuh Service untuk internal load balancing dan Ingress untuk external access. Saya biasanya gunakan NGINX Ingress Controller dengan cert-manager untuk auto TLS certificate. Untuk production, jangan lupa untuk setup NetworkPolicy untuk membatasi traffic antar pods.
Monitoring di Kubernetes
Monitoring sangat penting. Saya selalu setup Prometheus + Grafana untuk metrics monitoring, dan Jaeger untuk distributed tracing. Dengan Go yang sudah expose /metrics endpoint via prometheus client library, monitoring setup jadi sangat mudah.