【Linux集群系列教程】LB集群(keepalived结合LVS)+LAMP环境+WordPress博客

上一期教程LVS集群
关于LAMP+WordPress博客教程:https://www.wsjj.top/archives/88

LB01

虚拟机工作安排:
192.168.140.10主调度器
192.168.140.11从调度器
192.168.140.12负责web服务,安装httpdPHP
192.168.140.13负责web服务,安装httpdPHP
192.168.140.14负责NFS存储共享,存储网页数据,安装MySQL

一、配合keepalived管理LVS

  • 实现调度器的高可用
  • 自动生成LVS负载均衡规则
  • 对后端real server进行健康状态检测

二、关闭防火墙SElinux和设置时间同步

过程省略,一定要配置时间同步!否则会出现脑裂问题!

三、配置虚拟机网卡和静态IP

LB02

五台虚拟机都要修改网卡模式为NAT,默认NAT模式下使用VMnet8网卡

LB03

配置IP

过程省略
配置网卡教程:https://www.wsjj.top/archives/50

四、配置后端NFS服务器+MySQL服务器

按照规划,应该在192.168.140.14这台服务器上配置
关于NFS服务搭建教程:https://www.wsjj.top/archives/62

1.添加一块挂载硬盘

LB04

添加完后,自行重启服务器

2.安装nfs-utils服务

[root@node3-nfs ~]# yum install -y nfs-utils rpcbind

3.格式化新增的硬盘

[root@node3-nfs ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0 19.5G  0 part 
  ├─centos-root 253:0    0 17.5G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0   20G  0 disk 	#可以看到我们新增的硬盘已经检测到了
sr0              11:0    1 1024M  0 rom
[root@node3-nfs ~]# fdisk /dev/sdb
命令(输入 m 获取帮助):g		#转换成GPT格式的磁盘
Building a new GPT disklabel (GUID: A91567D7-42D7-404E-B38B-72069B1E4D6F)
命令(输入 m 获取帮助):n		#创建一个新分区
分区号 (1-128,默认 1):1		#默认1
第一个扇区 (2048-41943006,默认 2048):	#默认回车即可
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943006,默认 41943006):#默认回车
已创建分区 1
命令(输入 m 获取帮助):w		#保存
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。

A.格式化分区

[root@node3-nfs ~]# mkfs -t xfs /dev/sdb1

B.配置永久挂载

[root@node3-nfs ~]# mkdir /wordpress	#创建好数据目录
[root@node3-nfs ~]# vim /etc/fstab
/dev/sdb1       /wordpress      xfs     defaults        0 0
[root@node3-nfs ~]# mount -a	#挂载所有

检查刚刚挂载的分区

[root@node3-nfs ~]# df -hT | grep -i sdb1
/dev/sdb1               xfs        20G   33M   20G    1% /wordpress

4.配置NFS

[root@node3-nfs ~]# vim /etc/exports
/wordpress      192.168.140.12(rw,no_root_squash)  192.168.140.13(rw,no_root_squash)

A.准备WordPress安装包

使用截止教程编写日期,最新6.2版本

[root@node3-nfs ~]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz
[root@node3-nfs ~]# tar xf latest-zh_CN.tar.gz
[root@node3-nfs ~]# mv ./wordpress/* /wordpress
[root@node3-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

B.设置权限

[root@node3-nfs ~]# chmod -R 777 /wordpress

C.启动NFS服务,并设置开机自启动

[root@node3-nfs ~]# systemctl enable --now nfs-server
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@node3-nfs ~]# showmount -e localhost	#可以检查是否创建服务成功
Export list for localhost:
/wordpress 192.168.140.13,192.168.140.12

5.配置web服务器挂载NFS网络存储

需要回到192.168.140.12192.168.140.13配置NFS共享

A.两台web服务器需要安装NFS用来支持NFS文件系统

[root@node1 ~]# yum install -y nfs-utils
[root@node2 ~]# yum install -y nfs-utils

B.创建挂载目录

[root@node1 ~]# mkdir /wordpress
[root@node2 ~]# mkdir /wordpress

C.配置永久挂载

[root@node1 ~]# vim /etc/fstab
192.168.140.14:/wordpress /wordpress nfs defaults 0 0

[root@node2 ~]# vim /etc/fstab
192.168.140.14:/wordpress /wordpress nfs defaults 0 0

D.挂载

[root@node1 ~]# mount -a
[root@node2 ~]# mount -a
[root@node1 ~]# ls /wordpress/		#可以看到NFS服务器上的文件已经挂载到本地了!
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@node2 ~]# 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

6.回到NFS服务器,配置MySQL

回到192.168.140.14配置MySQL数据库
MySQL5.7安装教程:https://www.wsjj.top/archives/65

A.创建WordPress数据库

创建一个库名为wordpress的库,字符集为utf8mb4

mysql> create database wordpress charset utf8mb4;
Query OK, 1 row affected (0.00 sec)

B.创建授权登录用户

我们这里要授权2web服务器登录,所以ip要写2web服务器的IP
我们创建了用户wordpress,并且设置了密码为WWW.1.com,并且只允许对wordpress库有权限

mysql> grant all on wordpress.* to 'wordpress'@'192.168.140.12' identified by 'WWW.1.com';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all on wordpress.* to 'wordpress'@'192.168.140.13' identified by 'WWW.1.com';
Query OK, 0 rows affected, 1 warning (0.00 sec)

至此我们NFS服务器和MySQL服务配置完毕。

五、配置2台web服务器

需要到192.168.140.12192.168.140.13配置

1.安装httpd

[root@node1 ~]# yum install -y httpd
[root@node2 ~]# yum install -y httpd
[root@node1 ~]# systemctl enable --now httpd	#启动httpd服务,并且加入到开机自启项
[root@node2 ~]# systemctl enable --now httpd

2.安装PHP7.4

[root@node1 ~]# yum install -y epel-release
[root@node2 ~]# yum install -y epel-release
[root@node1 ~]# yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@node2 ~]# yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@node1 ~]# yum install -y yum-utils
[root@node2 ~]# yum install -y yum-utils
[root@node1 ~]# yum-config-manager --enable remi-php74
[root@node2 ~]# yum-config-manager --enable remi-php74
[root@node1 ~]# yum install -y php  php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json php-redis
[root@node2 ~]# yum install -y php  php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json php-redis

A.查看PHP版本

[root@node1 ~]# php -v
PHP 7.4.33 (cli) (built: Feb 14 2023 09:31:03) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
[root@node2 ~]# php -v
PHP 7.4.33 (cli) (built: Feb 14 2023 09:31:04) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

3.配置httpd虚拟主机

A.编写虚拟主机配置文件

[root@node1 ~]# vim /etc/httpd/conf.d/wp.conf
<VirtualHost *:80>      
     ServerName blog1.wsjj.top  
     DocumentRoot /wordpress
     ErrorLog /var/log/httpd/wp_error.log       
     CustomLog /var/log/httpd/wp_access.log combined    
</VirtualHost>

<Directory /wordpress>
    Require all granted         
</Directory>
[root@node2 ~]# vim /etc/httpd/conf.d/wp.conf
<VirtualHost *:80>      
     ServerName blog2.wsjj.top  
     DocumentRoot /wordpress
     ErrorLog /var/log/httpd/wp_error.log       
     CustomLog /var/log/httpd/wp_access.log combined    
</VirtualHost>

<Directory /wordpress>
    Require all granted         
</Directory>

B.检查配置文件

这里的AH00558不是报错,可以到主配置文件/etc/httpd/conf/httpd.conf修改ServerName后面的内容为Localhost:80即可

[root@node1 ~]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::20c:29ff:fe0f:55d2. Set the 'ServerName' directive globally to suppress this message
Syntax OK
[root@node2 ~]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::20c:29ff:fe0f:55d2. Set the 'ServerName' directive globally to suppress this message
Syntax OK

[root@node1 ~]# httpd -t	#修改主配置文件之后再检查
Syntax OK

C.重启服务器

[root@node1 ~]# systemctl restart httpd
[root@node2 ~]# systemctl restart httpd

4.测试2台web服务器

LB05

LB06

5.配置WordPress

两台web服务器任意一台即可,因为我们后端采用NFS共享存储

LB07

LB09

LB10

LB11

LB12

wp8

6.修改wp-config.php文件,防止今后插件无法安装

需要回到NFS服务器上配置192.168.140.14

[root@node3-nfs ~]# vim /wordpress/wp-config.php	#在文件最后加入以下三行内容
define("FS_METHOD", "direct");
define("FS_CHMOD_DIR", 0777);
define("FS_CHMOD_FILE", 0777);

7.在两台web服务器配置VIP和arp内核参数

当net.ipv4.conf.all.arp_ignore等于0的时候,响应任意网卡上接收到的对本机IP地址的arp请求包括环回网卡上的地址,而不管该目的IP是否在接收网卡上。
当net.ipv4.conf.all.arp_ignore等于1的时候,只响应目的IP地址为接收网卡上的本地地址arp请求。
当net.ipv4.conf.all.arp_announce等于0的时候,本机所有IP地址都向任何一个接口通告ARP报文。
当net.ipv4.conf.all.arp_announce等于1的时候,尽量仅向该网卡回应与该网段匹配的ARP报文。
当net.ipv4.conf.all.arp_announce等于2的时候,该网卡回应与该网段匹配的ARP报文。

[root@node1 ~]# ip addr add dev lo 192.168.140.100/32
[root@node2 ~]# ip addr add dev lo 192.168.140.100/32

配置永久绑定IP

[root@node1 ~]# vim /etc/rc.d/rc.local
ip addr add dev lo 192.168.140.100/32
[root@node2 ~]# vim /etc/rc.d/rc.local
ip addr add dev lo 192.168.140.100/32
[root@node1 ~]# chmod a+x /etc/rc.d/rc.local
[root@node2 ~]# chmod a+x /etc/rc.d/rc.local
[root@node1 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@node1 ~]# sysctl -p
[root@node2 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
[root@node2 ~]# sysctl -p

至此两台web服务器都配置完毕!

六、配置主从调度器

1.安装keepalivedipvsadm

[root@master01 ~]# yum install -y keepalived ipvsadm
[root@master02 ~]# yum install -y keepalived ipvsadm

2.编辑调度器配置文件

[root@master01 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id master_LVS		#指定调度器id
}

vrrp_instance wordpress {		#指定虚拟组名
    state MASTER	#指定主备状态,与之相对的为BACKUP
    interface ens33		#指定VIP在哪块网卡上
    virtual_router_id 51	#组ID
    priority 100	#优先级0-255数字越大权限越高
    advert_int 1	#心跳时间间隔
    authentication {
        auth_type PASS
        auth_pass redhat	#设置密码
    }
    virtual_ipaddress {
        192.168.140.100		#设置VIP
    }
}
virtual_server 192.168.140.100 80 {		#指定虚拟服务器为192.168.140.100
    delay_loop 6
    lb_algo rr		#算法轮询
    lb_kind DR		#指定工作模式为DR
    persistence_timeout 300		#超时时间
    protocol TCP	#基于TCP连接

    real_server 192.168.140.12 80 {		#web1服务器
        weight 1
        TCP_CHECK {
            connect_port 80	#指定80端口
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.140.13 80 {		#web2服务器
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

3.配置备用调度器的配置文件

快速拷贝主配置文件

[root@master01 ~]# scp /etc/keepalived/keepalived.conf root@192.168.140.11:/etc/keepalived

修改备用调度器配置文件

[root@master02 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id salve_LVS	#修改组名
}

vrrp_instance wordpress {
    state BACKUP	#修改模式为备用
    interface ens33
    virtual_router_id 51
    priority 50		#修改权重
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass redhat
    }
    virtual_ipaddress {
        192.168.140.100
    }
}

virtual_server 192.168.140.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 300
    protocol TCP

    real_server 192.168.140.12 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.140.13 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}

4.启动调度器和备用调度器

[root@master01 ~]# systemctl enable --now keepalived.service
[root@master02 ~]# systemctl enable --now keepalived.service

5.查看LVS工作状态

[root@master01 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.140.100:80 rr persistent 300
  -> 192.168.140.12:80            Route   1      0          0         
  -> 192.168.140.13:80            Route   1      0          0

[root@master02 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.140.100:80 rr persistent 300
  -> 192.168.140.12:80            Route   1      0          0         
  -> 192.168.140.13:80            Route   1      0          0

6.测试

LB13

7.模拟主调度器挂掉

[root@master01 ~]# systemctl stop keepalived.service

A.查看网页是否正常

LB13

B.查看备用调度器工作状态

[root@master02 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.140.100:80 rr persistent 300
  -> 192.168.140.12:80            Route   1      0          0         
  -> 192.168.140.13:80            Route   1      0          2

8.模拟挂掉一个web服务器

[root@node2 ~]# systemctl stop httpd

A.测试访问

LB13

B.查看备用节点工作状态

[root@master02 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.140.100:80 rr persistent 300
  -> 192.168.140.12:80            Route   1      0          8

七、为WordPress配置Redis数据库

关于Redis教程链接:https://www.wsjj.top/archives/91 https://www.wsjj.top/archives/92
回到NFS服务器192.168.140.14,安装Redis
尝试修改配置文件/wordpress/wp-config.php在文件最后加入以下三行内容
define(“FS_METHOD”, “direct”);
define(“FS_CHMOD_DIR”, 0777);
define(“FS_CHMOD_FILE”, 0777);

1.安装Redis

如果提示找不到软件包,请配置epel

[root@node3-nfs ~]# yum install -y redis

2.修改Redis配置文件

[root@node3-nfs ~]# vim /etc/redis.conf
bind 192.168.140.14		#指定IP地址
port 6379	#端口保持默认
daemonize yes	#启用后台运行
logfile /var/log/redis/redis_6379.log
appendonly yes	#启用aof日志
appendfilename "appendonly_6379.aof"

3.启动Redis

如果提示找不到netstat命令,需要安装yum install -y net-tools

[root@node3-nfs ~]# redis-server /etc/redis.conf
[root@node3-nfs ~]# netstat -tunlp | grep redis
tcp        0      0 192.168.140.14:6379     0.0.0.0:*               LISTEN      2363/redis-server 1

4.本地连接测试

[root@node3-nfs ~]# redis-cli -h 192.168.140.14
192.168.140.14:6379>

5.配置WordPress连接Redis

A.后台Redis安装插件

下载完,暂时不要启用
如果下载失败,请确认是否拥有权限!执行chmod -R 777 /wordpress再试试

LB14

B.修改WordPress配置文件

随便一台服务器上修改web1web2NFS
找到相应字段并修改

[root@node3-nfs ~]# vim /wordpress/wp-content/plugins/redis-cache/includes/object-cache.php
protected function build_parameters() {
        $parameters = [
            'scheme' => 'tcp',
            'host' => '192.168.140.14',		#这里修改IP地址
            'port' => 6379,
            'database' => 0,
            'timeout' => 1,
            'read_timeout' => 1,
            'retry_interval' => null,
            'persistent' => false,
        ];

C.查看插件启用状态

改完上面的配置文件,就可以安全的启用啦!

LB15