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


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

    #使用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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    api-server提供的特殊访问接口Kubenetes Proxy API接口 可以访问node节点数据

    #Kubenetes Proxy API接口 代理rest请求 转发到后端某个node上的kubelet进程的rest端口
    curl http://localhost:8111/api/v1/nodes/dev4-control-plane/proxy/pods
    
    • 1
    • 2

    k8s认证授权分两类
    一类是集群外访问apiServer 例如:cubectl客户端等 使用token,证书等进行认证
    一类是集群内部资源的认证授权例如对pod,svc等资源 使用serviceAccount认证
    每个pod在创建时 都会有个默认的认证信息 例如:

    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    k8s 默认使用serviceAccount管理权限
    serviceAccount本身不对权限管理 只是创建个账号 进行认证
    授权是rbac来管理

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

    创建serviceAccount

    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: nfs-client-provisioner
      namespace: default
    
    • 1
    • 2
    • 3
    • 4
    • 5
    #查看
    kubectl get sa
    
    • 1
    • 2

    对应pod

    apiVersion: v1
    kind: Pod
    metadata:
     name: sa-nginx-pod
     namespace: default
    spec:
     containers:
     - name: nginx-containers
       image: docker.io/library/nginx
     serviceAccountName: ssa-test
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    查看 kubectl 配置

    #查看kubectl 配置 包括连接的集群 账号等
    kubectl config view
    
    • 1
    • 2

    rbac授权方式

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    ClusterRole与Role区别 :
    ClusterRole不限制namespace
    Role:只能限制在一个namespace中

  • 相关阅读:
    ISO 5659-2塑料 烟生成 第2 部分:单室法测定烟密度试验方法
    android update_engine分析二
    西瓜书研读——第三章 线性模型:多元线性回归
    gitlab自动定时备份文件,备份失败发送邮件
    5G 安全评估流程指南
    MySQL数据库基础:数据类型详解-文本字符串类型
    Django之ORM
    合并二叉树(力扣617)
    配电房无人值守方案
    Ubuntu 系统如何修改时间
  • 原文地址:https://blog.csdn.net/ltgsoldier1/article/details/132651772