로컬에서 k9s로 여러 Kubernetes cluster에 연결하기

생성일
Sep 9, 2024 06:12 AM
태그
kubernetes
기본적으로 config 파일이 아래 경로에 존재하면 k9s를 이용해 바로 cluster에 연결할 수 있다.
~/.kube
 
KUBECONFIG는 대략 아래와 같이 구성된다.
apiVersion: v1 kind: Config clusters: - cluster: server: https://cluster1.example.com certificate-authority: /path/to/cluster1-ca.crt # Path to the certificate authority for cluster1 name: cluster1 contexts: - context: cluster: cluster1 user: user-cluster1 namespace: default name: context-cluster1 current-context: context-cluster1 users: - name: user-cluster1 user: token: <TOKEN_FOR_CLUSTER1>
 
실무에서는 환경별로 클러스터를 따로 두는 경우도 있다. 예를들면 아래처럼 말이다.
- cluster-dev - cluster-test - cluster-prod
 
만약 그렇다면 어떻게 여러 클러스터를 선택적으로 연결하게 할 수 있을까?
config라는 파일명으로 각 클러스터의 설정들을 병합해 주면 된다.
 
만약 config-dev, config-test, config-prod라는 3개의 config 파일이 존재한다고 가정하자.
다음과 같은 명령어로 쉽게 세개의 파일을 머지해 config 파일을 생성할 수 있다.
 
export KUBECONFIG=~/.kube/config-dev:~/.kube/config-test:~/.kube/config-prod kubectl config view --merge --flatten > ~/.kube/config
 
이제 k9s를 통해 2개의 context 모두 연결할 수 있고, 원하는 context를 선택해 연결한 후 작업할 수 있다.