Paket 2 · Praktik LanjutanModul J — Container Orchestration

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
KomponenFungsi
API serverPintu semua perintah
etcdState cluster
SchedulerMenempatkan Pod ke node
Controller managerMenjaga state sesuai keinginan
kubeletMenjalankan Pod di node
kube-proxyNetworking service

Objek inti

ObjekFungsi
PodUnit terkecil, satu atau beberapa container
DeploymentMengelola replika Pod dan rollout
ServiceIP/DNS stabil untuk Pod
IngressRouting HTTP dari luar
ConfigMapKonfigurasi non-rahasia
SecretData sensitif
NamespaceIsolasi logis
StatefulSetWorkload dengan identitas/storage stabil
Job/CronJobTugas 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> -- sh

Deployment

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-secrets
kubectl 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 production

Service

apiVersion: v1
kind: Service
metadata:
  name: app
  namespace: production
spec:
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 3000
  type: ClusterIP
TypeFungsi
ClusterIPInternal cluster
NodePortPort di setiap node
LoadBalancerLoad 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: 80

Ingress 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/app

Secret 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=production

Pisahkan environment dengan namespace dan RBAC.

Alur deploy

kubectl apply → Deployment membuat ReplicaSet → Pod dijadwalkan → Service mengarahkan trafik

Rolling update bawaan mengganti Pod bertahap.

Local cluster

ToolCatatan
k3sRingan, cocok untuk lab/edge
kindKubernetes dalam Docker
minikubeVM/container lokal
k3dkind dengan k3s

Contoh k3s:

curl -sfL https://get.k3s.io | sh -
sudo k3s kubectl get nodes

Praktik: 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 web

Ringkasan

  • Kubernetes mengelola container secara deklaratif.
  • Pod, Deployment, Service, dan Ingress adalah objek inti.
  • kubectl adalah alat utama.
  • Secret perlu perlindungan tambahan.
  • Rolling update dan scaling menjadi bawaan.

Selanjutnya

Siapkan produksi di Kubernetes Production Concepts.

On this page