여러 클러스터에 대한 접근 구성하기
여러 클러스터에 대한 접근 구성하기 (Configure Access to Multiple Clusters)
이 페이지는 구성 파일을 사용해 여러 클러스터에 대한 접근을 구성하는 방법을 보여줘요. 클러스터, 사용자, 컨텍스트가 하나 이상의 구성 파일에 정의되면 kubectl config use-context 명령으로 클러스터 사이를 빠르게 전환할 수 있어요.
출처: 문서
본문
시작하기 전에 (Before you begin)
Kubernetes 클러스터가 있어야 하고 kubectl 명령줄 도구가 클러스터와 통신하도록 구성되어 있어야 해요. 이 튜토리얼은 컨트롤 플레인 호스트로 작동하지 않는 노드가 최소 두 개 있는 클러스터에서 실행하는 것을 권장해요. 아직 클러스터가 없다면 minikube를 이용해 만들거나, 아래 Kubernetes 플레이그라운드 중 하나를 사용할 수 있어요.
- iximiuz Labs (https://labs.iximiuz.com/playgrounds?category=kubernetes&filter=all)
- Killercoda (https://killercoda.com/playgrounds/scenario/kubernetes)
- KodeKloud (https://kodekloud.com/public-playgrounds)
kubectl이 설치되어 있는지 확인하려면 kubectl version --client를 실행해요. kubectl 버전은 클러스터의 API 서버와 한 마이너 버전 이내여야 해요.
클러스터, 사용자, 컨텍스트 정의하기 (Define clusters, users, and contexts)
두 개의 클러스터가 있다고 가정해요. 하나는 개발 작업용이고 하나는 테스트 작업용이에요. 개발 클러스터에서는 프론트엔드 개발자가 frontend라는 네임스페이스에서 작업하고, 스토리지 개발자는 storage라는 네임스페이스에서 작업해요. 테스트 클러스터에서는 개발자들이 default 네임스페이스에서 작업하거나 필요에 따라 보조 네임스페이스를 만들어요. 개발 클러스터 접근에는 인증서로 인증이 필요해요. 테스트 클러스터 접근에는 사용자 이름과 비밀번호로 인증이 필요해요.
config-exercise라는 디렉터리를 만들어요. config-exercise 디렉터리에 config-demo라는 파일을 다음 내용으로 만들어요:
apiVersion: v1
kind: Config
preferences: {}
clusters:
- cluster:
name: development
- cluster:
name: test
users:
- name: developer
- name: experimenter
contexts:
- context:
name: dev-frontend
- context:
name: dev-storage
- context:
name: exp-test
구성 파일은 클러스터, 사용자, 컨텍스트를 설명해요. config-demo 파일은 두 클러스터, 두 사용자, 세 컨텍스트를 설명하는 프레임워크를 가지고 있어요.
config-exercise 디렉터리로 가서 다음 명령을 입력해 구성 파일에 클러스터 세부 정보를 추가해요:
kubectl config --kubeconfig=config-demo set-cluster development --server=https://1.2.3.4 --certificate-authority=fake-ca-file
kubectl config --kubeconfig=config-demo set-cluster test --server=https://5.6.7.8 --insecure-skip-tls-verify
구성 파일에 사용자 세부 정보를 추가해요:
kubectl config --kubeconfig=config-demo set-credentials developer --client-certificate=fake-cert-file --client-key=fake-key-seefile
kubectl config --kubeconfig=config-demo set-credentials experimenter --username=exp --password=some-password
- 사용자를 삭제하려면
kubectl --kubeconfig=config-demo config unset users.<name>을 실행할 수 있어요. - 클러스터를 제거하려면
kubectl --kubeconfig=config-demo config unset clusters.<name>을 실행할 수 있어요. - 컨텍스트를 제거하려면
kubectl --kubeconfig=config-demo config unset contexts.<name>을 실행할 수 있어요.
구성 파일에 컨텍스트 세부 정보를 추가해요:
kubectl config --kubeconfig=config-demo set-context dev-frontend --cluster=development --namespace=frontend --user=developer
kubectl config --kubeconfig=config-demo set-context dev-storage --cluster=development --namespace=storage --user=developer
kubectl config --kubeconfig=config-demo set-context exp-test --cluster=test --namespace=default --user=experimenter
config-demo 파일을 열어 추가된 세부 정보를 확인해요. config-demo 파일을 여는 대신 config view 명령을 사용할 수도 있어요.
kubectl config --kubeconfig=config-demo view
출력은 두 클러스터, 두 사용자, 세 컨텍스트를 보여줘요:
apiVersion: v1
clusters:
- cluster:
certificate-authority: fake-ca-file
server: https://1.2.3.4
name: development
- cluster:
insecure-skip-tls-verify: true
server: https://5.6.7.8
name: test
contexts:
- context:
cluster: development
namespace: frontend
user: developer
name: dev-frontend
- context:
cluster: development
namespace: storage
user: developer
name: dev-storage
- context:
cluster: test
namespace: default
user: experimenter
name: exp-test
current-context: ""
kind: Config
preferences: {}
users:
- name: developer
user:
client-certificate: fake-cert-file
client-key: fake-key-file
- name: experimenter
user:
# Documentation note (this comment is NOT part of the command output).
# Storing passwords in Kubernetes client config is risky.
# A better alternative would be to use a credential plugin
# and store the credentials separately.
# See https://kubernetes.io/docs/reference/access-authn-authz/authentication/#client-go-credential-plugins
password: some-password
username: exp
위의 fake-ca-file, fake-cert-file, fake-key-file은 인증서 파일의 경로명에 대한 자리 표시자예요. 이것들을 환경에서 인증서 파일의 실제 경로명으로 바꿔야 해요.
때로는 별도의 인증서 파일 대신 여기에 Base64로 인코딩된 데이터를 포함하고 싶을 수도 있어요. 그 경우에는 키에 -data 접미사를 추가해야 해요. 예: certificate-authority-data, client-certificate-data, client-key-data.
각 컨텍스트는 (클러스터, 사용자, 네임스페이스)의 삼중항이에요. 예를 들어 dev-frontend 컨텍스트는 "개발 클러스터의 frontend 네임스페이스에 접근하려면 developer 사용자의 자격 증명을 사용하라"고 말해요.
현재 컨텍스트를 설정해요:
kubectl config --kubeconfig=config-demo use-context dev-frontend
이제 kubectl 명령을 입력할 때마다 그 작업이 dev-frontend 컨텍스트에 나열된 클러스터와 네임스페이스에 적용돼요. 그리고 명령은 dev-frontend 컨텍스트에 나열된 사용자의 자격 증명을 사용해요.
현재 컨텍스트와 관련된 구성 정보만 보려면 --minify 플래그를 사용해요.
kubectl config --kubeconfig=config-demo view --minify
출력은 dev-frontend 컨텍스트와 관련된 구성 정보를 보여줘요:
apiVersion: v1
clusters:
- cluster:
certificate-authority: fake-ca-file
server: https://1.2.3.4
name: development
contexts:
- context:
cluster: development
namespace: frontend
user: developer
name: dev-frontend
current-context: dev-frontend
kind: Config
preferences: {}
users:
- name: developer
user:
client-certificate: fake-cert-file
client-key: fake-key-file
이제 잠시 동안 테스트 클러스터에서 작업하고 싶다고 가정해요. 현재 컨텍스트를 exp-test로 변경해요:
kubectl config --kubeconfig=config-demo use-context exp-test
이제 입력하는 모든 kubectl 명령이 테스트 클러스터의 default 네임스페이스에 적용돼요. 그리고 명령은 exp-test 컨텍스트에 나열된 사용자의 자격 증명을 사용해요.
새 현재 컨텍스트인 exp-test와 관련된 구성을 보세요.
kubectl config --kubeconfig=config-demo view --minify
마지막으로, 잠시 동안 개발 클러스터의 storage 네임스페이스에서 작업하고 싶다고 가정해요. 현재 컨텍스트를 dev-storage로 변경해요:
kubectl config --kubeconfig=config-demo use-context dev-storage
새 현재 컨텍스트인 dev-storage와 관련된 구성을 보세요.
kubectl config --kubeconfig=config-demo view --minify
두 번째 구성 파일 만들기 (Create a second configuration file)
config-exercise 디렉터리에 config-demo-2라는 파일을 다음 내용으로 만들어요:
apiVersion: v1
kind: Config
preferences: {}
contexts:
- context:
cluster: development
namespace: ramp
user: developer
name: dev-ramp-up
위 구성 파일은 dev-ramp-up이라는 새 컨텍스트를 정의해요.
KUBECONFIG 환경 변수 설정하기 (Set the KUBECONFIG environment variable)
KUBECONFIG라는 환경 변수가 있는지 확인해요. 있다면 KUBECONFIG 환경 변수의 현재 값을 저장해 나중에 복원할 수 있도록 해요. 예:
Linux
export KUBECONFIG_SAVED="$KUBECONFIG"
Windows PowerShell
$Env:KUBECONFIG_SAVED=$ENV:KUBECONFIG
KUBECONFIG 환경 변수는 구성 파일에 대한 경로 목록이에요. 이 목록은 Linux와 Mac에서는 콜론으로 구분되고, Windows에서는 세미콜론으로 구분돼요. KUBECONFIG 환경 변수가 있다면 목록의 구성 파일들을 잘 알아두세요.
일시적으로 두 경로를 KUBECONFIG 환경 변수에 추가해요. 예:
Linux
export KUBECONFIG="${KUBECONFIG}:config-demo:config-demo-2"
Windows PowerShell
$Env:KUBECONFIG=("config-demo;config-demo-2")
config-exercise 디렉터리에서 다음 명령을 입력해요:
kubectl config view
출력은 KUBECONFIG 환경 변수에 나열된 모든 파일에서 병합된 정보를 보여줘요. 특히 병합된 정보가 config-demo-2 파일의 dev-ramp-up 컨텍스트와 config-demo 파일의 세 컨텍스트를 가지고 있음을 주목해요:
contexts:
- context:
cluster: development
namespace: frontend
user: developer
name: dev-frontend
- context:
cluster: development
namespace: ramp
user: developer
name: dev-ramp-up
- context:
cluster: development
namespace: storage
user: developer
name: dev-storage
- context:
cluster: test
namespace: default
user: experimenter
name: exp-test
kubeconfig 파일이 어떻게 병합되는지에 대한 더 많은 정보는 kubeconfig 파일로 클러스터 접근 구성하기를 참고해요.
$HOME/.kube 디렉터리 탐색하기 (Explore the $HOME/.kube directory)
이미 클러스터가 있고 kubectl로 클러스터와 상호작용할 수 있다면, $HOME/.kube 디렉터리에 config라는 파일이 있을 거예요.
$HOME/.kube로 가서 어떤 파일이 있는지 확인해요. 일반적으로 config라는 파일이 있어요. 이 디렉터리에 다른 구성 파일도 있을 수 있어요. 이 파일들의 내용을 간단히 알아두세요.
$HOME/.kube/config를 KUBECONFIG 환경 변수에 추가하기 (Append $HOME/.kube/config to your KUBECONFIG environment variable)
$HOME/.kube/config 파일이 있고 아직 KUBECONFIG 환경 변수에 나열되어 있지 않다면, 지금 KUBECONFIG 환경 변수에 추가해요. 예:
Linux
export KUBECONFIG="${KUBECONFIG}:${HOME}/.kube/config"
Windows Powershell
$Env:KUBECONFIG="$Env:KUBECONFIG;$HOME\.kube\config"
이제 KUBECONFIG 환경 변수에 나열된 모든 파일에서 병합된 구성 정보를 봐요. config-exercise 디렉터리에서 입력해요:
kubectl config view
정리하기 (Clean up)
KUBECONFIG 환경 변수를 원래 값으로 되돌려요. 예:
Linux
export KUBECONFIG="$KUBECONFIG_SAVED"
Windows PowerShell
$Env:KUBECONFIG=$ENV:KUBECONFIG_SAVED
kubeconfig가 나타내는 주체 확인하기 (Check the subject represented by the kubeconfig)
클러스터에 인증한 후 얻게 될 속성(사용자 이름, 그룹)이 항상 분명한 것은 아니에요. 한 번에 둘 이상의 클러스터를 관리한다면 훨씬 더 어려울 수 있어요.
선택한 Kubernetes 클라이언트 컨텍스트에 대해 사용자 이름 같은 주체 속성을 확인하는 kubectl 하위 명령이 있어요: kubectl auth whoami.
자세한 내용은 클라이언트에 대한 인증 정보에 대한 API 접근을 읽어보세요.