创建一个可以访问所有 namespace 的ServiceAccount
如果我们现在创建一个新的 ServiceAccount,需要他操作的权限作用于所有的 namespace,这个时候我们就需要使用到 ClusterRole 和 ClusterRoleBinding 这两种资源对象了
1 创建serviceaccount 对象
=====sa.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: sa-test
namespace: test
2 然后创建一个 ClusterRoleBinding 对象
====crb.yaml
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: crb-test
subjects:
- kind: ServiceAccount
name: sa-test
namespace: test
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
3=======pod.yaml
[root@master1 ~]# vim sa-pod.yml
apiVersion: v1
kind: Pod
metadata:
name: nginx-test
namespace: test
spec:
containers:
- name: c1
image: nginx:1.15-alpine
ports:
- name: httpd
containerPort: 80
serviceAccountName: sa-test # 指定sa为sa-test
----------------
从上面我们可以看到我们没有为这个资源对象声明 namespace,因为这是一个 ClusterRoleBinding 资源对象,是作用于整个集群的,我们也没有单独新建一个 ClusterRole 对象,而是使用的 cluster-admin 这个对象
K8S 安全认证机制(认证、授权(Role/ClusterRole/ClusterRole/ClusterRoleBinding)、准入控制)_途径日暮不赏丶的博客-CSDN博客