개발강의정리/DevOps

[데브옵스를 위한 쿠버네티스 마스터] 클러스터 유지와 보안, 트러블슈팅 - kube config 파일을 사용한 인증

nineDeveloper 2021. 1. 20.
728x90

kube config 파일을 사용한 인증


직접 curl을 사용하여 요청

  • key와 cert, cacert 키를 가지고 직접 요청 가능
  • 그러나 매번 이 요청을 사용하기에는 무리가 있음
$ curl https://kube-api-server:6443/api/v1/pods\
--key user.key \
--cert user.crt \
--cacert ca.crt \

kubectl config 명령 확인

  • set-credentials: 로그인 관련 정보 등록
  • set-cluster: 클러스트를 추가로 등록
  • set-context: 만든 credentials 와 cluster 를 조합해서 어떤 서버에 어떤 아이디를 가지고 조회를 할 건지 정함
$ kubectl config
Modify kubeconfig files using subcommands like "kubectl config set current-context my-context"

 The loading order follows these rules:

  1.  If the --kubeconfig flag is set, then only that file is loaded. The flag may only be set once and no merging takes
place.
  2.  If $KUBECONFIG environment variable is set, then it is used as a list of paths (normal path delimiting rules for
your system). These paths are merged. When a value is modified, it is modified in the file that defines the stanza. When
a value is created, it is created in the first file that exists. If no files in the chain exist, then it creates the
last file in the list.
  3.  Otherwise, ${HOME}/.kube/config is used and no merging takes place.

Available Commands:
  current-context 현재-컨텍스트를 표시합니다
  delete-cluster  kubeconfig에서 지정된 클러스터를 삭제합니다
  delete-context  kubeconfig에서 지정된 컨텍스트를 삭제합니다
  delete-user     Delete the specified user from the kubeconfig
  get-clusters    kubeconfig에 정의된 클러스터를 표시합니다
  get-contexts    하나 또는 여러 컨텍스트를 설명합니다
  get-users       Display users defined in the kubeconfig
  rename-context  Renames a context from the kubeconfig file.
  set             kubeconfig 파일에서 단일값을 설정합니다
  set-cluster     kubeconfig에서 클러스터 항목을 설정합니다
  set-context     kubeconfig에서 컨텍스트 항목을 설정합니다
  set-credentials kubeconfig에서 사용자 항목을 설정합니다
  unset           kubeconfig 파일에서 단일값 설정을 해제합니다
  use-context     kubeconfig 파일에서 현재-컨텍스트를 설정합니다
  view            병합된 kubeconfig 설정 또는 지정된 kubeconfig 파일을 표시합니다

Usage:
  kubectl config SUBCOMMAND [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).

kube config 파일 확인하기

  • $ kubectl config view
  • $ kubectl config view --kube config=<config file>

토큰 정보는 REDACTED 로 표시되지 않음

$ kubectl config view

.kube 디렉토리로 이동해서 config 파일 내용을 보면 BASE64 로 인코딩된 토큰 정보를 확인할 수 있음

$ cd .kube
$ cat config

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM1ekNDQWMrZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRJd01USXlOakV5TXpNMU1Wb1hEVE13TVRJeU5ERXlNek0xTVZvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBS24rCjV2...
    server: https://10.0.2.15:6443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    namespace: office
    user: gasbugs
  name: gasbugs@kubernetes
- context:
    cluster: kubernetes
    user: kubernetes-admin
  name: kubernetes-admin@kubernetes
- context:
    cluster: kubernetes
    namespace: frontend
    user: user1
  name: user1-context
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: gasbugs
  user:
    client-certificate: /home/server1/gasbugs.crt
    client-key: /home/server1/gasbugs.key
- name: kubernetes-admin
  user:
    client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURFekNDQWZ1Z0F3SUJBZ0lJQzV1aVZZcVkvdTB3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB5TURFeU1qWXhNak16TlRGYUZ3MHlNVEV5TWpZeE1qTXpOVFJhTURReApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1Sa3dGd1lEVlFRREV4QnJkV0psY201bGRHVnpMV0ZrCmJX...
    client-key-data: LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlFb2dJQkFBS0NBUUVBc0syR2dFQUg5MFRwelRiWHRnTmFFNmZNajZac0FTSXlpcTFLak1iQVlGZDhWZUp5ClZ3d1g0VVBXc3MzRXlJNDhqSDdFK3gzbjNPd1dnRytKWlBqWFpSS2pOd0lhOTVlZ3pIZnVBanlxeUlpM0tqSUgKOE1YaWFiWnhmUFdyTWFIVSs3ZjlRUFg4T0tsVWFaVStEaFFaS2Q1c1MwWTZjbWY0VWhzbmlOK29vR0R4Wm5Y...
- name: user1
  user:
    token: password1

kube config의 구성

  • ~/.kube/config 파일을 확인하면 세 가지 부분으로 작성됨
  • clusters: 연결할 쿠버네티스 클러스터의 정보 입력
  • users: 사용할 권한을 가진 사용자 입력
  • contexts: cluster와 user를 함께 입력하여 권한 할당

  • 각각의 user와 cluster가 잘 맞는지 확인


인증 사용자 바꾸기

  • kubectl config use-context user@kube-cluster

인증 테스트

  • kubectl get pod
  • forbidden이 나오면 성공!

use-context 를 사용하지 않고도 아래와 같은 방법들로 user 나 context 를 선택해서 계정 권한을 확인할 수 있음

$ kubectl get pod --context user1-context
Error from server (Forbidden): pods is forbidden: User "user1" cannot list resource "pods" in API group "" in the namespace "frontend"

$ kubectl get pod --user user1
Error from server (Forbidden): pods is forbidden: User "user1" cannot list resource "pods" in API group "" in the namespace "default"

$ kubectl get pod --as user1
Error from server (Forbidden): pods is forbidden: User "user1" cannot list resource "pods" in API group "" in the namespace "default"

연습문제

  • john을 유저로 사용할 수 있도록 세팅하라
728x90

댓글

💲 추천 글