Kubernetes セキュリティ
RBAC、Pod Security Standards、ネットワークポリシーなど、Kubernetesのセキュリティ機能を学びます。
目次
セキュリティの階 層
┌─────────────────────────────────────────────────────────────┐
│ Kubernetesセキュリティ階層 │
├─────────────────────────────────────────────────────────────┤
│ │
│ クラスターレベル │
│ ├── ネットワークポリシー(Pod間通信制御) │
│ ├── RBAC(APIアクセス制御) │
│ └── Admission Controller(リソー ス検証) │
│ │
│ ノードレベル │
│ ├── カーネルハードニング │
│ ├── コンテナランタイムセキュリティ │
│ └── Kubelet認証 │
│ │
│ Podレベル │
│ ├── Pod Security Standards │
│ ├── SecurityContext │
│ └── Service Account │
│ │
│ コンテナレベル │
│ ├── イメージスキャン │
│ ├── 読み取り専用ファイルシステム │
│ └── リソース制限 │
│ │
│ アプリケーションレベル │
│ ├── シークレット管理 │
│ ├── TLS通信 │
│ └── 認証・認可 │
│ │
└─────────────────────────────────────────────────────────────┘
RBAC
基本概念
┌─────────────────────────────────────────────────────────────┐
│ RBAC │
│ │
│ Subject(誰が) │
│ ├── User(ユーザー) │
│ ├── Group(グループ) │
│ └── ServiceAccount(サービスアカウント) │
│ │
│ │ │
│ ▼ │
│ RoleBinding / ClusterRoleBinding(紐付け) │
│ │ │
│ ▼ │
│ Role / ClusterRole(何ができるか) │
│ └── Resources + Verbs │
│ │
└───────────────────────────────────────────────── ────────────┘
Role と ClusterRole
# Role: Namespace内のリソースに対する権限
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
namespace: production
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
- apiGroups: [""]
resources: ["pods/log"]
verbs: ["get"]
---
# ClusterRole: クラスター全体のリソースに対する権限
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: secret-reader
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "watch", "list"]
RoleBinding と ClusterRoleBinding
# RoleBinding: RoleをSubjectに紐付け
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: production
subjects:
# ServiceAccountへの紐付け
- kind: ServiceAccount
name: idp-server
namespace: production
# Userへの紐付け
- kind: User
name: developer@example.com
apiGroup: rbac.authorization.k8s.io
# Groupへの紐付け
- kind: Group
name: developers
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
---
# ClusterRoleBinding: クラスター全体での紐付け
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: read-secrets-global
subjects:
- kind: ServiceAccount
name: monitoring
namespace: monitoring
roleRef:
kind: ClusterRole
name: secret-reader
apiGroup: rbac.authorization.k8s.io
よく使うVerbs
| Verb | 説明 |
|---|---|
| get | 単一リソースの取得 |
| list | リソース一覧の取得 |
| watch | リソースの変更監視 |
| create | リソースの作成 |
| update | リソースの更新 |
| patch | リソースの部分更新 |
| delete | リソースの削除 |
権限の確認
# 自分の権限確認
kubectl auth can-i create pods
kubectl auth can-i delete pods --namespace production
# 他のユーザーの権限確認(管理者のみ)
kubectl auth can-i create pods --as developer@example.com
# ServiceAccountの権限確認
kubectl auth can-i list secrets --as system:serviceaccount:production:idp-server
# 全権限の一覧
kubectl auth can-i --list
ServiceAccount
基本設定
# ServiceAccount作成
apiVersion: v1
kind: ServiceAccount
metadata:
name: idp-server
namespace: production
automountServiceAccountToken: false # 明示的に無効化
---
# Podでの使用
apiVersion: apps/v1
kind: Deployment
metadata:
name: idp-server
spec:
template:
spec:
serviceAccountName: idp-server
automountServiceAccountToken: false # トークン不要な場合
containers:
- name: idp-server
image: my-idp-server:latest
AWS IRSA(IAM Roles for Service Accounts)
# EKSでAWSリソースにアクセスする場合
apiVersion: v1
kind: ServiceAccount
metadata:
name: idp-server
namespace: production
annotations:
eks.amazonaws.com/role-arn: arn:aws:iam::123456789012:role/idp-server-role
---
# 対応するIAMロール(Terraform)
# resource "aws_iam_role" "idp_server" {
# name = "idp-server-role"
# assume_role_policy = data.aws_iam_policy_document.assume_role.json
# }