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


    pod绑定sa

    [root@k8s2 ~]# kubectl create sa admin
    [root@k8s2 secret]# vim pod5.yaml
    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: mypod
    5. spec:
    6. serviceAccountName: admin
    7. containers:
    8. - name: nginx
    9. image: nginx
    1. kubectl apply -f pod5.yaml
    2. kubectl get pod -o yaml

    认证

    1. [root@k8s2 secret]# cd /etc/kubernetes/pki/
    2. [root@k8s2 pki]# openssl genrsa -out test.key 2048
    3. [root@k8s2 pki]# openssl req -new -key test.key -out test.csr -subj "/CN=test"
    4. [root@k8s2 pki]# openssl x509 -req -in test.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out test.crt -days 365
    1. [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
    2. [root@k8s2 pki]# kubectl config set-context test@kubernetes --cluster=kubernetes --user=test
    3. [root@k8s2 pki]# kubectl config view

    切换用户

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

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

    切回admin

    [root@k8s2 pki]# kubectl config use-context kubernetes-admin@kubernetes
    [root@k8s2 rbac]# vim roles.yaml
    1. kind: Role
    2. apiVersion: rbac.authorization.k8s.io/v1
    3. metadata:
    4. namespace: default
    5. name: myrole
    6. rules:
    7. - apiGroups: [""]
    8. resources: ["pods"]
    9. verbs: ["get", "watch", "list", "create", "update", "patch", "delete"]
    10. ---
    11. kind: RoleBinding
    12. apiVersion: rbac.authorization.k8s.io/v1
    13. metadata:
    14. name: test-read-pods
    15. namespace: default
    16. subjects:
    17. - kind: User
    18. name: test
    19. apiGroup: rbac.authorization.k8s.io
    20. roleRef:
    21. kind: Role
    22. name: myrole
    23. apiGroup: rbac.authorization.k8s.io
    [root@k8s2 rbac]# kubectl apply -f roles.yaml
    [root@k8s2 rbac]# kubectl config use-context test@kubernetes
    1. [root@k8s2 rbac]# kubectl run demo --image nginx
    2. [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
    1. kind: ClusterRole
    2. apiVersion: rbac.authorization.k8s.io/v1
    3. metadata:
    4. name: myclusterrole
    5. rules:
    6. - apiGroups: [""]
    7. resources: ["pods"]
    8. verbs: ["get", "watch", "list", "delete", "create", "update"]
    9. - apiGroups: ["extensions", "apps"]
    10. resources: ["deployments"]
    11. verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
    12. ---
    13. apiVersion: rbac.authorization.k8s.io/v1
    14. kind: RoleBinding #RoleBinding必须指定namespace
    15. metadata:
    16. name: rolebind-myclusterrole
    17. namespace: default
    18. roleRef:
    19. apiGroup: rbac.authorization.k8s.io
    20. kind: ClusterRole
    21. name: myclusterrole
    22. subjects:
    23. - apiGroup: rbac.authorization.k8s.io
    24. kind: User
    25. name: test
    26. ---
    27. apiVersion: rbac.authorization.k8s.io/v1
    28. kind: ClusterRoleBinding #ClusterRoleBinding全局授权,无需指定namespace
    29. metadata:
    30. name: clusterrolebinding-myclusterrole
    31. roleRef:
    32. apiGroup: rbac.authorization.k8s.io
    33. kind: ClusterRole
    34. name: myclusterrole
    35. subjects:
    36. - apiGroup: rbac.authorization.k8s.io
    37. kind: User
    38. name: test
    [root@k8s2 rbac]# kubectl apply -f clusteroles.yaml
    1. [root@k8s2 rbac]# kubectl config use-context test@kubernetes
    2. [root@k8s2 rbac]# kubectl get deployments.apps -A

    切回admin

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

    回收

    1. [root@k8s2 rbac]# kubectl delete -f roles.yaml
    2. [root@k8s2 rbac]# kubectl config delete-user test
    3. [root@k8s2 rbac]# kubectl config delete-context test@kubernetes
  • 相关阅读:
    AntV/G2 柱状图+折线图双轴图表
    numpy和matlab的多维数组展平:ravel, flatten, reshape, (:)
    微积分学习笔记(2):用Go语言画函数图像
    简单理解Vue中的数据代理
    面对全新的编程语言,这些思路可以帮助你察觉漏洞
    07、vue : 无法加载文件 C:\Users\JH\AppData\Roaming\npm\vue.ps1,因为在此系统上禁止运行脚本。
    如何用VisualStudio编写一个利用滑块绘制扇形的小程序 既可以正向绘制也可以反向绘制
    Oracle基础入门
    PXE高效批量网络装机
    ubuntu安装gptsovits
  • 原文地址:https://blog.csdn.net/dgffd/article/details/134299555