🔧 DevOps
Kubernetes Deployment
Last updated: 2025-09-25 02:29:54
Kubernetes Container Orchestration
Kubernetes automates deployment, scaling, and management of containerized applications.
Basic Kubernetes Objects
# Pod definition (pod.yaml)
apiVersion: v1
kind: Pod
metadata:
name: my-app-pod
labels:
app: my-app
spec:
containers:
- name: app-container
image: my-app:latest
ports:
- containerPort: 3000
env:
- name: NODE_ENV
value: "production"
- name: DB_HOST
valueFrom:
secretKeyRef:
name: db-secret
key: host
# Service definition (service.yaml)
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: LoadBalancer
Deployments and Scaling
# Deployment definition (deployment.yaml)
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: app-container
image: my-app:latest
ports:
- containerPort: 3000
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
livenessProbe:
httpGet:
path: /health
port: 3000
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 3000
initialDelaySeconds: 5
periodSeconds: 5
# Horizontal Pod Autoscaler (hpa.yaml)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: my-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app-deployment
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
ConfigMaps and Secrets
# ConfigMap (configmap.yaml)
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
app.properties: |
server.port=3000
app.name=MyApp
app.version=1.0.0
nginx.conf: |
server {
listen 80;
location / {
proxy_pass http://localhost:3000;
}
}
# Secret (secret.yaml)
apiVersion: v1
kind: Secret
metadata:
name: app-secrets
type: Opaque
data:
database-url: cG9zdGdyZXM6Ly91c2VyOnBhc3N3b3JkQGRiOjU0MzIvbXlkYg==
api-key: YWJjZGVmZ2hpams=
# Using ConfigMap and Secret in Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-deployment
spec:
template:
spec:
containers:
- name: app-container
image: my-app:latest
env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: app-secrets
key: database-url
- name: API_KEY
valueFrom:
secretKeyRef:
name: app-secrets
key: api-key
volumeMounts:
- name: config-volume
mountPath: /app/config
volumes:
- name: config-volume
configMap:
name: app-config
kubectl Commands
# Apply configurations
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f configmap.yaml
kubectl apply -f secret.yaml
# Get resources
kubectl get pods
kubectl get services
kubectl get deployments
kubectl get nodes
# Describe resources
kubectl describe pod my-app-pod
kubectl describe service my-app-service
# Scale deployment
kubectl scale deployment my-app-deployment --replicas=5
# Rolling update
kubectl set image deployment/my-app-deployment app-container=my-app:v2
# Check rollout status
kubectl rollout status deployment/my-app-deployment
# Rollback deployment
kubectl rollout undo deployment/my-app-deployment
# View logs
kubectl logs -f deployment/my-app-deployment
kubectl logs -f pod/my-app-pod
# Execute commands in pod
kubectl exec -it my-app-pod -- /bin/bash
# Port forwarding for debugging
kubectl port-forward service/my-app-service 8080:80
# Delete resources
kubectl delete -f deployment.yaml
kubectl delete pod my-app-pod
kubectl delete service my-app-service