翼度科技»论坛 云主机 LINUX 查看内容

lvs+keepalived集群

9

主题

9

帖子

27

积分

新手上路

Rank: 1

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

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

IP主机名角色
192.168.26.101rs1后端真实服务器/nginx
192.168.26.102rs2后端真实服务器/nginx
192.168.26.103masterkeepalived服务器master 
192.168.26.104backupkeepalived服务器backup
192.168.26.105nfsNfs服务器
192.168.26.201web数据库服务器
192.168.26.12cesi客户端
 
一.基础环境搭建

1.1master配置

1.1.1安装配置keepalived

[root@master ~]# yum -y install keepalived
[root@master ~]# cat /etc/keepalived/keepalived.conf
  1. ! Configuration File for keepalivedglobal_defs {    router_id [b]master[/b]}vrrp_instance VI_1 { state[b] MASTER[/b] interface ens33 virtual_router_id 51 priority [b]150[/b] 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 }}
复制代码
 1.1.2查看下ipvsadm -ln是否帮你自动添加了lvs集群规则
  1. [root@master ~]# systemctl restart keepalived.service    #生效   [root@master ~]# yum -y install ipvsadm                  
  2. [root@master ~]# ipvsadm -ln                             #查看当前ipvs模块中记录的连接
  3. IP Virtual Server version 1.2.1 (size=4096)
  4. Prot LocalAddress:Port Scheduler Flags
  5.   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
  6. TCP  192.168.26.222:80 wrr
  7.   -> 192.168.26.101:80            Route   1      0          0         
  8.   -> 192.168.26.102:80            Route   1      0          0         
复制代码
 
1.2Backup配置
  1. [root@backup ~]#  yum -y install keepalived
  2. [root@backup ~]# cat /etc/keepalived/keepalived.conf
  3. ! Configuration File for keepalived
  4. global_defs {
  5.     router_id [b]backup[/b]
  6. }
  7. vrrp_instance VI_1 {
  8.  state[b] BACKUP[/b]
  9.  interface ens33
  10.  virtual_router_id 51
  11.  priority 100
  12.  advert_int 1
  13.  authentication {
  14.      auth_type PASS
  15.      auth_pass 1111
  16.  }
  17.  virtual_ipaddress {
  18.      192.168.26.222/24 dev ens33 label ens33:1
  19.  }
  20. }
  21. virtual_server 192.168.26.222 80 {
  22.  delay_loop 6
  23.  lb_algo wrr
  24.  lb_kind DR
  25.  # persistence_timeout 50
  26.  protocol TCP
  27.  real_server 192.168.26.101 80 {
  28.     weight 1
  29.     HTTP_GET {
  30.    url {
  31.   path /
  32.   status_code 200
  33.   }
  34.    }
  35.   connect_timeout 3
  36.   nb_get_retry 3
  37.   delay_before_retry 3
  38.  }
  39.  real_server 192.168.26.102 80 {
  40.       weight 1
  41.       HTTP_GET {
  42.        url {
  43.         path /
  44.         status_code 200
  45.         }
  46.       }
  47.        connect_timeout 3
  48.        nb_get_retry 3
  49.               delay_before_retry 3
  50.  }
复制代码
[root@backup ~]# systemctl restart keepalived.service
[root@master ~]# systemctl restart keepalived.service
[root@master ~]# yum -y install ipvsadm
[root@backup ~]# 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抑制
  1. #关闭防火墙和slinux
  2. [root@rs1 ~]# systemctl stop firewalld.service
  3. [root@rs1 ~]# setenforce 0
  4. [root@rs1 ~]# yum -y install nginx
  5. [root@rs1 ~]# echo "rs1" > /usr/share/nginx/html/index.html[root@rs1 ~]# systemctl enable --now nginxCreated symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.[root@rs1 ~]# curl 127.0.0.1rs1[root@rs1 ~]# ip addr add 192.168.26.222/32 dev ens33 label ens33:1 [root@rs1 ~]#  vim /etc/sysctl.conf  #在末尾插入[root@rs1 ~]# 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 = 2
复制代码
1.4RS2同上
  1. [root@rs2 ~]# systemctl stop firewalld.service [root@rs2 ~]# setenforce 0[root@rs2 ~]# yum -y install nginx
  2. [root@rs2 ~]# echo "rs2" > /usr/share/nginx/html/index.html
  3. [root@rs2 ~]# systemctl enable --now nginx
  4. Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
  5. [root@rs2 ~]#  ip addr add 192.168.26.222/32 dev lo label lo:1
  6. [root@rs2 ~]# curl 127.0.0.1
  7. rs2
  8. [root@rs2 ~]# vim /etc/sysctl.conf
  9. [root@rs2 ~]# sysctl -p
  10. net.ipv4.conf.all.arp_ignore = 1
  11. net.ipv4.conf.all.arp_announce = 2
  12. net.ipv4.conf.lo.arp_ignore = 1
  13. net.ipv4.conf.lo.arp_announce = 2
复制代码
 
 1.5测试


二.实现后端服务器上运行wordpress博客站点

2.1Rs1  rs2继续配置 都配置PHP环境(编译安装)
  1. [root@rs1 ~]# yum -y remove php-fpm php-mysqlnd php-json  #(删除自带yum安装的)
  2. Loaded plugins: fastestmirror
  3. No Match for argument: php-fpm
  4. No Match for argument: php-mysqlnd
  5. No Match for argument: php-json
  6. No Packages marked for removal
  7. [root@rs1 ~]# yum -y install gcc openssl-devel libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel
  8. [root@rs1 ~]# cd /usr/local/src
  9. [root@rs1 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) [application/octet-stream]Saving to: ‘php-7.4.11.tar.xz’100%[============================================================================================>] 10,302,076  2.26MB/s   in 4.6s   2023-03-23 15:57:02 (2.16 MB/s) - ‘php-7.4.11.tar.xz’ saved [10302076/10302076][root@rs1 src]# tar xf php-7.4.11.tar.xz [root@rs1 src]# cd php-7.4.11/
  10.                           #编译安装
  11. [root@rs1 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
复制代码
  1. +--------------------------------------------------------------------+
  2. | License:                                                           |
  3. | This software is subject to the PHP License, available in this     |
  4. | distribution in the file LICENSE. By continuing this installation  |
  5. | process, you are bound by the terms of this license agreement.     |
  6. | If you do not agree with the terms of this license, you must abort |
  7. | the installation process at this point.                            |
  8. +--------------------------------------------------------------------+
  9. Thank you for using PHP.
复制代码
  1. [root@rs1 php-7.4.11]# [b]make -j 8 && make install[/b]
复制代码
  1. [root@rs1 php-7.4.11]# [b]cp /usr/local/src/php-7.4.11/php.ini-production /etc/php.ini[/b]
  2. [root@rs1 php-7.4.11]# cd /usr/local/php74/etc/
  3. [root@rs1 etc]# [b]cp php-fpm.conf.default php-fpm.conf[/b]
  4. [root@rs1 etc]# pwd
  5. [b]/usr/local/php74/etc[/b]
  6. [root@rs1 etc]# cd php-fpm.d/
  7. [root@rs1 php-fpm.d]# [b]cp www.conf.default www.conf[/b]
  8. [root@rs1 php-fpm.d]# vim www.conf
复制代码
user = nginx
group = nginx
打开
listen.allowed_clients = 127.0.0.1
pm.status_path = /status

  1. [root@rs1 php-fpm.d]#  [b]/usr/local/php74/sbin/php-fpm -t[/b]
  2. [23-Mar-2023 16:07:09] NOTICE: configuration file /usr/local/php74/etc/php-fpm.conf test is successful
  3. [root@rs1 php-fpm.d]#  /usr/local/php74/sbin/php-fpm
  4. [root@rs1 php-fpm.d]#  ss -ntl
  5. State      Recv-Q Send-Q                      Local Address:Port                                     Peer Address:Port              
  6. LISTEN     0      128                             127.0.0.1:9000                                                *:*                  
  7. LISTEN     0      128                                     *:80                                                  *:*                  
  8. LISTEN     0      128                                     *:22                                                  *:*                  
  9. LISTEN     0      100                             127.0.0.1:25                                                  *:*                  
  10. LISTEN     0      128                                  [::]:80                                               [::]:*                  
  11. LISTEN     0      128                                  [::]:22                                               [::]:*                  
  12. LISTEN     0      100                                 [::1]:25                                               [::]:*                  
  13. [root@rs1 php-fpm.d]# vim /etc/nginx/nginx.conf
  14. 域名登录
复制代码
 
         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;
  }

 
[root@rs1 php-fpm.d]# systemctl restart nginx.service


 
2.2部署 WordPress
  1. [root@rs1 ~]# 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) [application/octet-stream]Saving to: ‘wordpress-6.0.1-zh_CN.tar.gz’100%[============================================================================================>] 21,933,976  4.26MB/s   in 6.8s   2023-03-23 16:23:35 (3.09 MB/s) - ‘wordpress-6.0.1-zh_CN.tar.gz’ saved [21933976/21933976][root@rs1 ~]# tar xf wordpress-6.0.1-zh_CN.tar.gz[root@rs1 ~]# cp -r wordpress/* /usr/share/nginx/html/[root@rs1 ~]# cd /usr/share/nginx/html/[root@rs1 html]# cp wp-config-sample.php wp-config.php[root@rs1 html]# vim wp-config.php
  2. [align=center][img]https://img2023.cnblogs.com/blog/2861433/202303/2861433-20230323162711808-1653396380.png[/img][/align]
复制代码
 
  1. [root@rs1 html]# [b]chown -R nginx.nginx .  #修改权限所属[/b]
复制代码
 
2.3rocky安装数据库
  1. [root@web ~]# systemctl stop firewalld.service[root@web ~]# setenforce 0[root@web ~]# curl -LsS -O https://downloads.mariadb.com/MariaDB/mariadb_repo_setup[root@web ~]# bash mariadb_repo_setup --mariadb-server-version=10.6# [info] Checking for script prerequisites.# [info] MariaDB Server version 10.6 is valid# [info] Repository file successfully written to /etc/yum.repos.d/mariadb.repo# [info] Adding trusted package signing keys.../etc/pki/rpm-gpg ~~# [info] Successfully added trusted package signing keys# [info] Cleaning package cache...25 文件已删除[root@web ~]# dnf -y install mariadb-server
  2. [root@web ~]#  systemctl enable --now mariadb
  3. Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
  4. [root@web ~]# mysql
  5. Welcome to the MariaDB monitor.  Commands end with ; or \g.
  6. Your MariaDB connection id is 6
  7. Server version: 10.6.12-MariaDB MariaDB Server
  8. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10. MariaDB [(none)]> [b] create database wordpress;[/b]
  11. Query OK, 1 row affected (0.001 sec)
  12. MariaDB [(none)]> [b]create user wordpress@'192.168.26.%' identified by '123456';[/b]
  13. Query OK, 0 rows affected (0.001 sec)
  14. MariaDB [(none)]> [b]grant all on wordpress.* to wordpress@'192.168.26.%';[/b]
  15. Query OK, 0 rows affected (0.001 sec)
  16. MariaDB [(none)]> quit
复制代码
 百度访问RS1IP 192.168.26.101
 


 
 
 

2.4RS2配置
  1. [root@rs2 ~]# yum -y remove php-fpm php-mysqlnd php-json
  2. Loaded plugins: fastestmirror
  3. No Match for argument: php-fpm
  4. No Match for argument: php-mysqlnd
  5. No Match for argument: php-json
  6. No Packages marked for removal
  7. [root@rs2 ~]# yum -y install gcc openssl-devel libxml2-devel bzip2-devel libmcrypt-devel sqlite-devel oniguruma-devel
  8. [root@rs2 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) [application/octet-stream]Saving to: ‘php-7.4.11.tar.xz’100%[============================================================================================>] 10,302,076  2.26MB/s   in 4.6s   2023-03-23 15:57:02 (2.16 MB/s) - ‘php-7.4.11.tar.xz’ saved [10302076/10302076][root@rs2 src]# tar xf php-7.4.11.tar.xz [root@rs2 src]# cd php-7.4.11/
  9. [root@rs2 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
复制代码
  1. +--------------------------------------------------------------------+
  2. | License:                                                           |
  3. | This software is subject to the PHP License, available in this     |
  4. | distribution in the file LICENSE. By continuing this installation  |
  5. | process, you are bound by the terms of this license agreement.     |
  6. | If you do not agree with the terms of this license, you must abort |
  7. | the installation process at this point.                            |
  8. +--------------------------------------------------------------------+
  9. Thank you for using PHP.
复制代码
  1. [root@rs1 php-7.4.11]# make -j 8 && make install
复制代码
  1. [root@rs1 ~]#  scp /usr/local/php74/etc/php-fpm.conf root@192.168.26.102:/usr/local/php74/etc/
  2. root@192.168.26.102's password:
  3. php-fpm.conf                                                                                        100% 5387     3.5MB/s   00:00    
  4. [root@rs1 ~]# scp /usr/local/php74/etc/php-fpm.d/www.conf  root@192.168.26.102:/usr/local/php74/etc/php-fpm.d/
  5. root@192.168.26.102's password:
  6. www.conf                                                                                            100%   19KB  11.1MB/s   00:00  
  7. [root@rs2 php-7.4.11]# ls /usr/local/php74/etc/php-fpm.conf  php-fpm.conf.default  php-fpm.d[root@rs2 php-7.4.11]# ls /usr/local/php74/etc/php-fpm.d/www.conf  www.conf.default[root@rs2 php-7.4.11]# vim /etc/nginx/nginx.conf
  8. [align=center][img]https://img2023.cnblogs.com/blog/2861433/202303/2861433-20230323165226920-651822431.png[/img][/align]
复制代码
 
  1. [/code][b]     index        index.php;[/b]
  2. [code]
复制代码
     # Load configuration files for the default server block.
  1. [/code]        include /etc/nginx/default.d/*.conf;
  2. [code]
复制代码
插入   location ~ \.php$ {
  1. [/code][b]   root html;[/b]
  2. [code]
复制代码
   fastcgi_pass 127.0.0.1:9000;
  1. [/code][b]   fastcgi_index index.php;[/b]
  2. [code]
复制代码
   fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  1. [/code][b]   include fastcgi_params;[/b]
  2. [code]
复制代码
  }

 
  1. [root@rs2 php-7.4.11]# systemctl restart nginx.service[root@rs2 php-7.4.11]#
复制代码
 
2.5NFS服务器

  [root@nfs ~]# yum -y install nfs-utils
  1. [root@nfs ~]# mkdir /code[root@nfs ~]#  vim /etc/exports
  2. /code * (rw)
  3. [root@nfs ~]# chmod 777 /code/[root@nfs ~]# 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都执行
[root@rs1 ~]# tar zcf xkf.tar.gz /usr/share/nginx/html/
tar: Removing leading `/' from member names
[root@rs1 ~]# ls
\  anaconda-ks.cfg  wordpress  wordpress-6.0.1-zh_CN.tar.gz  xkf.tar.gz
[root@rs1 ~]# scp xkf.tar.gz root@192.168.26.105:/root

  1. [root@nfs ~]# tar xf xkf.tar.gz[root@nfs ~]# cd usr/share/nginx/html/[root@nfs html]# cd ..[root@nfs nginx]# mv html/* /code[root@nfs nginx]# ls /code/404.html  img          nginx-logo.png   wp-admin              wp-config-sample.php  wp-links-opml.php  wp-settings.php50x.html  index.html   poweredby.png    wp-blog-header.php    wp-content            wp-load.php        wp-signup.phpen-US     index.php    readme.html      wp-comments-post.php  wp-cron.php           wp-login.php       wp-trackback.phpicons     license.txt  wp-activate.php  wp-config.php         wp-includes           wp-mail.php        xmlrpc.php
  2. [root@rs1 ~]# mount -t nfs 192.168.26.105:/code /usr/share/nginx/html/
  3. [root@rs1 ~]# df -Th
  4. 192.168.26.105:/code    nfs4       40G  1.7G   39G   5% /usr/share/nginx/html
  5. [root@rs2 php-7.4.11]# showmount -e 192.168.26.105
  6. Export list for 192.168.26.105:
  7. /code *
  8. [root@rs2 php-7.4.11]# mount -t nfs 192.168.26.105:/code /usr/share/nginx/html/
  9. [root@rs2 php-7.4.11]# df -Th
  10. 192.168.26.105:/code    nfs4       40G  1.7G   39G   5% /usr/share/nginx/htm
复制代码
 

 

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

举报 回复 使用道具