はじめに
Kubernetesのセキュリティは、誰が何をできるかとPodが何を実行できるかの制御から始まります。このチュートリアルでは、RBAC(ロールベースアクセス制御)とPodセキュリティ標準を実装して、Kubernetesクラスターを保護します。
前提条件
- 稼働中のKubernetesクラスター(minikube、kind、またはマネージド)
kubectlのインストールと設定- Kubernetesの基本知識(Pod、Namespace、Deployment)
1. RBACコンポーネントの理解
Kubernetes RBACには4つの主要オブジェクトがあります:
| オブジェクト | スコープ | 目的 |
|------------|---------|------|
| Role | Namespace | Namespace内の権限を定義 |
| ClusterRole | クラスター | クラスター全体の権限を定義 |
| RoleBinding | Namespace | ユーザー/グループにRoleを付与 |
| ClusterRoleBinding | クラスター | クラスター全体でClusterRoleを付与 |
2. ラボ用Namespaceの作成
# 専用のNamespaceを作成
kubectl create namespace security-lab
# 確認
kubectl get namespaces | grep security-lab
3. RBACロールの作成
読み取り専用ロール
リソースの閲覧のみを許可するロールを作成します:
# read-only-role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: security-lab
name: pod-reader
rules:
apiGroups: [""]
resources: ["pods", "pods/log"]
verbs: ["get", "list", "watch"]
apiGroups: [""]
resources: ["services", "configmaps"]
verbs: ["get", "list"]
限定的な書き込み権限を持つ開発者ロール
# developer-role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: security-lab
name: developer
rules:
apiGroups: [""]
resources: ["pods", "services", "configmaps", "secrets"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
apiGroups: ["apps"]
resources: ["deployments", "replicasets"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
apiGroups: [""]
resources: ["pods/exec", "pods/portforward"]
verbs: ["create"]
apiGroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["get", "list"]
# 両方のロールを適用
kubectl apply -f read-only-role.yaml
kubectl apply -f developer-role.yaml
4. ServiceAccountとバインディングの作成
# service-accounts.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: viewer-sa
namespace: security-lab
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: developer-sa
namespace: security-lab
# role-bindings.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: viewer-binding
namespace: security-lab
subjects:
kind: ServiceAccount
name: viewer-sa
namespace: security-lab
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: developer-binding
namespace: security-lab
subjects:
kind: ServiceAccount
name: developer-sa
namespace: security-lab
roleRef:
kind: Role
name: developer
apiGroup: rbac.authorization.k8s.io
kubectl apply -f service-accounts.yaml
kubectl apply -f role-bindings.yaml
5. RBAC権限のテスト
kubectl auth can-iで権限を確認します:
# viewerの権限テスト
kubectl auth can-i get pods \
--namespace security-lab \
--as system:serviceaccount:security-lab:viewer-sa
# 出力: yes
kubectl auth can-i create pods \
--namespace security-lab \
--as system:serviceaccount:security-lab:viewer-sa
# 出力: no
# developerの権限テスト
kubectl auth can-i create deployments \
--namespace security-lab \
--as system:serviceaccount:security-lab:developer-sa
# 出力: yes
6. Podセキュリティ標準(PSS)
Kubernetes 1.25以降、PodSecurityPolicyはPodセキュリティ標準に置き換えられました。
3つのセキュリティレベル
| レベル | 説明 |
|--------|------|
| Privileged | 制限なし(デフォルト) |
| Baseline | 既知の権限昇格を防止 |
| Restricted | 厳格に制限、セキュリティベストプラクティス |
7. Podセキュリティ標準の適用
# Namespaceに制限付きセキュリティ標準を適用
kubectl label namespace security-lab \
pod-security.kubernetes.io/enforce=restricted \
pod-security.kubernetes.io/audit=restricted \
pod-security.kubernetes.io/warn=restricted
8. セキュリティ適用のテスト
このPodは拒否されます(特権コンテナ):
# privileged-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: privileged-test
namespace: security-lab
spec:
containers:
- name: nginx
image: nginx:latest
securityContext:
privileged: true
このPodは成功します(制限準拠):
# secure-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: secure-nginx
namespace: security-lab
spec:
securityContext:
runAsNonRoot: true
runAsUser: 1000
fsGroup: 1000
seccompProfile:
type: RuntimeDefault
containers:
- name: nginx
image: nginxinc/nginx-unprivileged:latest
ports:
- containerPort: 8080
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
9. 本番対応のセキュアなDeployment
完全なセキュリティコンテキスト、リソース制限、NetworkPolicyを含むDeploymentテンプレートを使用して、多層防御を実現します。RBAC(APIプレーン)、Podセキュリティ(ランタイムプレーン)、NetworkPolicy(ネットワークプレーン)の3つを全て実装することが重要です。
まとめ
このチュートリアルで学んだこと:
1. 最小権限のRBACロールの作成
2. ServiceAccountへのロールバインディング
3. スクリプトとcan-iによる権限監査
4. NamespaceレベルでのPodセキュリティ標準の適用
5. 制限ポリシーに準拠するセキュアなPod仕様の作成
6. トラフィック制御のためのNetworkPolicyの追加