DEV Community

郑沛沛
郑沛沛

Posted on

Kubernetes for Beginners: Deploy Your First App in 15 Minutes

Kubernetes (K8s) orchestrates containers at scale. It sounds intimidating, but deploying your first app is surprisingly straightforward.

Core Concepts in 60 Seconds

  • Pod: Smallest unit — one or more containers running together
  • Deployment: Manages pods — handles scaling, updates, rollbacks
  • Service: Stable network endpoint for accessing pods
  • Namespace: Virtual cluster for organizing resources

Setup: Local K8s with minikube

# Install minikube
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube

# Start cluster
minikube start

# Verify
kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

Deploy Your First App

Step 1: Create a Deployment

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp
          image: myuser/myapp:v1
          ports:
            - containerPort: 8000
          resources:
            requests:
              memory: "128Mi"
              cpu: "100m"
            limits:
              memory: "256Mi"
              cpu: "500m"
          readinessProbe:
            httpGet:
              path: /health
              port: 8000
            initialDelaySeconds: 5
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /health
              port: 8000
            initialDelaySeconds: 15
            periodSeconds: 20
Enter fullscreen mode Exit fullscreen mode
kubectl apply -f deployment.yaml
kubectl get pods  # watch them come up
Enter fullscreen mode Exit fullscreen mode

Step 2: Expose with a Service

# service.yaml
apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  selector:
    app: myapp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8000
  type: LoadBalancer
Enter fullscreen mode Exit fullscreen mode
kubectl apply -f service.yaml
minikube service myapp-service  # opens in browser
Enter fullscreen mode Exit fullscreen mode

Step 3: ConfigMaps and Secrets

# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: myapp-config
data:
  LOG_LEVEL: "info"
  MAX_CONNECTIONS: "100"

---
# secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: myapp-secrets
type: Opaque
stringData:
  DATABASE_URL: "postgresql://user:pass@db:5432/mydb"
  API_KEY: "sk-secret-key-here"
Enter fullscreen mode Exit fullscreen mode

Use them in your deployment:

spec:
  containers:
    - name: myapp
      envFrom:
        - configMapRef:
            name: myapp-config
        - secretRef:
            name: myapp-secrets
Enter fullscreen mode Exit fullscreen mode

Scaling

# Manual scaling
kubectl scale deployment myapp --replicas=5

# Auto-scaling
kubectl autoscale deployment myapp --min=2 --max=10 --cpu-percent=70
Enter fullscreen mode Exit fullscreen mode

Rolling Updates

# Update image
kubectl set image deployment/myapp myapp=myuser/myapp:v2

# Watch rollout
kubectl rollout status deployment/myapp

# Rollback if something goes wrong
kubectl rollout undo deployment/myapp
Enter fullscreen mode Exit fullscreen mode

Essential kubectl Commands

kubectl get pods                    # list pods
kubectl get pods -w                 # watch pods in real-time
kubectl describe pod <name>         # detailed pod info
kubectl logs <pod-name>             # view logs
kubectl logs <pod-name> -f          # stream logs
kubectl exec -it <pod-name> -- bash # shell into pod
kubectl top pods                    # resource usage
kubectl get events --sort-by='.lastTimestamp'  # recent events
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  1. Start with minikube for local development
  2. Deployment + Service is the minimum viable K8s setup
  3. Always set resource requests and limits
  4. Use readiness and liveness probes for reliability
  5. ConfigMaps for config, Secrets for sensitive data

6. Rolling updates give you zero-downtime deployments

🚀 Level up your AI workflow! Check out my AI Developer Mega Prompt Pack — 80 battle-tested prompts for developers. $9.99

Top comments (0)