keepalived安装配置

打印 上一主题 下一主题

主题 759|帖子 759|积分 2277


  • 安装工具和依赖包
    1. yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel
    复制代码
  • yum安装keepalived
    1. yum install -y keepalived
    复制代码
  • 源码包安装keepalived
    1. [root@master src]# pwd
    2. /usr/local/src
    3. [root@master src]# wget https://www.keepalived.org/software/keepalived-2.2.7.tar.gz
    4. [root@master src]# tar xvf keepalived-2.2.7.tar.gz
    5. [root@master src]# cd keepalived-2.2.7
    6. [root@master keepalived-2.2.7]# ./configure --prefix=/usr/local/keepalived
    7. [root@master keepalived-2.2.7]# make && make install
    复制代码
  • 安装后配置

    • 环境变量文件:  /usr/local/etc/sysconfig/keepalived --修改KEEPALIVED_OPTIONS="-f /etc/keepalived/keepalived.conf -D"
    • 执行文件:         /usr/local/sbin/keepalived
    • 配置文件:         /usr/local/etc/keepalived/  --mv /usr/local/etc/keepalived/keepalived.conf.sample /usr/local/etc/keepalived/keepalived.conf
    1. keepalived --help
    2. Usage: keepalived [OPTION...]
    3.   -f, --use-file=FILE          Use the specified configuration file
    4.                                 default '/usr/local/etc/keepalived/keepalived.conf'
    5.                                      or '/etc/keepalived/keepalived.conf'
    6.   -P, --vrrp                   Only run with VRRP subsystem
    7.   -C, --check                  Only run with Health-checker subsystem
    8.       --all                    Force all child processes to run, even if have no configuration
    9.   -l, --log-console            Log messages to local console
    10.   -D, --log-detail             Detailed log messages
    11.   -S, --log-facility=([0-7]|local[0-7]|user|daemon)
    12.                                Set syslog facility to LOG_LOCAL[0-7], user or daemon (default)
    13.   -G, --no-syslog              Don't log via syslog
    14.   -u, --umask=MASK             umask for file creation (in numeric form)
    15.   -X, --release-vips           Drop VIP on transition from signal.
    16.   -V, --dont-release-vrrp      Don't remove VRRP VIPs and VROUTEs on daemon stop
    17.   -I, --dont-release-ipvs      Don't remove IPVS topology on daemon stop
    18.   -R, --dont-respawn           Don't respawn child processes
    19.   -n, --dont-fork              Don't fork the daemon process
    20.   -d, --dump-conf              Dump the configuration data
    21.   -p, --pid=FILE               Use specified pidfile for parent process
    22.   -r, --vrrp_pid=FILE          Use specified pidfile for VRRP child process
    23.   -T, --genhash                Enter into genhash utility mode (this should be the first option used).
    24.   -c, --checkers_pid=FILE      Use specified pidfile for checkers child process
    25.   -a, --address-monitoring     Report all address additions/deletions notified via netlink
    26.   -s, --namespace=NAME         Run in network namespace NAME (overrides config)
    27.   -m, --core-dump              Produce core dump if terminate abnormally
    28.   -M, --core-dump-pattern=PATN Also set /proc/sys/kernel/core_pattern to PATN (default 'core')
    29.   -e, --all-config             Error if any configuration file missing (same as includet)
    30.   -i, --config-id id           Skip any configuration lines beginning '@' that don't match id
    31.                                 or any lines beginning @^ that do match.
    32.                                 The config-id defaults to the node name if option not used
    33.       --signum=SIGFUNC         Return signal number for STOP, RELOAD, DATA, STATS, STATS_CLEAR
    34.   -t, --config-test[=LOG_FILE] Check the configuration for obvious errors, output to
    35.                                 stderr by default
    36.   -v, --version                Display the version number
    37.   -h, --help                   Display this help message
    复制代码
    可以发现默认有两个配置文件路径 '/usr/local/etc/keepalived/keepalived.conf' or '/etc/keepalived/keepalived.conf'
    但是安装后并没有 /etc/keepalived目录
    稳妥起见,将两个目录进行连接 ln -s /usr/local/etc/keepalived /etc/keepalived

  • 配置keepalived
    1. #1号服务器配置
    2. vrrp_script chk_nginx {
    3.         script "/usr/bin/chk_nginx.sh"
    4.         interval 2
    5. }
    6. vrrp_instance VI_1 {
    7.     state MASTER
    8.     nopreempt
    9.     interface ens33
    10.     virtual_router_id 51
    11.     priority 100
    12.     advert_int 1
    13.     authentication {
    14.         auth_type AH
    15.         auth_pass 123456
    16.     }
    17.     unicast_src_ip 192.168.175.141
    18.     unicast_peer {
    19.         192.168.175.143
    20.     }
    21.     virtual_ipaddress {
    22.         192.168.175.200
    23.     }
    24.      track_script {
    25.        chk_nginx
    26.     }
    27. }
    28. #2号服务器配置
    29. vrrp_script chk_nginx {
    30.         script "/usr/bin/chk_nginx.sh"
    31.         interval 2
    32. }
    33. vrrp_instance VI_2 {
    34.     state MASTER
    35.     nopreempt
    36.     interface ens33
    37.     virtual_router_id 51
    38.     priority 100
    39.     advert_int 1
    40.     authentication {
    41.         auth_type AH
    42.         auth_pass 123456
    43.     }
    44.     unicast_src_ip 192.168.175.143
    45.     unicast_peer {
    46.         192.168.175.141
    47.     }
    48.     virtual_ipaddress {
    49.         192.168.175.200
    50.     }
    51.      track_script {
    52.        chk_nginx
    53.     }
    54. }
    复制代码
    chk_nginx
    1. #!/bin/bash
    2. # 设置最大重试次数为 5
    3. MAX_RETRIES=5
    4. RETRY_COUNT=0
    5. # 检查 Nginx 是否在运行中
    6. if [ $(ps -C nginx --no-header |wc -l) -gt 0 ]; then
    7.     echo "Nginx 已经在运行中"
    8. else
    9.     echo "Nginx 未启动,正在启动 Nginx ..."
    10.     # 启动 Nginx
    11.     systemctl start nginx
    12.     # 等待 Nginx 启动完成
    13.     while ! systemctl status nginx.service | grep -q "running"; do
    14.         echo "等待 Nginx 启动完成 ..."
    15.         sleep 1s
    16.         # 检查重试次数是否超过最大重试次数
    17.         RETRY_COUNT=$((RETRY_COUNT + 1))
    18.         if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then
    19.             systemctl stop keepalived.service
    20.             exit 1
    21.         fi
    22.     done
    23.    
    24.     if [ $(ps -C nginx --no-header |wc -l) -gt 0 ]; then
    25.         echo "Nginx 启动完成"
    26.     else
    27.         systemctl stop keepalived.service
    28.         echo "Nginx 启动失败"
    29.     fi
    30. fi
    复制代码
  • 配置启动服务

    安装后已经配置好了系统服务keepalived.service, 服务位置在/usr/lib/systemd/system目录
    1. vi /usr/local/etc/sysconfig/keepalived
    2. # Options for keepalived. See `keepalived --help' output and keepalived(8) and
    3. # keepalived.conf(5) man pages for a list of all options. Here are the most
    4. # common ones :
    5. #
    6. # --vrrp               -P    Only run with VRRP subsystem.
    7. # --check              -C    Only run with Health-checker subsystem.
    8. # --dont-release-vrrp  -V    Dont remove VRRP VIPs & VROUTEs on daemon stop.
    9. # --dont-release-ipvs  -I    Dont remove IPVS topology on daemon stop.
    10. # --dump-conf          -d    Dump the configuration data.
    11. # --log-detail         -D    Detailed log messages.
    12. # --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)
    13. #
    14. KEEPALIVED_OPTIONS="-f /etc/keepalived/keepalived.conf -D"
    复制代码
    1. vi /usr/lib/systemd/system/keepalived.service
    2. [Unit]
    3. Description=LVS and VRRP High Availability Monitor
    4. After=network-online.target syslog.target
    5. Wants=network-online.target
    6. Documentation=man:keepalived(8)
    7. Documentation=man:keepalived.conf(5)
    8. Documentation=man:genhash(1)
    9. Documentation=https://keepalived.org
    10. [Service]
    11. Type=forking
    12. PIDFile=/run/keepalived.pid
    13. KillMode=process
    14. # - 表示如果文件不存在则忽略不会报错
    15. EnvironmentFile=-/usr/local/etc/sysconfig/keepalived
    16. # KEEPALIVED_OPTIONS参数在 /usr/local/etc/sysconfig/keepalived文件中配置
    17. ExecStart=/usr/local/sbin/keepalived  $KEEPALIVED_OPTIONS
    18. ExecReload=/bin/kill -HUP $MAINPID
    19. [Install]
    20. WantedBy=multi-user.target
    21. #启动服务
    22. systemctl start keepalived.service
    23. #设置服务开机启动
    24. systemctl enable keepalived.service
    复制代码
  • 日志处理

    为keepalived单独写日志文件
    1. vi /usr/local/etc/sysconfig/keepalived
    2. KEEPALIVED_OPTIONS="-f /etc/keepalived/keepalived.conf -D -S 0"
    3. vi /etc/rsyslog.conf
    4. # rsyslog configuration file
    5. # For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
    6. # or latest version online at http://www.rsyslog.com/doc/rsyslog_conf.html
    7. # If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html
    8. #### GLOBAL DIRECTIVES ####
    9. # Where to place auxiliary files
    10. global(workDirectory="/var/lib/rsyslog")
    11. # Use default timestamp format
    12. module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat")
    13. #### MODULES ####
    14. module(load="imuxsock"           # provides support for local system logging (e.g. via logger command)
    15.        SysSock.Use="off") # Turn off message reception via local log socket;
    16.                           # local messages are retrieved through imjournal now.
    17. module(load="imjournal"             # provides access to the systemd journal
    18.        StateFile="/run/log/imjournal.state") # File to store the position in the journal
    19. #module(load="imklog") # reads kernel messages (the same are read from journald)
    20. #module(load="immark") # provides --MARK-- message capability
    21. $imjournalRatelimitInterval 0
    22. # Include all config files in /etc/rsyslog.d/
    23. include(file="/etc/rsyslog.d/*.conf" mode="optional")
    24. #### RULES ####
    25. # Log all kernel messages to the console.
    26. # Logging much else clutters up the screen.
    27. #kern.*                                                 /dev/console
    28. # Log anything (except mail) of level info or higher.
    29. # Don't log private authentication messages!
    30. *.info;mail.none;authpriv.none;cron.none                /var/log/messages
    31. # The authpriv file has restricted access.
    32. authpriv.*                                              /var/log/secure
    33. # Log all the mail messages in one place.
    34. mail.*                                                  -/var/log/maillog
    35. # Log cron stuff
    36. cron.*                                                  /var/log/cron
    37. # Everybody gets emergency messages
    38. *.emerg                                                 :omusrmsg:*
    39. # Save news errors of level crit and higher in a special file.
    40. uucp,news.crit                                          /var/log/spooler
    41. # Save boot messages also to boot.log
    42. local7.*                                                /var/log/boot.log
    43. # Save keepalived log to keepalived.log
    44. local0.*                                                /var/log/keepalived.log
    45. #重启日志服务:
    46. systemctl restart rsyslog
    47. #重启keepalived服务
    48. systemctl restart keepalived.service
    49. #查看日志文件
    50. tail -f /var/log/keepalived.log
    复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

愛在花開的季節

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表