Minikube에 Consul 배포하기
Minikube에 Consul 배포하기 (Consul on Minikube)
이 문서는 minikube로 로컬 Kubernetes 클러스터를 만들고 그 클러스터에 Consul 데이터센터를 배포하는 방법을 설명해요. 배포 후 CLI, UI, API로 Consul을 사용하는 방법까지 실습해 볼게요.
출처: 문서
본문
이 문서는 minikube로 로컬 Kubernetes 클러스터를 만들고 minikube 클러스터에 Consul 데이터센터를 배포하는 방법을 설명합니다. Consul을 배포한 후에는 CLI, UI 및/또는 API를 사용해 Consul을 다룹니다.
요구 사항 (Requirements)
Minikube에 Consul을 배포하려면 다음이 필요합니다:
Minikube 클러스터 생성 (Create a Minikube cluster)
다음 명령은 minikube로 Kubernetes 클러스터를 만듭니다. 클러스터 이름을 dc1으로 설정하고 4GB 메모리를 사용하며 Kubernetes 버전을 v1.22.0으로 지정합니다.
$ minikube start --profile dc1 --memory 4096 --kubernetes-version=v1.22.0
😄 minikube v1.28.0 on Darwin 12.6
🎉 minikube 1.25.3 is available! Download it: https://github.com/kubernetes/minikube/releases/tag/v1.25.1
💡 To disable this notice, run: 'minikube config set WantUpdateNotification false'
✨ Automatically selected the docker driver. Other choices: hyperkit, virtualbox
👍 Starting control plane node minikube in cluster minikube
🔥 Creating docker container (CPUs=2, Memory=4096MB) ...
🐳 Preparing Kubernetes v1.22.0 on Docker 20.10.0 ...
▪ Generating certificates and keys ...
▪ Booting up control plane ...
▪ Configuring RBAC rules ...
🔎 Verifying Kubernetes components...
🌟 Enabled addons: storage-provisioner, default-storageclass
🏄 Done! kubectl is now configured to use "dc1" cluster and "default" namespace by default
추가 클러스터 파라미터를 지정하는 방법에 대한 정보는 minikube 문서를 참조하세요.
Consul 배포 (Deploy Consul)
공식 Consul Helm 차트 또는 Consul K8S CLI를 사용해 완전한 Consul 데이터센터를 배포할 수 있습니다. 차트는 합리적인 기본값을 제공하지만, minikube와 더 쉽게 통합하고 유용한 기능을 활성화하기 위해 몇 가지 값을 재정의할 것입니다. 이러한 설치 옵션에 대해 자세히 알아보려면 Consul Kubernetes 설치 문서를 검토할 수 있습니다.
values 파일 생성 (Create a values file)
배포를 사용자 지정하려면 Consul 배포를 커스터마이즈할 values.yaml 파일을 만드세요.
values.yaml:
# Contains values that affect multiple components of the chart.
global:
# The main enabled/disabled setting.
# If true, servers, clients, Consul DNS and the Consul UI will be enabled.
enabled: true
# The prefix used for all resources created in the Helm chart.
name: consul
# The name of the datacenter that the agents should register as.
datacenter: dc1
# Enables TLS across the cluster to verify authenticity of the Consul servers and clients.
tls:
enabled: true
# Enables ACLs across the cluster to secure access to data and APIs.
acls:
# If true, automatically manage ACL tokens and policies for all Consul components.
manageSystemACLs: true
# Configures values that configure the Consul server cluster.
server:
enabled: true
# The number of server agents to run. This determines the fault tolerance of the cluster.
replicas: 1
# Contains values that configure the Consul UI.
ui:
enabled: true
# Registers a Kubernetes Service for the Consul UI as a NodePort.
service:
type: NodePort
# Configures and installs the automatic Consul Connect sidecar injector.
connectInject:
enabled: true
클러스터에 Consul 설치 (Install Consul in your cluster)
이제 공식 Consul Helm 차트 또는 Consul K8S CLI를 사용해 Kubernetes 클러스터에 완전한 Consul 데이터센터를 배포할 수 있습니다.
Consul K8S CLI (Mac & Linux)
Helm
consul-k8s로 kind 클러스터에 Consul을 설치합니다. 프롬프트가 나타나면 y로 설치를 확인합니다.
$ consul-k8s install -config-file=values.yaml -set global.image=hashicorp/consul:1.14.0
## ...
Proceed with installation? (y/N) y
추가 설정에 대해 더 알아보려면 공식 Consul K8S CLI 문서를 검토하세요.
Helm으로 kind 클러스터에 Consul을 설치합니다. 프롬프트가 나타나면 y로 설치를 확인합니다.
$ helm install --values values.yaml consul hashicorp/consul --create-namespace --namespace consul --version "1.0.0"
기본 설정에 대해 더 알아보려면 공식 Helm 차트 값을 검토하세요.
Consul 리소스가 성공적으로 생성되었는지 확인합니다.
$ kubectl get pods --namespace consul
NAME READY STATUS RESTARTS AGE
consul-connect-injector-6fc8d669b8-2n82l 1/1 Running 0 2m34s
consul-connect-injector-6fc8d669b8-9mqfm 1/1 Running 0 2m34s
consul-controller-554c7f79c4-2xc64 1/1 Running 0 2m34s
consul-server-0 1/1 Running 0 2m34s
consul-webhook-cert-manager-64889c4964-wxc9b 1/1 Running 0 2m34s
CLI를 Consul 클러스터와 상호작용하도록 구성 (Configure your CLI to interact with Consul cluster)
이 섹션에서는 터미널에 환경 변수를 설정하여 Consul CLI가 Consul 클러스터와 상호작용할 수 있게 합니다. Consul CLI는 이 환경 변수들을 동작 기본값으로 읽으며 consul 명령을 실행할 때 이 값들을 참조합니다.
토큰은 사용자, 서비스, Consul 에이전트를 인증하는 데 사용되는 ACL 시스템의 산출물입니다. 이 Consul 데이터센터에서는 ACL이 활성화되어 있으므로 리소스에 대한 접근을 요청하는 엔티티는 해당 리소스에 권한을 부여하는 정책, 서비스 아이덴티티 또는 노드 아이덴티티와 연결된 토큰을 포함해야 합니다. ACL 시스템은 토큰을 확인하고 연결된 권한에 따라 리소스 접근을 허용하거나 거부합니다. 부트스트랩 토큰은 모든 리소스와 API에 대한 무제한 권한을 가집니다.
해당 Kubernetes 시크릿에서 ACL 부트스트랩 토큰을 가져와 환경 변수로 설정합니다.
$ export CONSUL_HTTP_TOKEN=$(kubectl get --namespace consul secrets/consul-bootstrap-acl-token --template={{.data.token}} | base64 -d)
Consul 대상 주소를 설정합니다. 기본적으로 Consul은 http의 경우 포트 8500, https의 경우 8501에서 실행됩니다.
$ export CONSUL_HTTP_ADDR=https://127.0.0.1:8501
Consul 클러스터와의 통신을 단순화하기 위해 SSL 검증 확인을 제거합니다.
$ export CONSUL_HTTP_SSL_VERIFY=false
참고
프로덕션 환경에서는 이 SSL 검증을 true로 유지할 것을 권장합니다. 이 검증을 제거하는 것은 TLS가 구성되지 않은 Consul 클러스터가 있는 개발 환경 및 데모 목적에서만 하세요.
Consul 서비스 보기 (View Consul services)
이 섹션에서는 CLI, UI 및/또는 API로 Consul 서비스를 보고 서비스 메시의 세부 사항을 살펴봅니다.
CLI
UI
API
별도의 터미널 창을 열고 consul-ui 서비스 이름을 대상으로 kubectl port-forward를 사용해 Consul 서버를 노출합니다.
$ kubectl port-forward svc/consul-ui --namespace consul 8501:443
원래 터미널에서 CLI 명령 consul catalog services를 실행해 Consul에 등록된 서비스 목록을 반환합니다. Consul 클러스터에서 실행 중인 유일한 서비스가 consul 서비스이므로 해당 서비스만 반환되는 것을 확인하세요.
$ consul catalog services
consul
에이전트는 서버 또는 클라이언트 모드로 실행됩니다. 서버 에이전트는 서비스·노드 IP 주소, 헬스 체크, 구성을 포함한 모든 상태 정보를 저장합니다. 클라이언트 에이전트는 데이터센터의 대부분을 구성하는 가벼운 프로세스입니다. 클라이언트는 서버 에이전트에게 서비스 헬스 상태를 보고합니다. 클라이언트는 서비스가 실행되는 모든 파드에서 실행되어야 합니다.
CLI 명령 consul members를 실행해 환경의 Consul 에이전트 목록을 반환합니다.
$ consul members
Node Address Status Type Build Protocol DC Partition Segment
consul-server-0 10.244.0.12:8301 alive server 1.14.0 2 dc1 default <all>
토큰 값을 터미널에 출력하고 클립보드에 복사합니다. 이 ACL 토큰을 Consul UI에서 인증하는 데 사용합니다.
$ echo $CONSUL_HTTP_TOKEN
fe0dd5c3-f2e1-81e8-cde8-49d26cee5efc
별도의 터미널 창을 열고 consul-ui 서비스 이름을 대상으로 kubectl port-forward를 사용해 Consul UI를 노출합니다.
$ kubectl port-forward svc/consul-ui --namespace consul 8501:443
브라우저에서 https://localhost:8501을 열어 Consul UI를 찾습니다. 이 환경은 리소스에 자체 서명된 TLS 인증서를 사용하므로 인증서 경고를 진행(proceed)하세요.
왼쪽 탐색 창에서 Services를 클릭해 배포된 서비스를 검토합니다. 지금은 consul 서비스만 찾을 수 있습니다.
기본적으로 익명(anonymous) ACL 정책은 Consul 서비스, 노드, 의도의 내용을 볼 수 있게 합니다. Consul UI에서 변경을 가하고 더 많은 세부 사항을 보려면 오른쪽 상단의 Log In을 클릭하고 부트스트랩 ACL 토큰을 입력하세요.
ACL 토큰으로 인증에 성공하면 이제 UI에서 추가 Consul 구성 요소를 보고 변경할 수 있습니다. 왼쪽 탐색 창의 Access Controls 섹션 아래에서 더 많은 옵션을 보고 관리할 수 있습니다.
왼쪽 탐색 창에서 Nodes를 클릭합니다.
에이전트는 서버 또는 클라이언트 모드로 실행됩니다. 서버 에이전트는 서비스·노드 IP 주소, 헬스 체크, 구성을 포함한 모든 상태 정보를 저장합니다. 클라이언트 에이전트는 데이터센터의 대부분을 구성하는 가벼운 프로세스입니다. 클라이언트는 서버 에이전트에게 서비스 헬스 상태를 보고합니다. 클라이언트는 서비스가 실행되는 모든 파드에서 실행되어야 합니다.
별도의 터미널 창을 열고 consul-ui 서비스 이름을 대상으로 kubectl port-forward를 사용해 Consul 서버를 노출합니다.
$ kubectl port-forward svc/consul-ui --namespace consul 8501:443
원래 터미널에서 Consul에 등록된 서비스 목록을 봅니다.
$ curl -k \
--header "X-Consul-Token: $CONSUL_HTTP_TOKEN" \
$CONSUL_HTTP_ADDR/v1/catalog/services
샘플 출력:
{"consul":[]}
에이전트는 서버 또는 클라이언트 모드로 실행됩니다. 서버 에이전트는 서비스·노드 IP 주소, 헬스 체크, 구성을 포함한 모든 상태 정보를 저장합니다. 클라이언트 에이전트는 데이터센터의 대부분을 구성하는 가벼운 프로세스입니다. 클라이언트는 서버 에이전트에게 서비스 헬스 상태를 보고합니다. 클라이언트는 서비스가 실행되는 모든 파드에서 실행되어야 합니다.
환경의 서버·클라이언트 Consul 에이전트 목록을 봅니다.
$ curl -k \
--header "X-Consul-Token: $CONSUL_HTTP_TOKEN" \
$CONSUL_HTTP_ADDR/v1/agent/members\?pretty
샘플 출력:
[
{
"Name": "consul-server-0",
"Addr": "10.244.0.13",
"Port": 8301,
"Tags": {
"acls": "1",
"bootstrap": "1",
"build": "1.14.0",
"dc": "dc1",
"ft_fs": "1",
"ft_si": "1",
"grpc_port": "8502",
"id": "8016fc4d-767f-8552-b018-0812228bd135",
"port": "8300",
"raft_vsn": "3",
"role": "consul",
"segment": "",
"use_tls": "1",
"vsn": "2",
"vsn_max": "3",
"vsn_min": "2",
"wan_join_port": "8302"
},
"Status": 1,
"ProtocolMin": 1,
"ProtocolMax": 5,
"ProtocolCur": 2,
"DelegateMin": 2,
"DelegateMax": 5,
"DelegateCur": 4
}
]
Consul 카탈로그에 나열된 모든 서비스는 확장성 문제를 단순화하고 애플리케이션 탄력성을 개선하는 Consul의 서비스 디스커버리 기능을 사용할 수 있습니다. 더 알아보려면 서비스 디스커버리 개요 페이지를 검토하세요.
정리 (Clean up)
minikube delete를 실행해 로컬 데모 환경을 정리합니다.
$ minikube delete --profile dc1
🔥 Deleting "dc1" in docker ...
🔥 Deleting container "dc1" ...
🔥 Removing /Users/consul-user/.minikube/machines/dc1 ...
💀 Removed all traces of the "dc1" cluster.