k8s--kubernetes访问控制


一、k8s访问控制介绍

在这里插入图片描述在这里插入图片描述

  • Authentication(认证)
    认证方式现共有8种,可以启用一种或多种认证方式,只要有一种认证方式通过,就不再进行其它方式的认证。通常启用X509 Client Certs和Service Accout Tokens两种认证方式。
  • Kubernetes集群有两类用户:由Kubernetes管理的Service Accounts (服务账户;验证pod)和(Users Accounts) 普通账户。k8s中账号的概念不是我们理解的账号,它并不真的存在,它只是形式上存在。
  • Authorization(授权)
    必须经过认证阶段,才到授权请求,根据所有授权策略匹配请求资源属性,决定允许或拒绝请求。授权方式现共有6种,AlwaysDeny、AlwaysAllow、ABAC、RBAC、Webhook、Node。默认集群强制开启RBAC
  • Admission Control(准入控制)
    用于拦截请求的一种方式,运行在认证、授权之后,是权限认证链上的最后一环,对请求API资源对象进行修改和校验
  • 访问k8s的API Server的客户端主要分为两类:
    1.kubectl :用户家目录中的 .kube/config 里面保存了客户端访问API Server的密钥相关信息,这样当用kubectl访问k8s时,它就会自动读取该配置文件,向API Server发起认证,然后完成操作请求。
    2.pod:Pod中的进程需要访问API Server,如果是人去访问或编写的脚本去访问,这类访问使用的账号为:UserAccount;而Pod自身去连接API Server时,使用的账号是:ServiceAccount,生产中后者使用居多
  • 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的区别是:
    api它是一个特殊链接,只有在核心v1群组中的对象才能使用。
    apis 它是一般API访问的入口固定格式名。

二、UserAccount与serviceaccount(sa)

  • UserAccount与serviceaccount:
    用户账户是针对人而言的。 服务账户是针对运行在 pod 中的进程而言的。
    用户账户是全局性的。 其名称在集群各 namespace 中都是全局唯一的,未来的用户资源不会做 namespace 隔离, 服务账户是 namespace 隔离的。
    通常情况下,集群的用户账户可能会从企业数据库进行同步,其创建需要特殊权限,并且涉及到复杂的业务流程。 服务账户创建的目的是为了更轻量,允许集群用户为了具体的任务创建服务账户 ( 即权限最小化原则 )。

1.serviceaccount:pod绑定sa

将认证信息添加到serviceAccount中,要比直接在Pod指定imagePullSecrets要安全很多。

[root@k8s2 ~]# kubectl create sa admin    ##创建sa

[root@k8s2 secret]# vim pod3.yaml         ##把serviceaccount和pod绑定起来:
apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  serviceAccountName: admin               ##指定sa,没有指定会拉取不到
  containers:
    - name: nginx
      image: nginx
[root@server2 secret]# kubectl apply -f pod3.yaml     ##
[root@server2 secret]# kubectl get pod 
NAME    READY   STATUS    RESTARTS   AGE
mypod   1/1     Running   0          12s
[root@server2 secret]# kubectl describe pod mypod
Volumes:
  admin-token-md4rv:                                   ##所要的sa认证信息
    Type:        Secret (a volume populated by a Secret)
    SecretName:  admin-token-md4rv

2.UserAccount

(1)认证

[root@k8s2 secret]# cd /etc/kubernetes/pki/
[root@k8s2 pki]# openssl genrsa -out test.key 2048                  ##生成指定key
[root@k8s2 pki]# openssl req -new -key test.key -out test.csr -subj "/CN=test"     ##生成证书请求
[root@k8s2 pki]# openssl  x509 -req -in test.csr -CA ca.crt -CAkey ca.key  -CAcreateserial -out test.crt -days 365     ##生成证书
[root@k8s2 pki]#  kubectl config set-credentials test --client-certificate=/etc/kubernetes/pki/test.crt --client-key=/etc/kubernetes/pki/test.key --embed-certs=true     ##生成用户、并设置认证至集群
[root@k8s2 pki]# kubectl config set-context test@kubernetes --cluster=kubernetes --user=test     ##设置账户
[root@k8s2 pki]# kubectl config view       ##查看配置内容
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://192.168.56.172:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
- context:
    cluster: kubernetes
    user: test
  name: test@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED
- name: test
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

切换用户test
[root@k8s2 pki]# kubectl config use-context test@kubernetes

[root@server2 pki]# kubectl get pod ##默认用户没有任何权限,需要授权
在这里插入图片描述
切回admin
[root@k8s2 pki]# kubectl config use-context kubernetes-admin@kubernetes

[root@k8s2 rbac]# vim roles.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"]

---
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
[root@k8s2 rbac]# kubectl apply -f roles.yaml
role.rbac.authorization.k8s.io/myrole created
rolebinding.rbac.authorization.k8s.io/test-read-pods created

[root@k8s2 rbac]# kubectl config use-context test@kubernetes
Switched to context "test@kubernetes".

[root@k8s2 rbac]# kubectl run demo --image nginx
pod/demo created
[root@k8s2 rbac]# kubectl get pod
NAME   READY   STATUS    RESTARTS   AGE
demo   1/1     Running   0          2s

现在只能操作pod资源,其它不行
在这里插入图片描述
切回admin
[root@k8s2 rbac]# kubectl config use-context kubernetes-admin@kubernetes

(2)授权

  • RBAC(Role Based Access Control):基于角色访问控制授权。
    允许管理员通过Kubernetes API动态配置授权策略。RBAC就是用户通过角色与权限进行关联。
    RBAC只有授权,没有拒绝授权,所以只需要定义允许该用户做什么即可。
    RBAC包括四种类型:Role、ClusterRole、RoleBinding、ClusterRoleBinding。
    在这里插入图片描述

  • RBAC的三个基本概念:
    Subject:被作用者,它表示k8s中的三类主体, user, group, serviceAccount
    Role:角色,它其实是一组规则,定义了一组对 Kubernetes API 对象的操作权限。
    RoleBinding:定义了“被作用者”和“角色”的绑定关系。

  • Role 和 ClusterRole
    Role是一系列的权限的集合,Role只能授予单个namespace 中资源的访问权限。
    ClusterRole 跟 Role 类似,但是可以在集群中全局使用。

  • RoleBinding和ClusterRoleBinding
    RoleBinding是将Role中定义的权限授予给用户或用户组。它包含一个subjects列表(users,groups ,service accounts),并引用该Role。
    RoleBinding是对某个namespace 内授权,ClusterRoleBinding适用在集群范围内使用。

[root@k8s2 rbac]# vim roles.yaml
kind: Role                                                    ##角色示例
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: default                                ##namespace
  name: myrole
rules:
- apiGroups: [""]
  resources: ["pods"]                                 ##设置什么样的资源具有什么样的权限
  verbs: ["get", "watch", "list", "create", "update", "patch", "delete"]

---
kind: RoleBinding                                             # 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                                           ##绑定的类型是role
  name: myrole                                      ##
  apiGroup: rbac.authorization.k8s.io

---
kind: ClusterRole                                          ###集群角色,针对全局
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: myclusterrole
rules:                                                       ##通过规则可以设置控制的内容
- apiGroups: [""] 
  resources: ["pods"]                            ##pod
  verbs: ["get", "watch", "list", "delete", "create", "update"]
- apiGroups: ["extensions", "apps"]
  resources: ["deployments"]                     ##控制器
  verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]

---
apiVersion: rbac.authorization.k8s.io/v1      ##绑定角色
kind: RoleBinding						#RoleBinding必须指定namespace;RoleBinding示例
metadata:
  name: rolebind-myclusterrole
  namespace:  default
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole                               ##绑定的是集群role
  name: myclusterrole
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: User
  name: test

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding				#ClusterRoleBinding全局授权,无需指定namespace
metadata:
  name: clusterrolebinding-myclusterrole
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole                      ##绑定的是集群,但是不需要指定namespace
  name: myclusterrole
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: User                                   ##
  name: test

[root@k8s2 rbac]# kubectl apply -f roles.yaml
role.rbac.authorization.k8s.io/myrole unchanged
rolebinding.rbac.authorization.k8s.io/test-read-pods unchanged
clusterrole.rbac.authorization.k8s.io/myclusterrole unchanged
rolebinding.rbac.authorization.k8s.io/rolebind-myclusterrole unchanged
clusterrolebinding.rbac.authorization.k8s.io/clusterrolebinding-myclusterrole created        

注意:
RoleBinding可以绑定角色和集群角色,但是必须指定namespace;
ClusterRoleBinding只能绑定集群角色,属于全局授权,无需指定namespace

使用test用户进行测试
[root@k8s2 rbac]# kubectl config use-context test@kubernetes
在这里插入图片描述
切回admin
[root@k8s2 rbac]# kubectl config use-context kubernetes-admin@kubernetes
回收
[root@k8s2 rbac]# kubectl delete -f roles.yaml

[root@k8s2 rbac]# kubectl config delete-user test
[root@k8s2 rbac]# kubectl config delete-context test@kubernetes

(3)服务账户的自动化

  • 服务账户的自动化
    服务账户准入控制器(Service account admission controller)
    如果该 pod 没有 ServiceAccount 设置,将其 ServiceAccount 设为 default。
    保证 pod 所关联的 ServiceAccount 存在,否则拒绝该 pod。
    如果 pod 不包含 ImagePullSecrets 设置,那么将 ServiceAccount 中的 ImagePullSecrets 信息添加到 pod 中。
    将一个包含用于 API 访问的 token 的 volume 添加到 pod 中。
    将挂载于 /var/run/secrets/kubernetes.io/serviceaccount 的 volumeSource 添加到 pod 下的每个容器中。
  • Token 控制器(Token controller)
    检测服务账户的创建,并且创建相应的 Secret 以支持 API 访问。
    检测服务账户的删除,并且删除所有相应的服务账户 Token Secret。
    检测 Secret 的增加,保证相应的服务账户存在,如有需要,为 Secret 增加 token。
    检测 Secret 的删除,如有需要,从相应的服务账户中移除引用。
  • 服务账户控制器(Service account controller)
    服务账户管理器管理各命名空间下的服务账户,并且保证每个活跃的命名空间下存在一个名为 “default” 的服务账户
  • Kubernetes 还拥有“用户组”(Group)的概念:
    ServiceAccount对应内置“用户”的名字是:
    system:serviceaccount:<ServiceAccount名字 >
    而用户组所对应的内置名字是:
    system:serviceaccounts:<Namespace名字 >
    示例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 来供用户直接使用:
cluster-amdin
admin
edit
view

  - 示例:(最佳实践)
  		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
  
  [root@server2 rbac]# kubectl describe clusterrole cluster-admin 
  [root@server2 rbac]# kubectl describe clusterrole view
  [root@server2 rbac]# kubectl describe clusterrole admin
  [root@server2 rbac]# kubectl describe clusterrole edit

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值