送只羊抵油钱 发表于 2023-3-23 19:00:11

lvs+keepalived集群

使用lvs+keepalived架构架构实现后端web服务器(该web服务器要求搭建wordpress博客站)的负载均衡

最终客户端访问域名     我的名字.wordpress.cn 能够访问搭建的博客站 ,达到负载均衡的目的

IP主机名角色192.168.26.101rs1后端真实服务器/nginx192.168.26.102rs2后端真实服务器/nginx192.168.26.103masterkeepalived服务器master 192.168.26.104backupkeepalived服务器backup192.168.26.105nfsNfs服务器192.168.26.201web数据库服务器192.168.26.12cesi客户端 
一.基础环境搭建

1.1master配置

1.1.1安装配置keepalived

# yum -y install keepalived
# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalivedglobal_defs {    router_id master}vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 150 advert_int 1 authentication {   auth_type PASS   auth_pass 1111 }virtual_ipaddress {   192.168.26.222/24 dev ens33 label ens33:1 }}virtual_server 192.168.26.222 80 { delay_loop 6 lb_algo wrr lb_kind DR # persistence_timeout 50 protocol TCP real_server 192.168.26.101 80 {    weight 1    HTTP_GET {   url {path /status_code 200}    }connect_timeout 3nb_get_retry 3delay_before_retry 3 } real_server 192.168.26.102 80 {      weight 1      HTTP_GET {       url {      path /      status_code 200      }      }       connect_timeout 3       nb_get_retry 3            delay_before_retry 3 }} 1.1.2查看下ipvsadm -ln是否帮你自动添加了lvs集群规则# systemctl restart keepalived.service    #生效   # yum -y install ipvsadm                  
# ipvsadm -ln                           #查看当前ipvs模块中记录的连接
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.26.222:80 wrr
  -> 192.168.26.101:80            Route   1      0          0         
  -> 192.168.26.102:80            Route   1      0          0         
 
1.2Backup配置#yum -y install keepalived
# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
    router_id backup
}

vrrp_instance VI_1 {
 state BACKUP
 interface ens33
 virtual_router_id 51
 priority 100
 advert_int 1
 authentication {
     auth_type PASS
     auth_pass 1111
 }
 virtual_ipaddress {
     192.168.26.222/24 dev ens33 label ens33:1
 }
}

virtual_server 192.168.26.222 80 {
 delay_loop 6
 lb_algo wrr
 lb_kind DR
 # persistence_timeout 50
 protocol TCP

 real_server 192.168.26.101 80 {
    weight 1
    HTTP_GET {
   url {
  path /
  status_code 200
  }
   }
  connect_timeout 3
  nb_get_retry 3
  delay_before_retry 3
 }

 real_server 192.168.26.102 80 {
      weight 1
      HTTP_GET {
       url {
        path /
        status_code 200
        }
      }
       connect_timeout 3
       nb_get_retry 3
              delay_before_retry 3
 }# systemctl restart keepalived.service
# systemctl restart keepalived.service
# yum -y install ipvsadm
# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.26.222:80 wrr
  -> 192.168.26.101:80            Route   1      0          0         
  -> 192.168.26.102:80            Route   1      0          0   


 
1.3Rs1 安装nginx 添加虚拟IP arp抑制

#关闭防火墙和slinux
# systemctl stop firewalld.service
# setenforce 0
# yum -y install nginx


# echo "rs1" > /usr/share/nginx/html/index.html# systemctl enable --now nginxCreated symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.# curl 127.0.0.1rs1# ip addr add 192.168.26.222/32 dev ens33 label ens33:1 #vim /etc/sysctl.conf#在末尾插入# sysctl -pnet.ipv4.conf.all.arp_ignore = 1net.ipv4.conf.all.arp_announce = 2net.ipv4.conf.lo.arp_ignore = 1net.ipv4.conf.lo.arp_announce = 21.4RS2同上

# systemctl stop firewalld.service # setenforce 0# yum -y install nginx

# echo "rs2" > /usr/share/nginx/html/index.html
# systemctl enable --now nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
#  ip addr add 192.168.26.222/32 dev lo label lo:1
# curl 127.0.0.1
rs2
# vim /etc/sysctl.conf
# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2


 
 1.5测试

https://img2023.cnblogs.com/blog/2861433/202303/2861433-20230323154337916-1650740675.png
二.实现后端服务器上运行wordpress博客站点

2.1Rs1  rs2继续配置 都配置PHP环境(编译安装)

# yum -y remove php-fpm php-mysqlnd php-json#(删除自带yum安装的)
Loaded plugins: fastestmirror
No Match for argument: php-fpm
No Match for argument: php-mysqlnd
No Match for argument: php-json
No Packages marked for removal
# yum -y install gcc openssl-devel libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel
# cd /usr/local/src
# wget https://www.php.net/distributions/php-7.4.11.tar.xz--2023-03-23 15:56:56--https://www.php.net/distributions/php-7.4.11.tar.xzResolving www.php.net (www.php.net)... 185.85.0.29, 2a02:cb40:200::1adConnecting to www.php.net (www.php.net)|185.85.0.29|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 10302076 (9.8M) Saving to: ‘php-7.4.11.tar.xz’100%[============================================================================================>] 10,302,0762.26MB/s   in 4.6s   2023-03-23 15:57:02 (2.16 MB/s) - ‘php-7.4.11.tar.xz’ saved # tar xf php-7.4.11.tar.xz # cd php-7.4.11/

                        #编译安装
# ./configure --prefix=/usr/local/php74 --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-zlib --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.# make -j 8 && make install# cp /usr/local/src/php-7.4.11/php.ini-production /etc/php.ini
# cd /usr/local/php74/etc/
# cp php-fpm.conf.default php-fpm.conf
# pwd
/usr/local/php74/etc
# cd php-fpm.d/
# cp www.conf.default www.conf
# vim www.confuser = nginx
group = nginx
打开
listen.allowed_clients = 127.0.0.1
pm.status_path = /status
https://img2023.cnblogs.com/blog/2861433/202303/2861433-20230323161914812-2091434195.pnghttps://img2023.cnblogs.com/blog/2861433/202303/2861433-20230323161927649-1611092900.png
https://img2023.cnblogs.com/blog/2861433/202303/2861433-20230323161934936-2013106764.png
#  /usr/local/php74/sbin/php-fpm -t
NOTICE: configuration file /usr/local/php74/etc/php-fpm.conf test is successful

#  /usr/local/php74/sbin/php-fpm
#  ss -ntl
State      Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port              
LISTEN     0      128                             127.0.0.1:9000                                                *:*                  
LISTEN     0      128                                     *:80                                                  *:*                  
LISTEN     0      128                                     *:22                                                  *:*                  
LISTEN     0      100                             127.0.0.1:25                                                  *:*                  
LISTEN     0      128                                  [::]:80                                               [::]:*                  
LISTEN     0      128                                  [::]:22                                               [::]:*                  
LISTEN     0      100                                 [::1]:25                                               [::]:*                  
# vim /etc/nginx/nginx.conf
域名登录https://img2023.cnblogs.com/blog/2861433/202303/2861433-20230323191643347-2125300483.png 
         index        index.php;
     # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
插入   location ~ \.php$ {
   root html;
   fastcgi_pass 127.0.0.1:9000;
   fastcgi_index index.php;
   fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
   include fastcgi_params;
  }
https://img2023.cnblogs.com/blog/2861433/202303/2861433-20230323162216435-1428266705.png
 
# systemctl restart nginx.service


 
2.2部署 WordPress

# wget https://cn.wordpress.org/wordpress-6.0.1-zh_CN.tar.gz--2023-03-23 16:23:27--https://cn.wordpress.org/wordpress-6.0.1-zh_CN.tar.gzResolving cn.wordpress.org (cn.wordpress.org)... 198.143.164.252Connecting to cn.wordpress.org (cn.wordpress.org)|198.143.164.252|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 21933976 (21M) Saving to: ‘wordpress-6.0.1-zh_CN.tar.gz’100%[============================================================================================>] 21,933,9764.26MB/s   in 6.8s   2023-03-23 16:23:35 (3.09 MB/s) - ‘wordpress-6.0.1-zh_CN.tar.gz’ saved # tar xf wordpress-6.0.1-zh_CN.tar.gz# cp -r wordpress/* /usr/share/nginx/html/# cd /usr/share/nginx/html/# cp wp-config-sample.php wp-config.php# vim wp-config.php
https://img2023.cnblogs.com/blog/2861433/202303/2861433-20230323162711808-1653396380.png 
# chown -R nginx.nginx .#修改权限所属 
2.3rocky安装数据库

# systemctl stop firewalld.service# setenforce 0# curl -LsS -O https://downloads.mariadb.com/MariaDB/mariadb_repo_setup# bash mariadb_repo_setup --mariadb-server-version=10.6# Checking for script prerequisites.# MariaDB Server version 10.6 is valid# Repository file successfully written to /etc/yum.repos.d/mariadb.repo# Adding trusted package signing keys.../etc/pki/rpm-gpg ~~# Successfully added trusted package signing keys# Cleaning package cache...25 文件已删除# dnf -y install mariadb-server
#  systemctl enable --now mariadb
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 10.6.12-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>  create database wordpress;
Query OK, 1 row affected (0.001 sec)

MariaDB [(none)]> create user wordpress@'192.168.26.%' identified by '123456';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> grant all on wordpress.* to wordpress@'192.168.26.%';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> quit 百度访问RS1IP 192.168.26.101
 https://img2023.cnblogs.com/blog/2861433/202303/2861433-20230323163834724-952528818.png
https://img2023.cnblogs.com/blog/2861433/202303/2861433-20230323163850926-1565278112.png
 
 
 https://img2023.cnblogs.com/blog/2861433/202303/2861433-20230323163906792-3090683.png
2.4RS2配置

# yum -y remove php-fpm php-mysqlnd php-json
Loaded plugins: fastestmirror
No Match for argument: php-fpm
No Match for argument: php-mysqlnd
No Match for argument: php-json
No Packages marked for removal
# yum -y install gcc openssl-devel libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel

# wget https://www.php.net/distributions/php-7.4.11.tar.xz--2023-03-23 15:56:56--https://www.php.net/distributions/php-7.4.11.tar.xzResolving www.php.net (www.php.net)... 185.85.0.29, 2a02:cb40:200::1adConnecting to www.php.net (www.php.net)|185.85.0.29|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 10302076 (9.8M) Saving to: ‘php-7.4.11.tar.xz’100%[============================================================================================>] 10,302,0762.26MB/s   in 4.6s   2023-03-23 15:57:02 (2.16 MB/s) - ‘php-7.4.11.tar.xz’ saved # tar xf php-7.4.11.tar.xz # cd php-7.4.11/


# ./configure --prefix=/usr/local/php74 --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-zlib --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --enable-mbstring --enable-xml --enable-sockets --enable-fpm --enable-maintainer-zts --disable-fileinfo+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE. By continuing this installation  |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.# make -j 8 && make install#  scp /usr/local/php74/etc/php-fpm.conf root@192.168.26.102:/usr/local/php74/etc/
root@192.168.26.102's password:
php-fpm.conf                                                                                        100% 5387     3.5MB/s   00:00    
# scp /usr/local/php74/etc/php-fpm.d/www.conf  root@192.168.26.102:/usr/local/php74/etc/php-fpm.d/
root@192.168.26.102's password:
www.conf                                                                                            100%   19KB  11.1MB/s   00:00  


# ls /usr/local/php74/etc/php-fpm.confphp-fpm.conf.defaultphp-fpm.d# ls /usr/local/php74/etc/php-fpm.d/www.confwww.conf.default# vim /etc/nginx/nginx.conf
https://img2023.cnblogs.com/blog/2861433/202303/2861433-20230323165226920-651822431.png 
     index        index.php;
     # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
插入   location ~ \.php$ {
   root html;
   fastcgi_pass 127.0.0.1:9000;
   fastcgi_index index.php;
   fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
   include fastcgi_params;
  }
https://img2023.cnblogs.com/blog/2861433/202303/2861433-20230323191733454-51623068.png
 
# systemctl restart nginx.service#  
2.5NFS服务器

  # yum -y install nfs-utils

# mkdir /code#vim /etc/exports
/code * (rw)
# chmod 777 /code/# systemctl start nfs-server.service
  客户端rs1rs2 挂载
yum -y install nfs-utils
systemctl restart nfs-server
yum -y install rpcbind
systemctl enable --now nfs-server rpcbind
showmount -e 192.168.26.105 测试
Export list for 192.168.26.105:
/code *                    #以上rs1,2都执行
# tar zcf xkf.tar.gz /usr/share/nginx/html/
tar: Removing leading `/' from member names
# ls
\  anaconda-ks.cfg  wordpress  wordpress-6.0.1-zh_CN.tar.gz  xkf.tar.gz
# scp xkf.tar.gz root@192.168.26.105:/root


# tar xf xkf.tar.gz# cd usr/share/nginx/html/# cd ..# mv html/* /code# ls /code/404.htmlimg          nginx-logo.png   wp-admin            wp-config-sample.phpwp-links-opml.phpwp-settings.php50x.htmlindex.html   poweredby.png    wp-blog-header.php    wp-content            wp-load.php      wp-signup.phpen-US   index.php    readme.html      wp-comments-post.phpwp-cron.php         wp-login.php       wp-trackback.phpicons   license.txtwp-activate.phpwp-config.php         wp-includes         wp-mail.php      xmlrpc.php
# mount -t nfs 192.168.26.105:/code /usr/share/nginx/html/
# df -Th
192.168.26.105:/code    nfs4       40G  1.7G   39G   5% /usr/share/nginx/html

# showmount -e 192.168.26.105
Export list for 192.168.26.105:
/code *
# mount -t nfs 192.168.26.105:/code /usr/share/nginx/html/
# df -Th
192.168.26.105:/code    nfs4       40G  1.7G   39G   5% /usr/share/nginx/htm https://img2023.cnblogs.com/blog/2861433/202303/2861433-20230323190551841-355430861.png
 

来源:https://www.cnblogs.com/Xkf-IE/p/17245989.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: lvs+keepalived集群