1. 流程
Authentication(认证)
-
认证方式现共有8种,可以启用一种或多种认证方式,只要有一种认证方式通过,就不再进行其它方式的认证。通常启用X509 Client Certs和Service Accout Tokens两种认证方式。
-
Kubernetes集群有两类用户:由Kubernetes管理的Service Accounts (服务账户)和(Users Accounts) 普通账户。k8s中账号的概念不是我们理解的账号,它并不真的存在,它只是形式上存在。
Authorization(授权)
必须经过认证阶段,才到授权请求,根据所有授权策略匹配请求资源属性,决定允许或拒绝请求。授权方式现共有6种,AlwaysDeny、AlwaysAllow、ABAC、RBAC、Webhook、Node。默认集群强制开启RBAC。
Admission Control(准入控制)
用于拦截请求的一种方式,运行在认证、授权之后,是权限认证链上的最后一环,对请求API资源对象进行修改和校验。
API Server
访问k8s的API Server的客户端主要分为两类:
- kubectl : 家目录中的 .kube/config 里面保存了客户端访问API Server的密钥相关信息,用kubectl访问k8s时,会自动读取该配置文件,向API Server发起认证,然后完成操作请求。
- pod:Pod中的进程需要访问API Server,若是用户或脚本访问使用的账号为:UserAccount;而Pod自身去连接API Server时,使用的账号是:ServiceAccount 。
2. 用户
用户分为两类:UserAccoun
与serviceaccount
UA一般针对真实用户,SA 针对运行在pod中的进程
- 用户账户是全局性的。 其名称在集群各 namespace 中都是全局唯一的,未来的用户资源不会做 namespace 隔离, 服务账户是 namespace 隔离的。
创建SA用户
kubectl create serviceaccount admin
添加secrets到serviceaccount中:
kubectl patch serviceaccount admin -p '{"imagePullSecrets": [{"name": "myregistrykey"}]}'
把serviceaccount和pod绑定起来:
apiVersion: v1
kind: Pod
metadata:
name: myapp
labels:
app: myapp
spec:
containers:
- name: myapp
image: reg.westos.org/westos/game2048
ports:
- name: http
containerPort: 80
serviceAccountName: admin
证信息添加到serviceAccount中,要比直接在Pod指定imagePullSecrets要安全很多
创建UA用户
cd /etc/kubernetes/pki/
openssl genrsa -out test.key 2048
openssl req -new -key test.key -out test.csr -subj "/CN=test"
openssl x509 -req -in test.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out test.crt -days 365
openssl x509 -in test.crt -text -noout
ubectl config set-credentials test --client-certificate=/etc/kubernetes/pki/test.crt --client-key=/etc/kubernetes/pki/test.key --embed-certs=true
kubectl config view
kubectl config set-context test@kubernetes --cluster=kubernetes --user=test
kubectl config use-context test@kubernetes
此时用户通过认证,但还没有权限操作集群资源,需要继续添加授权
3. 角色
RBAC(Role Based Access Control):基于角色访问控制授权。
- 从允许管理员通过Kubernetes API动态配置授权策略。RBAC就是用户通过角色与权限进行关联。
- RBAC只有授权,没有拒绝授权,所以只需要定义允许该用户做什么即可。
- RBAC包括四种类型:Role、ClusterRole、RoleBinding、ClusterRoleBinding。
创建角色
普通角色role
vim role.yaml
kubectl apply -f role.yaml
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: default
name: myrole
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list", "create", "update", "patch", "delete"]
此时用户通过认证,但还没有权限操作集群资源,需要继续添加授权。
clusterrole
集群角色
vim clusterrole.yaml
kubectl apply -f clusterrole.yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: myclusterrole
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list", "delete", "create", "update"]
- apiGroups: ["extensions", "apps"]
resources: ["deployments"]
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
角色绑定
RoleBinding和ClusterRoleBinding
- RoleBinding是将Role中定义的权限授予给用户或用户组。它包含一个subjects列表(users,groups ,service accounts),并引用该Role。
- RoleBinding是对某个namespace 内授权,ClusterRoleBinding适用在集群范围内使用。
rolebinding
vim rolebind.yaml
kubectl apply -f rolebind.yaml
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: test-read-pods
namespace: default
subjects:
- kind: User
name: test
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: myrole
apiGroup: rbac.authorization.k8s.io
将role 与user绑定之后,在指定的namespace中user可以使用role中指定的权限。
若给用户使用rolebinding方式绑定clusterrole,依然需要指定namespace,效果与绑定role相同。
#使用rolebinding绑定clusterRole:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: rolebind-myclusterrole
namespace: default
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: myclusterrole
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: test
clusterrolebinding
vim crcrb.yaml
kubectl apply crcrb.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: clusterrolebinding-myclusterrole
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: myclusterrole
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: test
此时,用户在所有namespace下都拥有指定的权限