Lightweight Kubernetes
2 Weeks - Complete Guide
Kubernetes production-ready dalam ~40MB - hanya 512MB RAM!
βββββββββββββββββββββββββββββββββββ
β k3s Server β
β API Server, Scheduler β
β Controller Manager β
β Kubelet, Kube-proxy β
β CoreDNS, Traefik, Service LB β
β SQLite (embedded) β
βββββββββββββββββββββββββββββββββββ
k3s server βββΊ k3s agent βββΊ k3s agent
(master) (worker) (worker)
# Install dan start
curl -sfL https://get.k3s.io | sh -
# Cek status
sudo k3s kubectl get nodes
# Semua pods
sudo k3s kubectl get pods -A
curl -sfL https://get.k3s.io | \
INSTALL_K3S_VERSION=v1.28.4+k3s1 sh -
/usr/local/bin/k3s-uninstall.sh
curl -sfL https://get.k3s.io | \
K3S_KUBECONFIG_MODE="644" sh -
curl -sfL https://get.k3s.io | \
INSTALL_K3S_ARCH=armhf sh -
curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash
# Create cluster
k3d cluster create mycluster
# With options
k3d cluster create mycluster \
--servers 1 \
--agents 3 \
--port "8080:80"
# List clusters
k3d cluster list
# Delete cluster
k3d cluster delete mycluster
docker run -d --name k3s \
--privileged \
rancher/k3s:latest server
~/.kube/config
# Export kubeconfig
sudo k3s kubeconfig cluster-wide > ~/.kube/config
# Dengan mode
sudo k3s server --write-kubeconfig-mode 644
export KUBECONFIG=/path/to/kubeconfig
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
# Alias
alias k=kubectl
# Nodes
kubectl get nodes
kubectl get nodes -o wide
# Pods
kubectl get pods -A
kubectl get pods -n namespace
# Describe
kubectl describe pod nginx -n namespace
# Logs
kubectl logs pod-name -n namespace
# Exec
kubectl exec -it pod-name -n namespace -- /bin/sh
kubectl create ns myapp
kubectl get ns
kubectl config set-context --current --namespace=myapp
kubectl run nginx --image=nginx --restart=Never
kubectl create deployment nginx --image=nginx --replicas=3
# Atau YAML
kubectl apply -f deployment.yaml
kubectl scale deployment nginx --replicas=5
# Auto-scale
kubectl autoscale deployment nginx --min=2 --max=10
kubectl expose deployment nginx --port=80
| Type | Description |
|---|---|
| ClusterIP | Internal only |
| NodePort | Expose via node port |
| LoadBalancer | External LB |
| ExternalName | CNAME alias |
# ClusterIP
kubectl expose deployment nginx --port=80
# NodePort
kubectl expose deployment nginx --type=NodePort --port=80
# LoadBalancer
kubectl expose deployment nginx --type=LoadBalancer --port=80
kubectl get pods -n kube-system | grep traefik
kubectl apply -f - <
# /etc/hosts
127.0.0.1 myapp.local
kubectl create configmap myconfig \
--from-literal=key1=value1
# Use in Pod
env:
- name: KEY1
valueFrom:
configMapKeyRef:
name: myconfig
key: key1
kubectl create secret generic mysecret \
--from-literal=password=secret123
# Use in Pod
env:
- name: PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
curl -fsSL https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
# Add repo
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
# Install chart
helm install my-nginx bitnami/nginx
# List releases
helm list
# Upgrade
helm upgrade my-nginx bitnami/nginx
# Uninstall
helm uninstall my-nginx
kubectl get sc
kubectl apply -f - <
volumes:
- name: data
persistentVolumeClaim:
claimName: mypvc
# Di master: ambil token
sudo cat /var/lib/rancher/k3s/server/node-token
# Di worker:
curl -sfL https://get.k3s.io | \
K3S_URL=https://master-ip:6443 \
K3S_TOKEN=TOKEN_HERE \
sh -
# 3 servers minimum untuk HA
curl -sfL https://get.k3s.io | \
K3S_URL=https://server-ip:6443 \
K3S_TOKEN=TOKEN \
K3S_FLAGS="server --cluster-init" \
sh -
kubectl describe pod nginx
kubectl logs nginx -f
kubectl get events --sort-by='.lastTimestamp'
kubectl cluster-info
kubectl top nodes
| Issue | Solution |
|---|---|
| Pod Pending | Cek resources |
| ImagePullBackOff | |
| CrashLoopBackOff | kubectl logs |
| Service unreachable | Cek endpoints |
curl -sSL https://get.k3sup.dev | sh -
brew install k3sup
# Create server
k3sup server --ip 192.168.1.100 --user root
# Join workers
k3sup join --ip 192.168.1.101 \
--server-ip 192.168.1.100 --user root
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
# Install
curl -sfL https://get.k3s.io | sh -
# Nodes
kubectl get nodes
# Deploy
kubectl create deployment nginx --image=nginx
# Expose
kubectl expose deployment nginx --port=80
# Scale
kubectl scale deployment nginx --replicas=3
# Logs
kubectl logs -f deployment/nginx
# Delete
kubectl delete deployment nginx
Next Steps
1. Install k3s: curl -sfL https://get.k3s.io | sh -
2. Coba deploy aplikasi sederhana
3. Eksperimen dengan k3d untuk testing
4. Build homelab cluster dengan Raspberry Pi
# Traefik sudah include di K3s
kubectl get pods -n kube-system | grep traefik
# Cek ingressclass
kubectl get ingressclass
kubectl apply -f - <
# Dari certificate files
kubectl create secret tls myapp-tls \
--cert=fullchain.pem \
--key=privkey.pem
# Atau dari Let's Encrypt (CertManager)
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
name: letsencrypt
spec:
acme:
server: https://acme-v02.api.letsencrypt.org/directory
privateKeySecretRef:
name: letsencrypt
solvers:
- http01:
ingress:
class: traefik
# Install Longhorn
kubectl apply -f https://raw.githubusercontent.com/longhorn/longhorn/master/deploy/longhorn.yaml
# Cek Longhorn pods
kubectl get pods -n longhorn-system
# Buka Longhorn UI
kubectl expose service longhorn-frontend -n longhorn-system --type=NodePort --port=80
kubectl apply -f - <
apiVersion: v1
kind: Pod
metadata:
name: app-with-storage
spec:
containers:
- name: app
image: myapp
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: my-data-pvc
# List storage classes
kubectl get sc
# Default storage class
kubectl patch storageclass longhorn \
-p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
# Create snapshot
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: my-snapshot
spec:
volumeSnapshotClassName: longhorn
source:
persistentVolumeClaimName: my-data-pvc
# Restore from snapshot
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: restored-pvc
spec:
dataSource:
name: my-snapshot
kind: VolumeSnapshot
apiGroup: snapshot.storage.k8s.io
storageClassName: longhorn
resources:
requests:
storage: 5Gi
Apa itu K3s?
Berapa minimum RAM untuk K3s?
Apa bedanya K3s server dan agent?