Kubernetes
Introduction
Kubernetes (K8s) is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It provides a framework to run distributed systems resiliently.
Key Features:
- Container orchestration
- Self-healing
- Horizontal scaling
- Load balancing
- Service discovery
- Automated rollouts/rollbacks
Architecture
Control Plane Components
Components:
- kube-apiserver
- etcd
- kube-scheduler
- kube-controller-manager
- cloud-controller-manager
Node Components
Components:
- kubelet
- kube-proxy
- Container runtime
Workload Resources
Pod Configuration
apiVersion: v1
kind: Pod
metadata:
name: web-app
labels:
app: web
spec:
containers:
- name: frontend
image: nginx:1.19
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
livenessProbe:
httpGet:
path: /healthz
port: 80
initialDelaySeconds: 3
periodSeconds: 3
Deployment Configuration
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
spec:
replicas: 3
selector:
matchLabels:
app: web
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.19
ports:
- containerPort: 80
Services & Networking
Service Types
# ClusterIP Service
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
type: ClusterIP
selector:
app: web
ports:
- port: 80
targetPort: 8080
---
# LoadBalancer Service
apiVersion: v1
kind: Service
metadata:
name: web-lb
spec:
type: LoadBalancer
selector:
app: web
ports:
- port: 80
targetPort: 8080
Ingress Configuration
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: example.com
http:
paths:
- path: /app
pathType: Prefix
backend:
service:
name: web-service
port:
number: 80
Storage
Persistent Volume
apiVersion: v1
kind: PersistentVolume
metadata:
name: app-data
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
storageClassName: standard
hostPath:
path: "/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: app-data-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
Best Practices
Resource Management
Best Practices:
- Set resource requests/limits
- Use namespaces
- Implement pod disruption budgets
- Configure HPA
- Use node affinity
- Monitor resource usage
Security
Security Measures:
- RBAC configuration
- Network policies
- Pod security policies
- Secret management
- Image scanning
- Audit logging