搭建Keepalived+LNMP架构web动态博客 实现高可用与负载均衡

Posted kingzhe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建Keepalived+LNMP架构web动态博客 实现高可用与负载均衡相关的知识,希望对你有一定的参考价值。

环境准备:

192.168.193.80  node1

192.168.193.81 node2

关闭防火墙

[[email protected] ~]# systemctl stop firewalld        #两台都关闭
[[email protected] ~]# setenforce 0
setenforce: SELinux is disabled

host文件

[[email protected] ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.193.80 node1
192.168.193.81 node2

keepalived为主备模式

master为node1节点

backup为node2节点

 

yum源

[epel]
name=epel
enabled=1
gpgcheck=0
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
[centos]
name=centos base
enabled=1
gpgcheck=0
baseurl=http://mirrors.163.com/centos/7/os/x86_64/

 

master节点配置

下载keeplived

[[email protected] ~]# yum -y install keepalived
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
5,7 s/^/#/g    #5-7行加注释
9,16 s/^/#/g    #9-16
36,157 s/^/#/g  #注释掉

 

vrrp_instance VI_1 
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100            #优先级
    advert_int 1         #一秒检查一次  一秒给baskup发送一个报文
    authentication 
        auth_type PASS      #这个是密码认证,和51相辅相成,证明在一个网段里
        auth_pass 1111      #密码
    
    virtual_ipaddress 
        192.168.193.252      #这个就vip   可以有一个也可以有多个
    
[[email protected] ~]# scp /etc/keepalived/keepalived.conf 192.168.193.81:/etc/keepalived/keepalived.conf  #把配置文件传到node2节点

配置backup端

[[email protected] ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication 
        auth_type PASS
        auth_pass 1111
    
    virtual_ipaddress 
        192.168.193.252
    
[[email protected] ~]# systemctl start keepalived          #启动
[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:84:7a:50 brd ff:ff:ff:ff:ff:ff
    inet 192.168.193.80/24 brd 192.168.193.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.193.252/32 scope global ens33       #这个就是虚拟IP
       valid_lft forever preferred_lft forever
    inet6 fe80::9a7d:fb7d:d10f:e5e8/64 scope link 
       valid_lft forever preferred_lft forever
[[email protected] ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:5b:98:47 brd ff:ff:ff:ff:ff:ff
    inet 192.168.193.81/24 brd 192.168.193.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::80ce:27dd:68f9:5adc/64 scope link 
       valid_lft forever preferred_lft forever

实验停止node1节点IP是否发生漂移

[[email protected] ~]# systemctl stop  keepalived
[[email protected] ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:5b:98:47 brd ff:ff:ff:ff:ff:ff
    inet 192.168.193.81/24 brd 192.168.193.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.193.252/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::80ce:27dd:68f9:5adc/64 scope link 
       valid_lft forever preferred_lft forever

发生漂移,这样就成功了,利用了VRRP协议

配置LVS

node1配置

vrrp_instance VI_1 
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication 
        auth_type PASS
        auth_pass 1111
    
    virtual_ipaddress 
        192.168.193.252
    


virtual_server 192.168.193.252 80 
    delay_loop 3
    lb_algo rr
    lb_kind DR
    protocol TCP

    real_server 192.168.193.80 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.193.81 80 
        weight 1
        HTTP_GET 
            url 
              path /
              status_code 200
            
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        
    

node2配置

vrrp_instance VI_1 
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication 
        auth_type PASS
        auth_pass 1111
    
    virtual_ipaddress 
        192.168.193.252
    


virtual_server 192.168.193.252 80 
    delay_loop 3
    lb_algo rr
    lb_kind DR
    protocol TCP

    real_server 192.168.193.80 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.193.81 80 
        weight 1
        HTTP_GET 
            url 
              path /
              status_code 200
            
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        
    
[[email protected] ~]# yum -y install 
[[email protected] ~]# bash  1.sh  start
启动成功
[[email protected] ~]# bash  1.sh  start
启动成功

 

#!/bin/bash
case $1 in
start)
        echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
        echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
        ifconfig ens33:0 192.168.193.252/32 broadcast 192.168.193.252 up
        if [ $? -eq 0 ];then
                route add -host 192.168.193.252 dev ens33:0
        fi
        echo "启动成功"
;;
stop)
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
        ifconfig ens33:0 down
        route del -host 192.168.193.252
        echo "删除成功"
;;
*)
        echo "usage start|stop"
;;
esac
[[email protected] ~]# systemctl restart   keepalived   
[[email protected] ~]# systemctl restart   keepalived   
[[email protected] ~]# yum -y install ipvsadm 

[[email protected] ~]# 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.193.252:80 rr -> 192.168.193.80:80 Route 1 0 0 -> 192.168.193.81:80 Route 1 0 0

这样Keeplived就继承了LVS的功能  高可用与负载均衡

 继续搭建nginx

以下操作2台节点同样操作

[[email protected] ~]# yum install nginx php php-fpm mariadb-server  php-mysql  -y

[[email protected] ~]# yum install nginx php php-fpm mariadb-server  php-mysql  -y
 38     server 
 39         listen       80 default_server;
 40         listen       [::]:80 default_server;
 41         server_name  _;
 42         root         /var/www/html;   #修改网站访问目录
 43         index      index.php  index。html;     #修改动态静态



 47 
 48         location /               #复制
 49         
 50  
 51         location ~ ^/.*\\.php$              #添加
 52         fastcgi_pass 127.0.0.1:9000; 
 53         include fastcgi.conf;
 54         
 55         

检测配置文件是否有错

[[email protected] ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] ~]# systemctl restart mariadb     #启动数据库
[[email protected] ~]# cd /var/www/html/   #进入网站根目录
[[email protected] html]# rz          #上传动态博客压缩包
rz waiting to receive.
 zmodem trl+C ?

  100%    4548 KB 4548 KB/s 00:00:01       0 Errorszip...

[[email protected] html]# ls
wordpress-3.3.1-zh_CN.zip

[[email protected] html]# unzip wordpress-3.3.1-zh_CN.zip

[[email protected] html]# mv wordpress/*  . 

[[email protected] html]# cp wp-config-sample.php wp-config.php

 

[[email protected] html]#  vim wp-config.php
 18 define(DB_NAME, king);
 19 
 20 /** MySQL 数据库用户名 */
 21 define(DB_USER, king);
 22 
 23 /** MySQL 数据库密码 */
 24 define(DB_PASSWORD, 123);
[[email protected] html]# mysql -uroot
MariaDB [(none)]> create database king;
MariaDB [(none)]> grant all on *.* to [email protected]localhost identified by 123;
MariaDB [(none)]> exit
Bye
[[email protected] ~]# systemctl restart nginx        #两台同样操作
[[email protected] ~]# systemctl restart php-fpm
[[email protected] ~]# systemctl restart keepalived
[[email protected] ~]# 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.193.252:80 rr
  -> 192.168.193.80:80            Route   1      0          0         
  -> 192.168.193.81:80            Route   1      0          0      

[[email protected] ~]# 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.193.252:80 rr
  -> 192.168.193.80:80            Route   1      0          0         
  -> 192.168.193.81:80            Route   1      0          0   

现在访问虚拟IP试验

 技术图片

 

以上是关于搭建Keepalived+LNMP架构web动态博客 实现高可用与负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

@中期架构搭建 - - lnmp+keepalived+ 显示error页面

keepalived高可用haproxy+varnish+lnmp实现站点搭建与ansible实现

keepalived+haproxy搭建LNMP架构并做数据同步

keepalived+haproxy搭建LNMP架构并做数据同步

轻松搭建web网站架构服务

LNMP动态网站架构及web应用部署,搭建discuz论坛