MaxScale 未连接到 Galera Cluster

MaxScale 未连接到 Galera Cluster

我正在开发一个分布式 HA 云系统,并且我有一个Galera Cluster包含 3 个独立服务器的Docker Swarm。它按预期运行。

由于引导程序的原因,我为成员提供了 3 个独立的服务Galera(无法通过简单的副本解决)。因此,我必须有一个复制的负载平衡器/代理才能使用集群(一个服务有Swarm3 个副本,将在连接字符串中使用)。

设计应该类似这样:

设计

我试图设置MaxScale服务但maxctrl list servers命令(在运行的容器内)显示:

Error: Could not connect to MaxScale

使用命令启动系统:

docker-compose up --remove-orphans --force-recreate

我创建了一个最小设置来测试Galera ClusterMaxScalelocalhost节点结构仅用于测试。实际产品有 3 个管理器节点和 3 个数据库节点)。请参阅下面的系统,我可以在其中重现该问题。

我的docker-compose.yml文件:

version: "3.8"

services:
  galera0:
      image: bitnami/mariadb-galera:10.4.31
      hostname: galera0
      networks:
        - galera_net
      volumes:
        - ./my.cnf:/opt/bitnami/mariadb/conf/my_custom.cnf:ro
        - ./init_db_for_maxscale.sql:/docker-entrypoint-initdb.d/init_db_for_maxscale.sql
      environment:
        MARIADB_GALERA_CLUSTER_NAME: my_galera
        MARIADB_GALERA_MARIABACKUP_USER: my_mariabackup_user
        MARIADB_GALERA_MARIABACKUP_PASSWORD: my_mariabackup_password
        MARIADB_ROOT_PASSWORD: my_root_password
        MARIADB_GALERA_CLUSTER_BOOTSTRAP: "yes"
        MARIADB_GALERA_FORCE_SAFETOBOOTSTRAP: "yes"
        MARIADB_USER: my_user
        MARIADB_PASSWORD: my_password
        MARIADB_DATABASE: my_database
        MARIADB_REPLICATION_USER: my_replication_user
        MARIADB_REPLICATION_PASSWORD: my_replication_password
      healthcheck:
        test: ['CMD', '/opt/bitnami/scripts/mariadb-galera/healthcheck.sh']
        interval: 15s
        timeout: 5s
        retries: 10

  galera1:
      image: bitnami/mariadb-galera:10.4.31
      hostname: galera1
      networks:
        - galera_net
      volumes:
        - ./my.cnf:/opt/bitnami/mariadb/conf/my_custom.cnf:ro
        - ./init_db_for_maxscale.sql:/docker-entrypoint-initdb.d/init_db_for_maxscale.sql
      environment:
        MARIADB_GALERA_CLUSTER_NAME: my_galera
        MARIADB_GALERA_CLUSTER_ADDRESS: gcomm://galera0:4567,0:0:0:0:4567
        MARIADB_GALERA_MARIABACKUP_USER: my_mariabackup_user
        MARIADB_GALERA_MARIABACKUP_PASSWORD: my_mariabackup_password
        MARIADB_ROOT_PASSWORD: my_root_password
        MARIADB_REPLICATION_USER: my_replication_user
        MARIADB_REPLICATION_PASSWORD: my_replication_password
        MARIADB_INIT_SLEEP_TIME: 30
      healthcheck:
        test: [ 'CMD', '/opt/bitnami/scripts/mariadb-galera/healthcheck.sh' ]
        interval: 15s
        timeout: 5s
        retries: 10
      depends_on:
        galera0:
          condition: service_healthy

  galera2:
      image: bitnami/mariadb-galera:10.4.31
      hostname: galera2
      networks:
        - galera_net
      volumes:
        - ./my.cnf:/opt/bitnami/mariadb/conf/my_custom.cnf:ro
        - ./init_db_for_maxscale.sql:/docker-entrypoint-initdb.d/init_db_for_maxscale.sql
      environment:
        MARIADB_GALERA_CLUSTER_NAME: my_galera
        MARIADB_GALERA_CLUSTER_ADDRESS: gcomm://galera0:4567,0:0:0:0:4567
        MARIADB_GALERA_MARIABACKUP_USER: my_mariabackup_user
        MARIADB_GALERA_MARIABACKUP_PASSWORD: my_mariabackup_password
        MARIADB_ROOT_PASSWORD: my_root_password
        MARIADB_REPLICATION_USER: my_replication_user
        MARIADB_REPLICATION_PASSWORD: my_replication_password
        MARIADB_INIT_SLEEP_TIME: 30
      healthcheck:
        test: [ 'CMD', '/opt/bitnami/scripts/mariadb-galera/healthcheck.sh' ]
        interval: 15s
        timeout: 5s
        retries: 10
      depends_on:
        galera0:
          condition: service_healthy

  mariadb-maxscale:
    image: mariadb/maxscale:23.08
    hostname: mariadb-maxscale
    networks:
      - galera_net
    volumes:
      - ./maxscale.cnf:/etc/maxscale.cnf
    depends_on:
      galera0:
        condition: service_healthy
      galera1:
        condition: service_healthy
      galera2:
        condition: service_healthy

networks:
  galera_net: {}

maxscale.cfn内容:

########################
## Server list
########################

[mariadb1]
type            = server
address         = galera0
port            = 3306
protocol        = MariaDBBackend
serv_weight     = 1

[mariadb2]
type            = server
address         = galera1
port            = 3306
protocol        = MariaDBBackend
serv_weight     = 1

[mariadb3]
type            = server
address         = galera2
port            = 3306
protocol        = MariaDBBackend
serv_weight     = 1

#########################
## MaxScale configuration
#########################

[MaxScale]
threads                 = auto
log_augmentation        = 1
ms_timestamp            = 1
syslog                  = 1
# log_debug               = 1

#########################
# Monitor for the servers
#########################

[MariaDB-Monitor]
type                    = monitor
module                  = mariadbmon
servers                 = mariadb1,mariadb2,mariadb3
user                    = maxscale
password                = my_s3cret
# auto_failover           = true
# auto_rejoin             = true
# enforce_read_only_slaves = 1
monitor_interval        = 2000

#########################
## Service definitions for read/write splitting and read-only services.
#########################

[Read-Write-Service]
type            = service
router          = readwritesplit
servers         = mariadb1,mariadb2,mariadb3
user            = maxscale
password        = my_s3cret
# max_slave_connections           = 100%
# max_sescmd_history              = 1500
# causal_reads                    = true
# causal_reads_timeout            = 10
# transaction_replay              = true
# transaction_replay_max_size     = 1Mi
# delayed_retry                   = true
# master_reconnection             = true
# master_failure_mode             = fail_on_write
# max_slave_replication_lag       = 3

[Read-Only-Service]
type            = service
router          = readconnroute
servers         = mariadb1,mariadb2,mariadb3
router_options  = slave
user            = maxscale
password        = my_s3cret

##########################
## Listener definitions for the service
## Listeners represent the ports the service will listen on.
##########################

[Read-Write-Listener]
type            = listener
service         = Read-Write-Service
protocol        = MariaDBClient
port            = 4006

[Read-Only-Listener]
type            = listener
service         = Read-Only-Service
protocol        = MariaDBClient
port            = 4008

init_db_for_maxscale.sql内容:

CREATE USER IF NOT EXISTS 'maxscale'@'%' IDENTIFIED BY 'my_s3cret';
GRANT SELECT ON mysql.user TO 'maxscale'@'%';
GRANT SELECT ON mysql.db TO 'maxscale'@'%';
GRANT SELECT ON mysql.tables_priv TO 'maxscale'@'%';
GRANT SELECT ON mysql.columns_priv TO 'maxscale'@'%';
GRANT SELECT ON mysql.procs_priv TO 'maxscale'@'%';
GRANT SELECT ON mysql.proxies_priv TO 'maxscale'@'%';
GRANT SELECT ON mysql.roles_mapping TO 'maxscale'@'%';
GRANT SHOW DATABASES ON *.* TO 'maxscale'@'%';

GRANT ALL PRIVILEGES ON *.* TO maxscale@'%';

我检查了所有MariaDB容器,创建的用户可用,并且权限在数据库中设置(基于select * from mysql. user;)。

docker container logs <maxscale_continer>命令输出(无限重复):

mariadb-maxscale_1  | Starting...
mariadb-maxscale_1  | MaxScale PID = 1698
mariadb-maxscale_1  | 
mariadb-maxscale_1  | Awakened by User defined signal 1
mariadb-maxscale_1  | 'maxscale' monitor action done
mariadb-maxscale_1  | 'maxscale' process is not running
mariadb-maxscale_1  | 'maxscale' trying to restart
mariadb-maxscale_1  | 'maxscale' start: '/usr/bin/maxscale-restart'
mariadb-maxscale_1  | 'mariadb-maxscale' unmonitor on user request
mariadb-maxscale_1  | 'maxscale' unmonitor on user request
mariadb-maxscale_1  | Monit daemon with PID 12 awakened
mariadb-maxscale_1  | 'mariadb-maxscale' monitor on user request
mariadb-maxscale_1  | 'maxscale' monitor on user request
mariadb-maxscale_1  | Monit daemon with PID 12 awakened
mariadb-maxscale_1  | 'mariadb-maxscale' monitor action done
mariadb-maxscale_1  | Awakened by User defined signal 1
mariadb-maxscale_1  | 'maxscale' failed to start (exit status 0) -- '/usr/bin/maxscale-restart': Stopping...
mariadb-maxscale_1  | Starting...
mariadb-maxscale_1  | MaxScale PID = 1713
mariadb-maxscale_1  | 
mariadb-maxscale_1  | 'maxscale' monitor action done
mariadb-maxscale_1  | 'maxscale' trying to restart
mariadb-maxscale_1  | 'maxscale' process is not running
mariadb-maxscale_1  | 'maxscale' start: '/usr/bin/maxscale-restart'
mariadb-maxscale_1  | 'mariadb-maxscale' unmonitor on user request
mariadb-maxscale_1  | 'maxscale' unmonitor on user request
mariadb-maxscale_1  | Monit daemon with PID 12 awakened
mariadb-maxscale_1  | 'mariadb-maxscale' monitor on user request
mariadb-maxscale_1  | 'maxscale' monitor on user request
mariadb-maxscale_1  | Monit daemon with PID 12 awakened
mariadb-maxscale_1  | 'maxscale' failed to start (exit status 0) -- '/usr/bin/maxscale-restart': Stopping...
mariadb-maxscale_1  | Starting...
mariadb-maxscale_1  | MaxScale PID = 1728
mariadb-maxscale_1  | 
mariadb-maxscale_1  | 'mariadb-maxscale' monitor action done
mariadb-maxscale_1  | Awakened by User defined signal 1
mariadb-maxscale_1  | 'maxscale' monitor action done
mariadb-maxscale_1  | 'maxscale' process is not running
mariadb-maxscale_1  | 'maxscale' trying to restart
mariadb-maxscale_1  | 'maxscale' start: '/usr/bin/maxscale-restart'
mariadb-maxscale_1  | 'mariadb-maxscale' unmonitor on user request
mariadb-maxscale_1  | 'maxscale' unmonitor on user request
mariadb-maxscale_1  | Monit daemon with PID 12 awakened
mariadb-maxscale_1  | 'mariadb-maxscale' monitor on user request
mariadb-maxscale_1  | 'maxscale' monitor on user request
mariadb-maxscale_1  | Monit daemon with PID 12 awakened

笔记:

  • 我已经阅读了许多文档,并且我看到 IP 地址在 maxscale 配置中用于“服务器”,但我认为它应该通过主机名(服务名称)看到 MariaDB,因为 Docker 网络相同。
  • 我没有打开任何端口,因为所有端口都可以在 Docker 网络内部访问。

答案1

我刚刚找到了问题的解决方案。我查看了可以找到根本原因的完整日志(我不明白为什么它在 STDOUT 上不可见MaxScale... )docker exec -it <container_id> cat var/log/maxscale/maxscale.log

我必须在 MaxScale 配置中做以下更改:

  1. serv_weight = 1参数必须更改为rank = primaryMaxScale 参考。

  2. monitor_interval = 2000必须将其更改为monitor_interval = 2000ms(缺少“ms”)

上述更改后的输出:

 maxctrl list servers
┌──────────┬─────────┬──────┬─────────────┬─────────────────┬────────┬─────────────────┐
│ Server   │ Address │ Port │ Connections │ State           │ GTID   │ Monitor         │
├──────────┼─────────┼──────┼─────────────┼─────────────────┼────────┼─────────────────┤
│ mariadb1 │ galera0 │ 3306 │ 0           │ Master, Running │ 0-1-65 │ MariaDB-Monitor │
├──────────┼─────────┼──────┼─────────────┼─────────────────┼────────┼─────────────────┤
│ mariadb2 │ galera1 │ 3306 │ 0           │ Running         │ 0-1-65 │ MariaDB-Monitor │
├──────────┼─────────┼──────┼─────────────┼─────────────────┼────────┼─────────────────┤
│ mariadb3 │ galera2 │ 3306 │ 0           │ Running         │ 0-1-65 │ MariaDB-Monitor │
└──────────┴─────────┴──────┴─────────────┴─────────────────┴────────┴─────────────────┘

相关内容