启用RBAC,需要在 apiserver 中添加参数--authorization-mode=RBAC,如果使用的kubeadm安装的集群,1.6 版本以上的都默认开启了RBAC 查看是否开启: $ cat /etc/kubernetes/manifests/kube-apiserver.yaml

spec:
  containers:
  - command:
    - kube-apiserver
    - --advertise-address=192.168.1.243
    - --allow-privileged=true
    - --authorization-mode=Node,RBAC

Kubernetes有一个很基本的特性就是它的所有资源对象都允许执行 CRUD(Create、Read、Update、Delete)操作(也就是我们常说的增、删、改、查操作) 和rbac相关的资源对象包括: 1、Rule:规则,规则是一组属于不同 API Group 资源上的一组操作的集合 2、Role 和 ClusterRole:角色和集群角色,这两个对象都包含上面的 Rules 元素,二者的区别在于,在 Role 中,定义的规则只适用于单个命名空间,也就是和 namespace 关联的,而 ClusterRole 是集群范围内的,因此定义的规则不受命名空间的约束。 3、Subject:主题,对应在集群中尝试操作的对象,集群中定义了3种类型的主题资源: User Account:这是有外部独立服务进行管理的,对于用户的管理集群内部没有一个关联的资源对象,所以用户不能通过集群内部的 API 来进行管理 Group:这是用来关联多个账户的,集群中有一些默认创建的组,比如cluster-admin Service Account:通过Kubernetes API 来管理的一些用户帐号,和 namespace 进行关联的,适用于集群内部运行的应用程序,需要通过 API 来完成权限认证 4:RoleBinding 和 ClusterRoleBinding 简单来说就是把声明的 Subject 和我们的 Role 进行绑定的过程(给某个用户绑定上操作的权限),二者的区别也是作用范围的区别:RoleBinding 只会影响到当前 namespace 下面的资源操作权限,而 ClusterRoleBinding 会影响到所有的 namespace。

创建一个 User Account,只能访问 kube-system 这个命名空间 1、创建私钥 $ openssl genrsa -out dongyali.key 2048 2、创建证书签名请求文件 CN表示要创建的用户名,O表示要创建的组 penssl req -new -key dongyali.key -out dongyali.csr -subj "/CN=dongyali/O=booster" 3、生成最终的证书文件,设置证书的有效期为1000天 需要使用ca.crt和ca.key两个文件来批准证书请求,如果使用的是kubeadm安装的集群,这个两个文件位于/etc/kubernetes/pki/目录下面 $ openssl x509 -req -in dongyali.csr -CA /etc/kubernetes/pki/ca.crt -CAkey /etc/kubernetes/pki/ca.key -CAcreateserial -out dongyali.crt -days 1000 $ ls dongyali.csr dongyali.key dongyali.crt 4、使用刚刚创建的证书文件和私钥文件在集群中创建用户dongyali $ kubectl config set-credentials dongyali --client-certificate=dongyali.crt --client-key=dongyali.key 5、为用户创建上下文,并限制在kube-system空间内 $ kubectl config set-context dongyali-context --cluster=kubernetes --namespace=kube-system --user=dongyali 6、为用户dongyali创建角色 创建一个允许用户操作 Deployment、Pod、ReplicaSets 的角色

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: dongyali-role
  namespace: kube-system
rules:
- apiGroups: ["", "extensions", "apps"]
  resources: ["deployments", "replicasets", "pods"]
  verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]     # 也可以使用['*']

7、创建角色绑定,绑定用户dongyali和角色

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: dongyali-rolebinding
  namespace: kube-system
subjects:
- kind: User
  name: dongyali
  apiGroup: ""
roleRef:
  kind: Role
  name: dongyali-role
  apiGroup: ""

8、测试 $ kubectl get pods --context=dongyali-context $ kubectl --context=dongyali-context get pods --namespace=default Error from server (Forbidden): pods is forbidden: User "dongyali" cannot list pods in the namespace "default"

创建一个只能访问某个 namespace 的ServiceAccount 1、创建一个 ServiceAccount 对象 $ kubectl create sa dongyali-sa -n kube-system 2、创建role

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: dongyali-sa-role
  namespace: kube-system
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "watch", "list"]
- apiGroups: ["apps"]
  resources: ["deployments"]
  verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]

3、创建一个 RoleBinding 对象

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: dongyali-sa-rolebinding
  namespace: kube-system
subjects:
- kind: ServiceAccount
  name: dongyali-sa
  namespace: kube-system
roleRef:
  kind: Role
  name: dongyali-sa-role
  apiGroup: rbac.authorization.k8s.io

创建一个可以访问所有 namespace 的ServiceAccount 需要使用 ClusterRole 和 ClusterRoleBinding 这两种资源对象 1、新建一个 ServiceAcount 对象

apiVersion: v1
kind: ServiceAccount
metadata:
  name: dongyali-sa2
  namespace: kube-system

2、创建一个 ClusterRoleBinding 对象 使用现有的集群角色cluster-admin,不用创建新的了

kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: dongyali-sa2-clusterrolebinding
subjects:
- kind: ServiceAccount
  name: dongyali-sa2
  namespace: kube-system
roleRef:
  kind: ClusterRole
  name: cluster-admin
  apiGroup: rbac.authorization.k8s.io