📚 Khóa Học Kubernetes Từ A Đến Z

25 bài học từ cơ bản đến nâng cao. Học qua ví dụ thực tế và hands-on labs.

Xem Toàn Bộ Khóa Học →

📌 Phần 1: Nền Tảng

⚙️ Phần 2: Configuration

🌐 Phần 3: Networking

📈 Phần 4: Scaling

📦 Phần 5: Helm & GitOps

🔒 Phần 6-7: Security & Observability

📌

Core Concepts

Kubernetes là gì?

Kubernetes (K8s) là open-source platform để tự động hóa deployment, scaling và management của containerized applications.

💡 Key Components:
Pod: Đơn vị nhỏ nhất, chứa 1+ containers
Deployment: Quản lý replica sets của pods
Service: Expose pods ra network
ConfigMap/Secret: Configuration management
Ingress: HTTP routing

kubectl Basics

# Cài đặt kubectl
# macOS
brew install kubectl

# Ubuntu
sudo apt-get install -y kubectl

# Kiểm tra cluster
kubectl cluster-info
kubectl get nodes

# Namespace operations
kubectl get namespaces
kubectl create namespace dev
kubectl config set-context --current --namespace=dev

# Get resources
kubectl get pods
kubectl get deployments
kubectl get services
kubectl get all
🚀

Deployment & Service

Deployment YAML

# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app
          image: my-app:1.0.0
          ports:
            - containerPort: 8080
          resources:
            requests:
              memory: "128Mi"
              cpu: "100m"
            limits:
              memory: "256Mi"
              cpu: "500m"
          livenessProbe:
            httpGet:
              path: /health
              port: 8080
            initialDelaySeconds: 30
            periodSeconds: 10
          readinessProbe:
            httpGet:
              path: /ready
              port: 8080
            initialDelaySeconds: 5
            periodSeconds: 5
          env:
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: url

Service YAML

# service.yaml
apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  type: ClusterIP  # hoặc LoadBalancer, NodePort
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

---
# Ingress (cho HTTP routing)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-app-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
    - host: myapp.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: my-app-service
                port:
                  number: 80
# Apply resources
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

# Kiểm tra status
kubectl get pods -w
kubectl describe deployment my-app
kubectl logs -f deployment/my-app
📦

Helm - Package Manager

📘 Helm là gì?

Helm là package manager cho Kubernetes. Giúp đóng gói, version và deploy applications dễ dàng với Charts.

Helm Basics

# Cài đặt Helm
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

# Add repository
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update

# Search charts
helm search repo nginx

# Install chart
helm install my-nginx bitnami/nginx \
    --namespace web \
    --create-namespace \
    --set service.type=LoadBalancer

# List releases
helm list -A

# Upgrade
helm upgrade my-nginx bitnami/nginx --set replicaCount=3

# Uninstall
helm uninstall my-nginx -n web

Tạo Helm Chart

# Tạo chart mới
helm create my-app

# Cấu trúc:
# my-app/
# ├── Chart.yaml         # Metadata
# ├── values.yaml        # Default values
# ├── templates/
# │   ├── deployment.yaml
# │   ├── service.yaml
# │   ├── ingress.yaml
# │   └── _helpers.tpl

# Install local chart
helm install my-app ./my-app -f custom-values.yaml

# Package chart
helm package my-app
🔒

Security & Monitoring

RBAC - Role Based Access Control

# role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: dev
  name: developer-role
rules:
  - apiGroups: [""]
    resources: ["pods", "services", "configmaps"]
    verbs: ["get", "list", "watch", "create", "update"]
  - apiGroups: ["apps"]
    resources: ["deployments"]
    verbs: ["get", "list", "watch", "create", "update", "patch"]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: developer-binding
  namespace: dev
subjects:
  - kind: User
    name: developer@example.com
    apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: developer-role
  apiGroup: rbac.authorization.k8s.io
⚠️ Best Practice: Luôn áp dụng principle of least privilege. Chỉ cấp quyền tối thiểu cần thiết cho mỗi user/service account.

Monitoring với Prometheus & Grafana

# Install Prometheus stack với Helm
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update

helm install monitoring prometheus-community/kube-prometheus-stack \
    --namespace monitoring \
    --create-namespace

# Port forward để access Grafana
kubectl port-forward -n monitoring svc/monitoring-grafana 3000:80

# Grafana credentials (default)
# Username: admin
# Password: prom-operator