本节内容: kube 运行原理 & 安全防护 (service account, [cluster] role, [cluster] role binding,)
kubectl get events --watch
是用来实时查看kubectl接收的事件
kube 中由一系列的认证授权插件,在一个请求到达api服务器时,会经过这些插件,如果有一个通过则说明可以请求
kubectl exec -it curl-custom-sa -c main curl localhost:8001/api/v1/pods
kubectl get sa -o yaml
# 查看 token secret 名称kubectl describe secret my-sa-token-tc54g
# 查看token,默认是jwt格式kubectl exec -it curl-custom-sa -c main -- cat /var/run/secrets/kubernetes.io/serviceaccount/token
#apiVersion: v1
kind: ServiceAccount
metadata:name: my-sa
imagePullSecrets:- name: my-secret---apiVersion: v1
kind: Pod
metadata:name: curl-custom-sa
spec:serviceAccountName: my-sacontainers:- name: mainimage: curlimages/curl:7.87.0command: [ "sleep", "9999999" ]- name: ambassadorimage: luksa/kubectl-proxy:1.6.2
kubectl create role service-reader --verb=get --verb=list --resource=services -n bar
)创建kubectl create rolebinding test --role=service-reader --serviceaccount=foo:default -n foo
kubectl create ns foo
, kubectl create ns bar
kubectl create -f curl-custom-sa.yaml -n bar
kubectl create -f curl-custom-sa.yaml -n foo
apiVersion: v1
kind: Pod
metadata:name: curl-custom-sa
spec:serviceAccountName: defaultcontainers:- name: mainimage: curlimages/curl:7.87.0command: ["sleep", "9999999"]- name: ambassadorimage: luksa/kubectl-proxy:1.6.2
kubectl create -f role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:namespace: fooname: service-reader
rules:- apiGroups: [ "" ]verbs: [ "get", "list" ]resources: [ "services" ]
kubectl create rolebinding test --role=service-reader --serviceaccount=foo:default -n foo
kubectl create rolebinding test2 --role=service-reader --serviceaccount=bar:default -n foo
kubectl exec -it curl-custom-sa -c main -n bar -- curl http://localhost:8001/api/v1/namespaces/foo/services
kubectl exec -it curl-custom-sa -c main -n foo -- curl http://localhost:8001/api/v1/namespaces/bar/services
RoleBinding可以在foo空间中声明, 但是可以将role 绑定到 bar空间的service account,因为role的声明是在foo中,
若是如此配置,则可以让bar空间的pod访问到foo空间的services (如上述实践)
由此得出: RoleBinding、Role都是和命名空间相关的,既这个命名空间的资源可以让谁访问,是这个空间内的谁还是其他的谁(角色)
有些资源不在命名空间中,所以需要用集群范围的角色定义。 如 pv、node、ns
集群资源只能用 ClusterRoleBinding 而不能用 RoleBinding
kubectl create clusterrolebinding pv-test --clusterrole=pv-reader --serviceaccount=foo:default --serviceaccount=bar:default
kubectl exec -it curl-custom-sa -c main -n bar -- curl http://localhost:8001/api/v1/persistentvolumes
kubectl exec -it curl-custom-sa -c main -n foo -- curl http://localhost:8001/api/v1/persistentvolumes
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:name: pv-reader
rules:
- apiGroups: [""]verbs: ["get", "list"]resources: ["persistentvolumes"]
使用RoleBinding结合ClusterRole: ClusterRole中配置资源范围只有RoleBinding所在的命名空间
使用ClusterRoleBinding结合ClusterRole: ClusterRole中配置资源范围作用与整个集群。
kubectl create rolebinding view-test --clusterrole=view --serviceaccount=foo:default -n foo
kubectl create rolebinding view-test --clusterrole=view --serviceaccount=bar:default -n bar
kubectl exec -it curl-custom-sa -c main -n foo -- curl http://localhost:8001/api/v1/namespaces/foo/pods
kubectl exec -it curl-custom-sa -c main -n bar -- curl http://localhost:8001/api/v1/namespaces/bar/pods
kubectl exec -it curl-custom-sa -c main -n bar -- curl http://localhost:8001/api/v1/pods
# 失败, 403kubectl delete rolebinding view-test -n foo
kubectl create clusterrolebinding view-test --clusterrole=view --serviceaccount=foo:default
kubectl exec -it curl-custom-sa -c main -n foo -- curl http://localhost:8001/api/v1/pods
# 成功role、cluster role、role binding、 cluster role binding 之间的用法要多试试才能加深理解
上一篇:Vue基础20之Vuex第一节