尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

Linux学习34-Kubernetes 安全与简单仪表盘

Linux学习34-Kubernetes 安全与简单仪表盘 kubernetes访问控制Authentication认证认证方式现共有8种可以启用一种或多种认证方式只要有一种认证方式通过就不再进行其它方式的认证。通常启用X509 Client Certs和Service Accout Tokens两种认证方式。Kubernetes集群有两类用户由Kubernetes管理的Service Accounts 服务账户和Users Accounts 普通账户。k8s中账号的概念不是我们理解的账号它并不真的存在它只是形式上存在。Authorization授权必须经过认证阶段才到授权请求根据所有授权策略匹配请求资源属性决定允许或拒绝请求。授权方式现共有6种AlwaysDeny、AlwaysAllow、ABAC、RBAC、Webhook、Node。默认集群强制开启RBAC。Admission Control准入控制用于拦截请求的一种方式运行在认证、授权之后是权限认证链上的最后一环对请求API资源对象进行修改和校验。添加用户# 导出集群配置到 kubeadm-config.yml检查在 Kubernetes 中/var/run/secrets/kubernetes.io/serviceaccount 是一个自动挂载到 Pod 内部的目录用于提供 Pod 访问 Kubernetes API 服务器的身份凭证。其中 kube-api-access-4sngw 是挂载该目录的 Secret 名称随机生成不同 Pod 名称不同(ro) 表示该目录为只读权限。目录内容说明该目录下通常包含以下 3 个关键文件用于 Pod 与 API Server 通信时的身份认证和授权token服务账户Service Account的身份令牌JWT 格式API Server 通过此令牌识别 Pod 对应的服务账户。ca.crtKubernetes 集群的 CA 根证书用于 Pod 验证 API Server 的 HTTPS 证书合法性确保通信安全。namespace文本文件内容为当前 Pod 所在的命名空间名称如 default方便应用程序获取自身所在命名空间。查看 secret type查看 volumes, volumeMounts添加用户授权给三个角色以不同的权限apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:name: peter-cluster-viewroleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: viewsubjects:- apiGroup: rbac.authorization.k8s.iokind: Username: peter---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:name: kobe-cluster-editroleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: editsubjects:- apiGroup: rbac.authorization.k8s.iokind: Username: kobe---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:name: john-cluster-adminroleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: cluster-adminsubjects:- apiGroup: rbac.authorization.k8s.iokind: Username: john应用测试授权使用peter角色无法查看secret作为角色kobe无法查看node作为角色john拥有管理员权限使用命名空间测试授权作为角色john可以查看并创建命名空间进入peter角色将默认命名空间设为myspace应该看到peter没有myspace的编辑权限创建pod失败提示没有权限进入kobe角色添加镜像仓库的secret在 myspace 中重新创建 pod部署kubernetes-dashboardGitHub - kubernetes-retired/dashboard: General-purpose web UI for Kubernetes clusters · GitHubGitHub - helm/helm: The Kubernetes Package Manager · GitHub简易k9s命令行dashboard安装helm目前直接从github上拉取的命令已经失效需要前往网站手动下载解压缩修改镜像仓库的位置repository: reg.westos.org/kubernetesui/dashboard-authrepository: reg.westos.org/kubernetesui/dashboard-apirepository: reg.westos.org/kubernetesui/dashboard-webrepository: reg.westos.org/kubernetesui/dashboard-metrics-scraper方式改为LoadBalancerrepository: reg.westos.org/kubernetesui/kong新建项目拉取所需镜像并上传安装kubernetes-dashboard确认服务资源就绪全部为ready状态使用Loadbalancer方式暴露此服务使用浏览器访问添加SAapiVersion: v1kind: ServiceAccountmetadata:name: webadminnamespace: kube-system---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:name: webadmin-cluster-adminroleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: cluster-adminsubjects:- kind: ServiceAccountname: webadminnamespace: kube-system生成token使用token登录成功部署headlampIn-cluster | Headlamp部署在k8s集群内下载部署文件https://raw.githubusercontent.com/kubernetes-sigs/headlamp/main/kubernetes-headlamp.yaml修改镜像使用loadbalancer模式拉取所需镜像并上传部署查看新建serviceaccount绑定集群角色创建token浏览器访问刚才的外部ip使用token登录成功部署桌面应用使用windows 终端安装cat /etc/kubernetes/admin.conf打开windows终端将刚才的admin.conf粘贴后缀改为config进入桌面客户端路径正确点击connect状态变为active成功准入控制准入控制Admission Control是 K8s API Server 的最后一道安全关卡位于认证你是谁和授权你能干什么之后、数据写入 etcd 之前专门负责检查请求内容是否合规并决定是否允许执行。核心定位与流程请求处理链路为认证Authentication→ 授权Authorization→ 准入控制Admission Control→ 持久化到 etcd启动资源配额apiVersion: v1kind: ResourceQuotametadata:name: resource-quotaspec:hard:cpu: 10memory: 1Gipods: 3persistentvolumeclaims: 5requests.storage: 10Gi启动资源限制apiVersion: v1kind: LimitRangemetadata:name: resource-limitsspec:limits:- type: Podmax:cpu: 500mmemory: 750Mimin:cpu: 50mmemory: 50Mi- type: Containerdefault:cpu: 100mmemory: 150MidefaultRequest:cpu: 100mmemory: 150Mimax:cpu: 500mmemory: 750Mimin:cpu: 50mmemory: 50Mi# - type: PersistentVolumeClaim# max:# storage: 2Gi# min:# storage: 1Gi切换用户创建资源创建资源kubectl apply -f secret-db.ymlkubectl apply -f cm-dbinit.jsonkubectl apply -f sts-db-cm-secret-pvc.yml再打开一个终端监控watch kubectl -n myspace get pvc;echo;kubectl -n myspace get pods容器内存不够会被kill同时也无法创建podkubectl apply -f pod-db.ymlError from server (Forbidden): error when creating pod-db.yml: pods database is forbidden: exceeded quota: resource-quota, requested: pods1, used: pods3, limited: pods3查看配额# kubectl describe ns myspace编辑文件修改资源限制去掉注释vim sts-db-cm-secret-pvc.ymlresources:limits:cpu: 500mmemory: 512Mikubectl replace --force -f sts-db-cm-secret-pvc.yml只能启动两个实例探究问题会发现配额满了kubectl describe pods dbserver-0 | grep QoSkubectl describe sts dbserver |tail -n1Warning FailedCreate 23s (x16 over 3m7s) statefulset-controller create Pod dbserver-2 in StatefulSet dbserver failed error: pods dbserver-2 is forbidden: exceeded quota: resource-quota, requested: memory512Mi, used: memory1Gi, limited: memory1Gi切换用户放大配额内存为4Gexport KUBECONFIG~/john.confkubectl -n myspace edit resourcequotas resource-quota测试cpu跑满的效果# kubectl exec -it dbserver-0 -- bashrootdbserver-0:/# md5sum /dev/zero rootdbserver-0:/# pkill md5sum[1] Terminated md5sum /dev/zerorootdbserver-0:/# exit
返回列表