阿里云容器服务Kubernetes用户授权管理

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介:

阿里云容器服务Kubernetes用户授权管理分为2个级别:一个是RAM访问控制,即设定用户对k8s集群的访问权限,比如查看集群列表,创建/删除集群的权限,是否能够对集群进行扩容等等;另一个是需要在集群内对用户进行细粒度的授权,比如是否允许用户部署应用,以及对集群内资源的修改删除。本文将对这两个部分进行简单介绍,从而掌握对集群的正确授权管理,特别演示了如何单独创建集群用户并为其授权的过程。

Kubernetes 阿里云容器服务RAM访问控制

在RAM访问控制里面,有2个系统策略:AliyunCSFullAccess和AliyunCSReadOnlyAccess。很明显前者是具有对k8s集群的完全访问控制,能够在阿里云控制台创建删除集群。而后这只读权限的内容是"cs:Get",也就是说只具有Get权限。这两个系统策略的颗粒度是比较大的,要想精细化权限管理,需要进行自定义策略并赋给指定用户。下表是所有集群具有的操作(参考自定义 RAM 授权策略):

Action 说明
CreateCluster 创建集群
AttachInstances 向集群中添加已有 ECS 实例
ScaleCluster 扩容集群
GetClusters 查看集群列表
GetClusterById 查看集群详情
ModifyClusterName 修改集群名称
DeleteCluster 删除集群
UpgradeClusterAgent 升级集群 Agent
GetClusterLogs 查看集群的操作日志
GetClusterEndpoint 查看集群接入点地址
GetClusterCerts 下载集群证书
RevokeClusterCerts 吊销集群证书
BindSLB 为集群绑定负载均衡实例
UnBindSLB 为集群解绑负载均衡实例
ReBindSecurityGroup 为集群重新绑定安全组
CheckSecurityGroup 检测集群现有的安全组规则
FixSecurityGroup 修复集群的安全组规则
ResetClusterNode 重置集群中的节点
DeleteClusterNode 移除集群中的节点
GetClusterProjects 查看集群下的应用
CreateTriggerHook 为应用创建触发器
GetTriggerHook 查看应用的触发器列表
RevokeTriggerHook 删除应用的触发器
CreateClusterToken 创建 Token
UpdateClusterTags 编辑集群标签

阿里云环境如何用子账号对用户进行授权

1_cluster
具有阿里云RAM权限只是具有管理阿里云集群的第一步,要进行集群的使用和运维,还需要利用Kubernetes RBAC进行单独的访问控制。
1_accesscontrolrbac
其实这个跟阿里云的RAM访问控制Policy比较类似,就是描述谁可以对K8S的资源进行什么样的操作。不同的策略赋给不同的用户,阿里云预置的一组策略可以关联于角色Role,然后在阿里云集群授权中就可以将相应的Role赋给具体某一用户。这里面看到的管理员,运维人员,开发人员等其实就是不同的角色,后面我们可以看到他们对应的具体Policy。
2_shouquanguanli

3_shouquanguanli

阿里云主账号具有最大的管理权限,可以做集群的授权操作,但一般我们不建议用主账号做太多的操作,所以尽量用管理员账户操作就可以了。但对于K8S授权,即使是系统管理员,在对集群做授权时,也会提示“当前子账号不具备授权管理权限,请联系主帐号或权限管理员授权。”,这是因为K8S授权需要先用主账号对集群的管理员先赋予集群管理员角色,这样这个管理员才能对其它子账号进行授权操作。准确地说,授权需要满足2个条件,一个是RAM访问控制读权限(参考link),另一个是集群中对apiGroups的rbac.authorization.k8s.io的访问权限。

Kubernetes RBAC(Role Based Access Control)基于角色的访问控制

我们用下面的命令可以查看K8S中的ClusterRole,ClusterRoleBinding。

# kubectl get ClusterRole
NAME                                                                   AGE
admin                                                                  1y
ahas                                                                   245d
alibaba-log-controller                                                 1y
alicloud-disk-controller-runner                                        1y
arms-pilot-ack-arms-pilot-role                                         200d
cluster-admin                                                          1y
cs:admin                                                               340d
cs:dev                                                                 340d
cs:heapster                                                            1y
cs:ns:dev                                                              1y
cs:ops                                                                 337d
cs:restricted                                                          340d
deployment-reader                                                      1y
drain-node                                                             109d
edit                                                                   1y
flannel                                                                1y
kube-state-metrics                                                     1y
nginx-ingress-controller                                               320d
pod-reader                                                             1y
system:aggregate-to-admin                                              1y
system:aggregate-to-edit                                               1y
system:aggregate-to-view                                               1y
system:auth-delegator                                                  1y
system:aws-cloud-provider                                              1y
system:basic-user                                                      1y
system:certificates.k8s.io:certificatesigningrequests:nodeclient       1y
system:certificates.k8s.io:certificatesigningrequests:selfnodeclient   1y
system:cloud-controller-manager                                        1y
system:controller:attachdetach-controller                              1y
system:controller:certificate-controller                               1y
system:controller:clusterrole-aggregation-controller                   1y
system:controller:cronjob-controller                                   1y
system:controller:daemon-set-controller                                1y
system:controller:deployment-controller                                1y
system:controller:disruption-controller                                1y
system:controller:endpoint-controller                                  1y
system:controller:expand-controller                                    1y
system:controller:generic-garbage-collector                            1y
system:controller:horizontal-pod-autoscaler                            1y
system:controller:job-controller                                       1y
system:controller:namespace-controller                                 1y
system:controller:node-controller                                      1y
system:controller:persistent-volume-binder                             1y
system:controller:pod-garbage-collector                                1y
system:controller:pv-protection-controller                             1y
system:controller:pvc-protection-controller                            1y
system:controller:replicaset-controller                                1y
system:controller:replication-controller                               1y
system:controller:resourcequota-controller                             1y
system:controller:route-controller                                     1y
system:controller:service-account-controller                           1y
system:controller:service-controller                                   1y
system:controller:statefulset-controller                               1y
system:controller:ttl-controller                                       1y
system:coredns                                                         1y
system:csi-external-attacher                                           1y
system:csi-external-provisioner                                        1y
system:discovery                                                       1y
system:heapster                                                        1y
system:kube-aggregator                                                 1y
system:kube-controller-manager                                         1y
system:kube-dns                                                        1y
system:kube-scheduler                                                  1y
system:kubelet-api-admin                                               1y
system:node                                                            1y
system:node-bootstrapper                                               1y
system:node-problem-detector                                           1y
system:node-proxier                                                    1y
system:persistent-volume-provisioner                                   1y
system:volume-scheduler                                                1y
test-role                                                              2h
view                                                                   1y

查看ClusterRoleBinding:

# kubectl get clusterrolebinding
NAME                                                   AGE
202729146928739501-clusterrolebinding                  218d
202865947013061057-clusterrolebinding                  336d
203542248038995370-clusterrolebinding                  333d
204696546927216725-clusterrolebinding                  1d
admin                                                  1y
ahas                                                   245d
alibaba-log-controller                                 1y
aliyun-acr-credential-helper-rolebinding               1y
arms-pilot-ack-arms-pilot-role-binding                 200d
cluster-admin                                          1y
deployment-reader                                      1y
drain-node                                             109d
flannel                                                1y
global-job-controller-role-binding                     329d
kube-state-metrics                                     1y
kubeadm:kubelet-bootstrap                              1y
kubeadm:node-autoapprove-bootstrap                     1y
kubeadm:node-autoapprove-certificate-rotation          1y
kubeadm:node-proxier                                   1y
nginx-ingress-controller-starterservices-dev           224d
nginx-ingress-controller-starterservices-gadev         320d
node-masters-role-binding                              329d
pod-reader                                             1y
run-alicloud-disk-controller                           1y
system:aws-cloud-provider                              1y
system:basic-user                                      1y
system:cloud-controller-manager                        1y
system:cloud-node-controller                           1y
system:controller:attachdetach-controller              1y
system:controller:certificate-controller               1y
system:controller:clusterrole-aggregation-controller   1y
system:controller:cronjob-controller                   1y
system:controller:daemon-set-controller                1y
system:controller:deployment-controller                1y
system:controller:disruption-controller                1y
system:controller:endpoint-controller                  1y
system:controller:expand-controller                    1y
system:controller:generic-garbage-collector            1y
system:controller:horizontal-pod-autoscaler            1y
system:controller:job-controller                       1y
system:controller:namespace-controller                 1y
system:controller:node-controller                      1y
system:controller:persistent-volume-binder             1y
system:controller:pod-garbage-collector                1y
system:controller:pv-protection-controller             1y
system:controller:pvc-protection-controller            1y
system:controller:replicaset-controller                1y
system:controller:replication-controller               1y
system:controller:resourcequota-controller             1y
system:controller:route-controller                     1y
system:controller:service-account-controller           1y
system:controller:service-controller                   1y
system:controller:statefulset-controller               1y
system:controller:ttl-controller                       1y
system:coredns                                         1y
system:discovery                                       1y
system:kube-controller-manager                         1y
system:kube-dns                                        1y
system:kube-scheduler                                  1y
system:node                                            1y
system:node-proxier                                    1y
system:pvl-controller                                  1y
system:route-controller                                1y
system:shared-informers                                1y
system:volume-scheduler                                1y
test-rolebinding                                       2h
tiller-cluster-rule                                    1y

cs:admin, cs:ops, cs:dev, cs:restricted分别代表阿里云预置的4个角色:管理员,运维人员,开发人员,受限用户,它们的权限依次递减,其中受限用户只对部分资源具有读权限。

我们查看其中的一个ClusterRoleBinding, ID为202729146928739501用户赋予了cs:ops的角色。

# kubectl get clusterrolebinding/202729146928739501-clusterrolebinding -o yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  creationTimestamp: 2019-08-19T05:22:03Z
  name: 202729146928739501-clusterrolebinding
  resourceVersion: "111811902"
  selfLink: /apis/rbac.authorization.k8s.io/v1/clusterrolebindings/202729146928739501-clusterrolebinding
  uid: 46ec07c5-c241-11e9-b90f-00163e00519d
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cs:ops
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: User
  name: "202729146928739501"

接下来就可以继续查看cs:ops到底具有什么样的权限:

# kubectl get clusterrole/cs:ops -o yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  creationTimestamp: 2019-04-22T07:57:30Z
  name: cs:ops
  resourceVersion: "47812320"
  selfLink: /apis/rbac.authorization.k8s.io/v1/clusterroles/cs%3Aops
  uid: 472ef445-64d4-11e9-bb7e-00163e06e73f
rules:
- apiGroups:
  - ""
  resources:
  - pods
  - pods/attach
  - pods/exec
  - pods/portforward
  - pods/proxy
  verbs:
  - create
  - delete
  - deletecollection
  - get
  - list
  - patch
  - update
  - watch
- apiGroups:
  - ""
  resources:
  - configmaps
  - endpoints
  - persistentvolumeclaims
  - replicationcontrollers
  - replicationcontrollers/scale
  - secrets
  - serviceaccounts
  - services
  - services/proxy
  verbs:
  - create
  - delete
  - deletecollection
  - get
  - list
  - patch
  - update
  - watch
- apiGroups:
  - ""
  resources:
  - bindings
  - events
  - limitranges
  - namespaces/status
  - replicationcontrollers/status
  - pods/log
  - pods/status
  - resourcequotas
  - resourcequotas/status
  - componentstatuses
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - namespaces
  - nodes
  - persistentvolumes
  verbs:
  - get
  - list
  - watch
  - patch
- apiGroups:
  - apps
  resources:
  - daemonsets
  - deployments
  - deployments/rollback
  - deployments/scale
  - replicasets
  - replicasets/scale
  - statefulsets
  verbs:
  - create
  - delete
  - deletecollection
  - get
  - list
  - patch
  - update
  - watch
- apiGroups:
  - autoscaling
  resources:
  - horizontalpodautoscalers
  verbs:
  - create
  - delete
  - deletecollection
  - get
  - list
  - patch
  - update
  - watch
- apiGroups:
  - batch
  resources:
  - cronjobs
  - jobs
  verbs:
  - create
  - delete
  - deletecollection
  - get
  - list
  - patch
  - update
  - watch
- apiGroups:
  - extensions
  resources:
  - daemonsets
  - deployments
  - deployments/rollback
  - deployments/scale
  - ingresses
  - replicasets
  - replicasets/scale
  - replicationcontrollers/scale
  verbs:
  - create
  - delete
  - deletecollection
  - get
  - list
  - patch
  - update
  - watch
- apiGroups:
  - servicecatalog.k8s.io
  resources:
  - clusterserviceclasses
  - clusterserviceplans
  - clusterservicebrokers
  - serviceinstances
  - servicebindings
  verbs:
  - create
  - delete
  - get
  - list
  - patch
  - update
  - watch
- apiGroups:
  - servicecatalog.k8s.io
  resources:
  - clusterservicebrokers/status
  - clusterserviceclasses/status
  - clusterserviceplans/status
  - serviceinstances/status
  - serviceinstances/reference
  - servicebindings/status
  verbs:
  - update
- apiGroups:
  - storage.k8s.io
  resources:
  - storageclasses
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - alicloud.com
  resources:
  - '*'
  verbs:
  - create
  - delete
  - get
  - list
  - patch
  - update
  - watch
- apiGroups:
  - policy
  resources:
  - poddisruptionbudgets
  verbs:
  - create
  - delete
  - deletecollection
  - get
  - list
  - patch
  - update
  - watch

阿里云授权页面中的自定义就是可以选取集群中已经存在的一些角色,当然也可以创建自己的权限和角色,然后分配给指定的用户。

如何为Kubernetes创建一个独立于阿里云的用户并进行授权

除了在阿里云容器服务控制台中对阿里云用户进行授权外,集群管理者也可以单独创建一个用户并对其进行授权,这样就可以给一个没有阿里云账户的用户通过api或者kubectl命令来使用和管理集群。以下的步骤举例说明如何创建用户bob,并给bob授权,以及bob如何配置使用。

创建用户

登陆到master节点
# cd /etc/kubernetes/pki
创建bob 私钥
# openssl genrsa -out bob.key 2048
创建bob CSR,并将bob分配到test这个组中。
# openssl req -new -key bob.key -out bob.csr -subj "/CN=bob/O=test"
通过集群CA签发bob证书。管理员将证书文件和bob的私钥一起发给bob用户。
# openssl x509 -req -in bob.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out bob.crt -days 365

上面在创建bob CSR的时候/CN=bob代表用户名,/O=test代表用户所在的组。system:masters是系统组,如果选择这个组,用户缺省就具有了cluster admin的权限。

bob如何配置使用证书

bob得到CA签发的证书和私钥后,首先需要下载kubectl,然后通过下面命令配置.kube/config文件。

#添加集群连接信息。包括CA证书。
C:\Users\mdw3sr\Desktop>kubectl config set-cluster sandbox --certificate-authority=ca.crt --embed-certs=true --server=https://<K8S集群公网IP>:6443
Cluster "sandbox" set.
#将bob密钥信息加入配置中
C:\Users\mdw3sr\Desktop>kubectl config set-credentials bob --client-certificate=bob.crt --client-key=bob.key --embed-certs=true
User "bob" set.
#将新的context入口加入配置中
C:\Users\mdw3sr\Desktop>kubectl config set-context bob --cluster=sandbox --user=bob
Context "kubernetes" created.
#使用bob context
C:\Users\mdw3sr\Desktop>kubectl config use-context bob
Switched to context "bob".

这样运行kubectl cluster-info就能看到集群信息了,虽然还有些信息因为权限问题不能正确显示。

C:\Users\mdw3sr\Desktop>kubectl cluster-info
Kubernetes master is running at https://106.15.157.9:6443

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
Error from server (Forbidden): services is forbidden: User "bob" cannot list resource "services" in API group "" in the namespace "kube-system"
C:\Users\mdw3sr\Desktop>kubectl get pod
No resources found.
Error from server (Forbidden): pods is forbidden: User "bob" cannot list resource "pods" in API group "" in the namespace "default"

为用户bob设置权限

从上面结果可以看出,如果没有设置任何权限,bob在集群内是没有任何资源访问能力,所以管理员需要对bob进行集群内授权。

为bob创建ClusterRole和ClusterRoleBinding
# cat > test.yaml <<EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: test-role
rules:
- apiGroups: [""]
  resources: ["pods","services"]
  verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: test-rolebinding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: test-role
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: User
  name: bob
EOF

# kubectl apply -f test.yaml
clusterrole.rbac.authorization.k8s.io/test-role created
clusterrolebinding.rbac.authorization.k8s.io/test-rolebinding created

然后,bob这边再测试之前的命令,就可以正确执行了。结果如下:

C:\Users\mdw3sr\Desktop>kubectl cluster-info
Kubernetes master is running at https://106.10.157.9:6443
Heapster is running at https://106.10.157.9:6443/api/v1/namespaces/kube-system/services/heapster/proxy
KubeDNS is running at https://106.10.157.9:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
monitoring-influxdb is running at https://106.10.157.9:6443/api/v1/namespaces/kube-system/services/monitoring-influxdb/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

C:\Users\mdw3sr\Desktop>kubectl get pod
NAME                              READY     STATUS    RESTARTS   AGE
nginx-deployment-bb54749c-426cb   1/1       Running   0          104d
nginx-deployment-bb54749c-qcvc5   1/1       Running   0          104d
nginx-v1-5bc8bf76db-7rnkf         1/1       Running   1          230d
nginx-v1-5bc8bf76db-r7k5r         1/1       Running   0          185d
patch-demo-7f54465dd5-jhr5c       2/2       Running   0          29d
patch-demo-7f54465dd5-w5hx2       2/2       Running   0          29d

# 因为并没有赋给bob 获取namespace的权限,所以bob仍然不能访问namespace信息。
C:\Users\mdw3sr\Desktop>kubectl get ns
No resources found.
Error from server (Forbidden): namespaces is forbidden: User "bob" cannot list resource "namespaces" in API group "" at the cluster scope

大家也可以通过对bob设置不同的权限来学习验证具体的行为。

相关实践学习
使用ACS算力快速搭建生成式会话应用
阿里云容器计算服务 ACS(Container Compute Service)以Kubernetes为使用界面,采用Serverless形态提供弹性的算力资源,使您轻松高效运行容器应用。本文将指导您如何通过ACS控制台及ACS集群证书在ACS集群中快速部署并公开一个容器化生成式AI会话应用,并监控应用的运行情况。
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。 &nbsp; &nbsp; 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
相关文章
|
20天前
|
存储 Kubernetes 网络安全
关于阿里云 Kubernetes 容器服务(ACK)添加镜像仓库的快速说明
本文介绍了在中国大陆地区因网络限制无法正常拉取 Docker 镜像的解决方案。作者所在的阿里云 Kubernetes 集群使用的是较旧版本的 containerd(1.2x),且无法直接通过 SSH 修改节点配置,因此采用了一种无需更改 Kubernetes 配置文件的方法。通过为 `docker.io` 添加 containerd 的镜像源,并使用脚本自动修改 containerd 配置文件中的路径错误(将错误的 `cert.d` 改为 `certs.d`),最终实现了通过多个镜像站点拉取镜像。作者还提供了一个可重复运行的脚本,用于动态配置镜像源。虽然该方案能缓解镜像拉取问题,
168 2
|
6月前
|
存储 Kubernetes 监控
K8s集群实战:使用kubeadm和kuboard部署Kubernetes集群
总之,使用kubeadm和kuboard部署K8s集群就像回归童年一样,简单又有趣。不要忘记,技术是为人服务的,用K8s集群操控云端资源,我们不过是想在复杂的世界找寻简单。尽管部署过程可能遇到困难,但朝着简化复杂的目标,我们就能找到意义和乐趣。希望你也能利用这些工具,找到你的乐趣,满足你的需求。
633 33
|
6月前
|
存储 人工智能 Kubernetes
ACK Gateway with AI Extension:面向Kubernetes大模型推理的智能路由实践
本文介绍了如何利用阿里云容器服务ACK推出的ACK Gateway with AI Extension组件,在Kubernetes环境中为大语言模型(LLM)推理服务提供智能路由和负载均衡能力。文章以部署和优化QwQ-32B模型为例,详细展示了从环境准备到性能测试的完整实践过程。
|
7月前
|
Cloud Native Serverless 数据中心
阿里云ACK One:注册集群支持ACS算力——云原生时代的计算新引擎
阿里云ACK One:注册集群支持ACS算力——云原生时代的计算新引擎
233 10
|
6月前
|
安全 持续交付 云计算
课时5:阿里云容器服务:最原生的集成Docker和云服务
阿里云容器服务以服务化形式构建容器基础设施,大幅提升开发效率,简化应用部署流程。通过Docker容器和DevOps工具(如Jenkins),实现自动化部署与迭代,优化企业内部复杂部署问题。该服务支持GPU调度、混合云架构无缝迁移,并与阿里云产品体系无缝集成,提供安全防护、网络负载均衡等多重功能支持。凭借微服务架构,帮助企业突破业务瓶颈,提高资源利用率,轻松应对海量流量。
237 0
课时5:阿里云容器服务:最原生的集成Docker和云服务
|
6月前
|
存储 运维 Kubernetes
容器数据保护:基于容器服务 Kubernetes 版(ACK)备份中心实现K8s存储卷一键备份与恢复
阿里云ACK备份中心提供一站式容器化业务灾备及迁移方案,减少数据丢失风险,确保业务稳定运行。
|
6月前
|
人工智能 Kubernetes Serverless
阿里云向全球客户推出创新容器计算服务ACS,可降低算力成本高达55%
阿里云向全球客户推出创新容器计算服务ACS,可降低算力成本高达55%
|
7月前
|
弹性计算 监控 持续交付
面对热点事件,阿里云如何通过云上弹性与容器服务帮助客户应对流量洪峰
面对热点事件,阿里云如何通过云上弹性与容器服务帮助客户应对流量洪峰
176 0
|
2月前
|
存储 监控 测试技术
如何将现有的应用程序迁移到Docker容器中?
如何将现有的应用程序迁移到Docker容器中?
261 57
|
2月前
|
存储 监控 Java
如何对迁移到Docker容器中的应用进行性能优化?
如何对迁移到Docker容器中的应用进行性能优化?
252 59

相关产品

  • 容器计算服务
  • 容器服务Kubernetes版
  • 推荐镜像

    更多