①.ServiceAccount(服务账户)是Kubernetes集群中的一种资源对象,用于为Pod或其他资源提供身份验证和授权,以便它们能够与Kubernetes API进行交互。
②.ServiceAccount是Kubernetes中用于管理Pod身份验证和授权的重要资源,它使得Pod能够在集群中具有独立的身份,从而实现更精细的权限控制和安全策略。
③.?Service Account它并不是给kubernetes集群的用户使用的,而是给pod里面的进程使用的,它为pod提供必要的身份认证。----专门为pod里面的进程和apiserver通信提供认证的。
1. User account是为人设计的,而service account则是为Pod中的进程调用Kubernetes API或其他外部服务而设计的
2. User account是跨namespace的,而service account则是仅局限它所在的namespace;
3. 每个namespace都会自动创建一个default service account ? ?
4. Token controller检测service account的创建,并为它们创建secret
角色和sa需要rolebinding进行绑定,然后sa和pod进行绑定从而达到对资源限制的目的。
创建Service Account
创建sa是也会自动创建一个secret
# vim mysa.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: mysa
namespace: default
#应用
# kubectl apply -f mysa.yaml
查看mysa和自动创建的secret
#查看sa
[root@kube-master kubernetes]# kubectl get sa mysa
NAME SECRETS AGE
mysa 1 22h
#查看secret
[root@kube-master kubernetes]# kubectl get secret
NAME TYPE DATA AGE
default-token-j48pd kubernetes.io/service-account-token 3 4d20h
mysa-token-zw9b9 kubernetes.io/service-account-token 3 22h
创建角色和绑定
[root@kube-master kubernetes]# vim role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: mysa-role
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: mysa-binding
namespace: default
subjects: #定义对那个主体进行操作,
- kind: ServiceAccount
name: mysa
namespace: default
roleRef: #定义使用哪个角色
kind: Role
name: mysa-role
apiGroup: rbac.authorization.k8s.io
#####导入
kubectl apply -f role.yaml
使用mysa的sa资源配置pod
[root@kube-master kubernetes]# vim mysa-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: mysa-pod
spec:
serviceAccountName: mysa # 给pod指定Service Account
containers:
- name: app
image: 10.36.192.206:8088/newrain857/kubectl
command: ["tail","-f","/dev/null"]
###导入
kubectl apply -f mysa-pod.yaml
查看
kubectl get pod mysa-pod -o yaml
进入容器测试(可以看到,我们已经成功限制了pod对资源的访问)
[root@kube-master kubernetes]# kubectl exec -it mysa-pod -c app /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/ #
/ #
/ # kubectl get pod
NAME READY STATUS RESTARTS AGE
mysa-pod 1/1 Running 1 (6h20m ago) 22h
mysql 1/1 Running 1 (6h20m ago) 19h
nginx 1/1 Running 1 (6h21m ago) 17h
test-downward 1/1 Running 0 5h39m
##没有给delete权限就无法操作
/ # kubectl delete nginx
error: the server doesn't have a resource type "nginx"
角色绑定用户达到对用户限制的目的。
k8s里面有两种用户,一种是User,一种就是service account(服务使用的账号)。
User account是为人设计的属于用户账户(个人使用的账号),此外User Account是跨Namespace的,而ServiceAccount则是仅局限它所在的Namespace。
在RABC API中,通过如下的步骤进行授权:
1)定义角色:在定义角色时会指定此角色对于资源的访问控制的规则;
2)绑定角色:将主体与角色进行绑定,对用户进行访问授权。角色
- Role:授权特定命名空间的访问权限
- ClusterRole:授权所有命名空间的访问权限
角色绑定
- RoleBinding:将角色绑定到主体(即subject)
- ClusterRoleBinding:将集群角色绑定到主体
在K8s中这些资源分属于两个级别,名称空间(role/rolebinding)和集群级别(clusterrole/clusterrolebinding)这两个都是标准的K8s资源,可以直接定义。
rolebinding:将哪个用户绑定到哪个role上
clusterrolebinding:绑定到集群角色上
? ?如果使用clusterrolebinding绑定到clusterrole上,表示绑定的用户拥有所有namespace的权限
?Role 和 ClusterRole
Role普通角色:一个Role对象只能用于授予对某一单一命名空间中资源的访问权限,普通角色只是在当前的名称空间生效。简而言之,一个 Role 只可以用来对某一命名空间中的资源赋予访问权限。
?ClusterRole集群角色:整个Kubernetes集群范围内有效的角色则通过ClusterRole对象实现,可以访问整个集群资源。
对于User账户,我们可以通过创建k8s集群时自带的证书对我们(admin)权限(几乎)手动管理master中的凭证文件,并且将每个凭证文件和一个字符串对应起来,只要用户请求中有信息(一般来自本地保存的.kube/config文件中)能够证明自己有任意一个凭证文件的授权,那么这个请求就被认为是一个合法用户发出的,同时,在证书中还可以加入用户所在的组(group)的信息,k8s可以据此认为user在一个组中或者后期通过rolebinding等赋予操作权限。
#创建私钥,生成一个 2048 位的 aren.key 文件
[root@kube-master rbac]# umask 077; openssl genrsa -out aren.key 2048
#用此私钥创建一个csr(证书签名请求)文件
[root@kube-master rbac]# openssl req -new -key aren.key -out aren.csr -subj "/CN=aren" #这个地方是用户名
#拿着私钥和请求文件生成证书
[root@kube-master rbac]# openssl x509 -req -in aren.csr -CA /etc/kubernetes/pki/ca.crt -CAkey /etc/kubernetes/pki/ca.key -CAcreateserial -out aren.crt -days 365
Signature ok
subject=/CN=aren
Getting CA Private Key
#生成账号
[root@kube-master rbac]# kubectl config set-credentials aren --client-certificate=aren.crt --client-key=aren.key --embed-certs=true
User "aren" set.
#指的是创建这个账号的环境在当前名称空间中
[root@kube-master rbac]# kubectl config set-context aren@kubernetes --cluster=kubernetes --user=aren
Context "aren@kubernetes" modified.
#、切换用户
[root@kube-master rbac]# kubectl config use-context aren@kubernetes
Switched to context "aren@kubernetes".
#查看
[root@kube-master rbac]# kubectl config current-context
aren@kubernetes
先切回管理账号
[root@kube-master rbac]# kubectl config use-context kubernetes-admin@kubernetes
--verb: 相当于是权限
--resource:给什么资源使用
[root@kube-master rbac]# kubectl create role role-reader --verb=get,list,watch --resource=pod,svc
role.rbac.authorization.k8s.io/role-reader created
###########或者############
#yaml文件方式
[root@kube-master rbac]# vim role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: role-reader
rules: #定义规则
- apiGroups: [""] #表示当前pod使用核心的APIserver组,默认用""表示就可以
resources: ["pods","svc"]
verbs: ["get", "list", "watch", "create", "update", "delete"] #["*"]表示所有权限
#启动
# kubectl apply -f role.yaml
查看role
[root@kube-master rbac]# kubectl get role
NAME CREATED AT
role-reader 2023-12-16T08:54:10Z
arenrole-binding:绑定名
--role=arenrole-binding? 指定角色
--user=aren? 指定用户
[root@kube-master rbac]# kubectl create rolebinding arenrole-binding --role=role-reader --user=aren
rolebinding.rbac.authorization.k8s.io/arenrole-binding created
##############或者##################
#yaml格式
[root@kube-master rbac]# vim role-binding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: arenrole-binding
subjects: #定义对那个主体进行操作,有三种Subjects:Service Account、User Account、Groups
- kind: User
name: aren
apiGroup: rbac.authorization.k8s.io
roleRef: #定义使用哪个角色
kind: Role
name: role-reader
apiGroup: rbac.authorization.k8s.io
#加载启动
# kubectl apply -f role-binding.yaml
查看绑定
[root@kube-master rbac]# kubectl get rolebinding
NAME ROLE AGE
arenrole-binding Role/role-reader 4m1s
切换用户无法使用授权以外的权限。
#切换用户
[root@kube-master rbac]# kubectl config use-context aren@kubernetes
Switched to context "aren@kubernetes".
#查看权限(只授权了default名称空间pod和svc的get,list,watch权限)
[root@kube-master rbac]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysa-pod 1/1 Running 1 (8h ago) 24h
mysql 1/1 Running 1 (8h ago) 20h
nginx 1/1 Running 1 (8h ago) 19h
test-downward 1/1 Running 0 7h19m
#无权限删除
[root@kube-master rbac]# kubectl delete nginx
Error from server (Forbidden): pods "nginx" is forbidden: User "aren" cannot delete resource "pods" in API group "" in the namespace "default"
删除aren账号之前绑定的rolebinding
[root@kube-master rbac]# kubectl delete rolebinding arenrole-binding
rolebinding.rbac.authorization.k8s.io "arenrole-binding" deleted
[root@kube-master rbac]# kubectl create clusterrole myclusterrole --verb=get,list,watch --resource=pod,svc
clusterrole.rbac.authorization.k8s.io/myclusterrole created
##########或者#############
#yaml格式
[root@kube-master rbac]# vim clusterrole.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: myclusterrole
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- list
- watch
#启动
kubectl apply -f clusterrole.yaml
#查看clusterrole
[root@kube-master rbac]# kubectl get clusterrole myclusterrole
NAME CREATED AT
myclusterrole 2023-12-16T09:16:31Z
clusterrolebinding? 集群角色绑定名
-clusterrole=myclusterrole? 指定集群角色
--user=aren? ?指定用户
[root@kube-master rbac]# kubectl create clusterrolebinding arencluster-rolebinding --clusterrole=myclusterrole --user=aren
##########或者############
yaml格式
[root@kub-k8s-master ~]# vim clusterrolebinding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: arencluster-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: myclusterrole
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: aren
#启动
kubectl apply -f clusterrolebinding.yaml
#查看绑定
[root@kube-master rbac]# kubectl get clusterrolebinding arencluster-rolebinding
NAME ROLE AGE
arencluster-rolebinding ClusterRole/myclusterrole 56s
切换用户查看kube-system命名空间的pod(查看成功)
#切换账号
[root@kube-master rbac]# kubectl config use-context aren@kubernetes
Switched to context "aren@kubernetes".
#查看权限 查看kube-system空间的pod
[root@kube-master rbac]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-7c87c5f9b8-467xf 1/1 Running 20 (8h ago) 4d20h
calico-node-ql7kv 1/1 Running 8 (8h ago) 4d22h
calico-node-rmnbh 1/1 Running 10 (8h ago) 4d22h
calico-node-z22v9 1/1 Running 15 (150m ago) 4d22h
coredns-78fcd69978-54v6q 1/1 Running 8 (8h ago) 4d22h
coredns-78fcd69978-gt2rx 1/1 Running 8 (8h ago) 4d22h
etcd-kube-master 1/1 Running 10 (8h ago) 4d22h
kube-apiserver-kube-master 1/1 Running 11 (8h ago) 4d22h
kube-controller-manager-kube-master 1/1 Running 21 (151m ago) 4d22h
kube-proxy-j97dk 1/1 Running 8 (8h ago) 4d22h
kube-proxy-mmppf 1/1 Running 11 (8h ago) 4d22h
kube-proxy-r8288 1/1 Running 8 (8h ago) 4d22h
kube-scheduler-kube-master 1/1 Running 15 (149m ago) 3d20h
设置工作上下文
kubectl config set-context aren@kubernetes --cluster=kubernetes --user=aren
查看工作上下文
kubectl config view
切换用户
kubectl config use-context 用户名@kubernetes