• k8s安全机制


    目录

    一、kubernetes安全机制

    二、Authentication认证

    2.1 用户分类

    2.2 kubeconfig文件

    2.3 Authorization授权

    2.4 授权模块

    2.5 RBAC

    2.5.1  角色

    2.5.2 角色绑定

    2.5.3 主体 (subject)

    2.6  Admission Control准入控制

    2.7 LimitRanger示例

    2.8 ResourceQuota示例

    三、User访问案例 

    3.1 创建k8s用户(User)

    3.1.1  准备工作

    3.1.2  创建证书

    3.1.3 创建用户配置文件 

    3.2 创建系统用户 

    3.3 验证用户访问k8s资源

    3.4 创建role 

    3.5 创建rolebinding 

    3.6 再验证用户访问k8s资源 

    3.7 创建ClusterRole

    3.8 创建ClusterRoleBinding 

    3.9 再验证用户访问k8s资源 

    四、ServiceAccount访问案例 

    4.1 创建namespace

    4.2 验证namespace的sa与secret 

    4.3 创建pod验证其sa与secret 

    4.4 创建一个sa 

    4.5 创建pod并自定义sa 

    4.6 验证pod使用的sa 

    一、kubernetes安全机制

    api server是k8s集群的入口,默认有两个端口:

    • 本地端口8080: 用于接收HTTP请求, 不对外服务, 非认证或授权的HTTP请求通过该端口访问API Server

    • 安全端口6443: 用于接收认证授权的HTTPS请求,对外服务。

    用户通过安全端口访问k8s的api server需要过三关:认证、授权、准入控制

    • Authentication认证: 用于识别用户身份, 方式有: SSL证书,token, 用户名+密码等
    • Authorization授权: 确认是否对资源具有相关的权限
    • Admission Control准入控制: 判断操作是否符合集群的要求

    在这里插入图片描述

    无论kubectl客户端命令或api或ui,在创建或访问资源时,一定要有apiServer所对应的资源版本,只要资源版本与kind能够正确匹配才能进行继续操作,否侧操作会被终止。

    二、Authentication认证

    Authentication认证: 用于识别用户身份, 验证方式有:

    • SSL证书
    • token
    • 用户名+密码

    2.1 用户分类

    用户有两种:

    • 用户账户(user) : 是在集群外部访问apiserver时使用的用户,如kubectl命令就是作为kubernetes的admin用户来执行的。
    • 服务账户(ServiceAccount): 为了方便Pod里面的进程调用Kubernetes API或其他外部服务而设计的。
    • User account是为人设计的,而service account则是为Pod中的进程调用Kubernetes API而设计;
    • User account是跨namespace的,而service account则是仅局限它所在的namespace;
    • 每个namespace都会自动创建一个default service account

    参考: https://www.kubernetes.org.cn/service-account

    2.2 kubeconfig文件

    • kubeconfig文件用于组织有关群集、用户、命名空间和身份验证机制的信息,用于对k8s集群的访问。
    • 使用kubectl命令时默认使用~/.kube/config这个kubeconfig文件来访问k8s集群,也可以用--kubeconfig指定其它文件

    一个kubeconfig文件通常由以下几个配置段组成:

    • users: 用户账号及其认证信息列表
    • cluster: 目标集群列表
    • contexts: 以哪个user接入哪个cluster的连接组合。
    • current-context:当前使用的context

    示例: 查看admin用户的kubeconfig文件

    1. [root@master1 ~]# cat /root/.kube/config
    2. apiVersion: v1
    3. clusters:
    4. - cluster:
    5. certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUR1RENDQXFDZ0F3SUJBZ0lVRXJCMXhTc2NDMUV3V2lkckc3UEJXT29HSkFFd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1lURUxNQWtHQTFVRUJoTUNRMDR4RVRBUEJnTlZCQWdUQ0VoaGJtZGFhRzkxTVFzd0NRWURWUVFIRXdKWQpVekVNTUFvR0ExVUVDaE1EYXpoek1ROHdEUVlEVlFRTEV3WlRlWE4wWlcweEV6QVJCZ05WQkFNVENtdDFZbVZ5CmJtVjBaWE13SUJjTk1qQXhNVEEwTVRNek5qQXdXaGdQTWpFeU1ERXdNVEV4TXpNMk1EQmFNR0V4Q3pBSkJnTlYKQkFZVEFrTk9NUkV3RHdZRFZRUUlFd2hJWVc1bldtaHZkVEVMTUFrR0ExVUVCeE1DV0ZNeEREQUtCZ05WQkFvVApBMnM0Y3pFUE1BMEdBMVVFQ3hNR1UzbHpkR1Z0TVJNd0VRWURWUVFERXdwcmRXSmxjbTVsZEdWek1JSUJJakFOCkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQXpZNWhMTDhTYXU2MGVpNDR6RTlhNVdD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lsK0NjSXN6dEtUVWJBQ01LT1dYbUZXRk1uQVFZdnhxN2hIR25IaXpWb3k1VHNxVkZQRUE9Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
    6. server: https://192.168.122.11:6443
    7. name: cluster1
    8. contexts:
    9. - context:
    10. cluster: cluster1
    11. user: admin
    12. name: context-cluster1-admin
    13. current-context: context-cluster1-admin
    14. kind: Config
    15. preferences: {}
    16. users:
    17. - name: admin
    18. user:
    19. client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUQxekNDQXIrZ0F3SUJBZ0lVYUZlakwzVXErUTJqWnJIbzB4cmFaUVAyY3pZd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1lURUxNQWtHQTFVRUJoTUNRMDR4RVRBUEJnTlZCQWdUQ0VoaGJtZGFhRzkxTVFzd0NRWURWUVFIRXdKWQpVekVNTUFvR0ExVUVDaE1EYXpoek1ROHdEUVlEVlFRTEV3WlRlWE4wWlcweEV6QVJCZ05WQkFNVENtdDFZbVZ5CmJtVjBaWE13SUJjTk1qQXhNVEEwTVRNek9UQXdXaGdQTWpBM01ERXdNak14TXpNNU1EQmFNR2N4Q3pBSkJnTlYKQkFZVEFrTk9NUkV3RHdZRFZRUUlFd2hJWVc1bldtaHZkVEVMTUFrR0ExVUVCeE1DV0ZNeEZ6QVZCZ05WQkFvVApEbk41YzNSbGJUcHRZWE4wWlhKek1ROHdEUVlEVlFRTEV3WlRlWE4wWlcweERqQU1CZ05WQkFNVEJXRmtiV2x1Ck1JSUJJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBUThBTUlJQkNnS0NBUUVBNHdyc0dnSnE0T00zcUV5elE2Q3AKZGRjaWlUTnNuRG1odzBjcUwyWG1CTnA4cmxSUkFiUWY3QmNpTVRPVTF5TUdTYWo4YW9TNGhjUVFqcWpVRXRBKwphUVpOTXRPTXppZVZMMHpEZzMrV09Qb3o3MGdyeEU2QzFGVE5XcGFWRjljUUFWV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kx6TUJKdStoSSswN2pHUnBGanp1TDFPMVorOQpXZ0lxS2tKOW5DN3VSWEtxUmxBTXZTVVVjcUtLTUZSTWtBZEpmOUZHNEdVaHFSZUdhUlp2Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==

    2.3 Authorization授权

    认证环节之后是授权, 确认是否对资源具有相关的权限

    一个请求需要在其请求信息中包含用户名,请求的动作以及目标对象; 若存在某授权策略对于此请求给予了授权许可,即授权成功。

    2.4 授权模块

    • Node: 根据Pod对象调度的结果为Node进行授权。
    • ABAC(Attribute-based access control): 基于属性的访问控制(ABAC)定义了一种访问控制模式,通过使用将属性组合在一起的策略,将访问权限授予用户。这些策略可以使用任何类型的属性(用户属性、资源属性、对象、环境属性等)。
    • RBAC(Role-based access control): 基于角色的访问控制(RBAC)是一种基于企业中各个用户的角色来管理对计算机或网络资源的访问的方法。使用"rbac.authorization.k8s.io" API驱动授权策略,并支持动态配置。
    • Webhook: WebHook其实就是一个HTTP回调:在发生某些事情时发生的HTTP POST;通过HTTP POST的简单事件通知。实现WebHooks的web应用程序将在发生某些事情时向URL发送消息。

    2.5 RBAC

    在这里插入图片描述

    2.5.1  角色

    • Role 授权特定命名空间的访问权限

    • ClusterRole 授权所有命名空间的访问权限

    2.5.2 角色绑定

    • RoleBinding 将角色绑定到主机(即subject)

    • ClusterRoleBinding 将集群角色绑定到主体

    2.5.3 主体 (subject)

    • User 用户
    • Group 用户组
    • ServiceAccount 服务帐号

    2.6  Admission Control准入控制

    通过认证和鉴权之后,客户端并不能得到API Server的真正响应,这个请求还需通过Admission Control所控制的一个准入控制插件列表的层层考验。

    准入控制器

    Admission Control配备有一个“准入控制器”的插件列表,发送给API Server的任何请求都需要通过列表中每一个准入控制器的检查,检查不通过API Server拒绝此调用请求。

    此外,准入控制器还能够修改请求参数以完成一些自动化的任务,比如Service Account这个控制器。

    当前可配置的Admission Control准入控制如下:

    • AlwaysAdmit:允许所有请求;
    • AlwaysPullmages:在启动容器之前总去下载镜像,相当于在每个容器的配置项imagePullPolicy=Always
    • AlwaysDeny:禁止所有请求,一般用于测试;
    • DenyExecOnPrivileged:它会拦截所有想在Privileged Container上执行命令的请求,如果你的集群支持Privileged Container,你又希望限制用户在这些Privileged Container上执行命令,强烈推荐你使用它,其功能已经合并到DenyEscalatingExec中。
    • ImagePolicyWebhook:这个插件将允许后端的一个Webhook程序来完成admission controller的功能。ImagePolicyWebhook需要使用一个配置文件(通过kube-apiserver的启动参数–admission-control-config-file设置)定义后端Webhook的参数。目前该插件还处在Alpha版本。
    • Service Account:这个plug-in将ServiceAccount实现了自动化,默认启用,如果你想使用ServiceAccount对象,那么强烈推荐使用它。
    • SecurityContextDeny:这个插件将使用SecurityContext的Pod中的定义全部失效。SecurityContext在Container中定义了操作系统级别的安全设定(uid,gid,capabilityes,SELinux等)。在未启用PodSecurityPolicy的集群中建议启用该插件,以禁用容器设置的非安全访问权限。
    • ResourceQuota:用于资源配额管理目的,作用于namespace上,它会观察所有请求,确保在namespace上的配额不会超标。推荐在Admission Control参数列表中将这个插件安排在最后一个,以免可能被其他插件拒绝的Pod被过早分配资源。
    • LimitRanger:用于资源限制管理,作用于namespace上,确保对Pod进行资源限制。启用该插件还会为未设置资源限制的Pod进行默认设置,例如为namespace "default"中所有的Pod设置0.1CPU的资源请求。
    • InitialResources:是一个实验特性,旨在为未设置资源请求与限制的Pod,根据其镜像的历史资源的使用情况进行初始化的资源请求、限制设置。
    • NamespaceLifecycle:如果尝试在一个不存在的namespace中创建资源对象,则该创建请求将被拒绝。当删除一个namespace时,系统将会删除该namespace中所有对象,保存Pod,Service等。
    • DefaultStorageClass:为了实现共享存储的动态供应,为未指定StorageClass或PV的PVC尝试匹配默认的StorageClass,尽可能减少用户在申请PVC时所需了解的后端存储细节。
    • DefaultTolerationSeconds:这个插件为那些没有设置forgiveness tolerations并具有notready:NoExecute和unreachable:NoExecute两种taints的Pod设置默认的“容忍”时间,为5min。
    • PodSecurityPolicy:这个插件用于在创建或修改Pod时决定是否根据Pod的security context和可用的PodSecurityPolicy对Pod的安全策略进行控制。

    2.7 LimitRanger示例

    1. [root@master1 ~]# kubectl create ns limitrange
    2. namespace/limitrange created
    3. [root@master1 ~]# kubectl describe ns limitrange
    4. Name: limitrange
    5. Labels: <none>
    6. Annotations: <none>
    7. Status: Active
    8. No resource quota.
    9. No LimitRange resource. 看到无LimitRange
    1. [root@master1 ~]# vim limitrange.yml
    2. apiVersion: v1
    3. kind: LimitRange
    4. metadata:
    5. name: cpu-limit-range
    6. namespace: limitrange
    7. spec:
    8. limits:
    9. - default:
    10. cpu: 1000m
    11. defaultRequest:
    12. cpu: 1000m
    13. min:
    14. cpu: 500m
    15. max:
    16. cpu: 2000m
    17. maxLimitRequestRatio:
    18. cpu: 4
    19. type: Container
    1. [root@master1 ~]# kubectl apply -f limitrange.yml
    2. limitrange/cpu-limit-range created
    1. [root@master1 ~]# kubectl describe ns limitrange
    2. Name: limitrange
    3. Labels: <none>
    4. Annotations: <none>
    5. Status: Active
    6. No resource quota.
    7. Resource Limits
    8. Type Resource Min Max Default Request Default Limit Max Limit/Request Ratio
    9. ---- -------- --- --- --------------- ------------- -----------------------
    10. Container cpu 500m 2 1 1 4
    1. [root@master1 ~]# vim limitrange-pod.yml
    2. apiVersion: v1
    3. kind: Pod
    4. metadata:
    5. name: nginx
    6. namespace: limitrange
    7. spec:
    8. containers:
    9. - name: c1
    10. image: nginx:1.15-alpine
    11. imagePullPolicy: IfNotPresent
    12. resources:
    13. requests:
    14. cpu: 200m # cpu请求200m,小于namespace的cpu最小限制(500m)
    1. [root@master1 ~]# kubectl apply -f limitrange-pod.yml
    2. Error from server (Forbidden): error when creating "limitrange-pod.yml": pods "nginx" is forbidden: [minimum cpu usage per Container is 500m, but request is 200m, cpu max limit to request ratio per Container is 4, but provided ratio is 5.000000]

    说明: 可看到namespace的cpu最小限制是500m,创建此pod请求为200m,准入控制拒绝了。 改成限制范围内就可以创建。

    1. [root@master1 ~]# vim limitrange-pod.yml
    2. apiVersion: v1
    3. kind: Pod
    4. metadata:
    5. name: nginx
    6. namespace: limitrange
    7. spec:
    8. containers:
    9. - name: c1
    10. image: nginx:1.15-alpine
    11. imagePullPolicy: IfNotPresent
    12. resources:
    13. requests:
    14. cpu: 600m # 改成大于500m
    1. [root@master1 ~]# kubectl apply -f limitrange-pod.yml
    2. pod/nginx created
    3. [root@master1 ~]# kubectl get pods -n limitrange
    4. NAME READY STATUS RESTARTS AGE
    5. nginx 1/1 Running 0 11s 可以成功创建pod

    2.8 ResourceQuota示例

    1. [root@master1 ~]# vim resourcequota.yml
    2. apiVersion: v1
    3. kind: ResourceQuota
    4. metadata:
    5. name: quota
    6. namespace: limitrange
    7. spec:
    8. hard: # 硬限制
    9. pods: "1" # 限制此namespace里就不能超过1个pod
    1. [root@master1 ~]# kubectl apply -f resourcequota.yml
    2. resourcequota/quota created
    1. [root@master1 ~]# kubectl describe ns limitrange
    2. Name: limitrange
    3. Labels: <none>
    4. Annotations: <none>
    5. Status: Active
    6. Resource Quotas
    7. Name: quota
    8. Resource Used Hard
    9. -------- --- ---
    10. pods 1 1 # 硬限制为1个pod,已经有1个pod了
    11. Resource Limits
    12. Type Resource Min Max Default Request Default Limit Max Limit/Request Ratio
    13. ---- -------- --- --- --------------- ------------- -----------------------
    14. Container cpu 500m 2 1 1 4
    1. [root@master1 ~]# vim resourcequota-pod.yml
    2. apiVersion: v1
    3. kind: Pod
    4. metadata:
    5. name: nginx2 # 改一个pod名
    6. namespace: limitrange # 相同的namespace
    7. spec:
    8. containers:
    9. - name: c1
    10. image: nginx:1.15-alpine
    11. imagePullPolicy: IfNotPresent
    1. [root@master1 ~]# kubectl apply -f resourcequota-pod.yml
    2. Error from server (Forbidden): error when creating "resourcequota-pod.yml": pods "nginx2" is forbidden: exceeded quota: quota, requested: pods=1, used: pods=1, limited: pods=1
    3. 报错,pod数量超过了

    三、User访问案例 

    参考资料 https://kubernetes.io/docs/reference/access-authn-authz/rbac/

    3.1 创建k8s用户(User)

    3.1.1  准备工作

    1. [root@master1 ~]# mkdir rbac
    2. [root@master1 ~]# cd rbac/
    3. [root@master1 rbac]# cp /etc/kubernetes/ssl/ca-key.pem .
    4. [root@master1 rbac]# cp /etc/kubernetes/ssl/ca.pem .

    3.1.2  创建证书

    1. # 创建user私钥
    2. [root@master1 rbac]# openssl genrsa -out daniel.key 2048
    3. # 创建证书签署请求(subj中的CN为用户名,O为用户组)
    4. [root@master1 rbac]# openssl req -new -key daniel.key -out daniel.csr -subj "/O=k8s/CN=daniel"
    5. # 签署证书
    6. [root@master1 rbac]# openssl x509 -req -in daniel.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out daniel.crt -days 365
    7. Signature ok
    8. subject=/O=k8s/CN=daniel
    9. Getting CA Private Key
    10. [root@master1 rbac]# ls
    11. ca-key.pem ca.pem ca.srl daniel.crt daniel.csr daniel.key

    3.1.3 创建用户配置文件 

    过程复杂,直接用脚本

    1. [root@master1 rbac]# vim create-rbac-user.sh
    2. #设置集群连接
    3. kubectl config set-cluster kubernetes \
    4. --certificate-authority=ca.pem \
    5. --embed-certs=true \
    6. --server=https://192.168.122.11:6443 \
    7. --kubeconfig=daniel-kubeconfig
    8. #颁发证书
    9. kubectl config set-credentials daniel \
    10. --client-key=daniel.key \
    11. --client-certificate=daniel.crt \
    12. --embed-certs=true \
    13. --kubeconfig=daniel-kubeconfig
    14. #设置安全上下文
    15. kubectl config set-context daniel@kubernetes \
    16. --cluster=kubernetes \
    17. --user=daniel \
    18. --kubeconfig=daniel-kubeconfig
    19. #切换安全上下文
    20. kubectl config use-context daniel@kubernetes --kubeconfig=daniel-kubeconfig
    21. #查看安全上下文
    22. kubectl config view --kubeconfig=daniel-kubeconfig

    执行脚本 

    1. [root@master1 rbac]# sh create-rbac-user.sh
    2. Cluster "kubernetes" set.
    3. User "daniel" set.
    4. Context "daniel@kubernetes" created.
    5. Switched to context "daniel@kubernetes".
    6. apiVersion: v1
    7. clusters:
    8. - cluster:
    9. certificate-authority-data: DATA+OMITTED
    10. server: https://192.168.122.11:6443
    11. name: kubernetes
    12. contexts:
    13. - context:
    14. cluster: kubernetes
    15. user: daniel
    16. name: daniel@kubernetes
    17. - context:
    18. cluster: kubernetes
    19. user: daniel
    20. name: default
    21. current-context: daniel@kubernetes
    22. kind: Config
    23. preferences: {}
    24. users:
    25. - name: daniel
    26. user:
    27. client-certificate-data: REDACTED
    28. client-key-data: REDACTED

    查看kubeconfig文件 

    1. [root@master1 ~]# cat /root/rbac/daniel-kubeconfig
    2. apiVersion: v1
    3. clusters:
    4. - cluster:
    5. certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUR1RENDQXFDZ0F3SUJBZ0lVRXJCMXhTc2NDMUV3V2lkckc3UEJXT29HSkFFd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1lURUxNQWtHQTFVRUJoTUNRMDR4RVRBUEJnTlZCQWdUQ0VoaGJtZGFhRzkxTVFzd0NRWURWUVFIRXdKWQpVekVNTUFvR0ExVUVDaE1EYXpoek1ROHdEUVlEVlFRTEV3WlRlWE4wWlcweEV6QVJCZ05WQkFNVENtdDFZbVZ5CmJtVjBaWE13SUJjTk1qQXhNVEEwTVRNek5qQXdXaGdQTWpFeU1ERXdNVEV4TXpNMk1EQmFNR0V4Q3pBSkJnTlYKQkFZVEFrTk9NUkV3RHdZRFZRUUlFd2hJWVc1bldtaHZkVEVMTUFrR0ExVUVCeE1DV0ZNeEREQUtCZ05WQkFvVApBMnM0Y3pFUE1BMEdBMVVFQ3hNR1UzbHpkR1Z0TVJNd0VRWURWUVFERXdwcmRXSmxjbTVsZEdWek1JSUJJakFOCkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQXpZNWhMTDhTYXU2MGVpNDR6RTlhNVdDdHVCc00KN0tmMXJkd2RnVVo2VFpRK3VFNkdhdmN5SWhCT3l2bFprVEt0WmhEcXdmSml3TjZBVk5tdTEvTktrVkZ2UEUrO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klDQVRFLS0tLS0K
    6. server: https://192.168.122.11:6443
    7. name: kubernetes
    8. contexts:
    9. - context:
    10. cluster: kubernetes
    11. user: daniel
    12. name: daniel@kubernetes
    13. - context:
    14. cluster: kubernetes
    15. user: daniel
    16. name: default
    17. current-context: daniel@kubernetes
    18. kind: Config
    19. preferences: {}
    20. users:
    21. - name: daniel
    22. user:
    23. client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMvRENDQWVRQ0NRQzU2Z3poZFcrb1pUQU5CZ2txaGtpRzl3MEJBUXNGQURCaE1Rc3dDUVlEVlFRR0V3SkQKVGpFUk1BOEdBMVVFQ0JNSVNHRnVaMXBvYjNVeEN6QUpCZ05WQkFjVEFsaFRNUXd3Q2dZRFZRUUtFd05yT0hNeApEekFOQmdOVkJBc1RCbE41YzNSbGJURVRNQkVHQTFVRUF4TUthM1ZpWlhKdVpYUmxjekFlRncweU1ERXhNRGt4Ck16UTFNemRhRncweU1URXhNRGt4TXpRMU16ZGFNQjh4RERBS0JnTlZCQW9NQTJzNGN6RVBNQTBHQTFVRUF3d0cKWkdGdWFXVnNNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQTJ5SFEvekhveUtNMQo1VzVwM3dLTVBlcjNrbXZET0pTMFhmQVJRTUVYYTVkVmtvVForNmN1M1A2ay9aZTNwNlIydzZIczAvSzlScEE5ClprMGIzb1NGbzlsODlwWTZ5ODQyN1FVN0lHSzJIdUMrMkZ4Vys1c1ZsMFhqczRNUWNxWDgwVTh0R3dadTZBUmoKc1VkcmVwSDNxMmpTbFpEbDNpME1ockFaSmR6dk1XU1I5cnVzMjFhbzYzaUtXL291dFJqaEt0cDhoV1QvbFBiSgpnQkYyV3E1b2RsZ1ZoY2YxT0xBNGQ1L2JTUXUwMkQwdHFmM21RamVNN0FSYWJGdG8wcUhpS2lQNjBINkI4OEg2Ck1KYXdFWnp2bUt2UXc4YjhMVWxmN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uRWpmanNlb2lEMFZWMmY5dTMKKzQ4dVhLSDlURXkwUC9rWG5sV2FnSmNFbHNGcXh0bTdNa083UVZKMnhPN3hDUzZxTzFRVzhoWFRRVDRFb0JJRApJdGRlUktHZ2JMTGhZSXNqdGFkTUpFSWV6SFBKNFFDZkp3UVJRaUdEa1dULy8vdXM4QjVScGRUVWU2a0w5MEU3CjVaa3NROEVzeEViYnE0eXgxQVA3eTVFQ2dZQis0dEdtS1ZpbFFYa3pwZXR4MFJadXhEQWNORWYrRUlvRHJ1b1oKR1AzUGpHbjY2bFYzR0dQc3FteE1CU2FneWYrcFdxTlJGUjFqQU1Lbm8zSkoveEUyZHN0Yy84NnR5eTBCMVF1ZQpxYnBObWxySjZPYytzR3AvY1RvUnIxWElZMllpLzJJblc1T2l5MnRpN0Nhc3p4MHhoOWRqMG1LaXBzeTF6OXUxCmJGLzBid0tCZ0FmZm96OXNMeXpIK0pvT3NsUnNLaEhWTHhmSXoxUXZkT3FtY0hQN0paS0hqMFpyeGJsajlzYVoKMjROK0x1ZFZpRjl0V3VxOUYxVk1LbXY4K3pITW91d3JwcHduN1JEQm93dnk0M3lnYkZDNXZrd2dkMXdIcmcwOApycEMza0NLWjJlbERxSVZoZlYxaUlHSDhvQmlqSVJiUEFlTGtkRksveWFwS09Rd2pZNkM4Ci0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==

    3.2 创建系统用户 

    1. [root@master1 rbac]# useradd daniel
    2. [root@master1 rbac]# mkdir -p /home/daniel/.kube
    3. [root@master1 rbac]# cp /root/rbac/daniel-kubeconfig /home/daniel/.kube/config
    4. [root@master1 rbac]# chown daniel.daniel -R /home/daniel/
    5. [root@master1 rbac]# su - daniel

    说明: 切换登录用户时, 就使用了/home/daniel/.kube/config文件。

    3.3 验证用户访问k8s资源

    可以看到daniel用户对pod,svc,ns等资源都没有权限

    1. [daniel@master1 ~]$ kubectl get pod
    2. Error from server (Forbidden): pods is forbidden: User "daniel" cannot list resource "pods" in API group "" in the namespace "default"
    3. [daniel@master1 ~]$ kubectl get svc
    4. Error from server (Forbidden): services is forbidden: User "daniel" cannot list resource "services" in API group "" in the namespace "default"
    5. [daniel@master1 ~]$ kubectl get ns
    6. Error from server (Forbidden): namespaces is forbidden: User "daniel" cannot list resource "namespaces" in API group "" at the cluster scope

    3.4 创建role 

    1. [root@master1 rbac]# vim role-pods-read.yaml
    2. apiVersion: rbac.authorization.k8s.io/v1
    3. kind: Role
    4. metadata:
    5. name: role-pods-reader
    6. rules:
    7. - apiGroups:
    8. - ""
    9. resources:
    10. - pods
    11. verbs:
    12. - get
    13. - list
    14. - watch

    说明: 此role是表示对pods资源有读的相关权限(get,list,watch)

    1. [root@master1 rbac]# kubectl apply -f role-pods-read.yaml
    2. role.rbac.authorization.k8s.io/role-pods-reader created
    1. [root@master1 rbac]# kubectl get role |grep role-pods-reader
    2. role-pods-reader 2020-11-09T14:36:36Z

    3.5 创建rolebinding 

    1. [root@master1 rbac]# vim rolebinding-pods-read.yml
    2. apiVersion: rbac.authorization.k8s.io/v1
    3. kind: RoleBinding
    4. metadata:
    5. name: rolebinding-pods-reader
    6. roleRef:
    7. apiGroup: rbac.authorization.k8s.io
    8. kind: Role
    9. name: role-pods-reader # 对应上一步的role名
    10. subjects:
    11. - apiGroup: rbac.authorization.k8s.io
    12. kind: User
    13. name: daniel # 与前面创建的k8s用户一致
    1. [root@master1 rbac]# kubectl apply -f rolebinding-pods-read.yml
    2. rolebinding.rbac.authorization.k8s.io/rolebinding-pods-reader created
    1. [root@master1 rbac]# kubectl get rolebinding |grep rolebinding-pods-reader
    2. rolebinding-pods-reader Role/role-pods-reader 98s

    说明: 这样系统用户daniel与k8s用户daniel通过这个rolebindingrole里定义的权限都联系起来了

    3.6 再验证用户访问k8s资源 

    1. [root@master1 rbac]# su - daniel
    2. [daniel@master1 ~]$ kubectl get pods
    3. NAME READY STATUS RESTARTS AGE
    4. deploy-nginx-nfs-6f9bc4546c-dw64l 1/1 Running 1 26h
    5. deploy-nginx-nfs-6f9bc4546c-fv4wl 1/1 Running 1 26h
    6. nfs-client-provisioner-5b5ddcd6c8-d7sjw 1/1 Running 1 26h
    7. pod-cm1 1/1 Running 7 2d2h
    8. pod-cm2 1/1 Running 7 2d2h
    9. pod-mysql-secret1 1/1 Running 1 27h
    10. pod-mysql-secret2 1/1 Running 1 27h
    11. web-0 1/1 Running 1 26h
    12. web-1 1/1 Running 1 26h
    13. web-2 1/1 Running 1 26h
    14. [daniel@master1 ~]$ kubectl get pods -n kube-system
    15. Error from server (Forbidden): pods is forbidden: User "daniel" cannot list resource "pods" in API group "" in the namespace "kube-system"
    16. [daniel@master1 ~]$ kubectl get svc
    17. Error from server (Forbidden): services is forbidden: User "daniel" cannot list resource "services" in API group "" in the namespace "default"
    18. [daniel@master1 ~]$ kubectl get ns
    19. Error from server (Forbidden): namespaces is forbidden: User "daniel" cannot list resource "namespaces" in API group "" at the cluster scope

    说明: 由上可知,daniel 用户终于可以访问default命名空间里的pods资源了,但其它的还没有权限访问 

    3.7 创建ClusterRole

    1. [root@master1 rbac]# vim clusterrole-pods-reader.yaml
    2. apiVersion: rbac.authorization.k8s.io/v1
    3. kind: ClusterRole
    4. metadata:
    5. name: clusterrole-pods-reader
    6. rules:
    7. - apiGroups:
    8. - ""
    9. resources:
    10. - pods
    11. verbs:
    12. - get
    13. - list
    14. - watch
    1. [root@master1 rbac]# kubectl apply -f clusterrole-pods-reader.yaml
    2. clusterrole.rbac.authorization.k8s.io/clusterrole-pods-reader created
    1. [root@master1 rbac]# kubectl get clusterrole |grep pods-reader
    2. clusterrole-pods-reader 2022-08-06T14:55:30Z

    3.8 创建ClusterRoleBinding 

    1. [root@master1 rbac]# vim clusterrolebinding-pods-reader.yaml
    2. apiVersion: rbac.authorization.k8s.io/v1beta1
    3. kind: ClusterRoleBinding
    4. metadata:
    5. name: clusterrolebinding-pods-reader
    6. roleRef:
    7. apiGroup: rbac.authorization.k8s.io
    8. kind: ClusterRole
    9. name: clusterrole-pods-reader
    10. subjects:
    11. - apiGroup: rbac.authorization.k8s.io
    12. kind: User
    13. name: daniel
    1. [root@master1 rbac]# kubectl apply -f clusterrolebinding-pods-reader.yaml
    2. clusterrolebinding.rbac.authorization.k8s.io/clusterrolebinding-pods-reader created
    1. [root@master1 rbac]# kubectl get clusterrolebinding |grep pods-reader
    2. clusterrolebinding-pods-reader ClusterRole/clusterrole-pods-reader 27s

    3.9 再验证用户访问k8s资源 

    1. [root@master1 rbac]# su - daniel
    2. [daniel@master1 ~]$ kubectl get pod --all-namespaces

    现在所有命名空间的pods都可以访问了

    问题: 访问svc等其它资源如何操作?

    可参考查看admin这个clusterrole

    [root@master1 rbac]# kubectl get clusterrole admin -o yaml
    

    四、ServiceAccount访问案例 

    4.1 创建namespace

    1. [root@master1 ~]# kubectl create namespace sa
    2. namespace/sa created

    4.2 验证namespace的sa与secret 

    创建namespace默认会创建一个serviceaccount和一个secret

    1. [root@master1 ~]# kubectl get sa -n sa
    2. NAME SECRETS AGE
    3. default 1 37s
    4. [root@master1 ~]# kubectl get secret -n sa
    5. NAME TYPE DATA AGE
    6. default-token-dtbj5 kubernetes.io/service-account-token 3 49s
    1. [root@master1 ~]# kubectl describe sa default -n sa
    2. Name: default
    3. Namespace: sa
    4. Labels: <none>
    5. Annotations: <none>
    6. Image pull secrets: <none>
    7. Mountable secrets: default-token-dtbj5
    8. Tokens: default-token-dtbj5
    9. Events: <none>
    1. [root@master1 ~]# kubectl describe secret default-token-dtbj5 -n sa
    2. Name: default-token-dtbj5
    3. Namespace: sa
    4. Labels:
    5. Annotations: kubernetes.io/service-account.name: default
    6. kubernetes.io/service-account.uid: 8372360a-2b0e-41cd-9023-90514699573a
    7. Type: kubernetes.io/service-account-token
    8. Data
    9. ====
    10. ca.crt: 1350 bytes
    11. namespace: 2 bytes
    12. token: eyJhbGciOiJSUzI1NiIsImtpZCI6InA0ZmVpcVJFUEJVdzIxNF9CY0FEZGljQUFiLUl3S21VTGI5QkpiLXZocDAifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJzYSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkZWZhdWx0LXRva2VuLWR0Ymo1Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImRlZmF1bHQiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiI4MzcyMzYwYS0yYjBlLTQxY2QtOTAyMy05MDUxNDY5OTU3M2EiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6c2E6ZGVmYXVsdCJ9.FEeJ74ZDXlEMqnWaU0UlenWffN2T35RvT011p8ZX7r12k5yffn-19tZQvYDJnErB_N7XryON9FfxtcA91dkbvZA9RWNZR4eBwrePZk6jaqjdMFK12LGcffR13kTh733L2394QF47qshQAsyKAABs8SyR-d6_BiLfxNUbnefuGxWGG3nEiMGuayoyerB32Xt4bBIMo8k6JZD4JNqCb1Bzq3wdCmkSIRrzfxVSEo-eW72rsw-LkcKGdwZcmRqV-FZuTA8TP4pCnJRbuDPx9Td9M3zz_hWZ5B0FW1XkJo1wiuTrIrMj-p5Dt4aEoj0y3eNDJNdV094EgBIMqy89emOb2A

    4.3 创建pod验证其sa与secret 

    1. [root@master1 ~]# kubectl run nginx --image=nginx:1.15-alpine -n sa
    2. pod/nginx created
    1. [root@master1 ~]# kubectl describe pod nginx -n sa
    2. ......
    3. Mounts:
    4. /var/run/secrets/kubernetes.io/serviceaccount from default-token-dtbj5 (ro)
    5. ......
    6. Volumes:
    7. default-token-dtbj5:
    8. Type: Secret (a volume populated by a Secret)
    9. SecretName: default-token-dtbj5

    说明: 创建pod不指定serviceaccount则默认使用default.

    4.4 创建一个sa 

    1. [root@master1 ~]# kubectl create sa daniel -n sa
    2. serviceaccount/daniel created
    1. [root@master1 ~]# kubectl get sa -n sa
    2. NAME SECRETS AGE
    3. daniel 1 12s
    4. default 1 13m
    1. [root@master1 ~]# kubectl get secret -n sa
    2. NAME TYPE DATA AGE
    3. daniel-token-pjdmc kubernetes.io/service-account-token 3 18s
    4. default-token-dtbj5 kubernetes.io/service-account-token 3 13m

    4.5 创建pod并自定义sa 

    1. [root@master1 ~]# vim sa-pod.yml
    2. apiVersion: v1
    3. kind: Pod
    4. metadata:
    5. name: nginx2
    6. namespace: sa
    7. spec:
    8. containers:
    9. - name: c1
    10. image: nginx:1.15-alpine
    11. ports:
    12. - name: httpd
    13. containerPort: 80
    14. serviceAccountName: daniel # 指定sa为daniel

    4.6 验证pod使用的sa 

    1. [root@master1 ~]# kubectl apply -f sa-pod.yml
    2. pod/nginx2 created
    1. [root@master1 ~]# kubectl describe pod nginx2 -n sa
    2. ......
    3. Mounts:
    4. /var/run/secrets/kubernetes.io/serviceaccount from daniel-token-pjdmc (ro)
    5. ......
    6. Volumes:
    7. daniel-token-pjdmc:
    8. Type: Secret (a volume populated by a Secret)
    9. SecretName: daniel-token-pjdmc
    10. ......
  • 相关阅读:
    安全基础~通用漏洞6
    安卓逆向之雷电模拟器中控
    Pytest+Allure生成自动化测试报告!
    享元模式 rust和java的实现
    Python库 pdf2docx 轻松将PDF转换成docx
    redis问题:三种集群——主从、哨兵、cluster集群;16384槽等
    基于 CoreDNS 和 K8s 构建云原生场景下的企业级 DNS
    vue的孙组件获取祖组件数据的方法(this.$parent)
    40w粉拿下900w播放!这个UP主在B站发起1元买水挑战!
    Sentence-BERT+Milvus实现智能问答系统
  • 原文地址:https://blog.csdn.net/weixin_67470255/article/details/126169079