赞
踩
Percona XtraDB Cluster (PXC) 是一个完全开源的 MySQL 数据库集群解决方案,它可确保高可用性,防止停机和数据丢失,并为不断增长的环境提供线性可扩展性。它将 Percona Server 和 Percona XtraBackup 与 Galera 库集成在一起,以实现同步多源复制。
PXC是一个强大的MySQL集群解决方案,可以提供高可用性、容错性和可扩展性。
Traefik 是一种流行的开源反向代理和负载均衡器,广泛用于容器化环境。它提供了多种优势和功能,使其成为管理传入流量并将其路由到微服务或应用程序的热门选择。以下是常用Traefik的一些原因:
#拉取pxc镜像
docker pull percona/percona-xtradb-cluster:5.7.34-31.51
#此次指定版本,8.0后需要配置证书
新建docker-compose.yaml,这里我们选择traefik进行pxc集群的负载均衡,3306为统一的调用端口
version : '3.7' networks: traefik-pxc: driver: bridge ipam: driver: default config: - subnet: 172.16.238.0/24 services: traefik: image: traefik:v2.4 command: - "--providers.docker=true" - "--entrypoints.pxc.address=:3301" - "--api=true" # 启用Traefik的API - "--api.insecure=true"# 启用Traefik的Web UI - "--providers.docker" ports: - "8080:8080" # Traefik dashboard - "3301:3301" volumes: - "/var/run/docker.sock:/var/run/docker.sock" networks: - traefik-pxc db1: container_name: db1 image: percona/percona-xtradb-cluster:5.7.34-31.51 privileged: true networks: traefik-pxc: ipv4_address: 172.16.238.9 ports: - "30001:3306" environment: - "CLUSTER_NAME=JWSPXC" - "XTRABACKUP_PASSWORD=db123456" - "MYSQL_ROOT_PASSWORD=db123456" - "TZ=Asia/Shanghai" volumes: - ./data/v1/data:/var/lib/mysql - ./data/v1/backup:/data labels: - "traefik.enable=true" - "traefik.tcp.routers.pxc-cluster.rule=HostSNI(`*`)" - "traefik.tcp.routers.pxc-cluster.entrypoints=pxc" - "traefik.tcp.services.pxc-cluster.loadbalancer.server.port=3306" db2: container_name: db2 image: percona/percona-xtradb-cluster:5.7.34-31.51 privileged: true networks: traefik-pxc: ipv4_address: 172.16.238.3 environment: - "CLUSTER_NAME=JWSPXC" - "XTRABACKUP_PASSWORD=db123456" - "TZ=Asia/Shanghai" - "CLUSTER_JOIN=db1" ports: - "30002:3306" volumes: - ./data/v2/data:/var/lib/mysql - ./data/v2/backup:/data depends_on: - db1 labels: - "traefik.enable=true" - "traefik.tcp.routers.pxc-cluster.rule=HostSNI(`*`)" - "traefik.tcp.routers.pxc-cluster.entrypoints=pxc" - "traefik.tcp.services.pxc-cluster.loadbalancer.server.port=3306" db3: container_name: db3 image: percona/percona-xtradb-cluster:5.7.34-31.51 privileged: true networks: traefik-pxc: ipv4_address: 172.16.238.4 environment: - "CLUSTER_NAME=JWSPXC" - "XTRABACKUP_PASSWORD=db123456" - "TZ=Asia/Shanghai" - "CLUSTER_JOIN=db1" ports: - "30003:3306" volumes: - ./data/v3/data:/var/lib/mysql - ./data/v3/backup:/data depends_on: - db1 labels: - "traefik.enable=true" - "traefik.tcp.routers.pxc-cluster.rule=HostSNI(`*`)" - "traefik.tcp.routers.pxc-cluster.entrypoints=pxc" - "traefik.tcp.services.pxc-cluster.loadbalancer.server.port=3306" db4: container_name: db4 image: percona/percona-xtradb-cluster:5.7.34-31.51 privileged: true networks: traefik-pxc: ipv4_address: 172.16.238.5 environment: - "CLUSTER_NAME=JWSPXC" - "XTRABACKUP_PASSWORD=db123456" - "TZ=Asia/Shanghai" - "CLUSTER_JOIN=db1" ports: - "30004:3306" volumes: - ./data/v4/data:/var/lib/mysql - ./data/v4/backup:/data depends_on: - db1 labels: - "traefik.enable=true" - "traefik.tcp.routers.pxc-cluster.rule=HostSNI(`*`)" - "traefik.tcp.routers.pxc-cluster.entrypoints=pxc" - "traefik.tcp.services.pxc-cluster.loadbalancer.server.port=3306" db5: container_name: db5 image: percona/percona-xtradb-cluster:5.7.34-31.51 privileged: true networks: traefik-pxc: ipv4_address: 172.16.238.6 environment: - "CLUSTER_NAME=JWSPXC" - "XTRABACKUP_PASSWORD=db123456" - "TZ=Asia/Shanghai" - "CLUSTER_JOIN=db1" ports: - "30005:3306" volumes: - ./data/v5/data:/var/lib/mysql - ./data/v5/backup:/data depends_on: - db1 labels: - "traefik.enable=true" - "traefik.tcp.routers.pxc-cluster.rule=HostSNI(`*`)" - "traefik.tcp.routers.pxc-cluster.entrypoints=pxc" - "traefik.tcp.services.pxc-cluster.loadbalancer.server.port=3306"
先启动traefik
docker-copmose up -d traefik
再启动db1
docker-compose up -d db1
注意:第一个节点启动比较慢,需要等待1分钟时间,可以使用数据据连接工具连接成功后再启动接下来的节点,这里你可以直接连映射的30001端口。
然后我们再去traefik的UI界面8080端口,注意MySQL 连接通常使用 TCP/IP 进行通信,这里我们暴露的统一负载均衡端口为3301,有不了解traefik的同学,可以官方文档里面看一下,个人感觉还是很好上手的,要是只想了解docker的使用,可以看https://doc.traefik.io/traefik/routing/providers/docker/。
发现traefik已经负载成功,接着我们依次启动下面的节点
docker-compose up -d db2
docker-compose up -d db3
docker-compose up -d db4
docker-compose up -d db5
五个节点负载均衡成功
可以看到啊,我们在30001端口的节点创建gopan库,其他节点也同步了,接下来我们写个test再测试一下:
package test import ( "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" "testing" ) func TestName(t *testing.T) { // 数据库连接参数 dbHost := "localhost" dbPort := 3301 dbUser := "root" dbPassword := "db123456" dbName := "gopan" // 构建连接字符串 dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", dbUser, dbPassword, dbHost, dbPort, dbName) // 连接到数据库 db, err := sql.Open("mysql", dataSourceName) if err != nil { fmt.Println("Failed to connect to database:", err) return } defer db.Close() // 测试连接 err = db.Ping() if err != nil { fmt.Println("Failed to ping database:", err) return } fmt.Println("Connected to database successfully!") // 进行数据库操作... }
大功告成!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。