搭建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试验

原文地址:https://www.cnblogs.com/kingzhe/p/11156433.html

时间: 2024-10-10 08:33:35

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

(tengine+keepalived)+(apache+tomcat)+memcached+mysql实现高可用、负载均衡、可扩展架构

目录 1.高可用.负载均衡.可扩展架构的需要背景 2.系统架构 3.系统规划及说明 4.系统部署及测试 5.总结 1.高可用.负载均衡.可扩展架构的需要背景 从互联网诞生以来,网站架构随着互联网的快速发展发生着巨大的变化,现今,数据每天都在以爆炸式的增长,大数据.云计算等概念被业内炒得沸沸扬扬,这些前沿技术也在各行各业落地开花.每一种新技术的提出几乎都会或多或少影响着IT的基础架构,面对数据的快速增长.我们急需一套高可用.负载均衡.可扩展的架构来作为支撑. 2.系统架构 此次博文介绍一套高可用.

keepalived+nginx搭建高可用and负载均衡集群

keepalived+nginx搭建高可用and负载均衡集群 前言 因生产环境需要,现需要搭建一个前端为nginx的HA双向互备.后端为nginx代理的loadbalance负载均衡集群.nginx进程基于于Master+Slave(worker)多进程模型,自身具有非常稳定的子进程管理功能.在Master进程分配模式下,Master进程永远不进行业务处理,只是进行任务分发,从而达到Master进程的存活高可靠性,Slave(worker)进程所有的业务信号都由主进程发出,Slave(worke

keepalived + haproxy 实现web 双主模型的高可用负载均衡

1.本文的架构图: 阐述各服务器用途: 1.haproxy在本构架中实现的是:负载均衡 2.keepalived实现对haproxy的高可用 3.apache static 实现静态页面的访问 4.aoache dynamic实现动态页面的访问,图中有两个是实现负载均衡的 配置各功能模块: 一.配置haproxy和keepalived 验证: 1.当一台keepalived宕机后,VIP会不会转移到另外一台服务器 2.当一台haproxy服务出故障,VIP会不会转移到另外一台服务器 注意: 那如

高可用集群技术之keepalived实现lvs高可用并负载均衡web服务

Keepalived概述: Keepalived的作用是检测服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器. keepalived实现lvs高可用并负载均衡web服务: 准备工作: 2台keepalived节点为: node1.samlee.com

LVS专题: LVS+Keepalived并使用DNS轮询实现Director的高可用和负载均衡

LVS专题: LVS+Keepalived并使用DNS轮询实现Director的高可用和负载均衡 前言 LVS专题写到第三篇了, 前两篇我们对LVS的基础使用也有了一些了解, 这篇我们将做一个比较复杂的实验, 话不多说, 开始吧! 什么是KeepAlived What is Keepalived ? Keepalived is a routing software written in C. The main goal of this project is to provide simple a

keepalived实现nginx的高可用 + nginx负载均衡

前言 使用集群是网站解决高并发.海量数据问题的常用手段.当一台服务器的处理能力.存储空间不足时,不要企图去换更强大的服务器,对大型网站而言,不管多么强大的服务器,都满足不了网站持续增长的业务需求.这种情况下,更恰当的做法是增加一台服务器分担原有服务器的访问及存储压力.通过负载均衡调度服务器,将来自浏览器的访问请求分发到应用服务器集群中的任何一台服务器上,如果有更多的用户,就在集群中加入更多的应用服务器,使应用服务器的负载压力不再成为整个网站的瓶颈. 摘自<大型网站技术架构_核心原理与案例分析>

keepalived+nginx实现高可用and负载均衡集群

keepalived+nginx实现高可用and负载均衡集群 前言 因生产环境需要,现需要搭建一个前端为nginx的HA双向互备.后端为nginx代理的loadbalance负载均衡集群.nginx进程基于于Master+Slave(worker)多进程模型,自身具有非常稳定的子进程管理功能.在Master进程分配模式下,Master进程永远不进行业务处理,只是进行任务分发,从而达到Master进程的存活高可靠性,Slave(worker)进程所有的业务信号都 由主进程发出,Slave(work

可扩展、高可用、负载均衡网站架构设计方案

可扩展.高可用.负载均衡网站架构设计方案 基本需求: 1.  高可用性:将停止服务时间降低到最低甚至是不间断服务 2.  可扩展性:随着访问的增加,系统具备良好的伸缩能力 3.  可视性:系统.服务的状态处于一个实时的监控之下 4.  高性能高可靠性:经过优化的体系结构及合理的备份策略 5.  安全性:结构上的安全及主机的安全策略 基 本思路 1.对于访问频繁,用户量 大的对象(bbs,blog)采用某种合理的方式负载到多个服务器上.把数据库独立出来,准备2套mysql数据库,以实现 主从复制,

利用lvs keepalived配置redis高可用及负载均衡

需求 我们用ES做日志服务,架构是 上游数据来源=>redis=>logstash=>ES redis目前还是单点, 没有做高可用, 现在数据量越来越多, 如果下游消费不出问题还好, redis里面数据来了就走,但是下游一旦出问题, 分给redis的内存半小时就撑满了. 看到redis3.0 beta版本已经提供了集群功能, 但是需要client以集群模式接入, 我们这么多上游用户, 不太可能统一要求他们改造. 公司也有硬件的LB, 同事在E公司的时候就是用的硬件LB. 但接入还要申请,