keepalived 主從搭建

2021-10-24 09:47:37 字數 2354 閱讀 4797

基本環境:centos7

1、原始碼包安裝

cd /usr/local/src/  #進入到準備放原始檔的目錄

[root@localhost src]# pwd

/usr/local/src

[root@localhost src]# tar xvf keepalived-2.0.18.tar.gz # 解壓

[root@localhost src]# cd keepalived-2.0.18 #進入到解壓後的目錄

[root@localhost src keepalived-2.0.18]# ./configure --prefix=/usr/local/keepalived #校驗

[root@localhost src keepalived-2.0.18]# make && make install # 編譯安裝

2、初始化及啟動

[root@localhost /]# cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/keepalived 

# 將keepalived主程式加入到環境變數(安裝目錄下)

[root@localhost /]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived

# keepalived啟動指令碼(原始碼目錄下),放到/etc/init.d/目錄下就可以使用service命令便捷呼叫

[root@localhost /]# cp /usr/local/src/keepalived-2.0.18/keepalived/etc/init.d/keepalived /etc/init.d/keepalived

# 將配置檔案放到預設路徑下

[root@localhost /]# mkdir /etc/keepalived

[root@localhost /]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

加為系統服務:chkconfig –add keepalived

開機啟動:chkconfig keepalived on

檢視開機啟動的服務:chkconfig –list

啟動、關閉、重啟service keepalived start|stop|restart

3、keepalived 配置 

vim /etc/keepalived/keepalived.conf  #配置keepalived.conf

! configuration file for keepalived

global_defs

vrrp_script check_nginx_status

vrrp_instance vi_1

track_script

virtual_ipaddress

}

4、檢測指令碼

指令碼邏輯為:先檢測nginx是否在執行,沒有則重啟nginx,等2秒後再檢測一次,如果nginx依舊沒有啟動,則停止keepalived。

指令碼注意 開頭一定要有 #!/bin/bash,不然keepalived 執行不起。

#!/bin/bash

counter=$(ps - c nginx --no-heading|wc -l)

if [ "$" = "0" ]; then

/usr/local/nginx/sbin/nginx

sleep 2

counter=$(ps -c nginx --no-heading|wc -l)

if [ "$" = "0" ]; then

systemctl stop keepalived

fifi

指令碼賦許可權:chmod 755 check_nginx_status.sh

注意給執行許可權 chmod 755, 不能給777。至少root使用者不能給777許可權,安全機制導致不會被keepalived執行

5、配置檔案檢測:

請在v2.0.13及以上版本使用,之前的版本reload兩次後程序會終止,官方已恢復。

/usr/local/keepalived/sbin/keepalived -t
6、問題:

disabling track script check_nginx_status due to insecure

該問題原因為 check_nginx_status.sh 的許可權問題。 root 使用者777 許可權會出現該問題

MySQL主從搭建筆記

作業系統 windows10,64,mysql版本 解壓方式安裝的mysql8.0.20 1.my.ini配置 master 主從 master設定 server id 1 log bin mysql bin binlog format row master配置完後,重啟master的mysql服務...

LVS DR模式搭建與keepalived LVS

3 在分發器 dir 上編輯乙個shell指令碼 vim usr local sbin lvs dr.sh 寫入一下內容 bin bash echo 1 proc sys net ipv4 ip forward 開啟埠 ipv usr sbin ipvsadm vip 192.168.244.200...

KeepAlived集群高可用搭建

你的系統是否存在單點故障呢?那麼這裡我們基於docker演示下基於keepalive的高可用配置。keepalived是乙個用c語言編寫的路由軟體,它的主要目的是為了提供基於linux伺服器的負載均衡和高可用功能。其負載均衡是依賴lvs的,keepalived還實現了一些的檢測器來實現後端伺服器的健...