Overview
隨著應(yīng)用程序的膨脹,我們往往需要擴(kuò)展程序的基礎(chǔ)架構(gòu)。這篇文章是使用 Nginx 作為 MySQL 的 TCP 負(fù)載均衡器的實(shí)踐。
使用 TCP 負(fù)載均衡器的好處:
- 對(duì)數(shù)據(jù)庫(kù)進(jìn)行負(fù)載均衡,避免數(shù)據(jù)庫(kù)過(guò)載。
- 應(yīng)用程序可以使用同樣的 IP 或者主機(jī)名訪問(wèn)集群中的所有數(shù)據(jù)庫(kù)
- 如果一個(gè)節(jié)點(diǎn)崩潰,可以繞過(guò)該節(jié)點(diǎn)并保持應(yīng)用持續(xù)運(yùn)行
- 橫向擴(kuò)展比縱向擴(kuò)展更簡(jiǎn)單
Let's Do It
我將使用 Docker Compose 在 linux 系統(tǒng)中部署多個(gè) MySQL 節(jié)點(diǎn)和一個(gè) NGINX 節(jié)點(diǎn)。Docker Compose 啟動(dòng)文件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 的鏡像是因?yàn)楦菀撞渴?MySQL 主從復(fù)制架構(gòu)。主節(jié)點(diǎn)的主機(jī)名是 master.janwee.ubuntu,從節(jié)點(diǎn)的主機(jī)名是 slave.janwee.ubuntu。將三個(gè)節(jié)點(diǎn)都放在同一個(gè)網(wǎng)絡(luò)下,在 NGINX 負(fù)載均衡器中暴露 3306 和 33060 端口。
NGINX 節(jié)點(diǎn)中的 volumes 選項(xiàng)將配置文件 nginx/nginx.conf 掛載到容器內(nèi)部:
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;
}
}
這里設(shè)置了兩個(gè)輸入流:mariadb_read 和 mariadb_write。read 同時(shí)指向主節(jié)點(diǎn)和從節(jié)點(diǎn),write 指向主節(jié)點(diǎn)。
然后將 nginx 主機(jī)名 janwee.ubuntu 加入本地 DNS 配置文件 /etc/hosts中,在 Linux 終端中使用如下命令:
echo "127.0.0.1 db.janwee.ubuntu" >> /etc/hosts
在docker-compose.yml所在目錄下運(yùn)行如下命令啟動(dòng)容器組:
docker compose up -d
測(cè)試連接到 3306 端口并使用 SELECT @@hostname;查詢當(dāng)前主機(jī)名:
$ 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 |
+----------------------+
測(cè)試連接到 33060 端口并使用 SELECT @@hostname; 查詢當(dāng)前主機(jī)名:
$ 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 |
+----------------------+
測(cè)試結(jié)果表明,當(dāng)連接 3306 端口時(shí) NGINX 會(huì)在主節(jié)點(diǎn)和從節(jié)點(diǎn)之間變更,而連接33060時(shí)只會(huì)連接到主節(jié)點(diǎn)。
原文鏈接;
https://xie.infoq.cn/article/7a6044de8f0e058e5e0fa4295