(1)访问k8s集群的时候,需要经过三个步骤完成具体操作:
(2)进行访问时候,过程中都需要经过apiServer,apiServer做统一协调工作,访问过程需要证书、token、或者用户名+密码,如果访问Pod还需要serviceAccount
(3)认证:
(4)鉴权(授权):
基于RBAC进行鉴权操作
基于角色访问控制
(5)准入控制:
就是准入控制器的列表,如果列表有请求内容通过,没有则拒绝。
基于角色的访问控制
角色:
角色绑定:
主体:
创建命名空间
kubectl create ns roledemo
在新创建的命名空间创建pod
kubectl run nginx --image=nginx -n roledemo
kubectl get pods -n roledemo
创建角色
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: roledemo
name: pod-reader
rules:
- apiGroups: [""] # ""indicates the core API group
resources: ["pods"]
verbs: ["get","watch","list"]
kubectl apply -f rbac-role.yaml
kubectl get role -n roledemo
创建角色绑定
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: read-pods
namespace: roledemo
subjects:
- kind: User
name: lucy # Name is case sensitive
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role # this must be Role or ClusterRole
name: pod-reader # this must match the name of the Role or ClusterRole you wish to bind to
apiGroup: rbac.authorization.k8s.io
kubectl apply -f rbac-rolebinding.yaml
kubectl get role,rolebinding -n roledemo
使用证书识别身份
mkdir lucy
vi rbac-user.sh
cat > lucy-csr.json <<EOF
{
"CN": "lucy",
"hosts": [],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"L": "Beijing",
"ST": "Beijing"
}
]
}
EOF
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json profile=kubernetes lucy-csr.json | cfssljson -bare lucy
kubectl config set-cluster kubernetes \
--certificate-authority=ca.pem \
--embed-certs=true \
--server=https://192.168.31.63:6443 \
--kubeconfig=lucy-kubeconfig
kubectl config set-credentials lucy \
--client-key=lucy-key.pem \
bash rbac-user.sh