Table of Contents

Управление правами в Kubernetes

Реклама

Техническое задание

Запись вебинара

Шаг 1. Что у нас есть, для начала

Шаг 2. Создание учетной записи

Вариант 2.1 Использование сертификатов

user1@client1:~$ cat user1.req | base64 -w0
kube1:~/users# kubectl explain csr.spec.usages

kube1:~/users# cat user1.req.yaml
apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
  name: user1
spec:
  request: LS0t...S0tCg==
  signerName: kubernetes.io/kube-apiserver-client
  expirationSeconds: 8640000  # 100 * one day
  usages:
#  - digital signature
#  - key encipherment
  - client auth
kube1:~/users# kubectl apply -f user1.req.yaml

kube1:~/users# kubectl describe csr/user1

kube1:~/users# kubectl certificate approve user1

kube1:~/users# kubectl get csr

kube1:~/users# kubectl get csr/user1 -o yaml

kube1:~/users# kubectl get csr/user1 -o jsonpath="{.status.certificate}" | base64 -d | tee user1.crt


user1@client1:~$ scp root@kube1:users/user1.crt .

Вариант 2.2 Использование ServiceAccount

Шаг 2.3 Создание файла конфигурации kubectl

user1@client1:~$ kubectl config set-cluster cluster.local --insecure-skip-tls-verify=true --server=https://192.168.13.221:6443

user1@client1:~$ cat .kube/config

user1@client1:~$ kubectl config set-credentials user1 --client-certificate=user1.crt --client-key=user1.key --embed-certs=true
  ИЛИ
user1@client1:~$ kubectl config set-credentials user1 --token=...................................

user1@client1:~$ kubectl config get-users

user1@client1:~$ kubectl config set-context default-context --cluster=cluster.local --user=user1

user1@client1:~$ kubectl config use-context default-context

user1@client1:~$ kubectl auth whoami

user1@client1:~$ kubectl get pods
Error from server (Forbidden) или ...

Шаг 3. Использование Role и RoleBinding

Предоставление доступа к services/proxy в Namespace

kube1:~/users# cat lh-svc-proxy-role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: longhorn-system
  name: lh-svc-proxy-role
rules:
- apiGroups: [""]
  resources: ["services/proxy"]
  verbs: ["get"]
kube1:~/users# cat user1-lh-svc-proxy-rolebinding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: user1-lh-svc-proxy-rolebinding
  namespace: longhorn-system
subjects:
- kind: User
  name: user1
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: lh-svc-proxy-role
  apiGroup: rbac.authorization.k8s.io
kube1:~/users# kubectl apply -f lh-svc-proxy-role.yaml,user1-lh-svc-proxy-rolebinding.yaml

student@client1:~$ kubectl proxy

student@client1:~$ curl http://localhost:8001/api/v1/namespaces/longhorn-system/services/longhorn-frontend:80/proxy/

student@client1:~$ curl http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/

Предоставление полного доступа к Namespace




Поиск предоставленных ролей для учетной записи

kube1:~/users# kubectl get rolebindings --all-namespaces -o=json | jq '.items[] | select(.subjects[]?.name == "user1")'

Шаг 4. Использование ClusterRole и ClusterRoleBinding на

Предоставление доступа к services/port-forward в Cluster

kube1:~/users# cat svc-pfw-role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
#kind: Role
metadata:
  name: svc-pfw-role
#  namespace: my-pgcluster-ns
rules:
- apiGroups: [""]
  resources: ["services"]
  verbs: ["get"]
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list"]
- apiGroups: [""]
  resources: ["pods/portforward"]
  verbs: ["create"]
kube1:~/users# cat user1-svc-pfw-rolebinding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
#kind: RoleBinding
metadata:
  name: user1-svc-pfw-rolebinding
#  namespace: my-pgcluster-ns
subjects:
- kind: User
  name: user1
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
#  kind: Role
  name: svc-pfw-role
  apiGroup: rbac.authorization.k8s.io
kube1:~/users# kubectl apply -f svc-pfw-role.yaml,user1-svc-pfw-rolebinding.yaml

student@client1:~$ kubectl port-forward -n my-pgcluster-ns services/my-pgcluster-rw 5432:5432

student@client1:~$ psql postgres://keycloak:strongpassword@127.0.0.1:5432/postgres

Предоставление полного доступа к Cluster

kube1:~/users# kubectl get clusterroles |less

kube1:~/users# kubectl get clusterrolebindings cluster-admin -o yaml

kube1:~/users# kubectl get clusterrole view -o yaml

kube1:~/users# cat user1-view-clusterrolebinding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: user1-view-clusterrolebinding
subjects:
- kind: User
  name: user1
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: view
  apiGroup: rbac.authorization.k8s.io
kube1:~/users# kubectl apply -f user1-view-clusterrolebinding.yaml

student@client1:~$ kubectl get pods -A

student@client1:~$ kubectl port-forward -n my-pgcluster-ns services/my-pgcluster-rw 5432:5432
error: error upgrading connection: pods "my-pgcluster-3" is forbidden: User "user1" cannot create resource "pods/portforward" in API group "" in the namespace "my-pgcluster-ns"

Поиск предоставленных кластерных ролей для учетной записи

kube1:~/users# kubectl get clusterrolebindings -o=json | jq '.items[] | select(.subjects[]?.name == "user1")'

Шаг 5. Использование JSON Web Token (JWT) для доступа в Kubernetes

Вопросы?

Домашнее задание

  1. Куда и через сколько исчезает “kubectl get csr” после “approve” ?