이재홍의 언제나 최신 Kubernetes - Unit 8.2 Helm으로 MySQL 설치하기

저작권 안내
  • 책 또는 웹사이트의 내용을 복제하여 다른 곳에 게시하는 것을 금지합니다.
  • 책 또는 웹사이트의 내용을 발췌, 요약하여 강의 자료, 발표 자료, 블로그 포스팅 등으로 만드는 것을 금지합니다.

Helm으로 패키지 설치하기

이재홍 http://www.pyrasis.com

Helm으로 MySQL 설치하기

Helm을 설치했으니 이제 Helm으로 쿠버네티스 클러스터에 MySQL을 설치해보겠습니다. 참고로 Helm은 Minikube 클러스터나 kubeadm, kOps로 만든 클러스터 모두 사용 가능합니다.

참고로 Helm에서는 패키지를 차트(Chart)라고 부르는데, Helm 공식 저장소(리포지토리)는 차트들이 유지보수가 되지 않은지 오래 되었습니다. 따라서 여기서는 https://artifacthub.io/에 올라와있는 저장소를 사용하도록 하겠습니다.

다음 명령을 입력하여 bitnami 저장소를 추가합니다.

$ helm repo add bitnami https://charts.bitnami.com/bitnami

이제 helm 명령으로 MySQL을 설치해야 하는데 helm 명령의 기본 형식은 다음과 같습니다.

  • helm install <릴리스 이름> <옵션> <저장소 이름>/<차트 이름>

우리가 설치할 MySQL 차트는 https://artifacthub.io/packages/helm/bitnami/mysql입니다. 이 차트를 helm install hello-mysql bitnami/mysql과 같이 기본 설정으로 설치해도 되지만, 여기서는 각종 설정도 추가해서 설치해보겠습니다. 각 설정값(Parameter)은 https://artifacthub.io/packages/helm/bitnami/mysql#parameters에 잘 나와있습니다.

$ helm install hello-mysql --set auth.rootPassword=hellopassword bitnami/mysql
NAME: hello-mysql
LAST DEPLOYED: Fri Dec  2 12:16:56 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: mysql
CHART VERSION: 9.4.4
APP VERSION: 8.0.31

** Please be patient while the chart is being deployed **

Tip:

  Watch the deployment status using the command: kubectl get pods -w --namespace default

Services:

  echo Primary: hello-mysql.default.svc.cluster.local:3306

Execute the following to get the administrator credentials:

  echo Username: root
  MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default hello-mysql -o jsonpath="{.data.mysql-root-password}" | base64 -d)

To connect to your database:

  1. Run a pod that you can use as a client:

      kubectl run hello-mysql-client --rm --tty -i --restart='Never' --image  docker.io/bitnami/mysql:8.0.31-debian-11-r10 --namespace default --env MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD --command -- bash

  2. To connect to primary service (read/write):

      mysql -h hello-mysql.default.svc.cluster.local -uroot -p"$MYSQL_ROOT_PASSWORD"
  • helm install hello-mysql --set auth.rootPassword=hellopassword bitnami/mysql
    • helm install: Helm 설치 명령입니다.
    • hello-mysql: Helm 차트를 클러스터에 설치했을 때 릴리스 이름입니다. 이후 이 이름으로 쿠버네티스의 오브젝트들이 생성됩니다.
    • --set auth.rootPassword=hellopassword: 각종 설정값을 설정하는 부분입니다. --set 뒤에 <키>=<값> 형식으로 설정해주면 됩니다. 여러 개를 설정할 때는 <키>=<값>,<키>=<값>와 같이 키와 값을 콤마(,)로 구분해줍니다. 여기서는 auth.rootPassword=hellopassword와 같이 root 패스워드를 설정하였습니다.
    • bitnami/mysql: 우리는 bitnami 저장소를 추가했고, mysql 차트를 설치할 것이므로 bitnami/mysql로 입력해줍니다.

MySQL 차트 설치가 끝났으니, 설치된 릴리스 목록을 출력해보겠습니다.

$ helm ls
NAME            NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION
hello-mysql     default         1               2022-12-02 12:16:56.8201411 +0900 KST   deployed        mysql-9.4.4     8.0.31

릴리스 이름으로 hello-mysql이 설치된 것을 볼 수 있습니다.

이제 설치된 MySQL 데이터베이스에 접속해보겠습니다. 위에 나와있는 1. Run a pod that you can use as a client:에서 $MYSQL_ROOT_PASSWORD 부분을 hellopassword로 바꾸고, -p"$MYSQL_ROOT_PASSWORD" 부분을 -phellopassword으로 바꿔서 실행해봅니다. 그리고 show databases; 명령을 실행하여 데이터베이스 목록을 출력해봅니다.

$ kubectl run hello-mysql-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mysql:8.0.31-debian-11-r10 --namespace default --env MYSQL_ROOT_PASSWORD=hellopassword --command -- bash
If you don't see a command prompt, try pressing enter.
I have no name!@hello-mysql-client:/$ mysql -h hello-mysql.default.svc.cluster.local -uroot -phellopassword
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6504
Server version: 8.0.31 Source distribution

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| my_database        |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql>

데이터베이스 목록이 잘 출력되는 것을 볼 수 있습니다. Ctrl + D를 두 번 입력하여 MySQL 클라이언트와 파드에서 빠져나옵니다.

컨텍스트 전환

Helm은 현재 컨텍스트에 연결된 클러스터에 차트를 설치합니다. 먼저 .kube/config에 저장된 컨텍스트 목록을 확인하는 명령은 kubectl config get-contexts입니다.

$ kubectl config get-contexts
CURRENT   NAME                               CLUSTER                            AUTHINFO                           NAMESPACE
*         k8s.ap-northeast-2.aws.hello.com   k8s.ap-northeast-2.aws.hello.com   k8s.ap-northeast-2.aws.hello.com
          kubernetes-admin@kubernetes        kubernetes                         kubernetes-admin
          minikube                           minikube                           minikube                           default

만약 minikube 컨텍스트로 전환하고 싶다면 kubectl config use-context 명령을 사용합니다.

$ kubectl config use-context minikube
Switched to context "minikube".

이렇게 하면 Minikube 클러스터에 Helm으로 차트를 설치하게 됩니다.

릴리스 삭제

설치된 릴리스를 삭제할 때는 helm uninstall 명령을 사용합니다.

$ helm uninstall hello-mysql
release "hello-mysql" uninstalled

저작권 안내

이 웹사이트에 게시된 모든 글의 무단 복제 및 도용을 금지합니다.
  • 블로그, 게시판 등에 퍼가는 것을 금지합니다.
  • 비공개 포스트에 퍼가는 것을 금지합니다.
  • 글 내용, 그림을 발췌 및 요약하는 것을 금지합니다.
  • 링크 및 SNS 공유는 허용합니다.

Published

2022-10-22