๋ชจ๋ ํต์ ์ TLS๋ฅผ ํตํด Kube-apiserver๋ฅผ ์ด์ฉํ์ฌ ์์ฒญํจ
https://kubernetes.io/docs/reference/access-authn-authz/authentication/
https://kubernetes.io/docs/reference/access-authn-authz/authorization/#authorization-modules
user
, Service Account(์ ํ๋ฆฌ์ผ์ด์
์ ์ฌ์ฉ)
Apiserver ์๋น์ค ์คํ ์ --token-auth-file=<ํ ํฐํ์ผ.csv>
- static pod ์ฌ์ค์ ๋ฐ api์๋ฒ๋ฅผ ๋ค์ ์์ํด์ผํจ
ํจ์ค์๋, ์ ์ , ์ ์ ์์ด๋, ๊ทธ๋ฃน #ํํ์ ์ด์ ์์ฑํจ
--token-auth-file=<ํ ํฐํ์ผ.csv>
์ค์ ์ kube-apiserver ํ๋์ ์ ์ฉํ ๋ ์ปจํ
์ด๋ ์ด๊ธฐ ๋๋ฌธ์ ๋ถ๋ฅจ๋ง์ดํธ๋ฅผ ํตํด์ csvํ์ผ ๊ฒฝ๋ก๋ฅผ ์ค์ ํด์ฃผ๋๋ก ํ ๊ฒ
$ kubectl config set-credentials user1 --token=password1
: ์ ์ ์ ๋ํ ๋น๋ฐ๋ฒํธ
$ kubectl config set-context user1-context --cluster=kubernetes \ --namespace=frontend --user=user1
$ kubectl get pod --user user1
: user1์ผ๋ก ๋ก๊ทธ์ธ
kubectl config use-context kubernetes-admin@kubernetes
https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/
kubectl get sa default -o yaml
(default์๋ ์๋ ์์ฑ๋์ด์์)
kubectl create serviceaccount <์๋น์ค์ด์นด์ดํธ๋ช
>
sa์ ํจ๊ป ์ํฌ๋ฆฟ๋ ์์ฑ๋จ (์ํฌ๋ฆฟ ์์๋ ํ ํฐ๊ฐ ์์ฑ)
spec.serviceAccount: <์๋น์ค์ด์นด์ดํธ๋ช
>
- ์ค์ ํ์ง ์์ผ๋ฉด default sa๋ก ์ค์ ๋จ
- sa ์ฐ๊ฒฐ ์ ํ ํฐ์ด ๋ถ๋ฅจ ๋ง์ดํธ๋จ
https://kubernetes.io/ko/docs/tasks/administer-cluster/access-cluster-api/#kubectl-%ED%94%84%EB%A1%9D%EC%8B%9C-%EC%97%86%EC%9D%B4-%EC%A0%91%EA%B7%BC
- ํ๋ ๋ด /var/run/secrets/kubernetes.io/serviceaccount
ca.crt, namespace, token
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
curl -X GET https://$KUBERNETES_SERVICE_HOST/api --header "Authorization: Bearer $TOKEN" โinsecure
: ํ ํฐ ๊ฐ์ ๋ณ์๋ก curl ๋ช ๋ น ์คํ (insecure ์ธ์ฆ์ ๊ธฐ๊ฐ ๋ฌด์)- KUBERNETES_SERVICE_HOST -> pod๋ด์ printenv๋ฅผ ํตํด์ ํ์ธ
SSL ์ธ์ฆ
sudo ls /etc/kubernetes/pki
sudo ls /etc/kubernetes/pki/etcd
sudo ls /etc/kubernetes/manifests/
sudo ls /var/lib/kubelet/pki
sudo ls /var/lib/kubelet/
sudo cat /var/lib/kubelet/config.yaml
sudo openssl x509 -in <certificate๊ฒฝ๋ก> -text
Issure ์ ๋ํ ์ ๋ณด๋ฅผ ํ์ธํด์ผํจ (์ธ์ฆ์ ๋ฐ๊ธ ์ ๊ณต์)
kubeadm alpha certs check-expiration
: ๋ชจ๋ ์ธ์ฆ์ ์ ํจ ํ์ธ
kubeadm alpha certs renew all
: ๋ชจ๋ ์ธ์ฆ์ ์๋ก ๊ฐฑ์ https://kubernetes.io/ko/docs/tasks/administer-cluster/certificates/
openssl genrsa -out <ํคํ์ผ๋ช
.key> 2048
: ๊ธธ์ด 2048 ๊ฐ์ธํค ์์ฑ
openssl req -new -key <๊ฐ์ธํคํ์ผ.key> -out <csrํ์ผ.csr> -subj "/CN=<์ฌ์ฉ์์ด๋ฆ>/O=<๊ทธ๋ฃน์ด๋ฆ>"
- CN : ์ฌ์ฉ์์ด๋ฆ
- O: ๊ทธ๋ฃน ์ด๋ฆ
- ca์ ์ธ์ฆ ๊ฒฐ๊ณผ๋ฅผ csrํ์ผ๋ก ๋ฐ์
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key \ -CAcreateserial -out server.crt -days 10000 \ -extensions v3_ext -extfile csr.conf -sha256
openssl x509 -req -in <csrํ์ผ.csr> -CA /etc/kubernetes/pki/ca.crt -CAkey /etc/kubernetes/pki/ca.key -CAcreateserial -out <๊ฒฐ๊ณผcrtํ์ผ.crt> -days 500
$ kubectl config set-credentials <์ ์ ๋ช
> --client-certificate=.certs/<ca์ธ์ฆ์. crt> --client-key=.certs/<๊ฐ์ธํค.key>
$ kubectl config set-context <์ ์ ๋ช
>-context --cluster=kubernetes --namespace=office --user=<์ ์ ๋ช
>
kubectl config use-context <์ ์ ์ธ์ฆ ์ปจํ
์คํธ>
kubectl config use-context kubernetes-admin@kubernetes
: ์ฟ ๋ฒ๋คํฐ์ค ์ด๋๋ฏผ์ผ๋ก ๋์์ค๊ธฐ
๊ถํ์ด ์์ด์ ์๋ต์ด Forbidden ์ผ๋ก ์ค๋ ๋ชจ์ต
kube config ํ์ผ์ด ์์ผ๋ฉด curl ์ ํตํด์ ๋ช ๋ น ์ํ ํด์ผํจ -> ๋นํจ์จ์
kubeconfig ์์น
cd $HOME/.kube/
kube config view
kube config view --kube config=<configํ์ผ๋ช
>
๋ฑ๋กํ credential ๋ก๊ทธ์ธ , ์ปจํ ์คํธ ์ ๋ณด ๋ฑ์ด ๋ค์ด์์
- cluster: ์ฐ๊ฒฐํ ์ฟ ๋ฒ๋คํฐ์ค ํด๋ฌ์คํฐ ์ ๋ณด
- users: ์ฌ์ฉํ ๊ถํ ์ฌ์ฉ์
- contexts: cluster์ user๋ฅผ ์ด์ด์ฃผ๋ context ์ ๋ณด
kubectl config use-context <์ปจํ
์คํธ๋ช
>@<ํด๋ฌ์คํฐ๋ช
>
kubectl get pod --context <๋ฑ๋ก๋ ์ปจํ
์คํธ๋ช
>
kubectl get pod --user <๋ฑ๋ก๋ ์ ์ ๋ช
>
kubectl get pod --as <๋ฑ๋ก๋ ์ ์ ๋ช
>
https://kubernetes.io/docs/reference/access-authn-authz/rbac/
- rbac.authorization.k8s.ioAPI๋ฅผ ์ฌ์ฉ
- ๊ถํ ๊ฒฐ์ ์ ๋ด๋ฆฌ๊ณ Kubernetes Api๋ฅผ ํตํด ์ ์ฑ ์ ๋์ ์ผ๋ก ๊ตฌ์ฑ
- RBAC๋ฅผ ์ฌ์ฉํ์ฌ ๋ฃฐ ์ ์์ apiserver์
--authorization-mode=RBAC
์ต์ ํ์
- kubeadm ์ค์น์ ์ด๋ฏธ ์ค์ ๋์ด์์
kube-apiserver --authorization-mode=Example,RBAC --other-options --more-options
- ๋กค ์ ์ -> ๋กค ๋ถ์ฌ(๋ฐ์ธ๋ฉ)
๋์๋ณด๋์ ํด๋ฌ์คํฐ ๋กค์ ๋ถ์ฌํ์ฌ ์ฌ์ฉ ํจ : ๋งํฌ
apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: namespace: default name: pod-reader rules: - apiGroups: [""] # "" indicates the core API group resources: ["pods"] verbs: ["get", "watch", "list"]
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: # "namespace" omitted since ClusterRoles are not namespaced name: secret-reader rules: - apiGroups: [""] # # at the HTTP level, the name of the resource for accessing Secret # objects is "secrets" resources: ["secrets"] verbs: ["get", "watch", "list"]
apiVersion: rbac.authorization.k8s.io/v1 # This role binding allows "jane" to read pods in the "default" namespace. # You need to already have a Role named "pod-reader" in that namespace. kind: RoleBinding metadata: name: read-pods namespace: default subjects: # You can specify more than one "subject" - kind: User # ServiceAccount๋ก๋ ์ค์ ๊ฐ๋ฅ name: jane # "name" is case sensitive apiGroup: rbac.authorization.k8s.io roleRef: # "roleRef" specifies the binding to a Role / ClusterRole kind: Role #this must be Role or ClusterRole name: pod-reader # this must match the name of the Role or ClusterRole you wish to bind to apiGroup: rbac.authorization.k8s.io
apiVersion: rbac.authorization.k8s.io/v1 # This cluster role binding allows anyone in the "manager" group to read secrets in any namespace. kind: ClusterRoleBinding metadata: name: read-secrets-global subjects: - kind: Group name: manager # Name is case sensitive apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: secret-reader apiGroup: rbac.authorization.k8s.io
TLS ์์ ์ฌ์ฉํ๋ test ์ ์ ์ปจํ ์คํธ์ ๊ถํ ๋ถ์ฌ
apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: namespace: default name: pod-reader rules: - apiGroups: [""] # "" indicates the core API group resources: ["pods"] verbs: ["get", "watch", "list"]
apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: read-pods namespace: default subjects: - kind: User name: test apiGroup: rbac.authorization.k8s.io roleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.io
- rolebinding์ ๋ค์์คํ์ด์ค๋ฅผ default๋ก ์ง์ ํด์ฃผ์๊ธฐ ๋๋ฌธ์ ํด๋น ๋ค์์คํ์ด์ค์์ ์ฌ์ฉ ๊ฐ๋ฅ
- .csrํ์ผ๋ง๋ค๋ O์ต์ ์ด ๋ค์์คํ์ด์ค์ธ๊ฐ?? ๋ ํ์ธํด๋ด์ผ ํ ๋ฏํ๋ค