Kubernetes Basics
Kubernetes (K8s) orchestrates containerized applications at scale. Let's understand the fundamentals.
Core Concepts
Pods
The smallest deployable unit:
# pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: app
image: myapp:1.0
ports:
- containerPort: 3000
Deployments
Manage pod replicas and updates:
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: app
image: myapp:1.0
ports:
- containerPort: 3000
resources:
limits:
memory: "128Mi"
cpu: "500m"
Services
Expose pods to network:
# service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
ports:
- port: 80
targetPort: 3000
type: LoadBalancer
Essential Commands
# Apply configuration
kubectl apply -f deployment.yaml
# Get resources
kubectl get pods
kubectl get deployments
kubectl get services
# Describe a resource
kubectl describe pod my-app
# View logs
kubectl logs my-app-pod
# Execute command in pod
kubectl exec -it my-app-pod -- /bin/sh
# Scale deployment
kubectl scale deployment my-app --replicas=5
ConfigMaps and Secrets
Manage configuration:
# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
DATABASE_HOST: "db.example.com"
LOG_LEVEL: "info"
---
# secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: app-secrets
type: Opaque
data:
API_KEY: YXBpLWtleS12YWx1ZQ== # base64 encoded
Use in deployment:
spec:
containers:
- name: app
envFrom:
- configMapRef:
name: app-config
- secretRef:
name: app-secrets
Ingress
Route external traffic:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-app-service
port:
number: 80
Conclusion
Kubernetes is essential for modern deployments. Start small, learn the basics, then explore advanced features.