• 创建简单WEB高可用集群


    1. 环境介绍

    我这里弄了2个虚拟机,信息如下:

    node1:192.168.168.201

    node2:192.168.168.202

    2.配置主机名

    [root@node1 ~]# vim /etc/sysconfig/network           #编辑节点1主机名配置文件
    #编辑
    HOSTNAME=node1.linuxpanda.com
    [root@node1 ~]# hostname node1.linuxpanda.com        #主机名立即生效
    
    [root@node2 ~]# vim /etc/sysconfig/network          #编辑节点2主机名配置文件
    #编辑
    HOSTNAME=node2.linuxpanda.com
    [root@node2 ~]# hostname node2.linuxpanda.com       #主机名立即生效

    2.主机互信

    具体可以参见我的上一篇博客:http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_023_sshgenkey.html

    [root@node1 ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa         #生成rsa
    [root@node1 ~]# ssh-copy-id -i  ~/.ssh/id_rsa.pub root@192.168.168.202  #复制公钥
    
    [root@node2 ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa         #生成rsa
    [root@node2 ~]# ssh-copy-id -i  ~/.ssh/id_rsa.pub root@192.168.168.201  #复制公钥
    [root@node1 ~]# ssh 192.168.168.202 'ip addr show dev eth0 '    #测试node1 ssh node2
    [root@node2 ~]# ssh 192.168.168.201 'ip addr show dev eth0 '    #测试node2 ssh node1
     

    3.host文件配置

    host配置文件配置主要用于名称解析, 我们不建议使用dns解析,否则dns无法解析就会导致集群服务的崩溃。

    [root@node1 ~]# vim /etc/hosts
    #添加如下2行
    192.168.168.201 node1.linuxpanda.com node1
    192.168.168.202 node2.linuxpanda.com node2
    [root@node1 ~]# scp /etc/hosts root@node2:/etc/hosts #host文件复制一份到node2中去
    [root@node1 ~]# ping node2 -c 1 #node1 ping node2
    [root@node2 ~]# ping node1 -c 1 #node2 ping node1

     4.防火墙和selinux设置

    [root@node1 ~]# service iptables stop                     #关闭node防火墙
    iptables: Flushing firewall rules:                         [  OK  ]
    iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
    iptables: Unloading modules:                               [  OK  ]
    [root@node1 ~]# ssh node2 'service iptables stop'         #关闭node2防火墙
    iptables: Flushing firewall rules: [  OK  ]
    iptables: Setting chains to policy ACCEPT: filter [  OK  ]
    iptables: Unloading modules: [  OK  ]
    [root@node1 ~]# sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config  #修改node1selinux设置
    [root@node1 ~]# setenforce 0                                                 #立马生效
    [root@node1 ~]# ssh node2 "sed -i '/SELINUX/s/enforcing/disabled/'           #修改node1selinux设置
    [root@node1 ~]# ssh node2 " setenforce 0'                                    #立马生效

    5.时间同步

    具体参见我的另一篇博客:http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_022_ntp.html

    6.安装heartbeat

    [root@node1 ~]#  yum  install heartbeat heartbeat-devel heartbeat-lib
    [root@node2 ~]# ssh node2 'yum  install heartbeat heartbeat-devel heartbeat-lib'

    7.编辑配置文件

    [root@node1 ha.d]# cd /etc/ha.d/
    [root@node1 ha.d]# cp /usr/share/doc/heartbeat-3.0.4/{authkeys,ha.cf,haresources} -p .
    [root@node1 ha.d]# vim authkeys 
    #添加以下两行
    auth 1
    1 md5 oracle
    [root@node1 ha.d]# mv ha.cf ha.cf.bak
    [root@node1 ha.d]# vim ha.cf 
    #添加如下行
    logfile    /var/log/ha-log
    keepalive 2
    deadtime 30
    warntime 10
    initdead 120
    bcast    eth0        # Linux
    auto_failback on
    node    node1.linuxpanda.com
    node    node2.linuxpanda.com
    ping 192.168.168.1

    [root@node1 ha.d]# vim haresources 
    #添加如下行
    node1.linuxpanda.com IPaddr::192.168.168.200/24/eth0 httpd
    [root@node1 ha.d]# scp -p authkeys haresources ha.cf node2:/etc/ha.d/

     

    8.安装httpd服务

    [root@node1 ha.d]# yum install httpd
    [root@node1 ha.d]# service httpd start
    Starting httpd:                                            [  OK  ]
    [root@node1 ha.d]# echo "node1" > /var/www/html/index.html
    [root@node1 ha.d]# elinks node1 -source
    
    [root@node2 ha.d]# yum install httpd
    [root@node2 ha.d]# service httpd start
    Starting httpd:                                            [  OK  ]
    [root@node2 ha.d]# echo "node2" > /var/www/html/index.html
    [root@node2 ha.d]# elinks node2 -source
    [root@node1 ha.d]# for i in {1..2};do  ssh node$i 'service httpd stop';ssh node$i 'chkconfig httpd off' ; done;

    9.启动heartbeat

    [root@node1 ha.d]# for i in {1..2} ; do ssh node$i 'service heartbeat start' ; done;

    10.测试

    我们使用宿主机测试下:

    [root@node1 ha.d]# ip addr show dev eth0                       #确认node1的ip信息,发现200ip资源在node1上分配的。
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:0c:29:c9:20:88 brd ff:ff:ff:ff:ff:ff
        inet 192.168.168.201/24 brd 192.168.168.255 scope global eth0
        inet 192.168.168.200/24 brd 192.168.168.255 scope global secondary eth0
        inet6 fe80::20c:29ff:fec9:2088/64 scope link 
           valid_lft forever preferred_lft forever
    [root@node2 ha.d]# ip addr show dev eth0 #查看node2的ip信息
    2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
        link/ether 00:50:56:3f:42:13 brd ff:ff:ff:ff:ff:ff
        inet 192.168.168.202/24 brd 192.168.168.255 scope global eth0
        inet6 fe80::250:56ff:fe3f:4213/64 scope link
           valid_lft forever preferred_lft forever

    我们使用hb_standby脚本将node1转为standby,当然也是可以断点处理。

    [root@node1 heartbeat]# /usr/share/heartbeat/hb_standby 
    Going standby [all].

    查看日志信息:

    [root@node1 heartbeat]# tail -20 /var/log/ha-log
    
    May 11 12:39:45 node1.linuxpanda.com heartbeat: [8508]: info: node1.linuxpanda.com wants to go standby [all]
    May 11 12:39:46 node1.linuxpanda.com heartbeat: [8508]: info: standby: node2.linuxpanda.com can take our all resources
    May 11 12:39:46 node1.linuxpanda.com heartbeat: [9129]: info: give up all HA resources (standby).
    ResourceManager(default)[9142]:    2017/05/11_12:39:46 info: Releasing resource group: node1.linuxpanda.com IPaddr::192.168.168.200/24/eth0 httpd
    ResourceManager(default)[9142]:    2017/05/11_12:39:46 info: Running /etc/init.d/httpd  stop
    ResourceManager(default)[9142]:    2017/05/11_12:39:46 info: Running /etc/ha.d/resource.d/IPaddr 192.168.168.200/24/eth0 stop
    IPaddr(IPaddr_192.168.168.200)[9222]:    2017/05/11_12:39:46 INFO: IP status = ok, IP_CIP=
    /usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.168.200)[9196]:    2017/05/11_12:39:46 INFO:  Success
    May 11 12:39:46 node1.linuxpanda.com heartbeat: [9129]: info: all HA resource release completed (standby).
    May 11 12:39:46 node1.linuxpanda.com heartbeat: [8508]: info: Local standby process completed [all].
    May 11 12:39:47 node1.linuxpanda.com heartbeat: [8508]: WARN: 1 lost packet(s) for [node2.linuxpanda.com] [2213:2215]
    May 11 12:39:47 node1.linuxpanda.com heartbeat: [8508]: info: remote resource transition completed.
    May 11 12:39:47 node1.linuxpanda.com heartbeat: [8508]: info: No pkts missing from node2.linuxpanda.com!
    May 11 12:39:47 node1.linuxpanda.com heartbeat: [8508]: info: Other node completed standby takeover of all resources.

    测试成功。

  • 相关阅读:
    vlc的应用之三:动态调用vlc-0.9.4的libvlc.dll【转】
    Linux 下搭建流媒体服务器
    linux bash Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数
    lftp mirror 上传目录
    rxvt-unicode配置
    在命令行上 使用 mutt, fetchmail, maildrop, msmtp 收发邮件
    在命令行上 Ubuntu 下使用 mutt 和 msmtp 发送 Gmail 邮件
    git查看各个branch之间的关系图
    在Ubuntu Server是配置iptables防火墙
    Ubuntu 14.04 配置iptables防火墙
  • 原文地址:https://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_024_haweb.html
Copyright © 2020-2023  润新知