MariaDB, StatefulSet

Jeonghak Cho·2024년 11월 20일

MariaDB

목록 보기
7/11

데이터 베이스 서버 인스턴스를 생성한다.

mdb-sts.yml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mariadb
spec:
  serviceName: "mariadb"
  replicas: 3
  selector:
    matchLabels:
      app: mariadb
  template:
    metadata:
      labels:
        app: mariadb
    spec:
      initContainers:
      - name: init-mariadb
        image: mariadb
        imagePullPolicy: Always
        command:
        - bash
        - "-c"
        - |
          set -ex
          echo 'Starting init-mariadb';
          ls /mnt/config-map
          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          if [[ $ordinal -eq 0 ]]; then
            cp /mnt/config-map/primary.cnf /etc/mysql/conf.d/server-id.cnf
          else
            cp /mnt/config-map/replica.cnf /etc/mysql/conf.d/server-id.cnf
          fi
          echo server-id=$((3000 + $ordinal)) >> etc/mysql/conf.d/server-id.cnf
          ls /etc/mysql/conf.d/
          cat /etc/mysql/conf.d/server-id.cnf
        volumeMounts:
          - name: mariadb-config-map
            mountPath: /mnt/config-map
          - name: mariadb
            mountPath: /etc/mysql/conf.d/
      restartPolicy: Always
      containers:
      - name: mariadb
        image: mariadb
        ports:
        - containerPort: 3306
          name: mariadb
        env:
        - name: MARIADB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mariadb
              key: mariadb-root-password
        - name: MYSQL_INITDB_SKIP_TZINFO
          value: "1"
        volumeMounts:
        - name: datadir
          mountPath: /var/lib/mysql/
        - name: mariadb
          mountPath: /etc/mysql/conf.d/
      volumes:
      - name: mariadb-config-map
        configMap:
          name: mariadb
          defaultMode: 0544
      - name: mariadb
        emptyDir: {}
  volumeClaimTemplates:
  - metadata:
      name: datadir
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "local-storage"
      resources:
        requests:
          storage: 500M   
vagrant@master:~$ k get pv
NAME    CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                       STORAGECLASS    REASON   AGE
mypv1   500Mi      RWO            Delete           Bound    default/datadir-mariadb-0   local-storage            4m48s
mypv2   500Mi      RWO            Delete           Bound    default/datadir-mariadb-1   local-storage            4m48s
mypv3   500Mi      RWO            Delete           Bound    default/datadir-mariadb-2   local-storage            4m48s
vagrant@master:~$ k apply -f mdb-sts.yml
statefulset.apps/mariadb created
vagrant@master:~$ k get po -w
NAME        READY   STATUS            RESTARTS   AGE
mariadb-0   0/1     PodInitializing   0          4s
mariadb-0   1/1     Running           0          6s
mariadb-1   0/1     Pending           0          0s
mariadb-1   0/1     Pending           0          0s
mariadb-1   0/1     Init:0/1          0          0s
mariadb-1   0/1     Init:0/1          0          0s
mariadb-1   0/1     PodInitializing   0          3s
mariadb-1   1/1     Running           0          5s
mariadb-2   0/1     Pending           0          0s
mariadb-2   0/1     Pending           0          0s
mariadb-2   0/1     Init:0/1          0          0s
mariadb-2   0/1     Init:0/1          0          1s
mariadb-2   0/1     Init:0/1          0          3s
mariadb-2   0/1     PodInitializing   0          4s
mariadb-2   1/1     Running           0          6s

데이터 베이스 서버에 접속하여 데이터 복제에 사용하는 사용자 'repl'을 생성하고 권한을 준다.

vagrant@master:~$ k exec -it mariadb-0 -- mariadb -uroot -psecret
Defaulted container "mariadb" out of: mariadb, init-mariadb (init)
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 11.5.2-MariaDB-ubu2404-log mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]>     CREATE USER 'repl'@'%' IDENTIFIED BY 'secret';
L PRIVILEGES  ON *.* TO 'repl'@'%';Query OK, 0 rows affected (0.004 sec)

MariaDB [(none)]>     --GRANT REPLICATION REPLICA ON *.* TO 'repl'@'%';
MariaDB [(none)]>     GRANT ALL PRIVILEGES  ON *.* TO 'repl'@'%';
Query OK, 0 rows affected (0.005 sec)

MariaDB [(none)]> select user,host from mysql.user;
+-------------+-----------+
| User        | Host      |
+-------------+-----------+
| repl        | %         |
| root        | %         |
| healthcheck | 127.0.0.1 |
| healthcheck | ::1       |
| healthcheck | localhost |
| mariadb.sys | localhost |
| root        | localhost |
+-------------+-----------+
7 rows in set (0.001 sec)

마스터 서버 상태 정보를 조회한다.

MariaDB [(none)]> show master status;
+--------------------+----------+--------------+------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000002 |      643 |              |                  |
+--------------------+----------+--------------+------------------+
1 row in set (0.000 sec)

SLAVE 서버에 접속하여 mariadb-0를 마스터로 설정한다. SLAVE 상태 정보를 조회한다. mariadb-2 번 서버도 동일 과정을 반복한다.

vagrant@master:~$ k exec -it mariadb-1 -- mariadb -uroot -psecret
MariaDB [(none)]>     CHANGE MASTER TO
    ->     MASTER_HOST='mariadb-0.mdb.default.svc.cluster.local',
    ->     MASTER_USER='repl',
    ->     MASTER_PASSWORD='secret',
    ->     MASTER_USE_GTID=slave_pos,
    ->     MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected (0.020 sec)

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
                Slave_IO_State:
                   Master_Host: mariadb-0.mdb.default.svc.cluster.local
                   Master_User: repl
                   Master_Port: 3306
                 Connect_Retry: 10
               Master_Log_File:
           Read_Master_Log_Pos: 4
                Relay_Log_File: mariadb-relay-bin.000001
                 Relay_Log_Pos: 4
         Relay_Master_Log_File:
              Slave_IO_Running: No
             Slave_SQL_Running: No
               Replicate_Do_DB:
           Replicate_Ignore_DB:
            Replicate_Do_Table:
        Replicate_Ignore_Table:
       Replicate_Wild_Do_Table:
   Replicate_Wild_Ignore_Table:
                    Last_Errno: 0
                    Last_Error:
                  Skip_Counter: 0
           Exec_Master_Log_Pos: 0
               Relay_Log_Space: 256
               Until_Condition: None
                Until_Log_File:
                 Until_Log_Pos: 0
            Master_SSL_Allowed: Yes
            Master_SSL_CA_File:
            Master_SSL_CA_Path:
               Master_SSL_Cert:
             Master_SSL_Cipher:
                Master_SSL_Key:
         Seconds_Behind_Master: NULL
 Master_SSL_Verify_Server_Cert: Yes
                 Last_IO_Errno: 0
                 Last_IO_Error:
                Last_SQL_Errno: 0
                Last_SQL_Error:
   Replicate_Ignore_Server_Ids:
              Master_Server_Id: 0
                Master_SSL_Crl:
            Master_SSL_Crlpath:
                    Using_Gtid: Slave_Pos
                   Gtid_IO_Pos:
       Replicate_Do_Domain_Ids:
   Replicate_Ignore_Domain_Ids:
                 Parallel_Mode: optimistic
                     SQL_Delay: 0
           SQL_Remaining_Delay: NULL
       Slave_SQL_Running_State:
              Slave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0
    Slave_Transactional_Groups: 0
          Replicate_Rewrite_DB:
1 row in set (0.000 sec)

0개의 댓글