Kubernetes Fundamentals
Memahami arsitektur Kubernetes, objek inti, kubectl, Deployment, Service, Ingress, dan alur deploy aplikasi.
Kubernetes adalah platform orkestrasi container. Ia menjadwalkan container ke node, menjaga jumlah replika, dan menyediakan service discovery serta scaling.
Arsitektur
Control Plane
├── kube-apiserver
├── etcd
├── kube-scheduler
└── controller-manager
│
▼
Worker Nodes
├── kubelet
├── container runtime
└── kube-proxy| Komponen | Fungsi |
|---|---|
| API server | Pintu semua perintah |
| etcd | State cluster |
| Scheduler | Menempatkan Pod ke node |
| Controller manager | Menjaga state sesuai keinginan |
| kubelet | Menjalankan Pod di node |
| kube-proxy | Networking service |
Objek inti
| Objek | Fungsi |
|---|---|
| Pod | Unit terkecil, satu atau beberapa container |
| Deployment | Mengelola replika Pod dan rollout |
| Service | IP/DNS stabil untuk Pod |
| Ingress | Routing HTTP dari luar |
| ConfigMap | Konfigurasi non-rahasia |
| Secret | Data sensitif |
| Namespace | Isolasi logis |
| StatefulSet | Workload dengan identitas/storage stabil |
| Job/CronJob | Tugas sekali/terjadwal |
kubectl dasar
kubectl version --client
kubectl cluster-info
kubectl get nodes
kubectl get pods -A
kubectl get deployments
kubectl get svc
kubectl describe pod <pod>
kubectl logs <pod>
kubectl exec -it <pod> -- shDeployment
deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
namespace: production
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: app
image: ghcr.io/org/app:1.4.0
ports:
- containerPort: 3000
envFrom:
- secretRef:
name: app-secretskubectl apply -f deployment.yaml
kubectl get pods -n production
kubectl rollout status deployment/app -n production
kubectl scale deployment/app --replicas=5 -n production
kubectl rollout undo deployment/app -n productionService
apiVersion: v1
kind: Service
metadata:
name: app
namespace: production
spec:
selector:
app: web
ports:
- port: 80
targetPort: 3000
type: ClusterIP| Type | Fungsi |
|---|---|
| ClusterIP | Internal cluster |
| NodePort | Port di setiap node |
| LoadBalancer | Load balancer cloud |
Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: app
namespace: production
annotations:
cert-manager.io/cluster-issuer: letsencrypt
spec:
ingressClassName: nginx
tls:
- hosts: [app.example.com]
secretName: app-tls
rules:
- host: app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: app
port:
number: 80Ingress membutuhkan Ingress Controller (mis. ingress-nginx).
ConfigMap dan Secret
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_ENV: production
LOG_LEVEL: info
---
apiVersion: v1
kind: Secret
metadata:
name: app-secrets
type: Opaque
stringData:
DATABASE_URL: postgres://app:pass@db:5432/appSecret Kubernetes bukan vault
Secret disimpan base64, bukan otomatis terenkripsi. Aktifkan encryption at rest, RBAC ketat, dan pertimbangkan external secret manager.
Namespace
kubectl create namespace staging
kubectl get all -n staging
kubectl config set-context --current --namespace=productionPisahkan environment dengan namespace dan RBAC.
Alur deploy
kubectl apply → Deployment membuat ReplicaSet → Pod dijadwalkan → Service mengarahkan trafikRolling update bawaan mengganti Pod bertahap.
Local cluster
| Tool | Catatan |
|---|---|
| k3s | Ringan, cocok untuk lab/edge |
| kind | Kubernetes dalam Docker |
| minikube | VM/container lokal |
| k3d | kind dengan k3s |
Contoh k3s:
curl -sfL https://get.k3s.io | sh -
sudo k3s kubectl get nodesPraktik: deploy Nginx
kubectl create deployment web --image=nginx:alpine --replicas=2
kubectl expose deployment web --port=80 --target-port=80
kubectl get pods,svc
kubectl port-forward svc/web 8080:80
curl -I http://localhost:8080
kubectl scale deployment web --replicas=4
kubectl delete deployment web
kubectl delete svc webRingkasan
- Kubernetes mengelola container secara deklaratif.
- Pod, Deployment, Service, dan Ingress adalah objek inti.
kubectladalah alat utama.- Secret perlu perlindungan tambahan.
- Rolling update dan scaling menjadi bawaan.
Selanjutnya
Siapkan produksi di Kubernetes Production Concepts.
Server Automation dengan Ansible
Mengotomasi konfigurasi server dengan Ansible: inventory, playbook, module, role, template, handler, dan vault.
Kubernetes Production Concepts
Menyiapkan Kubernetes produksi: resource requests/limits, probes, HPA, PDB, storage, RBAC, network policy, observability, dan backup.