nginx + keepalived

nginx + keepalived

  • 利用keealived 实现nginx HA

  • keepalived安装

    下载keepalived官网:http://keepalived.org

    上传到主机

    解压编译安装

    tar -zxvf keepalived-2.2.2.tar.gz
    ./configure --prefix=/usr/local/keepalived
    make && make install
    
    
  • 环境准备(局域网地址就直接写在这边,如果是云主机注意打码哦)

    主机 ip
    Host1 192.168.31.128
    Host2 192.168.31.167
  • 添加到系统服务

    mkdir /etc/keepalived
    cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
    cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived
    cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
    
  • systemctl指令分享

    序号 命令 描述
    1 systemctl enable httpd.service 使某服务自动启动
    2 systemctl disable httpd.service 使某服务不自动启动
    3 systemctl status httpd.service 检查服务状态
    4 systemctl load keepalived.service 加入自定义服务
    5 停止服务,删除keepalived.service 删除服务
    6 systemctl list-units --type=service 显示所有已启动的服务
    7 systemctl start keepalived.service 启动某服务
    8 systemctl stop keepalived.service 停止某服务
    9 systemctl restart keepalived.service 重启某服务
  • 启动不成功的话修改service

    cd /usr/lib/systemd/system/
    vi keepalived.service
    
    [Unit]
    Description=LVS and VRRP High Availability Monitor
    After= network-online.target syslog.target
    Wants=network-online.target
     
    [Service]
    Type=forking
    PIDFile=/var/run/keepalived.pid
    KillMode=process
    EnvironmentFile=-/usr/local/etc/sysconfig/keepalived
    ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS
    ExecReload=/bin/kill -HUP $MAINPID
     
    [Install]
    
    #修改完之后执行
    systemctl load keepalived.service
    systemctl daemon-reload
    systemctl start keepalived.service
    
    
  • 修改Host1配置

    vi /etc/keepalived/keepalived.conf
    
    global_defs {
    }
    
    vrrp_instance VI_1 {
        state BACKUP
        interface enp0s3
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.31.168
        }
    }
    
  • 重启host1 keepalived

    systemctl restart keepalived.service
    
  • 查看ip

    ip addr
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 08:00:27:ca:1d:77 brd ff:ff:ff:ff:ff:ff
        inet 192.168.31.128/24 brd 192.168.31.255 scope global noprefixroute dynamic enp0s3
           valid_lft 43176sec preferred_lft 43176sec
        inet 192.168.31.168/32 scope global enp0s3
           valid_lft forever preferred_lft forever
        inet6 fe80::a00:27ff:feca:1d77/64 scope link
           valid_lft forever preferred_lft forever
    
  • host2同host1启动流程

  • nginx安装

    详情见https://www.cnblogs.com/koiiok/p/15004082.html

posted @ 2021-07-16 16:14  koiiok  阅读(67)  评论(0编辑  收藏  举报