LAB 35 AZ-500 ~60 phút Chương 08 Có phí AKS — xóa ngay sau lab

Bảo Mật AKS — Entra ID, Azure RBAC & Network Policy

Tạo AKS cluster với Microsoft Entra integration để dùng Entra ID làm identity provider, bật Azure RBAC for Kubernetes để phân quyền qua Azure IAM, cấu hình network policy Calico để kiểm soát traffic giữa pods.

Cảnh báo chi phí: AKS cluster (2 node Standard_B2s) tốn ~$0.10/giờ/node + phí quản lý. Tổng ~$0.20–0.30/giờ. Xóa cluster ngay sau lab bằng lệnh dọn dẹp cuối bài.

🎯 Mục Tiêu Lab

Tạo AKS cluster với Microsoft Entra integration bật sẵn

Bật Azure RBAC for Kubernetes — phân quyền qua Azure IAM thay vì Kubernetes RBAC riêng

Cấu hình network policy Calico cho phép/chặn traffic giữa pods

Lấy credentials và kiểm tra quyền truy cập cluster

Xem log và monitoring cơ bản qua Container Insights

📋 Chuẩn Bị

Yêu cầu:
  • Azure subscription với quyền Owner
  • Entra ID tenant với quyền tạo service principal
  • kubectl đã cài (az aks install-cli)
  • Azure CLI 2.50+
Naming convention:
  • RG: rg-az500-lab35
  • AKS: aks-az500-lab35
  • Log Analytics: law-az500-lab35

🏗️ Kịch Bản

Doanh nghiệp triển khai AKS cần: (1) dùng Entra ID để xác thực — không dùng local Kubernetes accounts; (2) phân quyền nhất quán qua Azure RBAC thay vì quản lý ClusterRoleBinding riêng; (3) network policy ngăn chặn lateral movement giữa pods nếu có pod bị compromise. Đây là baseline security cho AKS theo Microsoft Cloud Security Benchmark.

🧪 Các Bước Thực Hiện

1

Tạo AKS Cluster Với Entra Integration và Azure RBAC

Cách 1 — Portal
  1. 1.1Portal → Kubernetes services → + Create → Kubernetes cluster
  2. 1.2Basics: RG rg-az500-lab35 → Name aks-az500-lab35 → Region: Southeast Asia → Node size: Standard_B2s → Node count: 2
  3. 1.3Tab Authentication → Authentication method: Microsoft Entra ID with Azure RBAC
  4. 1.4Tab Networking → Network policy: Calico
  5. 1.5Tab Integrations → Container monitoring: Enable → Log Analytics workspace mới → Review + Create
Cách 2 — Azure CLI
Azure CLI— Tạo AKS với Entra integration, Azure RBAC, Calico network policy
# Tạo resource group
az group create --name rg-az500-lab35 --location southeastasia

# Tạo Log Analytics Workspace cho Container Insights
az monitor log-analytics workspace create \
  --resource-group rg-az500-lab35 \
  --workspace-name law-az500-lab35 \
  --location southeastasia

LAW_ID=$(az monitor log-analytics workspace show \
  --resource-group rg-az500-lab35 \
  --workspace-name law-az500-lab35 \
  --query id -o tsv)

# Tạo AKS cluster với:
# - Microsoft Entra integration (--enable-aad)
# - Azure RBAC for Kubernetes (--enable-azure-rbac)
# - Network policy Calico (--network-policy calico)
# - Container Insights (--enable-addons monitoring)
az aks create \
  --resource-group rg-az500-lab35 \
  --name aks-az500-lab35 \
  --location southeastasia \
  --node-count 2 \
  --node-vm-size Standard_B2s \
  --enable-aad \
  --enable-azure-rbac \
  --network-plugin azure \
  --network-policy calico \
  --enable-addons monitoring \
  --workspace-resource-id $LAW_ID \
  --generate-ssh-keys

# Xác nhận cluster đã tạo thành công
az aks show \
  --resource-group rg-az500-lab35 \
  --name aks-az500-lab35 \
  --query "{name:name, state:provisioningState, aadProfile:aadProfile, networkProfile:networkProfile.networkPolicy}" \
  -o table
Kết quả đầu ra: provisioningState: Succeeded, aadProfile.managed: true, aadProfile.enableAzureRBAC: true, networkPolicy: calico. Tạo cluster mất ~5-10 phút.
2

Gán Azure RBAC Role và Lấy Kubeconfig

Cách 1 — Portal
  1. 2.1AKS cluster → Access control (IAM)+ Add → Add role assignment
  2. 2.2Role: Azure Kubernetes Service RBAC Cluster Admin → Members: chọn tài khoản của bạn → Review + assign
  3. 2.3AKS → Connect → copy lệnh az aks get-credentials → chạy trong terminal
Cách 2 — Azure CLI
Azure CLI— Gán role AKS RBAC Cluster Admin và lấy kubeconfig
# Lấy AKS resource ID
AKS_ID=$(az aks show -g rg-az500-lab35 -n aks-az500-lab35 --query id -o tsv)

# Lấy user ID hiện tại
MY_ID=$(az ad signed-in-user show --query id -o tsv)

# Gán Azure Kubernetes Service RBAC Cluster Admin cho user hiện tại
az role assignment create \
  --role "Azure Kubernetes Service RBAC Cluster Admin" \
  --assignee $MY_ID \
  --scope $AKS_ID

# Lấy kubeconfig (--overwrite-existing để ghi đè nếu đã có)
az aks get-credentials \
  --resource-group rg-az500-lab35 \
  --name aks-az500-lab35 \
  --overwrite-existing

# Kiểm tra kết nối (sẽ yêu cầu đăng nhập Entra ID lần đầu)
kubectl get nodes
kubectl get namespaces
3

Cấu Hình Network Policy — Chặn Traffic Giữa Namespaces

Cách 1 — Portal

Network Policy chỉ cấu hình qua kubectl manifest — không có giao diện Portal cho NetworkPolicy objects. Dùng Cloud Shell trong Portal để chạy kubectl.

Cách 2 — kubectl / Azure CLI
kubectl— Tạo namespaces, deploy test pods và áp dụng NetworkPolicy
# Tạo hai namespace để test network policy
kubectl create namespace ns-frontend
kubectl create namespace ns-backend

# Deploy pod test trong mỗi namespace
kubectl run frontend-pod \
  --image=nginx \
  --namespace=ns-frontend \
  --labels="app=frontend"

kubectl run backend-pod \
  --image=nginx \
  --namespace=ns-backend \
  --labels="app=backend"

# Đợi pods running
kubectl get pods -n ns-frontend
kubectl get pods -n ns-backend

# Tạo NetworkPolicy: chỉ cho phép ns-frontend truy cập ns-backend
# Mặc định deny all ingress cho ns-backend
cat <
4

Kiểm Tra Quyền Truy Cập và Xem Monitoring

Azure CLI + kubectl— Kiểm tra RBAC và Container Insights
# Xem role assignments trên AKS resource
az role assignment list \
  --scope $(az aks show -g rg-az500-lab35 -n aks-az500-lab35 --query id -o tsv) \
  --query "[].{principal:principalName, role:roleDefinitionName}" \
  -o table

# Kiểm tra quyền hiện tại trong cluster
kubectl auth can-i create pods --namespace default
kubectl auth can-i delete nodes --namespace default

# Xem tất cả cluster role bindings
kubectl get clusterrolebindings | grep -i azure

# Xem logs container trong AKS
kubectl logs -n ns-frontend frontend-pod --tail=20

# Portal: AKS → Monitoring → Insights → xem node metrics, pod count
# Portal: AKS → Monitoring → Logs → chạy KQL query:
# KubePodInventory | where TimeGenerated > ago(1h) | summarize count() by Namespace

📊 Kết Quả Đầu Ra

AKS với Entra + Azure RBAC

aadProfile.managed: true, enableAzureRBAC: true — xác thực qua Entra ID

kubectl get nodes hoạt động

Sau khi đăng nhập Entra ID, kubectl trả về 2 nodes ở trạng thái Ready

NetworkPolicy tạo thành công

kubectl get networkpolicies -n ns-backend → thấy deny-all-ingress và allow-from-frontend

Container Insights active

Portal AKS → Monitoring → Insights → thấy node CPU/memory metrics

🧹 Dọn Dẹp Tài Nguyên

Azure CLI— Xóa AKS và toàn bộ tài nguyên lab
# Xóa toàn bộ resource group (AKS, Log Analytics, VNet node)
az group delete --name rg-az500-lab35 --yes --no-wait

# AKS tạo node resource group riêng (MC_*), cũng tự xóa theo
# Kiểm tra sau vài phút:
az group list --query "[?contains(name,'az500-lab35')].name" -o table

❓ Câu Hỏi Ôn Tập

1. Sự khác nhau giữa Kubernetes RBAC và Azure RBAC for AKS là gì?

Gợi ý: Kubernetes RBAC dùng ClusterRole/RoleBinding trong cluster, quản lý riêng biệt. Azure RBAC dùng Azure IAM role assignment, tập trung, tích hợp Entra ID, audit qua Azure Activity Log.

2. Tại sao network policy Calico quan trọng trong bảo mật AKS? Mặc định các pods có thể giao tiếp với nhau không?

Gợi ý: Mặc định AKS cho phép tất cả pods giao tiếp với nhau (allow all). Network policy cần thiết để implement least privilege networking và ngăn lateral movement.

3. Khi dùng Azure RBAC for Kubernetes, built-in role nào cấp quyền đọc tất cả resources trong cluster nhưng không thể thay đổi?

Gợi ý: Azure Kubernetes Service RBAC Reader — tương đương với cluster-reader trong Kubernetes RBAC.

4. Lệnh nào để lấy kubeconfig cho AKS cluster với Entra ID authentication?

Gợi ý: az aks get-credentials --resource-group <rg> --name <cluster>. Lần đầu chạy kubectl sẽ redirect đến Entra ID để đăng nhập. Thêm --admin để lấy cluster-admin credentials (chỉ dùng emergency).

5. Microsoft Defender for Containers khác gì so với Container Insights trong context bảo mật AKS?

Gợi ý: Container Insights = monitoring hiệu năng (CPU, memory, logs). Defender for Containers = security threat detection (runtime threats, image vulnerabilities, misconfigurations, anomalous behavior).

Zalo