postgres高可用学习篇三:haproxy+keepalived实现postgres负载均衡

妖精的绣舞 提交于 2019-12-01 07:44:36

环境: CentOS Linux release 7.6.1810 (Core) 内核版本:3.10.0-957.10.1.el7.x86_64

node1:192.168.216.130

node2:192.168.216.132

node3:192.168.216.134

VIP:192.168.216.138

基础环境准备

优化内核参数cat /etc/sysctl.conf

net.netfilter.nf_conntrack_tcp_be_liberal=1
vm.swappiness=1
vm.min_free_kbytes=102400
vm.dirty_expire_centisecs=1000
vm.dirty_background_bytes=67108864
vm.dirty_bytes=536870912
vm.zone_reclaim_mode=0
kernel.numa_balancing=0
kernel.sched_migration_cost_ns=5000000
kernel.sched_autogroup_enabled=0
net.ipv4.ip_nonlocal_bind=1
net.ipv4.ip_forward=1
net.ipv4.ip_local_port_range=1024 65535
net.netfilter.nf_conntrack_max=1048576

一、安装haproxy

 

二、1、分别在node1、node2、node3节点安装keepalived

yum install -y epel-release
yum install -y keepalived

2、更改node1节点上的keepalived.conf配置文件

cat /etc/keepalived/keepalived.conf
global_defs {
   router_id ocp_vrrp
}
 
vrrp_script haproxy_check {
   script "/bin/kill -0 `cat /var/run/haproxy/haproxy.pid`"
   interval 2
   weight 2
}
 
vrrp_instance VI_1 {
   interface ens32
   virtual_router_id 133
   priority  100
   advert_int 2
   state  BACKUP
   virtual_ipaddress {
       192.168.216.138
   }
   track_script {
       haproxy_check
   }
   authentication {
      auth_type PASS
      auth_pass 1ce24b6e
   }
}

这里需要注意的是:"virtual_ipaddress"也就是所谓的vip我们设置为192.168.216.138

3、更改node2节点上的keepalived.conf配置文件

[root@pgnode02 ~]# cat /etc/keepalived/keepalived.conf 
global_defs {
   router_id ocp_vrrp
}
 
vrrp_script haproxy_check {
   script "/bin/kill -0 `cat /var/run/haproxy/haproxy.pid`"
   interval 2
   weight 2
}
 
vrrp_instance VI_1 {
   interface ens32
   virtual_router_id 133
   priority  100
   advert_int 2
   state  BACKUP
   virtual_ipaddress {
       192.168.216.138
   }
   track_script {
       haproxy_check
   }
   authentication {
      auth_type PASS
      auth_pass 1ce24b6e
   }
}

这里需要注意的是:"virtual_ipaddress"也就是所谓的vip我们设置为192.168.216.138

4、更改node3节点上的keepalived.conf配置文件

[root@localhost keepalived]# cat /etc/keepalived/keepalived.conf 
global_defs {
   router_id ocp_vrrp
}
 
vrrp_script haproxy_check {
   script "/bin/kill -0 `cat /var/run/haproxy/haproxy.pid`"
   interval 2
   weight 2
}
 
vrrp_instance VI_1 {
   interface ens32
   virtual_router_id 133
   priority  100
   advert_int 2
   state  BACKUP
   virtual_ipaddress {
       192.168.216.138
   }
   track_script {
       haproxy_check
   }
   authentication {
      auth_type PASS
      auth_pass 1ce24b6e
   }
}

这里需要注意的是:"virtual_ipaddress"也就是所谓的vip我们设置为192.168.216.138

 5、依次在node1、node2、node3上启动keepalived

systemctl start keepalived

 验证node1、node2、node3上keepalived启动状态

 

node2 :

node3:

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!