K8S权限控制,限制用户在多个namespace上的访问权限 |
您所在的位置:网站首页 › ns绑定多个账号 › K8S权限控制,限制用户在多个namespace上的访问权限 |
前言
clusterrole.dev-log.yaml 用于提供对pod的完全权限和其它资源的查看权限. # 提供基本权限 apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRole metadata: name: dev-log rules: - apiGroups: - "" resources: - pods - pods/exec verbs: - create - get - list - watch - apiGroups: - "" resources: - pods verbs: - delete - apiGroups: - "" resources: - endpoints - services verbs: - get - list - watch - apiGroups: - "" resources: - bindings - events - limitranges - namespaces/status - pods/log - pods/status - replicationcontrollers/status - resourcequotas - resourcequotas/status verbs: - get - list - watch - apiGroups: - "" resources: - namespaces verbs: - get - list - watch - apiGroups: - apps resources: - deployments - deployments/rollback - deployments/scale - statefulsets verbs: - get - list - watch - apiGroups: - autoscaling resources: - horizontalpodautoscalers verbs: - get - list - watch - apiGroups: - batch resources: - cronjobs - jobs - scheduledjobs verbs: - get - list - watch - apiGroups: - extensions resources: - daemonsets - deployments - ingresses - replicasets verbs: - get - list - watch在default命名空间应用配置文件: $ kubectl apply -f clusterrole.dev-log.yaml -n default $ kubectl get ClusterRole -n default 在default命名空间创建 ServiceAccount创建ServiceAccount后,会自动创建一个绑定的 secret ,后面在kubeconfig文件中,会用到该secret中的token. $ kubectl create serviceaccount dev -n default $ kubectl get serviceaccount -n default 对ServiceAccount和集群角色建立绑定关系对需要的namespace进行授权,以下示例为对app命名空间授权。 $ kubectl create rolebinding rbd-dev --clusterrole=dev-log --serviceaccount=default:dev --namespace=app 获取ServiceAccount的secret中的token $ kubectl get serviceaccounts dev -o yaml apiVersion: v1 kind: ServiceAccount metadata: creationTimestamp: "2020-06-03T06:36:29Z" name: dev namespace: app resourceVersion: "2633621" selfLink:xxx uid: xx secrets: - name: dev-token-v97rh对应的secret名称为:dev-token-v97rh。 $ kubectl get secrets dev-token-v97rh -o yaml apiVersion: v1 data: ca.crt: xxx namespace: aGFkb29w token: xxx kind: Secret metadata: annotations: kubernetes.io/service-account.name: dev kubernetes.io/service-account.uid: xxx creationTimestamp: "2020-06-03T06:36:29Z" name: dev-token-v97rh namespace: app resourceVersion: "2633620" selfLink: xxx uid:xxx type: kubernetes.io/service-account-token该secret的token为: token=xxx 该token是经过base64处理的,需要进行解码处理 $ echo $token | base64 -d xxx 组装config文件将token填充到以下的config配置中: # config apiVersion: v1 kind: Config clusters: - cluster: server: K8S集群地址 certificate-authority-data: "ca.crt后的内容" name: k8s-dev users: - name: "devlog" user: token: "解码后的token字符串" contexts: - context: cluster: dev user: "dev" name: dev preferences: {} current-context: dev将该文件保存为config 并放入 $HOME/.kube/ 目录下即可。 至此,k8s限制用户在多个namespace上的访问权限操作完成。 后记K8S权限控制是很复杂的,本文只是最简单的一种,因为工作需要,做个记录。后面有其他的需求再做补充。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |