Kubernetes service accounts, RBAC K8s, dan OIDC workload identity memastikan pod hanya memiliki akses yang dibutuhkan di cluster

Setelah di episode 13 kita membahas Cloud IAM, pada episode ini kita masuk ke Kubernetes & workload identity — model akses di dalam cluster K8s yang mengelola bagaimana pod berinteraksi dengan cluster API dan resource eksternal.
Mengapa Kubernetes IAM penting? Karena K8s adalah platform container utama di production, dan setiap pod berjalan dengan identity tertentu. Tanpa RBAC yang benar, pod bisa mengakses resource yang tidak seharusnya — atau bahkan mengambil alih cluster.
Setiap pod berjalan dengan service account yang menentukan identity-nya di dalam cluster:
Pod "my-app" → Service Account "app-service-account"
→ Token untuk akses API server
→ Identity untuk cloud resource accesskubectl get serviceaccounts -n default
kubectl describe serviceaccount default -n defaultapiVersion: v1
kind: ServiceAccount
metadata:
name: my-app-sa
namespace: production| Komponen | Fungsi |
|---|---|
| Role | Permissions di satu namespace |
| ClusterRole | Permissions di seluruh cluster |
| RoleBinding | Menghubungkan Role ke user/service account |
| ClusterRoleBinding | Menghubungkan ClusterRole ke user/service account |
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
namespace: production
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["pods/log"]
verbs: ["get"]apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: production
subjects:
- kind: ServiceAccount
name: my-app-sa
namespace: production
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.ioWorkload identity menghubungkan service account K8s dengan identity cloud:
Pod (K8s SA) → OIDC Token → Cloud IAM (AWS/GCP/Azure) → Cloud Resources# 1. Create IAM role with trust policy
aws iam create-role \
--role-name my-app-role \
--assume-role-policy-document '{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {"Federated": "arn:aws:iam::ACCOUNT:oidc-provider/oidc.eks.REGION.amazonaws.com/id/CLUSTER_ID"},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"oidc.eks.REGION.amazonaws.com/id/CLUSTER_ID:sub": "system:serviceaccount:production:my-app-sa"
}
}
}]
}'
# 2. Attach policy
aws iam attach-role-policy --role-name my-app-role --policy-arn arn:aws:iam::policy/s3-read
# 3. Annotate K8s service account
kubectl annotate serviceaccount my-app-sa -n production \
eks.amazonaws.com/role-arn=arn:aws:iam::ACCOUNT:role/my-app-role# 1. Create GCP service account
gcloud iam service-accounts create my-app-sa
# 2. Grant role ke GCP service account
gcloud projects add-iam-policy-binding PROJECT \
--member="serviceAccount:my-app-sa@PROJECT.iam.gserviceaccount.com" \
--role="roles/storage.objectViewer"
# 3. Bind K8s SA ke GCP SA
gcloud container clusters update CLUSTER \
--update-addons GcpDiskDriver=ENABLED
# 4. Annotate K8s service account
kubectl annotate serviceaccount my-app-sa -n production \
iam.gke.io/gcp-service-account=my-app-sa@PROJECT.iam.gserviceaccount.comapiVersion: v1
kind: Pod
metadata:
name: secure-pod
spec:
serviceAccountName: my-app-sa
securityContext:
runAsNonRoot: true
runAsUser: 1000
fsGroup: 2000
containers:
- name: app
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]Warning
Jangan gunakan default service account untuk pod produksi. Selalu buat custom service account dengan minimum permissions yang dibutuhkan. Default service account memiliki permissions yang terlalu luas.
| Tool | Fungsi |
|---|---|
| Kubescape | Security posture assessment |
| Falco | Runtime security monitoring |
| Trivy | Image vulnerability scanning |
| kube-bench | CIS benchmark compliance |
# Install kube-bench
curl -L https://github.com/aquasecurity/kube-bench/releases/latest/download/kube-bench_linux_amd64 -o kube-bench
chmod +x kube-bench
./kube-bench run --targets master,node,policiesInti yang harus dibawa pulang:
Di episode 15 selanjutnya kita akan membahas Customer IAM (CIAM) — customer identity, social login, dan registration flows. Siapkan customer-facing identity kalian!