k8s基于rbac权限管理serviceAccount授权管理

测试通过http访问apiServer

curl没有证书不能通过https来访问apiServer需要使用kubectl代理

bash 复制代码
#使用kubectl代理
kubectl proxy --port=8111&
#curl访问 api/v1 是资源所属群组/版本 即创建资源时定义的apiVersion
#后边跟的是要访问的资源
#查看所有命名空间
#查看核心资源用api 其他都用apis/apps
curl http://localhost:8111/api/v1/namespaces
#查看kube-system命名空间下的所有deployments
curl http://localhost:8111/apis/apps/v1/namespaces/kube-system/deployments
#查看具体deployment
curl http://localhost:8111/apis/apps/v1/namespaces/kube-system/deployments/coredns

k8s认证授权分两类

一类是集群外访问apiServer 例如:cubectl客户端等 使用token,证书等进行认证

一类是集群内部资源的认证授权例如对pod,svc等资源 使用serviceAccount认证

每个pod在创建时 都会有个默认的认证信息 例如:

bash 复制代码
Volumes:
  kube-api-access-gm7dh:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true

k8s 默认使用serviceAccount管理权限

serviceAccount本身不对权限管理 只是创建个账号 进行认证

授权是rbac来管理

bash 复制代码
#创建serviceAccount 加上参数 -o yaml --dry-run 不会执行操作 而是会返回个yaml文件 我们可以在这个文件基础上进行修改方便编辑yaml文件
kubectl create serviceaccount sacc -o yaml --dry-run
# 

创建serviceAccount

bash 复制代码
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner
  namespace: default
bash 复制代码
#查看
kubectl get sa

对应pod

bash 复制代码
apiVersion: v1
kind: Pod
metadata:
 name: sa-nginx-pod
 namespace: default
spec:
 containers:
 - name: nginx-containers
   image: docker.io/library/nginx
 serviceAccountName: ssa-test

查看 kubectl 配置

bash 复制代码
#查看kubectl 配置 包括连接的集群 账号等
kubectl config view

rbac授权方式

bash 复制代码
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: cluster-role-test
rules:
  - apiGroups: [""]
    resources: ["persistentvolumes"]
    verbs: ["get", "list", "watch", "create", "delete"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["get", "list", "watch", "update"]
  - apiGroups: ["storage.k8s.io"]
    resources: ["storageclasses"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["create", "update", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: cluster-role-binding-test
subjects: #sa对象绑定到角色
  - kind: ServiceAccount
    name: ssa-test #sa对象
    namespace: default
roleRef:
  kind: ClusterRole
  name: cluster-role-test #角色
  apiGroup: rbac.authorization.k8s.io

ClusterRole与Role区别 :

ClusterRole不限制namespace

Role:只能限制在一个namespace中

相关推荐
底层玩家老张2 分钟前
数据库上K8s,运维为什么反而更累了?从StatefulSet到Operator的复盘
数据库·kubernetes·operator·数据库运维·架构选型
力江1 小时前
GitLab 私有化部署实战 —— 从 Omnibus 到 Docker 的踩坑之旅
docker·容器·gitlab
深念Y1 小时前
Docker 镜像体积从 110MB 降到 13MB 后端服务的瘦身
运维·docker·容器
秦jh_3 小时前
【Docker】镜像仓库
docker·容器
IT大白鼠3 小时前
使用 Docker 部署 Kubernetes 集群:容器方式搭建 K8s 环境
docker·容器·kubernetes
大大大大晴天️6 小时前
从 HDFS 到对象存储:计算存储分离如何重塑云原生大数据底座
大数据·云原生
阿里云云原生1 天前
Agent 观测与优化三城巡演丨第二站深圳精彩回顾 & PPT 下载
云原生
Henry-SAP1 天前
AI产业迎来标准化与商业化双突破
人工智能·云原生·sap·erp
IT大白鼠1 天前
Docker 实战 ELKF 日志监控:容器日志全栈收集分析方案
运维·docker·容器
报错小能手2 天前
Kubernetes入门实战课 3
云原生·容器·kubernetes