2024-08-30
Deploy and manage containerized applications with Kubernetes, including pods, services, deployments, and scaling strategies.
Kubernetes is the industry standard for container orchestration, enabling deployment, scaling, and management of containerized applications. This guide covers Kubernetes architecture, essential resources, deployment strategies, and production best practices.
kube-apiserver - API endpoint
etcd - Key-value store
kube-scheduler - Pod placement
kube-controller-manager - Controllers
cloud-controller-manager - Cloud provider APIs
kubelet - Container runtime manager
kube-proxy - Network proxy
Container runtime - Docker/containerd
Pods - Smallest deployable units
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp
labels:
app: webapp
spec:
replicas: 3
selector:
matchLabels:
app: webapp
template:
metadata:
labels:
app: webapp
spec:
containers:
- name: webapp
image: myapp:1.0.0
ports:
- containerPort: 3000
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "200m"
livenessProbe:
httpGet:
path: /health
port: 3000
initialDelaySeconds: 30
periodSeconds: 10
# service.yaml
apiVersion: v1
kind: Service
metadata:
name: webapp-service
spec:
selector:
app: webapp
type: ClusterIP
ports:
- port: 80
targetPort: 3000
---
# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: webapp-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
tls:
- hosts:
- app.example.com
secretName: tls-secret
rules:
- host: app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: webapp-service
port:
number: 80
# ConfigMap for non-sensitive data
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
DATABASE_HOST: postgres.default.svc.cluster.local
LOG_LEVEL: info
---
# Secret for sensitive data
apiVersion: v1
kind: Secret
metadata:
name: app-secret
type: Opaque
data:
DATABASE_PASSWORD: cGFzc3dvcmQxMjM= # base64 encoded
---
# Using in Pod
spec:
containers:
- name: app
envFrom:
- configMapRef:
name: app-config
- secretRef:
name: app-secret
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: webapp-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: webapp
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
spec:
replicas: 5
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 2 # Max pods above desired replicas
maxUnavailable: 1 # Max pods unavailable during update
# Deployment management
kubectl apply -f deployment.yaml
kubectl get deployments
kubectl rollout status deployment/webapp
kubectl rollout undo deployment/webapp
# Pod operations
kubectl get pods
kubectl describe pod pod-name
kubectl logs pod-name -f
kubectl exec -it pod-name -- /bin/bash
# Service discovery
kubectl get services
kubectl port-forward service/webapp 8080:80
# Debugging
kubectl get events --sort-by='.lastTimestamp'
kubectl top nodes
kubectl top pods
Resource Limits
Always set resource requests and limits to ensure proper scheduling and prevent resource starvation.
Health Checks
Implement liveness and readiness probes to ensure pods are healthy and ready to serve traffic.
Namespaces
Use namespaces to organize resources and implement resource quotas and network policies.
Published on 2024-08-30 • Category: DevOps
← Back to BlogFree online developer tools and utilities for encoding, formatting, generating, and analyzing data. No registration required - all tools work directly in your browser.
Built for developers, by developers. Privacy-focused and open source.
Free online tools for Base64 encoding, JSON formatting, URL encoding, hash generation, UUID creation, QR codes, JWT decoding, timestamp conversion, regex testing, and more.
© 2024 NarvikHub. All rights reserved.