What is Kubernetes?
Kubernetes (K8s) is an open-source container orchestration platform that automates deployment, scaling, and management of containerized applications.
Deployment Configuration
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-deployment
spec:
replicas: 3
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: my-api:latest
ports:
- containerPort: 3000
env:
- name: NODE_ENV
value: "production"
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: db-secret
key: url
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
Service Configuration
# service.yaml
apiVersion: v1
kind: Service
metadata:
name: api-service
spec:
selector:
app: api
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: LoadBalancer
Kubernetes Commands
# Apply deployment
kubectl apply -f deployment.yaml
# Get deployments
kubectl get deployments
# Get pods
kubectl get pods
# Describe pod
kubectl describe pod <pod-name>
# View logs
kubectl logs <pod-name>
# Scale deployment
kubectl scale deployment api-deployment --replicas=5
# Delete deployment
kubectl delete deployment api-deployment
Ingress Configuration
# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: api-ingress
spec:
rules:
- host: api.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: api-service
port:
number: 80
Best Practices
- Use ConfigMaps for configuration
- Use Secrets for sensitive data
- Set resource limits
- Use health checks (liveness/readiness probes)
- Implement horizontal pod autoscaling