Overview
隨著應用程序的膨脹,我們往往需要擴展程序的基礎架構。這篇文章是使用 Nginx 作為 MySQL 的 TCP 負載均衡器的實踐。
使用 TCP 負載均衡器的好處:
- 對數據庫進行負載均衡,避免數據庫過載。
- 應用程序可以使用同樣的 IP 或者主機名訪問集群中的所有數據庫
- 如果一個節點崩潰,可以繞過該節點并保持應用持續運行
- 橫向擴展比縱向擴展更簡單
Let's Do It
我將使用 Docker Compose 在 linux 系統中部署多個 MySQL 節點和一個 NGINX 節點。Docker Compose 啟動文件docker-compose.yml如下:
version: '3.8'
networks:
nginx_mariadb:
services:
load_balancer:
image: nginx:stable-alpine
contAIner_name: nginx-load-balancer
ports:
- "3306:3306"
- "33060:33060"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
depends_on:
- mariadb-master
- mariadb-slave
.NETworks:
- nginx_mariadb
mariadb-master:
image: 'bitnami/mariadb:latest'
hostname: "master.janwee.ubuntu"
container_name: mariadb_master
volumes:
- ./mariadb:/var/lib/mysql
restart: unless-stopped
tty: true
environment:
MARIADB_REPLICATION_MODE: master
MARIADB_DATABASE: nerddb
MARIADB_USER: janwee
MARIADB_PASSword: janwee
MARIADB_ROOT_PASSWORD: janwee
MARIADB_REPLICATION_USER: rplusr
MARIADB_REPLICATION_PASSWORD: janwee
networks:
- nginx_mariadb
mariadb-slave:
image: 'bitnami/mariadb:latest'
hostname: "slave.janwee.ubuntu"
container_name: mariadb_slave
restart: unless-stopped
tty: true
environment:
MARIADB_REPLICATION_MODE: slave
MARIADB_REPLICATION_USER: rplusr
MARIADB_REPLICATION_PASSWORD: janwee
MARIADB_MASTER_HOST: "master.janwee.ubuntu"
MARIADB_MASTER_PORT_NUMBER: 3306
MARIADB_MASTER_ROOT_PASSWORD: janwee
networks:
- nginx_mariadb
depends_on:
- mariadb-master
這里使用 bitnami 的鏡像是因為更容易部署 MySQL 主從復制架構。主節點的主機名是 master.janwee.ubuntu,從節點的主機名是 slave.janwee.ubuntu。將三個節點都放在同一個網絡下,在 NGINX 負載均衡器中暴露 3306 和 33060 端口。
NGINX 節點中的 volumes 選項將配置文件 nginx/nginx.conf 掛載到容器內部:
worker_processes 1;
# Configuration of connection processing
events {
worker_connections 1024;
}
# Configuration specific to TCP/UDP and affecting all virtual servers
stream {
log_format log_stream '$remote_addr - [$time_local] $protocol $status $bytes_sent $bytes_received $session_time "$upstream_addr"';
access_log /var/log/nginx/mysql.log log_stream;
upstream mariadb_read {
server master.janwee.ubuntu:3306;
server slave.janwee.ubuntu:3306;
}
# Configuration of a TCP virtual server
server {
listen 3306;
# Specify the several addresse
proxy_pass mariadb_read;
proxy_connect_timeout 1s;
error_log /var/log/nginx/mysql_error.log;
}
upstream mariadb_write {
server master.janwee.ubuntu:3306;
}
server {
listen 33060;
proxy_pass mariadb_write;
proxy_connect_timeout 1s;
error_log /var/log/nginx/mysql_error.log;
}
}
這里設置了兩個輸入流:mariadb_read 和 mariadb_write。read 同時指向主節點和從節點,write 指向主節點。
然后將 nginx 主機名 janwee.ubuntu 加入本地 DNS 配置文件 /etc/hosts中,在 Linux 終端中使用如下命令:
echo "127.0.0.1 db.janwee.ubuntu" >> /etc/hosts
在docker-compose.yml所在目錄下運行如下命令啟動容器組:
docker compose up -d
測試連接到 3306 端口并使用 SELECT @@hostname;查詢當前主機名:
$ mysql -h janwee.ubuntu -P 3306 -u root -pjanwee -e "select @@hostname";
+---------------------+
| @@hostname |
+---------------------+
| slave.janwee.ubuntu |
+---------------------+
$ mysql -h janwee.ubuntu -P 3306 -u root -pjanwee -e "select @@hostname";
+----------------------+
| @@hostname |
+----------------------+
| master.janwee.ubuntu |
+----------------------+
測試連接到 33060 端口并使用 SELECT @@hostname; 查詢當前主機名:
$ mysql -h janwee.ubuntu -P 33060 -u root -pjanwee -e "select @@hostname";
+---------------------+
| @@hostname |
+---------------------+
| master.janwee.ubuntu |
+---------------------+
$ mysql -h janwee.ubuntu -P 33060 -u root -pjanwee -e "select @@hostname";
+----------------------+
| @@hostname |
+----------------------+
| master.janwee.ubuntu |
+----------------------+
測試結果表明,當連接 3306 端口時 NGINX 會在主節點和從節點之間變更,而連接33060時只會連接到主節點。
原文鏈接;
https://xie.infoq.cn/article/7a6044de8f0e058e5e0fa4295