ååè¯æ 转载请æ æåºå¤
[TOC]
åºäºè§è²çæéæ§å¶ï¼ä½¿ç¨ rbac.authorization.k8s.io API ç»æ¥é©±å¨ææå³çï¼å 许管çåéè¿K8S API æ¥å¨æçé ç½®å®å ¨çç¥
RBAC 模å¼å¨1.6çæ¬ä»ç¶æ¯BETA ç¶æï¼è¦å¯ç¨RBAC éè¦å¨API SERVERçå¯å¨åæ°ä¸æ·»å
--authorization-mode=RBACAPI æ¦è§
æ¤ç« èå å« RBAC API 声æçåç§é¡¶å±ç±»åãç¨æ·å¯ä»¥å使ç¨å ¶ä»API èµæºï¼resourceï¼ï¼éè¿kubectlï¼APIè°ç¨çï¼ä¸æ ·æ¥ä½¿ç¨è¿äºèµæºè¿è¡äº¤äºã æ¯å¦kubectl -f (resource).yaml ï¼è¿ç§å½¢å¼ï¼ä¹å¯ä»¥ç¨äºæ¬ç« èçæéèµæº
è§è²ä¸é群è§è²(role and cluster role)
å¨ RBAC API ä¸ï¼ è§è²å å«äºä¸ç»ä»£è¡¨æéçè§åï¼æé声æåªæå纯çå 许声æï¼æ²¡ææç§æéç³æ为æç»è®¿é®çç³æå½¢å¼ãä¸ä¸ªè§è²å¯ä»¥ä½¿ç¨Role ç±»åå®ä¹å°ä¸ä¸ªnamespace ä¸ãæè ä¹å¯ä»¥ä½¿ç¨ClusterRole ç±»åå®ä¹å°æ´ä¸ªæ¥ç¾¤ä¼
ä¸ä¸ªRole ç±»åçè§è²åªè½è®¿é®å ¶æå¨çnamespaceçèµæºï¼ä¾å¦å¦ä¸ä¾åå®ä¹äºä¸ä¸ªdefault namespaceä¸è¯»åpod æéçè§è²
kind: Role apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: namespace: default name: pod-reader rules: - apiGroups: [""] # "" indicates the core API group resources: ["pods"] verbs: ["get", "watch", "list"]ClusterRole ç±»åçè§è²ææä¸Role ç±»å(kind)çè§è²ç¸ä¼¼ãä¸è¿å 为ClusterRoleç±»åæ¯é群èå´ï¼æä»¥å ¶è¿å¯ä»¥æ·»å ä¸åæé
é群èå´çèµæº(cluster-scope resource) æ¯å¦ nodes
éèµæºç±»åç endpoints ï¼non-resource endpointsï¼æ¯å¦ /healthz
夸å½å空é´èµæº (namespaced resource) æ¯å¦ kubectl get pod --all-namespaces
ä¸é¢è¿ä¸ªClusterRole å¯ä»¥è¯»åæä¸ªå ·ä½namespaceçsecret ä¹å¯ä»¥è¯»åææææ空é´çsecret
kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: # "namespace" omitted since ClusterRoles are not namespaced name: secret-reader rules: - apiGroups: [""] resources: ["secrets"] verbs: ["get", "watch", "list"]è§è²ç»å®ä¸é群è§è²ç»å®ï¼RoleBinding and ClusterRoleBindingï¼
è§è²ç»å®(Rolebinding)å°è§è²ï¼roleï¼ä¸ç¨æ·ï¼userï¼è系起æ¥ï¼è§è²ç»å®å å«äºä¸ç³»åçå对象ï¼users,groups,æè service accountsï¼,ç¶åå°è¿äºå¯¹è±¡è¿æ¥å°ä¸ä¸ªæ¥ææç§ææçè§è²(role)ä¸ï¼Rolebinding ç»å®æä¸ªå ·ä½namespaceçè§è²ï¼Roleï¼ï¼ ClusterRoleBindingï¼ç»å® ClusterRole ç±»åçè§è²ã
# This role binding allows "jane" to read pods in the "default" namespace. kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: read-pods namespace: default subjects: - kind: User name: jane apiGroup: rbac.authorization.k8s.io roleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.ioRoleBinding ä¹å¯ä»¥å¼ç¨CulsterRole æ¥é ç½®å½å空é´èå´å çèµæºç访é®çç¥ï¼è¿ç§æ¹å¼å 许管çåå®ä¹ä¸äºé对namespaceçéç¨çè§è²ï¼æçç解æ¯åºç¡æéï¼ï¼ç¶åéç¨æ¤ç±»åçClusterRole å°æä¸ªå ·ä½çnamespaceä¸ï¼èä¸å¿ 为æ¯ä¸ªnamespace 建ç«ç¬ç«çRole
æ¯å¦ä¸é¢è¿ä¸ªä¾åï¼è½ç¶å¼ç¨äºClusterRoleï¼ä½æ¯å ¶ä»ç¶åªè½è®¿é®Development å½å空é´ï¼RoleBinding çnamespace ï¼å çèµæº
# This role binding allows "dave" to read secrets in the "development" namespace. kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: read-secrets namespace: development # This only grants permissions within the "development" namespace. subjects: - kind: User name: dave apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: secret-reader apiGroup: rbac.authorization.k8s.ioClusterRoleBinding ç¨äºææé群èå´å ææå½å空é´çæéã ä¸é¢çä¾åï¼å 许manager ç»å çææç¨æ·ï¼è¯»åä»»ä½namespace ä¸çsecret
# This cluster role binding allows anyone in the "manager" group to read secrets in any namespace. kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: name: read-secrets-global subjects: - kind: Group name: manager apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: secret-reader apiGroup: rbac.authorization.k8s.ioå¼ç¨èµæº Referring to resource
大å¤æ°èµæºé½æ¯éè¿ä¸ä¸ªå符串æ¥è¡¨ç¤ºå ¶èªèº«çï¼æ¯å¦ pods, å°±å å®å¨API URL ä¸ç表ç°å½¢å¼ä¸æ ·ï¼å°½ç®¡å¦æ¤ï¼æäº API å å«äºåèµæºï¼æ¯å¦ podçlog ä»ç API URL å¦ä¸
GET /api/v1/namespaces/{namespace}/pods/{name}/logè¿ç§æ åµä¸ï¼logæ¯podçåèµæºï¼å¨RBAC ææä¸ï¼è¦è¡¨è¾¾è¿ç§å ³ç³»ï¼ç¨åææ å®ä¹èµæºä¸åèµæºï¼è¦å 许ä¸ä¸ªå¯¹è±¡(subject) 读åpod åpod çæ¥å¿ï¼è¦å¦ä¸ä¹¦åRole 对象
kind: Role apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: namespace: default name: pod-and-pod-logs-reader rules: - apiGroups: [""] resources: ["pods", "pods/log"] verbs: ["get", "list"]èµæºåºäºéè¿ç±»åå称(pods,secrets çï¼è¿è¡å¼ç¨ä¹å¤ï¼ä¹å¯ä»¥ç¨è¿èµæºå称ï¼Resource nameï¼å¼ç¨æä¸ªå ·ä½çèµæºãåæ¶ï¼å¯ä»¥éè¿Verbsï¼éå®å¨æä¸ªå ·ä½èµæºä¸çæä½æéï¼ä¾å¦
kind: Role apiVersion: rbac.authorization.k8s.io/v1beta1 metadata: namespace: default name: configmap-updater rules: - apiGroups: [""] resources: ["configmap"] resourceNames: ["my-configmap"] verbs: ["update", "get"]verbs éå®äºåªè½æ´æ°å读å configmap ç±»åä¸çmy-configmap å®ä¾ä¸çå 容ã
注æå¦æ使ç¨çresourceNames verbs ä¸çæéä¸è½ä¸º list, watch, create, deletecollectionï¼å 为èµæºå称ä¸å å«å¨create watch, create, deletecollectionçAPI URL ä¸ã
Role example
ä¸é¢çä¾ååªå å«äºrules é¨å
å 许å¨core api ç»ä¸è¯»åpod
rules: - apiGroups: [""] resources: ["pods"] verbs: ["get", "list", "watch"]å è®¸å¨ extensions å apps ,API ç»ä¸è¯»åååå ¥ deployments
rules: - apiGroups: ["extensions", "apps"] resources: ["deployments"] verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]å 许读å podï¼ åå¨jobs èµæºä¸è¯»ååå ¥
rules: - apiGroups: [""] resources: ["pods"] verbs: ["get", "list", "watch"] - apiGroups: ["batch", "extensions"] resources: ["jobs"] verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]å 许读å my-config (ConfigMap ç±»åçèµæºï¼å®ä¾çå 容( å¿ é¡»éè¿RoleBinding ç»å®å°ä¸ä¸ªå ·ä½çnamespaceï¼æéå¨æ¤namespace èå´å çæï¼
rules: - apiGroups: [""] resources: ["configmaps"] resourceNames: ["my-config"] verbs: ["get"]å 许å¨core api groupä¸è¯»å nodes èµæº (å 为node æ¯é群èå´å çèµæºï¼æä»¥å¿ é¡»ä½¿ç¨ClusterRole åClusterRoleBingdingæ¥ç»å®æè½çæï¼
rules: - apiGroups: [""] resources: ["nodes"] verbs: ["get", "list", "watch"]å 许对éèµæºç endpoint åå ¶subpath 使ç¨âgetâåâpostâ请æ±ï¼å¿ 须使ç¨ClusterRole åClusterRoleBingdingæ¥ç»å®æè½çæï¼
rules: - nonResourceURLs: ["/healthz", "/healthz/*"] # '*' in a nonResourceURL is a suffix glob match verbs: ["get", "post"]å¼ç¨å¯¹è±¡(Reffering Subjects)
RoleBinding å ClusterRoleBinding å° è§è²ï¼roleï¼ç»å®å°å¯¹è±¡ï¼subjectï¼ä¸ï¼å¯¹è±¡ï¼subjectï¼å¯ä»¥æ¯ç¨æ·ï¼userï¼ï¼ç»ï¼groupï¼ï¼æè service account
ç¨æ·å¯ä»¥éè¿å符æ¥è¡¨ç¤ºï¼å ¶å½¢å¼å å« ä¸è¬ç¨æ·åï¼email å½¢å¼çç¨æ·åï¼åæ°åid å½¢å¼çç¨æ·åï¼å ·ä½å½¢å¼åå³äºç®¡çåçé ç½®ãå¨RABC ææç³»ç»ä¸ï¼å¹¶ä¸è¦æ±æç§å ·ä½çå½¢å¼ï¼ä½æ¯åç¼ system: æ¯ä¸ä¸ªä¿çåï¼æ以ç¨æ·åä¸ä¸è½å å«æ¤åç¼ã
ç»å¨K8S ä¸éè¿Authenticator modules æ¥æä¾ï¼ç»(group) åç¨æ·ï¼userï¼ä¸æ ·ï¼éè¿å符æ¥è¡¨ç¤ºã并ä¸æ²¡æéå®å ·ä½çå½¢å¼ï¼ä¸è¿system: ä»ç¶ä¸è½ä½ä¸ºç»åçåç¼
Service Account çç¨æ·åæ¥æ system:serviceaccount: åç¼ï¼å¹¶ä¸å±äºæ¥æ system:serviceaccounts (注ææ«å°¾çSï¼åç¼çç»
RoleBinding ä¾å
ä¸é¢çä¾ååªå å«äºRoleBinding ä¸ç subjects é¨å
å¼ç¨ alice@example.com ç subject é¨å
subjects: - kind: User name: "alice@example.com" apiGroup: rbac.authorization.k8s.ioå¼ç¨ç» frontend-admins ç subject é¨å
subjects: - kind: Group name: "frontend-admins" apiGroup: rbac.authorization.k8s.iokube-system å½å空é´(namespace) ä¸ï¼å¼ç¨å为 default çservice account
subjects: - kind: ServiceAccount name: default namespace: kube-systemå¼ç¨ qa å½å空é´ä¸(namespace) ææçservice account
subjects: - kind: Group name: system:serviceaccounts:qa apiGroup: rbac.authorization.k8s.ioå¼ç¨ææå½å空é´ä¸çææ service account
subjects: - kind: Group name: system:serviceaccounts apiGroup: rbac.authorization.k8s.ioå¼ç¨ææ认è¯è¿çç¨æ·(user) 1.5 ä¹åççæ¬éç¨
subjects: - kind: Group name: system:authenticated apiGroup: rbac.authorization.k8s.ioå¼ç¨æææªéè¿è®¤è¯çç¨æ· 1.5ä¹åççæ¬éç¨
subjects: - kind: Group name: system:unauthenticated apiGroup: rbac.authorization.k8s.ioå¼ç¨ææç¨æ·
subjects: - kind: Group name: system:authenticated apiGroup: rbac.authorization.k8s.io - kind: Group name: system:unauthenticated apiGroup: rbac.authorization.k8s.ioé»è®¤è§è²ä¸è§è²ç»å® ï¼Default Roles and Role Bindingsï¼
api server å建äºä¸ç³»åçé»è®¤ ClusterRole å ClusterRoleBinding å¯¹è±¡ï¼ å ¶ä¸è®¸å¤å¯¹è±¡ä»¥ system: åç¼å¼å¤´ï¼ä»£è¡¨å ¶ä¸ç»å®çèµæºæ¯ä½ä¸ºåºç¡è®¾æ½éç¨åå æçãä¿®æ¹è¿äºèµæºä¼å¯¼è´æ´ä¸ªé群ä¸å¯ç¨ãä¸ä¸ªä¾åæ¯ system:node ClusterRoleãè¿ä¸ªè§è²æ¥æä¸ç³»åçkubelet æéï¼å¦æè¿ä¸ªè§è²è¢«ä¿®æ¹äºï¼å¯è½ä¼è®©kubelet åºç°å¼å¸¸ã
ææé»è®¤çé群è§è² ï¼ClusterRoleï¼ åå ¶è§è²ç»å®ï¼role bindingï¼é½å¸¦æå¦ä¸æ è®°
kubernetes.io/bootstrapping=rbac-defaultsèªå¨åè° Auto-reconciliation
å¨æ¯æ¬¡å¯å¨çæ¶åï¼ api server ä¼èªå¨æ´æ°é»è®¤çé群è§è²ï¼default cluster rolesï¼çä¿¡æ¯ï¼æ·»å 缺å°çæéå对象(user , group, service account çï¼ï¼è¿ä¸ªæºå¶å¯ä»¥è®©é群èªæä¿®å¤ï¼åæ¶ä¹å¯ä»¥è®©é群å¨roleï¼ role binding ï¼subject åçååçæ¶åï¼èªå¨åæ¥æ´æ°å°ææ°ç¶æã
å¦æè¦éåºèªå¨åè°ï¼åªéè¦å¨é»è®¤çcluster role æè role binding ä¸è®¾ç½® rbac.authorization.kubernetes.io/autoupdate 声æ ï¼annotation ï¼ä¸º false å³å¯ã注æï¼ç¼ºå°é»è®¤æé(permissions)æè 对象(subjects) ä¼å¯¼è´é群ä¸å¯ç¨
èªå¨åè°å¨ K8S 1.6+ ççæ¬ä¸å¯ç¨RBAC æææ¶è¢«æ¿æ´»ã
åç°è§è² (Discovery Roles)
Default ClusterRole Default ClusterRoleBinding Description æè¿° cluster-admin system:masters group å 许å¨ä»»ä½èµæºä¸æ§è¡ä»»ä½æä½,å½éè¿ClusterRoleBinding 使ç¨æ¤è§è²æ¶ï¼æææææ°å½ç©ºé´ä¸çææèµæºï¼å½ä½¿ç¨ Rolebinding æ¶ï¼æærolebingding æå¨ç å½å空é´ä¸çææèµæºï¼å¹¶å å«å½å空é´èªèº«æ代表çèµæº admin å 许管çå访é®ï¼ä¸è¬ä¸Rolebindingæé 使ç¨ï¼ç»å®å°ä¸ä¸ªå½å空é´ä¸ï¼å¯ä»¥è¯»å大å¤æ°å½å空é´ä¸çèµæºï¼å¹¶ä¸å¯ä»¥å¨å½å空é´ä¸å建 role å rolebindingï¼ä¸å 许访é®å½å空é´ä¸çèµæºé é¢ï¼åå½å空é´èªèº« å 许读åå½å空é´ä¸ç大å¤æ°èµæºï¼ä¸è¿ä¸å 许æ¥çåä¿®æ¹è§è² (role)åè§è²ç»å® (role binding) å 许读å½å空é´ä¸ç大å¤æ°èµæºï¼ä¸è¿ä¸å 许æ¥çåä¿®æ¹è§è² (role)åè§è²ç»å® (role binding),ä¸å 许æ¥çsecrets èµæºæ ¸å¿ç»ä»¶è§è² Core Component Roles
Default ClusterRole Default ClusterRoleBinding Description æè¿° system:kube-controller-manager system:kube-controller-manager user å è®¸è®¿é® kube-controller-manager ç»ä»¶æéçèµæºï¼æ§å¶å¨è§è²ä¸å å«å个æ§å¶å¾ªç¯æéçæéã system:node system:nodes group (deprecated in 1.7) å 许访é®kubeletç»ä»¶æéçèµæºï¼å æ¬å¯¹ææç§å¯ç读å访é®æéï¼ä»¥å对ææpodç访é®æéãå¨1.7çæ¬ä¸ï¼æ¨èä½¿ç¨ Node authorizer å NodeRestriction admission plugin æ¿ä»£è¿ä¸ªè§è²ï¼å¹¶ä¸å è®¸æ ¹æ®podçè°åº¦æ åµæ¥æäºå¯¹kubeletçAPI访é®æéï¼ä»1.7å¼å§ï¼å½å¯ç¨âNodeâææ模å¼æ¶ï¼ä¸ä¼èªå¨ç»å®å°âsystemï¼nodesâç» system:node-proxier system:kube-proxy user å 许访é®kube-proxyç»ä»¶æéçèµæºå ¶ä»ç»ä»¶è§è² (other Component Roles)
Default ClusterRole Default ClusterRoleBinding Description æè¿°æ§å¶å¨è§è² ï¼Controller Rolesï¼
Kubernetesæ§å¶å¨ç®¡çå¨è¿è¡æ ¸å¿æ§å¶ç¯è·¯ãå½ä½¿ç¨ --use-service-account-credentials åæ°è°ç¨æ¶ï¼æ¯ä¸ªæ§å¶å¾ªç¯é½å°ä½¿ç¨åç¬çæå¡å¸æ·(service account) å¯å¨ã对äºæ¯ä¸ªæ§å¶å¾ªç¯ä¸çè§è²é½ä»¥system: åç¼å¼å¤´ãå¦æä¸æ¯ç¨ --use-service-account-credentials åæ°ï¼å®å°ä½¿ç¨å ¶èªå·±çåè¯è¿è¡æææ§å¶å¾ªç¯ï¼è¿äºåè¯å¿ é¡»å å«ææç¸å ³çè§è²çæéãè¿äºè§è²å æ¬ï¼
system:controller:attachdetach-controller
system:controller:certificate-controller
system:controller:cronjob-controller
system:controller:daemon-set-controller
system:controller:deployment-controller
system:controller:disruption-controller
system:controller:endpoint-controller
system:controller:generic-garbage-collector
system:controller:horizontal-pod-autoscaler
system:controller:job-controller
system:controller:namespace-controller
system:controller:node-controller
system:controller:persistent-volume-binder
system:controller:pod-garbage-collector
system:controller:replicaset-controller
system:controller:replication-controller
system:controller:resourcequota-controller
system:controller:route-controller
system:controller:service-account-controller
system:controller:service-controller
system:controller:statefulset-controller
system:controller:ttl-controlle
é²æ¢éæ³ææ ï¼Privilege Escalation Prevention and Bootstrappingï¼
RBAC API é»æ¢ç¨æ·éè¿ç¼è¾è§è²(Role)æè§è²ç»å®(role binding)æ¥å级æéãå 为è¿æ¯å¨API级强å¶éå®çï¼æ以å³ä½¿RBACææ模å¼æªè¢«ä½¿ç¨ä¹éç¨ã
ç¨æ·åªè½å建èªèº«ææ¥æçæéèå´å çè§è²ï¼å³å¦æuser1 没æ读åé群èå´å secretçæéï¼é£ä¹user1 å°±ä¸è½å建æ¥æè¿ä¸ªæéçClusterRoleãè¦è®©ä¸ä¸ªç¨æ·å建/æ´æ°è§è²
æäºç¨æ·ä¸ä¸ªè§è²ï¼å 许ä»ä»¬æ ¹æ®éè¦å建/æ´æ°RoleæClusterRole对象ã
æäºç¨æ·ä½ å¸æä»ä»¬å¨å建è§è²æ¶ï¼å¯ä»¥æå®çæéãå¦æä»ä»¬è¯å¾å建æ¥æå ¶èªèº«æ²¡ææéçè§è²ï¼roleï¼æè é群è§è²ï¼cluster roleï¼ï¼API 请æ±ä¼èªè¡é»æ¢ã
ç¨æ·åªè½å¯¹å ¶è§è²æ¥æçæéå建 role bindingï¼å¨årole binding ç¸åçèå´å ï¼ï¼æè å¨å ¶è¢«æ¾ç¤ºææ(been given explicit permission)å¯ä»¥å¯¹æ个è§è²è¿è¡binding æ¶æè½å建ãä¾å¦ï¼å¦æuser1 没æ读åé群èå´å secretçæéï¼é£ä¹user1 å°±ä¸è½å建æ¥æ读åé群èå´å secret æé ç role çClusterRoleBindingãè¦è®©ä¸ä¸ªç¨æ·å建/æ´æ°è§è²ç»å®
æäºä»ä»¬ä¸ä¸ªè§è²å è®¸æ ¹æ®éè¦å建 Rolebinding æè ClusterRoleBinding 对象ã
æäºä»ä»¬å¿ è¦çæéæ¥ç»å®å ·ä½çè§è²ï¼roleï¼
2.1. éå¼å°ï¼ä»ä»¬ç¨åèªå·±ç¸å ³è§è²çç»å®æé
2.2. æ¾ç¤ºå°ï¼å¯ä»¥æäºä»ä»¬æä¸ªå ·ä½è§è²çç»å®æéï¼èªèº«ä¸ä¸å®æ¥æç®æ è§è²ï¼
ä¾å¦ï¼ä¸é¢è¿ä¸ªä¾åå 许 user1 å¨user-1-namespace å½å空é´ä¸ææå ¶ä»ç¨æ·ï¼userï¼ admin edit åview è§è²
apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRole metadata: name: role-grantor rules: - apiGroups: ["rbac.authorization.k8s.io"] resources: ["rolebindings"] verbs: ["create"] - apiGroups: ["rbac.authorization.k8s.io"] resources: ["clusterroles"] verbs: ["bind"] resourceNames: ["admin","edit","view"] apiVersion: rbac.authorization.k8s.io/v1beta1 kind: RoleBinding metadata: name: role-grantor-binding namespace: user-1-namespace roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: role-grantor subjects: - apiGroup: rbac.authorization.k8s.io kind: User name: user-1å½å建(bootstrap)第ä¸ä¸ªè§è²åè§è²ç»å®æ¶ï¼åå§ç¨æ·å¿ é¡»æäºä»ä»¬å°æªæ¥æçæé
å建åå§è§è²åè§è²ç»å®
使ç¨åsystem:masters ç»ä¸çè´¦æ·ï¼æ¯å¦å å« cluster-admin super-user ç管çè§è²çè´¦æ·
å¦æapi server 没æ使ç¨å®å ¨ç«¯å£ï¼--insecure-port),ä½ ä»ç¶è½éè¿å®å ¨ç«¯å£ä½¿ç¨apiï¼æ²¡æ强å¶è¦æ±è®¤è¯ä¸ææï¼ã
å½ä»¤è¡å·¥å · Command-line Utilities
两个kubectl å½ä»¤å¯ä»¥ç¨æ¥å¨å½å空é´èå´æé群èå´ææè§è²ã
kubectl create rolebinding
å¨ä¸ä¸ªå½å空é´ï¼namespaceï¼ä¸ææä¸ä¸ªè§è²ï¼Roleï¼æè é群è§è²ï¼ClusterRoleï¼ï¼ä¾å
kubectl create clusterrolebinding
ææé群èå´å çClusterRoleï¼å å«namespace æéã
ææ root ç¨æ·ä¸º cluster-admin çé群è§è²(ClusterRole)
kubectl create clusterrolebinding root-cluster-admin-binding --clusterrole=cluster-admin --user=rootæækubelet ç¨æ· system:node è§è²
kubectl create clusterrolebinding kubelet-node-binding --clusterrole=system:node --user=kubeletææå为 myapp çservice accountï¼ view è§è²
kubectl create clusterrolebinding myapp-view-binding --clusterrole=view --serviceaccount=acme:myapService Account æé ï¼Service Account Permissionsï¼
é»è®¤RBACçç¥åæ§å¶å¹³é¢(controll plane)ï¼èç¹åæ§å¶å¨æäºæ¥æèå´éå¶çæéï¼ä½ä¸åâkube-systemâå½å空é´ä¹å¤çæå¡å¸æ·(service account)æäºæéï¼è¶ åºææå·²éªè¯ç¨æ·çåç°æéï¼
è¿å 许æ¨æ ¹æ®éè¦åç¹å®æå¡å¸æ·æäºç¹å®è§è²ãç»ç²åº¦è§è²ç»å®æä¾æ´å¤§çå®å ¨æ§ï¼ä½éè¦æ´å¤çç²¾åæ¥ç®¡çã宽æ³çææå¯è½ç»æå¡å¸æ·æä¾ä¸å¿ è¦çï¼éæ§ææï¼API访é®æéï¼ä½æ¯æ´å®¹æ管çã
ä»æå®å ¨å°æä¸å®å ¨çæ¹æ³æ¯ï¼
è¿è¦æ±åºç¨ç¨åºå¨å ¶podè§èä¸æå®serviceAccountNameï¼å¹¶ä¸è¦å¨ä¹åå建好æå¡å¸æ·ï¼éè¿APIï¼åºç¨ç¨åºæ¸ åãapplication manifestãï¼kubectl çæ¹å¼å建ï¼ã
ä¾å¦ï¼å°âmy-namespaceâä¸çåªè¯»æéæäºâmy-saâæå¡å¸æ·ï¼
kubectl create rolebinding my-sa-view \ --clusterrole=view \ --serviceaccount=my-namespace:my-sa \ --namespace=my-namespaceå¦æåºç¨ç¨åºæ²¡ææå®serviceAccountNameï¼å®å°ä½¿ç¨âé»è®¤âæå¡å¸æ·ã
注æï¼æäºâé»è®¤âæå¡å¸æ·çæéå¯ç¨äºåä»»ä½å½å空é´å ï¼æªæå®æå¡è´¦æ·(service accountï¼ çPODã
ä¾å¦ï¼å°âmy-namespaceâä¸çåªè¯»æéæäºâé»è®¤âæå¡å¸æ·ï¼
shell kubectl create rolebinding default-view \ -- clusterrole=view \ --serviceaccount=my-namespace:default \ --namespace=my-namespace许å¤æ件(add-on)使ç¨âkube-systemâå½å空é´ä¸çâé»è®¤âæå¡å¸æ·è¿è¡ãè¦å 许è¿äºå 载项使ç¨è¶ 级ç¨æ·è®¿é®æéï¼è¯·å°cluster-adminæéæäºâkube-systemâå½å空é´ä¸çâé»è®¤âæå¡å¸æ·
注æï¼å¦æå¨kube-systemå½å空é´å æäºé»è®¤æå¡è´¦æ·cluster-admin æéï¼è¿æå³çkube-system æ¥æäºsuper userçç¹æ
kubectl create clusterrolebinding add-on-cluster-admin \ --clusterrole=cluster-admin \ --serviceaccount=kube-system:defaultå¦æå¸æå½å空é´ä¸çææåºç¨ç¨åºé½å ·æè§è²ï¼æ 论使ç¨ä»ä¹æå¡å¸æ·ï¼æ¨å¯ä»¥å°è§è²æäºè¯¥å称空é´çæå¡å¸æ·ç»ã
ä¾å¦ï¼å°âmy-namespaceâä¸çåªè¯»æéæäºè¯¥å½å空é´ä¸çæææå¡å¸æ·
kubectl create rolebinding serviceaccounts-view \ --clusterrole=view \ --group=system:serviceaccounts:my-namespace \ --namespace=my-namespaceå¦ææ¨ä¸æ³ä¸ºæ¯ä¸ªå½å空é´ç®¡çæéï¼å¯ä»¥å°ç¾¤éèå´çè§è²(ClusterRole)æäºæææå¡å¸æ·ã
ä¾å¦ï¼å°ææå½å空é´ä¸çåªè¯»æéæäºç¾¤éä¸çæææå¡å¸æ·ï¼
kubectl create clusterrolebinding serviceaccounts-view \ --clusterrole=view \ --group=system:serviceaccountså¦ææ¨æ ¹æ¬ä¸å ³å¿æéé®é¢ï¼å¯ä»¥åæææå¡å¸æ·æäºè¶ 级ç¨æ·æéã
è¦åï¼è¿å 许任ä½å ·æ读åæéçç¨æ·è®¿é® secretæå建ä¸ä¸ªpod以访é®è¶ 级ç¨æ·åæ®ã
kubectl create clusterrolebinding serviceaccounts-cluster-admin \ --clusterrole=cluster-admin \ --group=system:serviceaccountsä»1.5å级 (Upgrading from 1.5)
å¨Kubernetes 1.6ä¹åï¼è®¸å¤é¨ç½²(deployment)使ç¨é常宽æ³çABACçç¥ï¼å æ¬æäºæææå¡å¸æ·å®å ¨çAPI访é®æéã
é»è®¤RBACçç¥åæ§å¶å¹³é¢ç»ä»¶(controll plane component)ï¼èç¹(node)åæ§å¶å¨(controller)æäºd带èå´éå¶çæéï¼ä½ä¸åâkube-systemâå½å空é´ä¹å¤çæå¡å¸æ·æäºæéï¼è¶ åºå·²éªè¯ç¨æ·çåç°æéï¼ã
è½ç¶å®å ¨æ§æ´é«ï¼ä½è¿å¯è½ä¼å½±åå°ææèªå¨æ¥æ¶APIæéçç°æå·¥ä½è´è½½ã以ä¸æ¯åæ¤è½¬æ¢ç两ç§æ¹æ³
并è¡ææè
åæ¶è¿è¡RBACåABACææå¨ï¼å¹¶å æ¬æ§çABACçç¥ï¼
--authorization-mode=RBAC,ABAC --authorization-policy-file=mypolicy.jsonRBACææè å°å°è¯é¦å ææ请æ±ãå¦æå®æç»API请æ±ï¼åABACææå¨æ¥è¿è¯·æ±çææä»»å¡ãè¿æå³çRBACæABACå ¶ä¸ä¹ä¸å 许请æ±å³å¯ã
å½æ¥å¿çº§å«ä¸º2ææ´é«ï¼-v = 2ï¼è¿è¡æ¶ï¼æ¨å¯ä»¥å¨apiserveræ¥å¿ä¸çå°RBACçæç»æ¥å¿ï¼ä»¥RBAC DENY : 为åç¼ï¼ãæ¨å¯ä»¥ä½¿ç¨è¯¥ä¿¡æ¯æ¥ç¡®å®åªäºè§è²éè¦æäºåªäºç¨æ·(user)ï¼ç»(group)ææå¡å¸æ·(service account)ãä¸æ¦åæå¡å¸æ·æäºè§è²ï¼å¹¶ä¸å¨æå¡å¨æ¥å¿ä¸æ²¡æRBACæç»æ¶æ¯çå·¥ä½è´è½½æ£å¨è¿è¡ï¼åå¯ä»¥å é¤ABACææå¨
çµæ´»çRBACæéï¼Permissive RBAC Permissionsï¼
æ¨å¯ä»¥ä½¿ç¨RBACè§è²ç»å®æ¥å¤å¶ä¸ä¸ªå 许ççç¥ã
è¦åï¼ä»¥ä¸çç¥å 许æææå¡å¸æ·å å½é群管çåãè¿è¡å¨å®¹å¨ä¸çä»»ä½åºç¨ç¨åºé½ä¼èªå¨æ¥æ¶æå¡å¸æ·åæ®ï¼å¹¶ä¸å¯ä»¥é对APIæ§è¡ä»»ä½æä½ï¼å æ¬æ¥çsecretåä¿®æ¹æéãè¿æ¯ä¸æ¨èççç¥ã
kubectl create clusterrolebinding permissive-binding \ --clusterrole=cluster-admin \ --user=admin \ --user=kubelet \