暂无图片
暂无图片
暂无图片
暂无图片
1
暂无图片

pg 高可用测试 repmgr+Patroni + etcd + HAProxy+keepalived

原创 刘继超 2022-04-28
1908

一、总体架构
192.168.3.73 主库:repmgr+master+etcd+Patroni+HAProxy+keepalived
192.168.3.74 从库1 repmgr+standby+etcd+Patroni+HAProxy+keepalived
192.168.3.75 从库2 repmgr+standby+etcd+Patroni+keepalived
二、 安装keepalived
1 、安装keepalived前需要yum gcc openssl openssl‐devel libnl libnl‐devel ipvsadm

[root@pg73 dev]# mount /dev/cdrom /media/cdrom
mount: /dev/sr0 is write-protected, mounting read-only
[root@pg73 dev]# vi /etc/yum.repos.d/oracle.repo

[Server]
name=Red Hat Enterprise Linux $releasever Beta - $basearch - Source
baseurl=file:///media/cdrom
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
~
"/etc/yum.repos.d/oracle.repo" 6L, 184C written

[root@pg73 ~]# cd /etc/yum.repos.d/
[root@pg73 dev]# yum install gcc openssl openssl-devel libnl libnl-devel ipvsadm -y
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Server                                                                                                                                                                                  | 4.3 kB  00:00:00     
~~~~~~
Complete!
[root@pgxt dev]# 

复制

三、安装keepalived

root@pg73 src]# tar -zxf keepalived-2.2.7.tar.gz 
[root@pg73 src]# ll
total 1160
drwxrwxr-x 10 roo  roo     4096 Jan 17 03:10 keepalived-2.2.7
-rw-r--r--  1 root root 1180180 Mar 19 11:24 keepalived-2.2.7.tar.gz
[root@pg73 src]# cd keepalived-2.2.7/
[root@pg73 keepalived-2.2.7]# ll
total 1184
-rw-rw-r--  1 roo roo  43935 Jan 17 03:10 aclocal.m4
-rw-rw-r--  1 roo roo     41 Sep 28  2009 AUTHOR
-rwxrwxr-x  1 roo roo    100 Dec 21  2020 autogen.sh
drwxrwxr-x  2 roo roo     44 Jan 17 03:10 bin_install
drwxrwxr-x  2 roo roo     83 Jan 17 03:10 build-aux
-rwxrwxr-x  1 roo roo    100 May  5  2020 build_setup
-rw-rw-r--  1 roo roo 507110 Jan 27  2021 ChangeLog
-rwxrwxr-x  1 roo roo 423282 Jan 17 03:10 configure
-rw-rw-r--  1 roo roo 110816 Jan 17 03:05 configure.ac
-rw-rw-r--  1 roo roo    823 Dec 25  2017 CONTRIBUTORS
-rw-rw-r--  1 roo roo  18092 Nov 20  2012 COPYING
drwxrwxr-x  5 roo roo    210 Jan 17 03:10 doc
-rw-rw-r--  1 roo roo   2720 Nov 21 22:11 Dockerfile.in
-rw-rw-r--  1 roo roo   9490 Jan  9 01:34 INSTALL
drwxrwxr-x 10 roo roo    227 Jan 17 03:10 keepalived
-rw-rw-r--  1 roo roo  10413 Nov 20 22:28 keepalived.spec.in
drwxrwxr-x  2 roo roo   4096 Jan 17 03:10 lib
drwxrwxr-x  2 roo roo     43 Jan 17 03:10 m4
-rw-rw-r--  1 roo roo   3212 Nov 21 22:11 Makefile.am
-rw-rw-r--  1 roo roo  31619 Jan 17 03:10 Makefile.in
-rw-rw-r--  1 roo roo   2812 Feb 26  2021 README.md
drwxrwxr-x  3 roo roo     41 Jan 17 03:04 snap
-rw-rw-r--  1 roo roo   6898 Mar 15  2021 TODO
drwxrwxr-x  2 roo roo     26 Jan 17 03:10 tools
[root@pg73 keepalived-2.2.7]# ./configure --prefix=/usr/local/keepalived
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
configure: creating ./config.status
config.status: creating Makefile
config.status: creating keepalived/Makefile
config.status: creating lib/Makefile
config.status: creating keepalived/core/Makefile
config.status: creating keepalived.spec
config.status: creating Dockerfile
config.status: creating keepalived/check/Makefile
config.status: creating keepalived/vrrp/Makefile
config.status: creating keepalived/bfd/Makefile
config.status: creating doc/Makefile
config.status: creating bin_install/Makefile
config.status: creating keepalived/dbus/Makefile
config.status: creating keepalived/etc/Makefile
config.status: creating keepalived/etc/init/Makefile
config.status: creating keepalived/etc/init.d/Makefile
config.status: creating keepalived/etc/sysconfig/Makefile
config.status: creating keepalived/etc/keepalived/Makefile
config.status: creating keepalived/trackers/Makefile
config.status: creating doc/man/man8/Makefile
config.status: creating doc/man/man5/Makefile
config.status: creating doc/man/man1/Makefile
config.status: creating lib/config.h
config.status: creating lib/config_warnings.h
config.status: executing depfiles commands

Keepalived configuration
------------------------
Keepalived version       : 2.2.7
Compiler                 : gcc gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Preprocessor flags       : -D_GNU_SOURCE
Compiler flags           : -g -g -O2 -Wextra -Wunused -Wstrict-prototypes -Wabi -Wbad-function-cast -Wcast-align -Wcast-qual -Wdisabled-optimization -Wdouble-promotion -Wfloat-equal -Wframe-larger-than=5120 -Winit-self -Winline -Winvalid-pch -Wjump-misses-init -Wlogical-op -Wmissing-declarations -Wmissing-field-initializers -Wmissing-include-dirs -Wmissing-prototypes -Wnested-externs -Wold-style-definition -Woverlength-strings -Wpointer-arith -Wredundant-decls -Wshadow -Wstack-protector -Wstrict-overflow=4 -Wsuggest-attribute=format -Wsuggest-attribute=noreturn -Wsuggest-attribute=pure -Wsync-nand -Wtrampolines -Wundef -Wuninitialized -Wunknown-pragmas -Wunsafe-loop-optimizations -Wunsuffixed-float-constants -Wvariadic-macros -Wwrite-strings -fPIE -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -O2
Linker flags             : -pie -Wl,-z,relro -Wl,-z,now
Extra Lib                : -lm -lcrypto -lssl -lnl
Use IPVS Framework       : Yes
IPVS use libnl           : Yes
IPVS syncd attributes    : No
IPVS 64 bit stats        : No
HTTP_GET regex support   : No
fwmark socket support    : Yes
Use VRRP Framework       : Yes
Use VRRP VMAC            : Yes
Use VRRP authentication  : Yes
With track_process       : Yes
With linkbeat            : Yes
Use BFD Framework        : No
SNMP vrrp support        : No
SNMP checker support     : No
SNMP RFCv2 support       : No
SNMP RFCv3 support       : No
DBUS support             : No
Use JSON output          : No
libnl version            : 1
Use IPv4 devconf         : No
Use iptables             : No
Use nftables             : No
init type                : systemd
systemd notify           : No
Strict config checks     : No
Build documentation      : No
Default runtime options  : -D

[root@pg73 keepalived-2.2.7]#  make && make install

复制

四、配置


[root@pg74 sbin]# ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@pg74 sbin]# ln -s keepalived/etc/init.d/keepalived.rh.init /etc/rc.d/rc5.d/S99keepalived
[root@pg74 sbin]#  systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:keepalived(8)
           man:keepalived.conf(5)
           man:genhash(1)
           https://keepalived.org
[root@pg74 sbin]# cd
[root@pg74 ~]# 
[root@pg74 ~]#  systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:keepalived(8)
           man:keepalived.conf(5)
           man:genhash(1)
           https://keepalived.org
[root@pg74 ~]# whereis keepalived
keepalived: /usr/sbin/keepalived /usr/local/keepalived
[root@pg74 ~]# 
----------主
[root@pg73 keepalived]# cd /usr/local/keepalived/etc/keepalived
[root@pg73 keepalived]#  vi keepalived.conf
! Configuration File for keepalived

global_defs {
    router_id pg73
    script_user root
    enable_script_security
}

vrrp_script chk_alived {
    script "/home/postgres/pg_monitor.sh"
    interval 2
    weight 5
    fall 3
    rise 5
    timeout 2
}

vrrp_instance VI_1 {
    state MASTER
    interface enp0s3
    virtual_router_id 88
    priority 100
    advert_int 5
    authentication {
        auth_type PASS
        auth_pass postgres
    }
    virtual_ipaddress {
        192.168.3.220/24
    }
    track_script {
        chk_alived
    }
从一
[root@pg74 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
    router_id pg74
    script_user root
    enable_script_security
}

vrrp_script chk_alived {
    script "/home/postgres/pg_monitor.sh"
    interval 2
    weight 5
    fall 3
    rise 5
    timeout 2
}

vrrp_instance VI_1 {
    state MASTER
    interface enp0s3
    virtual_router_id 88
    priority 98
    advert_int 5
    authentication {
        auth_type PASS
        auth_pass postgres
    }
    virtual_ipaddress {
        192.168.3.220/24
    }
    track_script {
        chk_alived
    }
}

从二
[root@pg75 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
    router_id pg75
    script_user root
    enable_script_security
}

vrrp_script chk_alived {
    script "/home/postgres/pg_monitor.sh"
    interval 2
    weight 5
    fall 3
    rise 5
    timeout 2
}

vrrp_instance VI_1 {
    state MASTER
    interface enp0s3
    virtual_router_id 88
    priority 96
    advert_int 5
    authentication {
        auth_type PASS
        auth_pass postgres
    }
    virtual_ipaddress {
        192.168.3.220/24
    }
    track_script {
        chk_alived
    }
}

复制

五、检查

[root@pg73 keepalived]# ip -4 a show enp0s3
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 192.168.3.73/24 brd 192.168.3.255 scope global noprefixroute enp0s3
       valid_lft forever preferred_lft forever
[root@pg73 keepalived]# ip show enp0s3
Object "show" is unknown, try "ip help".
[root@pg73 keepalived]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2022-03-23 15:14:45 CST; 1min 50s ago
     Docs: man:keepalived(8)
           man:keepalived.conf(5)
           man:genhash(1)
           https://keepalived.org
  Process: 13284 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 13285 (keepalived)
    Tasks: 2
   CGroup: /system.slice/keepalived.service
           ├─13285 /usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived/keepalived.conf -D
           └─13286 /usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived/keepalived.conf -D

Mar 23 15:15:01 pg73 Keepalived_vrrp[13286]: Sending gratuitous ARP on enp0s3 for 192.168.3.220
Mar 23 15:15:01 pg73 Keepalived_vrrp[13286]: Sending gratuitous ARP on enp0s3 for 192.168.3.220
Mar 23 15:15:01 pg73 Keepalived_vrrp[13286]: Sending gratuitous ARP on enp0s3 for 192.168.3.220
Mar 23 15:15:01 pg73 Keepalived_vrrp[13286]: Sending gratuitous ARP on enp0s3 for 192.168.3.220
Mar 23 15:15:06 pg73 Keepalived_vrrp[13286]: (VI_1) Sending/queueing gratuitous ARPs on enp0s3 for 192.168.3.220
Mar 23 15:15:06 pg73 Keepalived_vrrp[13286]: Sending gratuitous ARP on enp0s3 for 192.168.3.220
Mar 23 15:15:06 pg73 Keepalived_vrrp[13286]: Sending gratuitous ARP on enp0s3 for 192.168.3.220
Mar 23 15:15:06 pg73 Keepalived_vrrp[13286]: Sending gratuitous ARP on enp0s3 for 192.168.3.220
Mar 23 15:15:06 pg73 Keepalived_vrrp[13286]: Sending gratuitous ARP on enp0s3 for 192.168.3.220
Mar 23 15:15:06 pg73 Keepalived_vrrp[13286]: Sending gratuitous ARP on enp0s3 for 192.168.3.220



[root@pg75 ~]#  psql "host=192.168.3.220 port=5000 user=repmgr dbname=repmgr password=repmgr" -c 'select inet_server_addr(),pg_is_in_recovery()'
 inet_server_addr | pg_is_in_recovery 
------------------+-------------------
 192.168.3.73     | f
(1 row)

You have new mail in /var/spool/mail/root
[root@pg75 ~]#  psql "host=192.168.3.220 port=5001 user=repmgr dbname=repmgr password=repmgr" -c 'select inet_server_addr(),pg_is_in_recovery()'
 inet_server_addr | pg_is_in_recovery 
------------------+-------------------
 192.168.3.75     | t
(1 row)

[root@pg75 ~]#  psql "host=192.168.3.220 port=5001 user=repmgr dbname=repmgr password=repmgr" -c 'select inet_server_addr(),pg_is_in_recovery()'
 inet_server_addr | pg_is_in_recovery 
------------------+-------------------
 192.168.3.74     | t
(1 row)

[root@pg75 ~]#  psql "host=192.168.3.220 port=5001 user=repmgr dbname=repmgr password=repmgr" -c 'select inet_server_addr(),pg_is_in_recovery()'
 inet_server_addr | pg_is_in_recovery 
------------------+-------------------
 192.168.3.75     | t
(1 row)

[root@pg75 ~]#  psql "host=192.168.3.220 port=5001 user=repmgr dbname=repmgr password=repmgr" -c 'select inet_server_addr(),pg_is_in_recovery()'
 inet_server_addr | pg_is_in_recovery 
------------------+-------------------
 192.168.3.74     | t
(1 row)


复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
5人已赞赏
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论

墨天轮福利君
暂无图片
3年前
评论
暂无图片 0
您好,您的文章已入选墨力原创作者计划合格奖,10墨值奖励已经到账请查收! ❤️我们还会实时派发您的流量收益。
3年前
暂无图片 点赞
评论