kubernetes API 访问控制
Authentication(认证)
Authorization(授权)
Admission Control(准入控制)
访问k8s的API Server的客户端主要分为两类:
kubectl向apiserver发起的命令,采用的是http方式,其实就是对URL发起增删改查的操作。
kubectl proxy --port=8888 &
curl http://localhost:8888/api/v1/namespaces/default
curl http://localhost:8888/apis/apps/v1/namespaces/default/deployments
以上两种api的区别是:
UserAccount与serviceaccount:
创建serviceaccount:
kubectl create serviceaccount admin
kubectl describe sa admin ##此时k8s为用户自动生成认证信息,但没有授权
添加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.harbor.com/library/game2048
ports:
- name: http
containerPort: 80
serviceAccountName: admin
将认证信息添加到serviceAccount中,要比直接在Pod指定imagePullSecrets要安全很多。
创建UserAccount:
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
kubectl 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
kubectl get pod
此时用户通过认证,但还没有权限操作集群资源,需要继续添加授权。
RBAC(Role Based Access Control):基于角色访问控制授权
RBAC的三个基本概念 :
Role 和 ClusterRole
Role示例
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"]
RoleBinding 和 ClusterRoleBinding
RoleBinding示例:
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
ClusterRole示例
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绑定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:
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
服务账户的自动化
服务账户准入控制器(Service account admission controller)
Token 控制器(Token controller)
服务账户控制器(Service account controller)
Kubernetes 还拥有“用户组”(Group)的概念:
示例:
1. 表示mynamespace中的所有ServiceAccount
subjects:
- kind: Group
name: system:serviceaccounts:mynamespace
apiGroup: rbac.authorization.k8s.io
2. 表示整个系统中的所有ServiceAccount
subjects:
- kind: Group
name: system:serviceaccounts
apiGroup: rbac.authorization.k8s.io
Kubernetes 还提供了四个预先定义好的 ClusterRole 来供用户直接使用:
示例:
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: readonly-default
subjects:
- kind: ServiceAccount
name: default
namespace: default
roleRef:
kind: ClusterRole
name: view
apiGroup: rbac.authorization.k8s.io