How To Set Up A Load-Balanced MySQL Cluster - Part 7

本文指导如何创建名为ldirector的数据库以供负载均衡器检查MySQL集群节点可用性,并配置集群节点接受虚拟IP地址请求。
6.4 Create A Database Called ldirector

Next we create the ldirector database on our MySQL cluster nodes sql1.example.com and sql2.example.com. This database will be used by our load balancers to check the availability of the MySQL cluster nodes.

sql1.example.com:

mysql -u root -p
GRANT ALL ON ldirectordb.* TO 'ldirector'@'%' IDENTIFIED BY 'ldirectorpassword';
FLUSH PRIVILEGES;
CREATE DATABASE ldirectordb;
USE ldirectordb;
CREATE TABLE connectioncheck (i INT) ENGINE=NDBCLUSTER;
INSERT INTO connectioncheck () VALUES (1);
quit;

sql2.example.com:

mysql -u root -p
GRANT ALL ON ldirectordb.* TO 'ldirector'@'%' IDENTIFIED BY 'ldirectorpassword';
FLUSH PRIVILEGES;
CREATE DATABASE ldirectordb;
quit;

6.5 Prepare The MySQL Cluster Nodes For Load Balancing

Finally we must configure our MySQL cluster nodes sql1.example.com and sql2.example.com to accept requests on the virtual IP address 192.168.0.105.

sql1.example.com / sql2.example.com:

apt-get install iproute

Add the following to /etc/sysctl.conf:

sql1.example.com / sql2.example.com:

vi /etc/sysctl.conf

# Enable configuration of arp_ignore option
net.ipv4.conf.all.arp_ignore = 1

# When an arp request is received on eth0, only respond if that address is
# configured on eth0. In particular, do not respond if the address is
# configured on lo
net.ipv4.conf.eth0.arp_ignore = 1

# Ditto for eth1, add for all ARPing interfaces
#net.ipv4.conf.eth1.arp_ignore = 1


# Enable configuration of arp_announce option
net.ipv4.conf.all.arp_announce = 2

# When making an ARP request sent through eth0 Always use an address that
# is configured on eth0 as the source address of the ARP request. If this
# is not set, and packets are being sent out eth0 for an address that is on
# lo, and an arp request is required, then the address on lo will be used.
# As the source IP address of arp requests is entered into the ARP cache on
# the destination, it has the effect of announcing this address. This is
# not desirable in this case as adresses on lo on the real-servers should
# be announced only by the linux-director.
net.ipv4.conf.eth0.arp_announce = 2

# Ditto for eth1, add for all ARPing interfaces
#net.ipv4.conf.eth1.arp_announce = 2

sysctl -p

Add this section for the virtual IP address to /etc/network/interfaces:

sql1.example.com / sql2.example.com:

vi /etc/network/interfaces

auto lo:0
iface lo:0 inet static
address 192.168.0.105
netmask 255.255.255.255
pre-up sysctl -p > /dev/null

ifup lo:0

<think> 嗯,用户要求解释关于负载均衡权重分配机制的专业表述。从用户提供的引用来看,ta应该是在研究负载均衡技术,特别是权重配置对流量分配的影响。 用户引用的三篇材料很有价值:[^1]介绍了负载均衡分类,[^2]说明了分布式负载均衡的意义,[^3]则展示了Nginx的实际配置示例。其中第三篇的upstream配置直接包含了weight参数,这正是问题的关键。 需要向用户解释清楚三个核心点: 1. 权重(weight)本质是算法计算的依据参数 2. 权重比值决定流量分配比例 3. 高权重接口会获得更多流量 用户可能正在配置负载均衡器,所以理解权重机制对ta很重要。ta特意强调“成员接口”这个术语,说明关注的是网络接口级别的负载均衡,而非服务器级别。 在解释时要特别注意: - 用$weight$这样的数学符号明确权重变量 - 说明这是概率性分配而非绝对分配 - 区分权重与其他参数(max_fails等)的关系 用户似乎有技术背景,但可能刚接触负载均衡概念,所以解释要兼顾准确性和易懂性。最后准备的问题要能帮助ta深化理解,比如问到具体配置场景和算法选择。 </think> 这句话解释了负载均衡中权重(`weight`)参数的核心作用:**权重值越高的服务器接口(成员接口),将被分配更大比例的客户端请求流量**。具体解析如下: --- ### 核心含义解析 1. **负载均衡成员接口** 指被负载均衡器管理的后端服务器群组中的单个服务器接口(如示例中的`127.0.0.1:8081`)。这些接口共同组成一个服务集群(如`upstream backend`)。 2. **权重的分配逻辑** - 权重(`weight`)是一个**相对值**,代表服务器的处理能力占比。 - 若服务器A权重=2,服务器B权重=1,则A将获得约$\frac{2}{2+1} \approx 66.7\%$的流量,B获得$\frac{1}{3} \approx 33.3\%$的流量。 - 权重越高 $\rightarrow$ 流量分配比例越大 $\rightarrow$ 承担更多用户请求。 3. **流量分配的本质** 负载均衡器(如Nginx)通过算法(如加权轮询)**动态计算权重比值**,确保: $$ \text{流量分配量} \propto \text{权重值} $$ 例如配置: ```nginx upstream backend { server srv1.example.com weight=3; # 预期流量占比 3/(3+1)=75% server srv2.example.com weight=1; # 预期流量占比 25% } ``` --- ### 技术背景补充 - **为何需要权重?** 服务器硬件性能可能不同(如CPU/内存差异)。高权重配置给高性能服务器更多流量,避免低性能服务器过载[^1][^3]。 - **与其他参数协作** - `max_fails`/`fail_timeout`:标记故障服务器,自动剔除低权重故障节点。 - `ip_hash`/`least_conn`:权重可与其他算法结合,实现更精细控制[^3]。 - **典型应用场景** - 灰度发布:新版本服务器初始设低权重,逐步调高。 - 资源优化:高性能服务器承担核心业务流量[^2]。 --- ### 简单总结 > 「权重是负载均衡器的流量分配杠杆:权重值加倍,理论流量分配比例也加倍。」
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值