LVS-NAT负载均衡nginx:
Director:
eno16777736:172.16.0.6 (桥接)
eno33554984:192.168.10.254 (VMnet1)
RS1: eno16777736:192.168.10.1 (VMnet1)(网关指向Director的VMnet1)
RS2: eno16777736:192.168.10.2 (VMnet1)(网关指向Director的VMnet1)
(1) 是否需要会话保持:
思路:在RS1和RS2分别布署两个独立的LAP,使用相同的MariaDB;DR以rr算法调度,访问wordpress和discuzx;
进行发布文章和上传图片;
将Director作为时间服务器:
/etc/chrony.conf
allow 192.168/24
~]# systemctl start chronyd.service
~]# ss -unl (:123 :323)
~]# iptables -F
~]# setenforce 0
同步时间:
~]# ntpdate 172.16.0.6
~]# date
为了方便识别:
~]# hostnamectl set-hostname rs2.ilinux.io
~]# hostnamectl set-hostname rs1.ilinux.io
RS1,RS2提供httpd, php, php-mysql, mariadb-server(仅安装在RS2)
注意:RS1,RS2没有联网,可以让DR代为请求服务器:
1) 打开dr的转发功能:# echo 1 > /proc/sys/net/ipv4/ip_forward
2) SNAT模式:# iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j SNAT –to-source 172.16.0.6
安装程序:~]# yum -y install httpd php php-mysql
~]# rpm -q httpd php php-mysql
安装后:
1) 关闭转发功能: # echo 0 > /proc/sys/net/ipv4/ip_forward
2) 清空nat表:# iptables -t nat -F
配置maridb-server:
~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON
log_bin=mysql-bin
~]# systemctl start mariadb.service
~]# ss -tnl ( *:3306)
~]# mysql_secure_installation (安全初始化)
~]# mysql -uroot -hlocalhost -pmagedu
授权两个账号以连接wordpress,discuzx
MariaDB [(none)]> CREATE DATABASE wordpress;
MariaDB [(none)]> CREATE DATABASE discuz;
MariaDB [(none)]> GRANT ALL ON wordpress.* TO ‘wpuser’@’192.168.10.%’ IDENTIFIED BY ‘wppass’;
MariaDB [(none)]> GRANT ALL ON discuz.* TO ‘dzuser’@’192.168.10.%’ IDENTIFIED BY ‘dzpass’;
MariaDB [(none)]> FLUSH PRIVILEGES;
~]# mysql -uwpuser -h192.168.10.2 -pwppass
~]# mysql -udzuser -h192.168.10.2 -pdzpass
测试RS1, RS2的服务是否正常:
1)测试Httpd服务是否正常:
~]# systemctl start httpd.service
~]# ss -tnl (:::80)
~]# vim /var/www/html/test1.html
<h1>RS1, 192.168.10.1</h1>
# curl http://192.168.10.1/test1.html
<h1>RS1, 192.168.10.1</h1>
RS2: 同理!
# curl http://192.168.10.2/test1.html
<h1>RS2, 192.168.10.2</h1>
2)测试httpd运行php动态资源环境;
~]# vim /var/www/html/phpinfo.php
<html>
<title>Test Page</title>
<body>
<h1>RS1.magedu.com</h1>
<?php
phpinfo();
?>
</body>
</html>
RS2:同上:
测试时,在172.16.0.67上添加一块网卡(VMnet1: 192.168.10.99),并执行以下命令;
sysctl -w net.ipv4.ip_forward=1
route add -net 192.168.10.0/24 dev eno33554985
测试:http://192.168.10.1/phpinfo.php
http://192.168.10.2/phpinfo.php
3)测试php连接maridb-server;
~]# vim /var/www/html/php-mysql.php
<?php
$conn = mysql_connect(‘192.168.10.2′,’wpuser’,’wppass’);
if ($conn)
echo “OK”;
else
echo “NO”;
?>
RS2:同上:
测试:http://192.168.10.2/php-mysql.php
http://192.168.10.1/php-mysql.php
~]# vim /var/www/html/php-mysql2.php
<?php
$conn = mysql_connect(‘192.168.10.2′,’dzuser’,’dzpass’);
if ($conn)
echo “OK”;
else
echo “NO”;
?>
RS2:同上:
测试:http://192.168.10.2/php-mysql2.php
http://192.168.10.1/php-mysql2.php
在Director上开启集群服务:
~]# yum -y install ipvsadm
配置web集群
~]# ipvsadm -A -t 172.16.0.6:80 -s rr
~]# ipvsadm -a -t 172.16.0.6:80 -r 192.168.10.1 -m
~]# ipvsadm -a -t 172.16.0.6:80 -r 192.168.10.2 -m
~]# ipvsadm -Ln
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.0.6:80 rr
-> 192.168.10.1:80 Masq 1 0 0
-> 192.168.10.2:80 Masq 1 0 0
测试集群是否正常:
http://172.16.0.6/ 没有回应!
抓包分析:
~]# tcpdump -i eno16777736 -nn tcp port 80
14:54:46.693421 IP 172.16.0.67.50144 > 172.16.0.6.80
14:54:47.701470 IP 172.16.0.67.50144 > 172.16.0.6.80
只有单向:–>(PREROUTING –INPUT –> FORWARD –> POSTROUTING)–>内网
打开转发功能:
~]# sysctl -w net.ipv4.ip_forward=1
测试正常:http://172.16.0.6/test1.html
布署wordpress测试:发文章,上传图片:
基于虚拟主机布署wordpresss 和 discuzz
~]# vim /etc/httpd/conf.d/ilinux.conf
<VirtualHost *:80>
ServerName www.ilinux.io
DocumentRoot “/data/web/ilinux”
<Directory “/data/web/ilinux”>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
CustomLog logs/ilinux_access_log combined
ErrorLog logs/ilinux_error_log
</VirtualHost>
# cp /etc/httpd/conf.d/ilinux.conf /etc/httpd/conf.d/iunix.conf
# sed -i ‘s,linux,unix,g’ /etc/httpd/conf.d/iunix.conf
# mkdir -pv /data/web/{ilinux,iunix}
# httpd -t
# systemctl restart httpd.service
RS2同上:
布署wordpress和discuzz
在172.16.0.67上安装samba服务:将wordpress和discuzz导入至samba共享中,而后复制至RS主机;
~]# yum -y install samba
~]# vim /etc/samba/smb.conf
[files]
comment=share files
path=/mydata
write list=centos
browseable=yes
~]# testparm
~]# systemctl start smb.service nmb.service
[root@localhost ~]# netstat -tunlp | grep -E ‘(s|n)mb’
tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN 12658/smbd
tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN 12658/smbd
udp 0 0 192.168.10.99:137 0.0.0.0:* 12654/nmbd
udp 0 0 172.16.0.67:138 0.0.0.0:* 12654/nmbd
添加centos用户:~]# useradd centos; smbpasswd -a centos
在windows中访问:\\172.16.0.67
~]# install -d -o centos -g centos /mydata
RS中下载文件:
# smbclient -L 192.168.10.99 -U centos
# smbclient //192.168.10.99/files -U centos
[root@rs1 ilinux]# tar xf wordpress-4.9.1-zh_CN.tar.gz
[root@rs1 ilinux]# ln -sv wordpress wp
[root@rs1 ilinux]# cp wp/wp-config-sample.php wp/wp-config.php
[root@rs1 ilinux]# vim wp/wp-config.php
/** WordPress数据库的名称 */
define(‘DB_NAME’, ‘wordpress’);
/** MySQL数据库用户名 */
define(‘DB_USER’, ‘wpuser’);
/** MySQL数据库密码 */
define(‘DB_PASSWORD’, ‘wppass’);
/** MySQL主机 */
define(‘DB_HOST’, ‘192.168.10.2’);
# setfacl -R -m u:apache:rwx ilinux/wordpress
# unzip Discuz_X3.2_SC_UTF8.zip
# setfacl -R -m u:apache:rwx iunix/upload/
# scp -r /data/web/* root@192.168.10.2:/data/web/
在网页中发表文章和上传图片:
1)测试wordpress:
在windows主机的浏览器中搜索:http://www.ilinux.io/wp/
发表文章后,只要刷新图片消失了;在刷新图片出现;
2)测试discuzz
在windows主机的浏览器中搜索:http://www.iunix.io/upload/
不能登陆
修改集群,再次测试:
~]# ipvsadm -E -t 172.16.0.6:80 -s sh
测试wordpress图片全部消失;
discuzz登陆不上
现在上传图片再也不会丢失了;
获取正在使用主机,让其停机;
~]# ipvsadm -Ln -c
TCP 01:20 TIME_WAIT 172.16.0.178:58402 172.16.0.6:80 192.168.10.2:80
TCP 01:18 TIME_WAIT 172.16.0.178:58383 172.16.0.6:80 192.168.10.2:80
~]# ipvsadm -d -t 172.16.0.6:80 -r 192.168.10.2
O(∩_∩)O哈哈~,discuzz可以登陆了。wordpress图片恢复;
会话不能验证:Say Sorry..
(2) 是否需要共享存储;
在RS1上共享出/data/web目录,让RS2挂载;
光盘挂载:安装nfs-utils程序
~]# mount -r /dev/cdrom /media/cdrom/
导出目录:
# vim /etc/exports
/data/web 192.168.10.0/24(rw)
查看10.2 httpd进程用户的id: 48
自己apache用户:48
~]# setfacl -R -m u:apache:rwx /data/web
~]# exportfs -arv
~]# systemctl start nfs.service
~]# ss -tnl ( *:2049 )
挂载目录:
# showmount -e 192.168.10.1
# mount -t nfs 192.168.10.1:/data/web /data/web
# mount
重新生成ipvs规:
[root@localhost ~]# ipvsadm -C
[root@localhost ~]# ipvsadm -A -t 172.16.0.6:80 -s rr
[root@localhost ~]# ipvsadm -a -t 172.16.0.6:80 -r 192.168.10.1 -m
[root@localhost ~]# ipvsadm -a -t 172.16.0.6:80 -r 192.168.10.2 -m
[root@localhost ~]# ipvsadm -Ln
访问:
此时刷新内容是一致的;
~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.0.6:80 rr
-> 192.168.10.1:80 Masq 1 0 105
-> 192.168.10.2:80 Masq 1 0 97
基于RR算法轮询调度,并没有基于SH算法来源地址哈希:将所有来自同一个IP的发到同一个主机;
验证不了,但还是需要SH算法和共享存储;
本文来自投稿,不代表Linux运维部落立场,如若转载,请注明出处:http://www.178linux.com/90450