k8s--Kubernetes存储--kubernetes访问控制
创始人
2025-05-28 02:10:11
0

文章目录

  • 一、k8s访问控制介绍
  • 二、UserAccount与serviceaccount(sa)
    • 1.serviceaccount:pod绑定sa
    • 2.UserAccount
      • (1)认证
      • (2)授权
      • (3)补充


一、k8s访问控制介绍

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

  • 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资源对象进行修改和校验。
  • 访问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: nginximage: 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+OMITTEDserver: https://192.168.56.172:6443name: kubernetes
contexts:
- context:cluster: kubernetesuser: kubernetes-adminname: kubernetes-admin@kubernetes
- context:cluster: kubernetesuser: testname: test@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-adminuser:client-certificate-data: REDACTEDclient-key-data: REDACTED
- name: testuser:client-certificate-data: REDACTEDclient-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: defaultname: 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-podsnamespace: default
subjects:
- kind: Username: testapiGroup: rbac.authorization.k8s.io
roleRef:kind: Rolename: myroleapiGroup: 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                                                    ##role示例
apiVersion: rbac.authorization.k8s.io/v1
metadata:namespace: defaultname: 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-podsnamespace: default
subjects:
- kind: Username: testapiGroup: rbac.authorization.k8s.io
roleRef:kind: Role                                           ##绑定的类型是rolename: myroleapiGroup: rbac.authorization.k8s.io---
kind: ClusterRole                                          #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"]---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding						#RoleBinding必须指定namespace;RoleBinding示例
metadata:name: rolebind-myclusterrolenamespace:  default
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRole                               ##绑定的是集群rolename: myclusterrole
subjects:
- apiGroup: rbac.authorization.k8s.iokind: Username: test---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding				#ClusterRoleBinding全局授权,无需指定namespace
metadata:name: clusterrolebinding-myclusterrole
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRole                      ##绑定的是集群,但是不需要指定namespacename: myclusterrole
subjects:
- apiGroup: rbac.authorization.k8s.iokind: Username: 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

使用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:
    而用户组所对应的内置名字是:
    system:serviceaccounts:
    示例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-amdinadmineditview- 示例:(最佳实践)kind: RoleBindingapiVersion: rbac.authorization.k8s.io/v1metadata:name: readonly-defaultsubjects:- kind: ServiceAccountname: defaultnamespace: defaultroleRef:kind: ClusterRolename: viewapiGroup: 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

相关内容

热门资讯

“国补”继续!10月将下达第四... 8月1日国家发展改革委召开新闻发布会,解读当前经济形势和经济工作。国家发展改革委政策研究室有关负责人...
港股IPO,重大调整! 8月1日,港交所就优化首次公开招股市场定价及公开市场规定的咨询文件刊发咨询总结,并就持续优化公众持股...
两江新区:148家智慧医疗装备... 近日,江小妹获悉, 2025年上半年, 作为重庆高新产业的“主阵地”, 两江新区生命健康产业增长势头...
造纸江湖浮沉:废纸回收价5年内... 本报(chinatimes.net.cn)记者何一华 李未来 北京报道 “最近收废纸的价格涨了,以前...
国邦医药已累计回购538万股公... 8月1日晚间,国邦医药发布公告称,截至7月31日,公司通过集中竞价交易方式累计回购股份537.527...
融资杠杆误区:满仓加杠杆不对,... 融资杠杆作为资本市场中通过借入资金放大投资规模的工具,其核心是借助信用机制扩大交易筹码,在提升资金使...
“反内卷”的风吹到了惠民保 记者 姜鑫 7月31日晚,国家金融监督管理总局办公厅发布了《关于推动城市商业医疗险高质量发展的通知》...
酒类即时零售调查:市场规模突破... 出品|搜狐财经 作者|饶婷 编辑|李文贤 【编者按】当数字浪潮席卷消费市场,酒业的 “触网” 之路早...
股票行情快报:赫美集团(002... 证券之星消息,截至2025年8月1日收盘,赫美集团(002356)报收于3.2元,上涨0.31%,换...
刘庆峰说马斯克不懂AI:科大讯... 文:互联网江湖 刘致呈 敢直言马斯克不懂AI的人,出现了。 最近,科大讯飞董事长刘庆峰在与正和岛总...
国债等利息收入增值税恢复征收,... 界面新闻记者 | 杨志锦 界面新闻编辑 | 王姝 财政部、税务总局8月1日发布公告称,自2025...
历史重演?美国7月非农暴雷,美... 来源:第一财经 作为本周最受关注的数据,美国7月非农就业报告意外不及预期,加之此前两个月数据大幅下修...
8月8日起,新发行国债等债券的... 两部门发文恢复征收国债、地方债、金融债券利息收入增值税 财政部、国家税务总局1日联合对外发布公告称,...
首个旧改做地转化宅地 广州市客... 观点网7月31日,广州越秀区挂牌市客运站商住地块,计划于9月1日10时竞价。 该地块位于环市路以南,...
就业引擎熄火,美联储还能“稳住... 来源:市场资讯 来源:汇通网 汇通财经APP讯——7月美国非农就业数据大幅低于预期,仅新增7.3万个...
【公告精选】601088,筹划... 中国神华:筹划发行股份及支付现金购买资产并募集配套资金 股票8月4日起停牌 九号公司:上半年净利12...
原创 一... “老铁们,这周行情像不像坐过山车?指数蔫了吧唧,个股却玩起高空蹦极!南新制药一周暴涨78%,市值差点...
星巴克中国业务竞购名单曝光!腾... 《科创板日报》8月1日讯(记者 徐赐豪),星巴克中国业务的未来发展又有了新变数。 据报道,有知情人士...
带娃去四川三天两夜旅游攻略!成... 我一直梦想着带家人来一场说走就走的旅行,而四川,这个充满魅力的地方,就成了我们的首选。四川不仅有壮丽...
江苏银行:截至目前绿色融资余额... 8月1日,上证报记者从江苏银行获悉,截至目前,该行绿色融资余额已突破7000亿元,服务绿色企业超12...