当前位置: 首页 > news >正文

kubernetes集群编排——k8s认证授权

pod绑定sa

[root@k8s2 ~]# kubectl create sa admin
[root@k8s2 secret]# vim pod5.yaml
apiVersion: v1
kind: Pod
metadata:name: mypod
spec:serviceAccountName: admincontainers:- name: nginximage: nginx
kubectl apply -f pod5.yamlkubectl get pod -o yaml

认证

[root@k8s2 secret]# cd /etc/kubernetes/pki/
[root@k8s2 pki]# openssl genrsa -out test.key 2048
[root@k8s2 pki]# openssl req -new -key test.key -out test.csr -subj "/CN=test"
[root@k8s2 pki]# openssl  x509 -req -in test.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out test.crt -days 365
[root@k8s2 pki]#  kubectl config set-credentials test --client-certificate=/etc/kubernetes/pki/test.crt --client-key=/etc/kubernetes/pki/test.key --embed-certs=true
[root@k8s2 pki]# kubectl config set-context test@kubernetes --cluster=kubernetes --user=test
[root@k8s2 pki]# kubectl config view

切换用户

[root@k8s2 pki]# kubectl config use-context test@kubernetes[root@k8s2 pki]# kubectl get pod

默认用户没有任何权限,需要授权

切回admin

[root@k8s2 pki]# kubectl config use-context kubernetes-admin@kubernetes
[root@k8s2 rbac]# vim roles.yaml
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:namespace: defaultname: myrole
rules:
- apiGroups: [""]resources: ["pods"]verbs: ["get", "watch", "list", "create", "update", "patch", "delete"]---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: test-read-podsnamespace: default
subjects:
- kind: Username: testapiGroup: rbac.authorization.k8s.io
roleRef:kind: Rolename: myroleapiGroup: rbac.authorization.k8s.io
[root@k8s2 rbac]# kubectl apply -f roles.yaml
[root@k8s2 rbac]# kubectl config use-context test@kubernetes
[root@k8s2 rbac]# kubectl run demo --image nginx[root@k8s2 rbac]# kubectl get pod

现在只能操作pod资源,其它不行

[root@k8s2 rbac]# kubectl get deployments.apps

切回admin

[root@k8s2 rbac]# kubectl config use-context kubernetes-admin@kubernetes

授权

[root@k8s2 rbac]# vim clusteroles.yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: myclusterrole
rules:
- apiGroups: [""]resources: ["pods"]verbs: ["get", "watch", "list", "delete", "create", "update"]
- apiGroups: ["extensions", "apps"]resources: ["deployments"]verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding                      #RoleBinding必须指定namespace
metadata:name: rolebind-myclusterrolenamespace:  default
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: myclusterrole
subjects:
- apiGroup: rbac.authorization.k8s.iokind: Username: test---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding               #ClusterRoleBinding全局授权,无需指定namespace
metadata:name: clusterrolebinding-myclusterrole
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: myclusterrole
subjects:
- apiGroup: rbac.authorization.k8s.iokind: Username: test
[root@k8s2 rbac]# kubectl apply -f clusteroles.yaml
[root@k8s2 rbac]# kubectl config use-context test@kubernetes[root@k8s2 rbac]# kubectl get deployments.apps -A

切回admin

[root@k8s2 rbac]# kubectl config use-context kubernetes-admin@kubernetes

回收

[root@k8s2 rbac]# kubectl delete -f roles.yaml[root@k8s2 rbac]# kubectl config delete-user test[root@k8s2 rbac]# kubectl config delete-context test@kubernetes
http://www.lryc.cn/news/227049.html

相关文章:

  • rabbitmq下载安装教程
  • 数据分析实战 | SVM算法——病例自动诊断分析
  • Splunk Connect for Kafka – Connecting Apache Kafka with Splunk
  • Unity | Shader(着色器)和material(材质)的关系
  • Leetcode—69.x的平方根【简单】
  • 再探单例模式
  • Postman使用json提取器和正则表达式实现接口的关联
  • 【11.10】现代密码学1——密码学发展史:密码学概述、安全服务、香农理论、现代密码学
  • 时间序列预测实战(九)PyTorch实现LSTM-ARIMA融合移动平均进行长期预测
  • 由日期计算当天是星期几
  • springboot模板引擎
  • 如何判断从本机上传到服务器的文件数据内容是一致的?用md5加密算法!
  • Ubuntu 20.04 DNS解析原理, 解决resolv.conf被覆盖问题
  • 探索经典算法:贪心、分治、动态规划等
  • 【Linux】编译Linux内核
  • 网页判断版本更新
  • ros1 基础学习08- 实现Server端自定义四 Topic模式控制海龟运动
  • 面试题之TCP粘包现象及其解决方法
  • Word 插入的 Visio 图片显示为{EMBED Visio.Drawing.11} 解决方案
  • Elasticsearch倒排索引、索引操作、映射管理
  • USEFUL PHRASES
  • 【OpenCV】 拟合直线 与 霍夫直线 对比 , fitLine()与 HoughLinesP()对比
  • Python与ArcGIS系列(六)查找和修复数据源
  • 聊聊logback的TimeBasedRollingPolicy
  • numpy 基础使用
  • sqlite3编译脚本
  • 环形链表解析(c语言)c语言版本!自我解析(看了必会)
  • 科技云报道:数智化升级,如何跨越数字世界与实体产业的鸿沟?
  • Rt-Thread 移植6--多线程(KF32)
  • HarmonyOS应用开发-首选项与后台通知管理