实验准备7台虚拟机,一台数据库,一台nfx服务器,两台web服务器,两台调度器,一台客户端;
ip分配如下:
MySQL:192.168.4.5
nfs:192.168.4.6
web1:192.168.4.11
web2:192.168.4.12
proxy1:192.168.4.21/192.168.2.21
proxy2:192.168.4.22/192.168.2.22
client: 192.168.2.7
vip: 192.168.2.66
dip:192.168.4.66(给web提供固定网关)
1、数据库环境、安装mariadb数据库
[root@mysql ~]# yum -y install mariadb mariadb-server.x86_64
[root@mysql ~]# systemctl restart mariadb.service
[root@mysql ~]# systemctl enable mariadb.service
2、nfs环境搭建,安装nfs,设置权限
[root@nfs ~]# yum -y install nfs-utils.x86_64
[root@nfs /]# tar -xf wordpress-4.9-zh_CN.tar.gz -C /
[root@nfs /]# ls wordpress
index.php wp-admin wp-content wp-load.php wp-signup.php
license.txt wp-blog-header.php wp-cron.php wp-login.php wp-trackback.php
readme.html wp-comments-post.php wp-includes wp-mail.php xmlrpc.php
wp-activate.php wp-config-sample.php wp-links-opml.php wp-settings.php
[root@nfs /]# chmod -R 777 wordpress
[root@nfs ~]# echo "/wordpress 192.168.4.0/24(rw,no_root_squash)" >> /etc/exports
[root@nfs ~]# cat /etc/exports
/wordpress 192.168.4.0/24(rw,no_root_squash)
[root@nfs ~]# systemctl restart nfs.service
[root@nfs ~]# systemctl enable nfs.service
3、web1、web2上分别安装nginx及相关依赖包(以下以web1为例,在web2上进行同样的操作)
[root@web1 ~]# yum -y install gcc pcre-devel.x86_64 openssl-devel.x86_64
[root@web1 ~]# tar -xf nginx-1.12.2.tar.gz
[root@web1 ~]# cd nginx-1.12.2/
[root@web1 nginx-1.12.2]# ./configure
[root@web1 nginx-1.12.2]# make && make install
[root@web1 nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
...
location / {
root html;
index index.php index.html index.htm;
}
...
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
...
[root@web1 nginx-1.12.2]# /usr/local/nginx/sbin/nginx
[root@web1 nginx-1.12.2]# yum -y install nfs-utils.x86_64
[root@web1 nginx-1.12.2]# mount 192.168.4.6:/wordpress /usr/local/nginx/html/
[root@web1 ~]# yum -y install php php-mysql.x86_64 php-fpm-5.4.16-46.el7.x86_64.rpm mariadb-devel.x86_64
[root@web1 ~]# systemctl restart php-fpm.service
[root@web1 ~]# systemctl enable php-fpm.service
[root@web1 ~]# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
[root@web1 ~]# chmod +x /etc/rc.local
web环境搭好了测试一下,如图所示则没问题(访问4.11或者4.12都要有页面)
4、proxy1、proxy2上分别安装keepalived(以下以proxy1为例,在proxy2上进行同样类似操作)
[root@proxy1 ~]# yum -y install keepalived.x86_64
[root@proxy1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
vrrp_iptables
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_1
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass dip
}
virtual_ipaddress {
192.168.4.66
}
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass wordpress
}
virtual_ipaddress {
192.168.2.66
}
}
virtual_server 192.168.2.66 80 {
delay_loop 6
lb_algo rr
lb_kind NAT
# persistence_timeout 50
protocol TCP
real_server 192.168.4.11 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.4.12 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@proxy1 ~]# systemctl restart keepalived.service
[root@proxy1 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward #临时打开路由模式
[root@proxy1 ~]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf #写入配置文件永久启用路由模式
[root@proxy1 ~]# ip a s #此刻proxy1为主,eth0、eth1分别有虚拟ip,一个做固定dip,一个做vip
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:33:f0:7c brd ff:ff:ff:ff:ff:ff
inet 192.168.4.21/24 brd 192.168.4.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 192.168.4.66/32 scope global eth0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:33:f0:86 brd ff:ff:ff:ff:ff:ff
inet 192.168.2.21/24 brd 192.168.2.255 scope global noprefixroute eth1
valid_lft forever preferred_lft forever
inet 192.168.2.66/32 scope global eth1
valid_lft forever preferred_lft forever
[root@proxy2 ~]# ip a s #proxy1为主时proxy2只有物理ip
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:46:18:94 brd ff:ff:ff:ff:ff:ff
inet 192.168.4.22/24 brd 192.168.4.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:46:18:9e brd ff:ff:ff:ff:ff:ff
inet 192.168.2.22/24 brd 192.168.2.255 scope global noprefixroute eth1
valid_lft forever preferred_lft forever
[root@proxy1 ~]# systemctl stop keepalived.service
[root@proxy1 ~]# ip a s #关闭主keepalived后,备升为主, 相应的dip、vip也浮动到proxy2上
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:33:f0:7c brd ff:ff:ff:ff:ff:ff
inet 192.168.4.21/24 brd 192.168.4.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:33:f0:86 brd ff:ff:ff:ff:ff:ff
inet 192.168.2.21/24 brd 192.168.2.255 scope global noprefixroute eth1
valid_lft forever preferred_lft forever
[root@proxy2 ~]# ip a s
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:46:18:94 brd ff:ff:ff:ff:ff:ff
inet 192.168.4.22/24 brd 192.168.4.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 192.168.4.66/32 scope global eth0
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:46:18:9e brd ff:ff:ff:ff:ff:ff
inet 192.168.2.22/24 brd 192.168.2.255 scope global noprefixroute eth1
valid_lft forever preferred_lft forever
inet 192.168.2.66/32 scope global eth1
检查web1、web2上分别设置了路由, 且为dip,
[root@web1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.4.66 0.0.0.0 UG 100 0 0 eth0
192.168.4.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
[root@web2 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.4.66 0.0.0.0 UG 100 0 0 eth0
192.168.4.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
访问192.168.2.66,无论关闭proxy1,还是proxy2,只要不同时关闭都能正常访问。说明,keepalived的vrrp测试有效
接下来就是在数据库里建库,给keepalived使用的用户
[root@mysql ~]# mysql
MariaDB [(none)]> create database wordpress character set utf8mb4;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on wordpress.* to admin@'%' identified by 'wordpress';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
接下来登入进去就可以创建网站了。