我在文件中有这些设置,keepalived.conf
但是当我停止HAProxy
服务时,它没有执行通知脚本,但是当我重新启动keepalived
服务时,它每次都会执行。以下是详细信息,
HAProxy: 1.8.8
保活: 2.0.18
操作系统: Ubuntu 18.04
蟒蛇: 2.7
云服务提供商: Hetzner
/etc/keepalived/keepalived.conf
vrrp_script chk_haproxy {
# Requires keepalived-1.1.13
script "/usr/bin/pkill -0 haproxy" # cheaper than pidof
interval 2 # check every 2 seconds
weight 2 # add 2 points of priority if OK
}
vrrp_instance real {
interface eth0
state MASTER
virtual_router_id 51
priority 101 # 101 on primary, 100 on secondary
virtual_ipaddress {
11.23.10.19/32 dev eth0 label eth0:1
}
track_script {
chk_haproxy
}
notify "/etc/keepalived/master.sh"
#notify_backup "/etc/keepalived/master.sh"
#notify_fault "/etc/keepalived/master.sh"
}
/etc/keepalived/master.sh
#!/bin/bash
export API_TOKEN='<api_token>'
export MASTER_SERVER_ID='<master_server_id>'
export BACKUP_SERVER_ID='<backup_server_id>'
BASE_API='https://api.hetzner.cloud/v1'
FLOATING_IP_ID='<floating_ip_id>'
INSTANCE="Load-Balancer-Master"
if [ "$HOSTNAME" = "$INSTANCE" ]; then
SERVER_ID=$BACKUP_SERVER_ID # switch to the backup server if
# master gets down
else
SERVER_ID=$MASTER_SERVER_ID # vice-versa
fi
echo "Server ID: " $SERVER_ID
HAS_FLOATING_IP=$(curl -H "Authorization: Bearer $API_TOKEN" -s 'https://api.hetzner.cloud/v1/servers/'$SERVER_ID|python -c "import sys,json; print( True if json.load(sys.stdin)['server']['public_net']['floating_ips'] else False)")
echo "Has Floating Ip: " $HAS_FLOATING_IP
if [ $HAS_FLOATING_IP = "False" ]; then
n=0
while [ $n -lt 10 ]
do
python /usr/local/bin/assign-ip $FLOATING_IP_ID $SERVER_ID && break
n=$((n+1))
sleep 3
done
fi
/usr/local/bin/assign-ip
#!/usr/bin/python
import os
import sys
import requests
import json
api_base = 'https://api.hetzner.cloud/v1'
def usage():
print('{0} [Floating IP] [Server ID]'.format(sys.argv[0]))
print('\nYour Hetzner API token must be in the "API_TOKEN"'
' environmental variable.')
def main(floating_ip_id, server_id):
payload = {'server': server_id}
headers = {'Authorization': 'Bearer {0}'.format(os.environ['API_TOKEN']),
'Content-type': 'application/json'}
url = api_base + "/floating_ips/{0}/actions/assign".format(floating_ip_id)
r = requests.post(url, headers=headers, data=json.dumps(payload))
resp = r.json()
if resp['action']['error']:
print('{0}: {1}'.format(resp['action']['command'], resp['error']['message']))
sys.exit(1)
else:
print('Moving IP address to server: {0} with status:{1}'.format(server_id, resp['action']['status']))
if __name__ == "__main__":
if 'API_TOKEN' not in os.environ or not len(sys.argv) > 2:
usage()
sys.exit()
main(sys.argv[1], sys.argv[2])
当我停止使用 HAProxy 服务器sudo service haproxy stop
并检查我收到此响应的状态时,
● haproxy.service - HAProxy Load Balancer
Loaded: loaded (/lib/systemd/system/haproxy.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2019-09-28 22:12:57 IST; 1s ago
Docs: man:haproxy(1)
file:/usr/share/doc/haproxy/configuration.txt.gz
Process: 26434 ExecStart=/usr/sbin/haproxy -Ws -f $CONFIG -p $PIDFILE $EXTRAOPTS (code=exited, stat
Process: 26423 ExecStartPre=/usr/sbin/haproxy -f $CONFIG -c -q $EXTRAOPTS (code=exited, status=0/SU
Main PID: 26434 (code=exited, status=143)
Sep 28 00:44:18 Load-Balancer-Master haproxy[26434]: Proxy nginx_pool started.
Sep 28 00:44:18 Load-Balancer-Master haproxy[26434]: Proxy nginx_pool started.
Sep 28 00:44:18 Load-Balancer-Master systemd[1]: Started HAProxy Load Balancer.
Sep 28 22:12:57 Load-Balancer-Master haproxy[26434]: [WARNING] 270/004418 (26434) : Exiting Master pr
Sep 28 22:12:57 Load-Balancer-Master haproxy[26434]: [ALERT] 270/004418 (26434) : Current worker 2643
Sep 28 22:12:57 Load-Balancer-Master haproxy[26434]: [WARNING] 270/004418 (26434) : All workers exite
Sep 28 22:12:57 Load-Balancer-Master systemd[1]: Stopping HAProxy Load Balancer...
Sep 28 22:12:57 Load-Balancer-Master systemd[1]: haproxy.service: Main process exited, code=exited, s
Sep 28 22:12:57 Load-Balancer-Master systemd[1]: haproxy.service: Failed with result 'exit-code'.
Sep 28 22:12:57 Load-Balancer-Master systemd[1]: Stopped HAProxy Load Balancer.
在/var/log/syslog我得到了这个,
Sep 28 18:35:41 Load-Balancer-Master systemd[1]: Started Session 114 of user driveu.
Sep 28 18:42:57 Load-Balancer-Master systemd[1]: Stopping HAProxy Load Balancer...
Sep 28 18:42:57 Load-Balancer-Master systemd[1]: haproxy.service: Main process exited, code=exited, status=143/n/a
Sep 28 18:42:57 Load-Balancer-Master systemd[1]: haproxy.service: Failed with result 'exit-code'.
Sep 28 18:42:57 Load-Balancer-Master systemd[1]: Stopped HAProxy Load Balancer.
Sep 28 18:42:57 Load-Balancer-Master Keepalived_vrrp[26884]: Script `chk_haproxy` now returning 1
Sep 28 18:42:57 Load-Balancer-Master Keepalived_vrrp[26884]: VRRP_Script(chk_haproxy) failed (exited with status 1)
Sep 28 18:42:57 Load-Balancer-Master Keepalived_vrrp[26884]: (real) Changing effective priority from 103 to 101
但是通知脚本不会被调用,浮动 ip 也不会分配给 BACKUP 实例。因为我真的很陌生,Keepalived
任何人都可以帮我解决这个问题吗?
更新:我已经解决了这个问题
接口应该是private
网络,并且必须指定MASTER
和BACKUP
使用unicast_src_ip
和的私有ip unicast_peer
。修改设置在这里,
vrrp_script chk_haproxy {
# Requires keepalived-1.1.13
script "/usr/bin/pkill -0 haproxy" # cheaper than pidof
interval 2 # check every 2 seconds
weight 2 # add 2 points of priority if OK
}
vrrp_instance real {
interface ens10 # changed it from eth0
state MASTER
virtual_router_id 51
priority 101 # 101 on primary, 100 on secondary
unicast_src_ip 192.168.0.3
unicast_peer {
192.168.0.2
}
authentication {
auth_type PASS
auth_pass password
}
virtual_ipaddress {
11.23.10.19/32 dev eth0 label eth0:1
}
track_script {
chk_haproxy
}
notify "/etc/keepalived/master.sh"
#notify_backup "/etc/keepalived/master.sh"
#notify_fault "/etc/keepalived/master.sh"
}