k8s-API 访问控制

打印 上一主题 下一主题

主题 910|帖子 910|积分 2730

目次
一 kubernetes API 访问控制
1.1 UserAccount与ServiceAccount
1.1.1 ServiceAccount
1.1.2 ServiceAccount示例:
二 认证(在k8s中创建认证用户)
2.1 创建UserAccount
2.2 RBAC(Role Based Access Control)
2.2.1 基于脚色访问控制授权:
2.2.2 role授权实施
2.2.3 clusterrole授权实施
2.2.4 服务账户的主动化

一 kubernetes API 访问控制

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资源对象举行修改和校验。
1.1 UserAccount与ServiceAccount



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



  • 服务账户控制器(Service account controller)

    • 服务账户管理器管理各定名空间下的服务账户
    • 每个生动的定名空间下存在一个名为 “default” 的服务账户

  • 服务账户准入控制器(Service account admission controller)

    • 相似pod中 ServiceAccount默认设为 default。
    • 包管 pod 所关联的 ServiceAccount 存在,否则拒绝该 pod。
    • 如果pod不包含ImagePullSecrets设置那么ServiceAccount中的ImagePullSecrets 被添加到pod中
    • 将挂载于 /var/run/secrets/kubernetes.io/serviceaccount 的 volumeSource 添加到 pod 下的每个容器中
    • 将一个包含用于 API 访问的 token 的 volume 添加到 pod 中

1.1.2 ServiceAccount示例:

创建名字为admin的ServiceAccount
  1. [root@k8s-master ~]# kubectl create sa howe
  2. serviceaccount/howe created
  3. [root@k8s-master ~]# kubectl get sa
  4. NAME      SECRETS   AGE
  5. default   0         7d9h
  6. howe      0         24m
  7. [root@k8s-master ~]# kubectl describe sa howe
  8. Name:                howe
  9. Namespace:           default
  10. Labels:              <none>
  11. Annotations:         <none>
  12. Image pull secrets:  <none>
  13. Mountable secrets:   <none>
  14. Tokens:              <none>
  15. Events:              <none>
复制代码
创建secrets
  1. [root@k8s-master ~]# kubectl create secret docker-registry docker-login --docker-username admin --docker-password redhat --docker-server reg.exam.com --docker-email howe@howe.com
  2. secret/docker-login created
  3. [root@k8s-master ~]# kubectl describe secrets docker-login
  4. Name:         docker-login
  5. Namespace:    default
  6. Labels:       <none>
  7. Annotations:  <none>
  8. Type:  kubernetes.io/dockerconfigjson
  9. Data
  10. ====
  11. .dockerconfigjson:  117 bytes
复制代码
将secrets注入到sa中
  1. [root@k8s-master ~]# kubectl edit sa howe
  2. apiVersion: v1
  3. imagePullSecrets:                        #注入
  4. - name: docker-login                #
  5. kind: ServiceAccount
  6. metadata:
  7.   creationTimestamp: "2024-09-10T12:10:28Z"
  8.   name: howe
  9.   namespace: default
  10.   resourceVersion: "329362"
  11.   uid: 94fb5724-b7fd-4464-841a-97cb01b802d1
  12.   
  13. [root@k8s-master ~]# kubectl describe sa howe
  14. Name:                howe
  15. Namespace:           default
  16. Labels:              <none>
  17. Annotations:         <none>
  18. Image pull secrets:  docker-login
  19. Mountable secrets:   <none>
  20. Tokens:              <none>
  21. Events:              <none>
复制代码
创建私有仓库并且使用pod访问私有仓库
  1. #已经建立名为api的仓库        上传nginx镜像
  2. [root@k8s-master ~]# docker tag nginx:latest reg.exam.com/api/nginx
  3. [root@k8s-master ~]# docker push reg.exam.com/api/nginx
  4. [root@k8s-master ~]# kubectl run testpod --image api/nginx
  5. pod/testpod created
  6. [root@k8s-master ~]# kubectl describe sa default
  7. Name:                default
  8. Namespace:           default
  9. Labels:              <none>
  10. Annotations:         <none>
  11. Image pull secrets:  <none>
  12. Mountable secrets:   <none>
  13. Tokens:              <none>
  14. Events:              <none>
  15. #如果显示为未拉取 是因为认证未通过 未将secrets注入到sa中
  16. [root@k8s-master ~]# kubectl get pods
  17. NAME                READY           STATUS                            RESTARTS      AGE
  18. testpod                0/1             ImagePullBackOff          0             25s
  19. #如果认证通过 会显示running
  20. [root@k8s-master ~]# kubectl get pods
  21. NAME                        READY   STATUS    RESTARTS      AGE
  22. testpod                     1/1     Running   0             25s
复制代码
  在创建pod时会镜像下载会受阻,由于docker私有仓库下载镜像必要认证
  pod绑定sa
  1. [root@k8s-master auth]# vim example1.yml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   name: testpod
  6. spec:
  7.   serviceAccountName: howe                        #指定了这个 Pod 要使用的服务账号为 howe
  8.   containers:
  9.   - image: reg.exam.com/api/nginx:latest        #制定私有仓库
  10.     name: testpod
  11.    
  12. [root@k8s-master auth]# kubectl apply -f example1.yml
  13. pod/testpod created
  14. [root@k8s-master auth]# kubectl get pods
  15. NAME                        READY   STATUS    RESTARTS      AGE
  16. testpod                     1/1     Running   0             11s
复制代码



二 认证(在k8s中创建认证用户)

2.1 创建UserAccount

  1. #建立证书
  2. [root@k8s-master auth]# cd /etc/kubernetes/pki/
  3. [root@k8s-master pki]# openssl genrsa -out howe.key 2048        #建立key
  4. [root@k8s-master pki]# openssl req -new -key howe.key -out howe.csr -subj "/CN=superhowe"
  5. #openssl req -new:表示生成一个新的证书签名请求。
  6. -key howe.key:指定用于生成 CSR 的私钥文件为 howe.key。
  7. -out howe.csr:指定输出的 CSR 文件为 howe.csr。
  8. -subj "/CN=superhowe":设置证书的主题,这里指定通用名称(Common Name)为 superhowe
  9. #生成证书
  10. [root@k8s-master pki]# openssl x509 -req  -in howe.csr -CA ca.crt -CAkey ca.key -CAcreateserial  -out howe.crt -days 365
  11. Certificate request self-signature ok
  12. subject=CN = superhowe
  13. #查看证书
  14. [root@k8s-master pki]# openssl x509 -in howe.crt -text -noout
复制代码
创建k8s中的用户
  1. #建立k8s中的用户
  2. [root@k8s-master pki]# kubectl config set-credentials howe --client-certificate /etc/kubernetes/pki/howe.crt --client-key /etc/kubernetes/pki/howe.key --embed-certs=true
  3. User "howe" set.
  4. #补充参数 --embed-certs=true
  5. [root@k8s-master pki]# cat ~/.bash_profile
  6. export KUBECONFIG=/etc/kubernetes/admin.conf
  7. 表示将证书嵌入到配置中,这样配置文件可以更加自包含,无需依赖外部的证书文件
  8. [root@k8s-master pki]# kubectl config view
  9. apiVersion: v1
  10. clusters:
  11. - cluster:
  12.     certificate-authority-data: DATA+OMITTED
  13.     server: https://172.25.250.100:6443
  14.   name: kubernetes
  15. contexts:
  16. - context:
  17.     cluster: kubernetes
  18.     user: kubernetes-admin
  19.   name: kubernetes-admin@kubernetes
  20. current-context: kubernetes-admin@kubernetes
  21. kind: Config
  22. preferences: {}
  23. users:
  24. - name: howe
  25.   user:
  26.     client-certificate-data: DATA+OMITTED
  27.     client-key-data: DATA+OMITTED
  28. - name: kubernetes-admin
  29.   user:
  30.     client-certificate-data: DATA+OMITTED
  31.     client-key-data: DATA+OMITTED
复制代码
 切换用户,用户在集群中只有效户身份没有授权
  1. #切换 Kubernetes 上下文到名为 howe@kubernetes 的上下文
  2. [root@k8s-master pki]# kubectl config use-context howe@kubernetes
  3. error: no context exists with the name: "howe@kubernetes"
  4. #以上报错不存在  需要先设置上下文
  5. [root@k8s-master pki]# kubectl config set-context howe@kubernetes --cluster kubernetes --user howe
  6. Context "howe@kubernetes" created.
  7. [root@k8s-master pki]# kubectl config use-context howe@kubernetes
  8. Switched to context "howe@kubernetes".
  9. #只通过认证 并未集群授权
  10. [root@k8s-master pki]# kubectl get pods
  11. Error from server (Forbidden): pods is forbidden: User "superhowe" cannot list resource "pods" in API group "" in the namespace "default"
复制代码
切换会集群管理
  1. [root@k8s-master pki]# kubectl config use-context kubernetes-admin@kubernetes
  2. Switched to context "kubernetes-admin@kubernetes".
复制代码
如果必要删除用户
  1. [root@k8s-master pki]# kubectl config delete-user howe
  2. deleted user howe from /etc/kubernetes/admin.conf
复制代码

 
2.2 RBAC(Role Based Access Control)

2.2.1 基于脚色访问控制授权:



  • 允许管理员通过Kubernetes API动态配置授权计谋。RBAC就是用户通过脚色与权限举行关联。
  • RBAC只有授权,没有拒绝授权,所以只必要定义允许该用户做什么即可
  • RBAC的三个基本概念

    • Subject:被作用者,它表现k8s中的三类主体, user, group, serviceAccount



  • Role:脚色,它其实是一组规则,定义了一组对 Kubernetes API 对象的操作权限。


  • RoleBinding:定义了“被作用者”和“脚色”的绑定关系


  • RBAC包罗四种类型:Role、ClusterRole、RoleBinding、ClusterRoleBinding
  • Role 和 ClusterRole

    • Role是一系列的权限的集合,Role只能授予单个namespace 中资源的访问权限。



  • ClusterRole 跟 Role 雷同,但是可以在集群中全局使用。


  • Kubernetes 还提供了四个预先定义好的 ClusterRole 来供用户直接使用


  • cluster-amdin、admin、edit、view
2.2.2 role授权实施

生成role的yaml文件
  1. [root@k8s-master rbac]# kubectl create role myrole --dry-run=client --verb=get --resource pods -o yaml > myrole.yml
复制代码
更改文件内容
  1. [root@k8s-master rbac]# vim myrole.yml
  2. apiVersion: rbac.authorization.k8s.io/v1
  3. kind: Role
  4. metadata:
  5.   creationTimestamp: null
  6.   name: myrole
  7. rules:
  8. - apiGroups:
  9.   - ""
  10.   resources:
  11.   - pods
  12.   verbs:
  13.   - get
  14.   - watch
  15.   - list
  16.   - create
  17.   - update
  18.   - path
  19.   - delete
复制代码
创建role
  1. [root@k8s-master rbac]# kubectl apply -f myrole.yml
  2. role.rbac.authorization.k8s.io/myrole created
  3. [root@k8s-master rbac]# kubectl describe role myrole
  4. Name:         myrole
  5. Labels:       <none>
  6. Annotations:  <none>
  7. PolicyRule:
  8.   Resources  Non-Resource URLs  Resource Names  Verbs
  9.   ---------  -----------------  --------------  -----
  10.   pods       []                 []              [get watch list create update path delete]
复制代码
创建脚色绑定
  1. [root@k8s-master rbac]# kubectl create rolebinding howe --role myrole --namespace default --user howe --dry-run=client -o yaml > rolebinding-myrole.yml
  2. [root@k8s-master rbac]# vim rolebinding-myrole.yml
  3. apiVersion: rbac.authorization.k8s.io/v1
  4. kind: RoleBinding
  5. metadata:
  6.   name: howe
  7.   namespace: default                                #角色绑定必须指定namespace
  8. roleRef:
  9.   apiGroup: rbac.authorization.k8s.io
  10.   kind: Role
  11.   name: myrole
  12. subjects:
  13. - apiGroup: rbac.authorization.k8s.io
  14.   kind: User
  15.   name: howe
  16.   
  17. [root@k8s-master rbac]# kubectl apply -f rolebinding-myrole.yml
  18. rolebinding.rbac.authorization.k8s.io/howe created
  19. [root@k8s-master rbac]# kubectl get rolebindings.rbac.authorization.k8s.io howe
  20. NAME   ROLE          AGE
  21. howe   Role/myrole   17s
复制代码
 切换用户测试授权
  1. [root@k8s-master rbac]# kubectl config use-context howe@kubernetes
  2. Switched to context "howe@kubernetes".
  3. [root@k8s-master rbac]# kubectl get pods
  4. No resources found in default namespace.
  5. #只针对pod进行了授权,所以svc依然不能操作
  6. [root@k8s-master rbac]# kubectl get svc
  7. Error from server (Forbidden): services is forbidden: User "system:anonymous" cannot list resource "services" in API group "" in the namespace "default"
  8. #切换回管理员
  9. [root@k8s-master rbac]# kubectl config use-context kubernetes-admin@kubernetes
  10. Switched to context "kubernetes-admin@kubernetes".
复制代码

2.2.3 clusterrole授权实施

创建集群脚色
  1. [root@k8s-master rbac]# kubectl create clusterrole myclusterrole --resource=deployment --verb get --dry-run=client -o yaml > myclusterrole.yml
  2. [root@k8s-master rbac]# vim myclusterrole.yml
  3. apiVersion: rbac.authorization.k8s.io/v1
  4. kind: ClusterRole
  5. metadata:
  6.   name: myclusterrole
  7. rules:
  8. - apiGroups:
  9.   - apps
  10.   resources:
  11.   - deployments
  12.   verbs:
  13.   - get
  14.   - list
  15.   - watch
  16.   - create
  17.   - update
  18.   - path
  19.   - delete
  20. - apiGroups:
  21.   - ""
  22.   resources:
  23.   - pods
  24.   verbs:
  25.   - get
  26.   - list
  27.   - watch
  28.   - create
  29.   - update
  30.   - path
  31.   - delete
  32. [root@k8s-master rbac]# kubectl apply -f myclusterrole.yml
  33. clusterrole.rbac.authorization.k8s.io/myclusterrole created
  34. [root@k8s-master rbac]# kubectl describe clusterrole myclusterrole
  35. Name:         myclusterrole
  36. Labels:       <none>
  37. Annotations:  <none>
  38. PolicyRule:
  39.   Resources         Non-Resource URLs  Resource Names  Verbs
  40.   ---------         -----------------  --------------  -----
  41.   pods              []                 []              [get list watch create update path delete]
  42.   deployments.apps  []                 []              [get list watch create update path delete]
复制代码
创建集群脚色绑定
  1. [root@k8s-master rbac]# kubectl create clusterrolebinding clusterrolebind-myclusterrole --clusterrole myclusterrole --user howe --dry-run=client -o yaml > clusterrolebind-myclusterrole.yml
  2. [root@k8s-master rbac]# vim clusterrolebind-myclusterrole.yml
  3. apiVersion: rbac.authorization.k8s.io/v1
  4. kind: ClusterRoleBinding
  5. metadata:
  6.   name: clusterrolebind-myclusterrole
  7. roleRef:
  8.   apiGroup: rbac.authorization.k8s.io
  9.   kind: ClusterRole
  10.   name: myclusterrole
  11. subjects:
  12. - apiGroup: rbac.authorization.k8s.io
  13.   kind: User
  14.   name: howe
  15. [root@k8s-master rbac]# kubectl apply -f clusterrolebind-myclusterrole.yml
  16. clusterrolebinding.rbac.authorization.k8s.io/clusterrolebind-myclusterrole created
  17. [root@k8s-master rbac]# kubectl describe clusterrolebindings.rbac.authorization.k8s.io clusterrolebind-myclusterrole
  18. Name:         clusterrolebind-myclusterrole
  19. Labels:       <none>
  20. Annotations:  <none>
  21. Role:
  22.   Kind:  ClusterRole
  23.   Name:  myclusterrole
  24. Subjects:
  25.   Kind  Name  Namespace
  26.   ----  ----  ---------
  27.   User  howe  
复制代码
测试:
  1. [root@k8s-master rbac]# kubectl get pods  -A
  2. [root@k8s-master rbac]# kubectl get deployments.apps -A
  3. [root@k8s-master rbac]# kubectl get svc -A
复制代码

2.2.4 服务账户的主动化

服务账户准入控制器(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 下的每个容器中。
服务账户控制器(Service account controller)
服务账户管理器管理各定名空间下的服务账户,并且包管每个生动的定名空间下存在一个名为 “default” 的服务账户








免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

玛卡巴卡的卡巴卡玛

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表