k8s通过三类插件完成权限控制
一,认证,一票通过
1,证书认证,
2,口令认证
3,引导令牌认证
4,jwt token 基于http协议携带json格式令牌
二,对应认证成功的用户基于资源管理的权限指派,授权。一票通过制
三,准入控制。一票否决制
此三类功能全部在apiserver中。
k8s用户信息:
1,常规用户:人类用户。k8s不保存用户账户,只要认证通过即可为任何账户
2,服务账号:为了让集群中pod的程序连入apiserver中进行认证
自建用户证书
#生成私钥
[root@node1 pki]# openssl genrsa -out ilinux.key 2048
Generating RSA private key, 2048 bit long modulus
....................................................+++
......................................................+++
e is 65537 (0x10001)
openssl req -new -key ilinux.key -out ilinux.csr -subj "/CN=ilinux/O=kubeusers"#生成证书申请
#使用k8s自带的CA为自建证书签名。指定CN为用户名称,O为组名称不存在没关系,
[root@node1 pki] openssl x509 -req -in ilinux.csr -CA ./ca.crt -CAkey ./ca.key -CAcreateserial -out ilinux.crt -days 3650
Signature ok
subject=/CN=ilinux/O=kubeusers
Getting CA Private Key
#创建新集群
[root@node1 ~] kubectl config set-cluster mykube --server="https://192.168.1.195:6443" --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true --kubeconfig=/tmp/ilinux.kubeconfig
Cluster "mykube" set
#添加用户
[root@node1 ~] kubectl config set-credentials ilinux --client-certificate=/etc/kubernetes/pki/ilinux.crt --client-key=/etc/kubernetes/pki/ilinux.key --username=ilinux --embed-certs=true --kubeconfig=/tmp/ilinux.kubeconfig
User "ilinux" set.
#将用户加入集群
kubectl config set-context ilinux@mykube --cluster=mykube --user=ilinux --kubeconfig=/tmp/ilinux.kubeconfig
#切换集群及用户
[root@node1 ~] kubectl config use-context ilinux@mykube --kubeconfig=/tmp/ilinux.kubeconfig
Switched to context "ilinux@mykube".
Service Account用户
#配置文件
apiVersion: v1
kind: ServiceAccount
metadata:
name: sa-demo
namespace: default
[root@node1 ~]# kubectl apply -f sa-demo.yml
serviceaccount/sa-demo created
[root@node1 ~]# kubectl get sa
NAME SECRETS AGE
default 1 12d
sa-demo 1 4s
k8s中创建认证功能很简单,但是认证到集群之后的权限确定于此角色的授权
且service account会自动的帮助我们创建一个secret文件帮我们认证
授权
k8s内建授权插件
1.ABAC:基于属性的访问控制,在某资源字段级别做控制
2.RBAC:基于角色的访问控制。通过赋予角色权限,将角色关联至用户。
3.Webhook:
4.node:根据pod对象调度结果为Node进行授权,可访问pod,此为自动进行
role(角色)级别由名称空间级别与集群级别的,集群级别的role可操作名称空间级别的资源
#role资源
[root@node1 chapter10]# cat pods-reader.yaml
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: default
name: res-reader
rules: #定义角色权限
- apiGroups: [""] # "" 表示core API group所有的资源都是有群组类型的,例如pod为:v1的类型, ""空表示所有。核心群组 *表示所有群组
resources: ["pods", "pods/log","services"] #角色对哪些资源有权限
verbs: ["get", "list", "watch"]#角色对资源权限的类型,此角色对上麦规定的资源有哪些权限
创建好角色后,可使用rolebinding绑定至用户,组等等
#定义rolebinding
[root@node1 ~]# vim ilinux-res-reader.yml
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1 #资源组类型
metadata:
name: ilinux-res-reader
namespace: default #属于哪个名称空间
subjects:#关联的账号
- kind: User #类型。为用户也可为group service account
name: ilinux
apiGroup: rbac.authorization.k8s.io#所属的api资源组
roleRef: #定义角色的配置
kind: Role #类型
name: res-reader #名称
apiGroup: rbac.authorization.k8s.io #资源组
#应用好文件之后,可使用之前创建的账户ilinux配置文件查询pod资源,就可读到了,但是读deploy资源时却没有权限。出现报错
[root@node1 ~]# kubectl apply -f ilinux-res-reader.yml
rolebinding.rbac.authorization.k8s.io/ilinux-res-reader created
[root@node1 ~]# kubectl get rolebinding
NAME AGE
ilinux-res-reader 5s
[root@node1 ~]# kubectl get rolebinding -o wide
NAME AGE
ilinux-res-reader 11s
[root@node1 ~]# kubectl get pods --kubeconfig=/tmp/ilinux.kubeconfig
NAME READY STATUS RESTARTS AGE
myweb-5f8b88984c-bmrnl 1/1 Running 0 3d20h
[root@node1 ~]# kubectl get svc --kubeconfig=/tmp/ilinux.kubeconfig
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 13d
myweb NodePort 10.100.112.31 <none> 80:30593/TCP 12d
[root@node1 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 13d
myweb NodePort 10.100.112.31 <none> 80:30593/TCP 12d
[root@node1 ~]# kubectl get deploy --kubeconfig=/tmp/ilinux.kubeconfig
Error from server (Forbidden): deployments.apps is forbidden: User "ilinux" cannot list resource "deployments" in API group "apps" in the namespace "default"
[root@node1 ~]#
修改role资源。将deploy资源类型添加至角色,应用之后账户即有了权限
[root@node1 ~] kubectl apply -f res-reader.yaml
role.rbac.authorization.k8s.io/res-reader configured
[root@node1 ~] kubectl get deploy --kubeconfig=/tmp/ilinux.kubeconfig
NAME READY UP-TO-DATE AVAILABLE AGE
myweb 1/1 1 1 12d
[root@node1 ~]
#创建集群级别的role
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: cluster-res-reader
rules:
- apiGroups: ["*","apps/v1"]
resources: ["pods", "pods/log","services","deployments"]
verbs: ["get", "list", "watch","delete"]
#创建clusterrolebinding
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: ilinux-res-reader
subjects:
- kind: User
name: ilinux
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole#角色资源为clusterrole
name: cluster-res-reader
apiGroup: rbac.authorization.k8s.io
#交叉式绑定使用rolebanding绑定clusterrole。使用rolebanding绑定的clusterrole角色,会使其降级为role角色,只对某名称空间生效
[root@node1 ~]# vim ilinux-res-2.yml
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: ilinux-default-res-reader
namespace: default
subjects:
- kind: User
name: ilinux
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole #绑定角色为ClusterRole
name: cluster-res-reader
apiGroup: rbac.authorization.k8s.io
Dashboard:面板,WebUi界面
多用户类型,将认证请求代理至kubernetes,有k8s处理,使用service account账户
部署dashboard
首先创建一个dashboard证书使用安全通信
openssl genrsa -out dashboard.key 2048
openssl req -new -key dashboard.key -out dashboard.csr -subj "/O=iLinux/CN=dashboard"
openssl x509 -req -in dashboard.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out dashboard.crt -days 3650
kubectl create secret generic kubernetes-dashboard-certs -n kube-system --from-file=dashboard.crt --from-file=dashboard.key
#部署dashboard pod
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
kubectl edit service/kubernetes-dashboard -n kube-system#修改一下svc的访问方式使用nodeport使外部能够访问
kubectl create serviceaccount ui-admin -n kube-system#创建sa账户
kubectl create clusterrolebinding cluster-ui-admin --clusterrole=cluster-admin --serviceaccount=kube-system:ui-admin #绑定角色给用户授权
##复制token登陆
[root@node1 ~]# kubectl describe secret ui-admin-token-lggqc -n kube-system
Name: ui-admin-token-lggqc
Namespace: kube-system
Labels: <none>
Annotations: kubernetes.io/service-account.name: ui-admin
kubernetes.io/service-account.uid: b13fce7e-9705-4c11-ab44-461e60d11227
Type: kubernetes.io/service-account-token
Data
====
ca.crt: 1025 bytes
namespace: 11 bytes
token: eyJhbGciOiJSUzI1NiIsImtpZCI6IkNtcVFKNWRVcnRDMnB2M0tMVkVRYUtPaEFiY0xqVVZZdUwwR2phcTIxQXcifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJ1aS1hZG1pbi10b2tlbi1sZ2dxYyIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJ1aS1hZG1pbiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6ImIxM2ZjZTdlLTk3MDUtNGMxMS1hYjQ0LTQ2MWU2MGQxMTIyNyIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTp1aS1hZG1pbiJ9.Y80KIn9fuPcJ0-6b3L7J6jSzRtDY-Bx5Zfa1GFfrw0YIeiFzG_qe7qyAl7NYr1KcmbZmHeFaebp6mDpFi489Hif7Fxno7kazE6G4k_eWbSmWnhrx4MMD5XxfNhgsv3yXI878-hzW2qOwi94zvPSkR_XYL4xdGJWhfHv5j-syOoskR3OouuUPu6Jr6rHeczFuc69qEJm3RjK1hNxGq_j8TEy0UYSff57HufOF1z0cn-gWkpHAY_Nm-8SqFKfQOvFqeZMDPwnSb3wwe90Lm2Rit1Y2hv8GMYpTaUvYkf2U3BcQNT9Jt26P6WWrH8UF1LgLYWKE8GydHIltwq3vPI4zkg
kubectl describe secret kubernetes-dashboard-token-w25fx -n kube-system #获取令牌登陆
准入控制
分为两类模块:
变异类模块:实现按照模块规范修改用户提交资源属性定义
校验类模块:校验用户给定配置信息是否符合配置规范
LimitRange资源需要结合LimitRanger控制器。名称空间级别,定义某名称空间所有pod需要定义资源限制如果不定义则会使用default资源
ResourceQuota资源:定义整个名称空间使用的资源总量是多少
定义资源限制
apiVersion: v1
kind: LimitRange
metadata:
name: cpu-limit-range
spec:
limits:最大限制
- default: #默认最大值
cpu: 1000m#cpu为1个核心
defaultRequest: #默认最小值
cpu: 1000m#cpu
min:#最小值
cpu: 500m#cpu
max:
cpu: 2000m
maxLimitRequestRatio:#当以最大值为最小的4倍
cpu: 4
type: Container#应用在容器中
定义ResourceQuota#
apiVersion: v1
kind: ResourceQuota
metadata:
name: quota-example
spec:
hard:#硬限制,不能超过
pods: "5" #pod数量
requests.cpu: "1" #cpu最小使用1G
requests.memory: 1Gi#内存最小使用
limits.cpu: "2" #最大限制
limits.memory: 2Gi#最大限制
count/deployments.apps: "2"#deploy控制器
count/deployments.extensions: "2"
persistentvolumeclaims: "2"#pvc不超过两个