目录
k8s安全框架
基于角色的权限访问控制:RBAC
案例
操作示例
测试
授权
案例:为指定用户授权访问不同命名空间权限
拓展
kube-apiserver --enable-admission-plugins=NamespaceLifecycle,LimitRanger ...
kube-apiserver --disable-admission-plugins=PodNodeSelector,AlwaysDeny ...
kubectl exec kube-apiserver-k8s-master -n kube-system -- kube-apiserver -h | grep enable-admission-plugins
kubectl get pods --kubeconfig=./aliang.kubeconfig
tar zxvf cfssl.tar.gz -C /usr/bin/
cd rbac/
vi cert.sh
bash cert.sh
cd /root/rbac
vi kubeconfig.sh
kubectl config set-cluster kubernetes \--certificate-authority=/etc/kubernetes/pki/ca.crt \--embed-certs=true \--server=https://192.168.209.110:6443 \--kubeconfig=aliang.kubeconfig# 设置客户端认证kubectl config set-credentials aliang \--client-key=aliang-key.pem \--client-certificate=aliang.pem \--embed-certs=true \--kubeconfig=aliang.kubeconfig# 设置默认上下文kubectl config set-context kubernetes \--cluster=kubernetes \--user=aliang \--kubeconfig=aliang.kubeconfig
kubectl config use-context kubernetes --kubeconfig=aliang.kubeconfig
bash kubeconfig.sh
kubectl get pod --kubeconfig=aliang.kubeconfig
vi rbac.yaml
kubectl apply -f rbac.yaml
apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata:namespace: defaultname: pod-readerrules:- apiGroups: [“”] # api组,例如apps组,空值表示是核心API组,像namespace、pod、service、pv、pvc都在里面resources: [“pods”] #资源名称(复数),例如pods、deployments、servicesverbs: [“get”, “watch”, “list”] # 资源操作方法
apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata:name: read-podsnamespace: defaultsubjects:- kind: User # 主体name: aliang # 主体名称apiGroup: rbac.authorization.k8s.ioroleRef: # 绑定的角色kind: Rolename: pod-reader # 角色名称apiGroup: rbac.authorization.k8s.io
kubectl api-resources
kubectl api-resources |grep deployment
vi rbac.yaml
kind: RoleapiVersion: rbac.authorization.k8s.io/v1metadata:namespace: defaultname: pod-readerrules:- apiGroups: ["","apps"]resources: ["pods","deployments"]verbs: ["get", "watch", "list"]---kind: RoleBindingapiVersion: rbac.authorization.k8s.io/v1metadata:name: read-podsnamespace: defaultsubjects:- kind: Username: aliangapiGroup: rbac.authorization.k8s.ioroleRef:kind: Rolename: pod-readerapiGroup: rbac.authorization.k8s.io
kubectl apply -f rbac.yaml
vi rbac.yaml
kind: RoleapiVersion: rbac.authorization.k8s.io/v1metadata:namespace: defaultname: pod-readerrules:- apiGroups: ["","apps"]resources: ["pods","deployments","services"]verbs: ["get", "watch", "list"]---kind: RoleBindingapiVersion: rbac.authorization.k8s.io/v1metadata:name: read-podsnamespace: defaultsubjects:- kind: Username: aliangapiGroup: rbac.authorization.k8s.ioroleRef:kind: Rolename: pod-readerapiGroup: rbac.authorization.k8s.io
kubectl apply -f rbac.yaml
kubectl get svc --kubeconfig=aliang.kubeconfig
mv aliang.kubeconfig .kube/config