Radditmq

dd dd0 dd1 mq集群
dd2 dd3 haproxy+keepalived

#配erl环境
./configure --prefix=/home/erlang && make && make install
vi /etc/profile
export PATH 加入:/home/erlang/bin
source /etc/profile
erl

#配rabbitmq
rabbitmq-server 启动出错 vi /etc/hosts(删除其他加的host,留127.0.0.1)
rpm -ivh --nodeps rabbitmq-server-3.7.10-1.el7.noarch.rpm
rabbitmq-plugins enable rabbitmq_management
rabbitmq-server
#查看
localhost:15672

rabbitmqctl add_user root root
rabbitmqctl set_user_tags root administrator
rabbitmqctl set_permissions -p / root '.*' '.*' '.*'
192.168.55.138:15672

rabbitmq-server -detached
rabbitmqctl stop
rabbitmqctl cluster_status

#rabbitmq分布式集群架构
dd dd0 dd1
#dd
scp /var/lib/rabbitmq/.erlang.cookie dd0:/var/lib/rabbitmq
scp /var/lib/rabbitmq/.erlang.cookie dd1:/var/lib/rabbitmq
dd0 dd1 reboot
#dd0加入dd #rabbitmqctl reset
rabbitmqctl stop_app
rabbitmqctl join_cluster rabbit@dd
rabbitmqctl start_app
#dd1加入dd
rabbitmqctl stop_app
rabbitmqctl join_cluster rabbit@dd
rabbitmqctl start_app
#dd查看集群
rabbitmqctl cluster_status
#dd
移除节点
rabbitmqctl stop_app
rabbitmqctl forget_cluster_node rabbit@dd0
rabbitmqctl cluster_status
#dd0 dd1
退出集群
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
rabbitmqctl cluster_status
--------------------------------------------------------------------------------
集群问题: thinks it's clustered with node rabbit@XXX
解决问题:所有节点
#rabbitmqctl stop
rm -fr /var/lib/rabbitmq/mnesia
rabbitmq-server -detached
rabbitmqctl cluster_status
--------------------------------------------------------------------------------
tar haproxy
make TARGET=linux26
make install
mkdir /etc/haproxy
groupadd haproxy
useradd -s /sbin/nologin -M -g haproxy haproxy
--------------------------------------------------------------------------------
dd1 dd2配置haproxy+keepalived

vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0 info
maxconn 8192
user haproxy
group haproxy
defaults
timeout connect 3500
timeout queue 11000
timeout tarpit 12000
timeout client 30000
timeout http-request 40000
timeout http-keep-alive 5000
timeout server 40000
timeout check 7000
option contstats
option log-health-checks
##监控查看本地状态#####
listen admin_stats
bind 0.0.0.0:9188
mode http
log 127.0.0.1 local0 err
stats refresh 30s
stats uri /haproxy
stats realm welcome login\ Haproxy
stats auth admin:admin
stats hide-version
stats admin if TRUE
###反代监控1前后分开
frontend server
bind *:5672
log global
mode tcp
#option forwardfor
default_backend rabbitmq
maxconn 3
backend rabbitmq
mode tcp
log global
balance roundrobin
server dd 192.168.55.138:5672 check inter 5s rise 2 fall 3
server dd0 192.168.55.139:5672 check inter 5s rise 2 fall 3
server dd1 192.168.55.140:5672 check inter 5s rise 2 fall 3
--------------------------------------------------------------------------------
###反代监控2前后结合
listen rabbitmq
bind 192.168.55.55:5673
balance roundrobin
mode tcp
option tcplog
option tcpka
bind-process 7
timeout client 15s
timeout connect 3s
timeout server 15s
server dd 192.168.55.138:5672 check inter 5s rise 2 fall 3
server dd0 192.168.55.139:5672 check inter 5s rise 2 fall 3
server dd1 192.168.55.140:5672 check inter 5s rise 2 fall 3
--------------------------------------------------------------------------------
参考:

Global settings
#---------------------------------------------------------------------
global
log 127.0.0.1 local0
chroot /var/lib/haproxy # 改变当前工作目录
pidfile /var/run/haproxy.pid # haproxy的pid存放路径,启动进程的用户必须有权限访问此文件
maxconn 4000 # 最大连接数,默认4000
user haproxy # 默认用户
group haproxy # 默认组
daemon # 创建1个进程进入deamon模式运行。此参数要求将运行模式设置为daemon
stats socket /var/lib/haproxy/stats # 创建监控所用的套接字目录
#---------------------------------------------------------------------
# defaults settings
#---------------------------------------------------------------------
# 注意:因为要使用tcp的负载,屏蔽掉与http相关的默认配置
defaults
mode http # 默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
log global
option dontlognull # 启用该项,日志中将不会记录空连接。所谓空连接就是在上游的负载均衡器
option redispatch # serverId对应的服务器挂掉后,强制定向到其他健康的服务器
retries 3 # 3次连接失败就认为服务不可用,也可以通过后面设置
timeout queue 1m
timeout connect 10s # 连接超时时间
timeout client 1m # 客户端连接超时时间
timeout server 1m # 服务器端连接超时时间
timeout check 10s
maxconn 3000 # 最大连接数
#---------------------------------------------------------------------
listen admin_stats #开启haproxy监控界面
bind *:80 #绑定80端口
mode http
option httplog
stats enable #开启统计
stats refresh 30s
stats uri /haproxy?stats #监控界面url为:http://ip:80/haproxy/stats
stats auth admin:admin
stats realm welcome\ Haproxy
stats admin if TRUE
#---------------------------------------------------------------------
listen rabbitmq_admin #代理rabbitmq集群的管理界面
bind *:15672 #绑定15672端口
mode http
option httplog
server dd 192.168.55.138:15672
server dd0 192.168.55.138:15672
server dd1 192.168.55.138:15672
#---------------------------------------------------------------------
listen rabbitmq_cluster #代理rabbitmq集群
bind *:5672 #绑定5672端口
mode tcp
option tcplog
balance roundrobin
server rmq-n1 10.200.37.201:5672 check inter 5000 rise 2 fall 2
server rmq-n2 10.200.37.202:5672 check inter 5000 rise 2 fall 2
server rmq-n3 10.200.37.203:5672 check inter 5000 rise 2 fall 2
--------------------------------------------------------------------------------
vi /etc/init.d/haproxy
#!/bin/sh
#
# haproxy
#
# chkconfig: - 85 15
# description: HAProxy is a free, very fast and reliable solution \
# offering high availability, load balancing, and \
# proxying for TCP and HTTP-based applications
# processname: haproxy
# config: /etc/haproxy/haproxy.cfg
# pidfile: /var/run/haproxy.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
exec="/usr/local/sbin/haproxy"
prog=$(basename $exec)
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
cfgfile=/etc/haproxy/haproxy.cfg
pidfile=/var/run/haproxy.pid
lockfile=/var/lock/subsys/haproxy
check() {
$exec -c -V -f $cfgfile $OPTIONS
}
start() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Starting $prog: "
# start it up here, usually something like "daemon $exec"
daemon $exec -D -f $cfgfile -p $pidfile $OPTIONS
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
# stop it here, often "killproc $prog"
killproc $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
stop
start
}
reload() {
$exec -c -q -f $cfgfile $OPTIONS
if [ $? -ne 0 ]; then
echo "Errors in configuration file, check with $prog check."
return 1
fi
echo -n $"Reloading $prog: "
$exec -D -f $cfgfile -p $pidfile $OPTIONS -sf $(cat $pidfile)
retval=$?
echo
return $retval
}
force_reload() {
restart
}
fdr_status() {
status $prog
}
case "$1" in
start|stop|restart|reload)
$1
;;
force-reload)
force_reload
;;
check)
check
;;
status)
fdr_status
;;
condrestart|try-restart)
[ ! -f $lockfile ] || restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}"
exit 2
esac

chmod 777 /etc/init.d/haproxy
service haproxy start

http://192.168.55.130:9188/haproxy

chkconfig --add haproxy
chkconfig --level 2345 haproxy on
--------------------------------------------------------------------------------
vi /etc/keepalived/keepalived.conf
主备的配置文件有三点不同
router_id不一样
优先级不一样
state主从不一样
其他都一样

vim /etc/keepalived/keepalived.conf
#Keepalived配置文件
global_defs {
router_id NodeA #路由ID, 主备的ID不能相同
}
vrrp_instance VI_1 {
state MASTER #Keepalived的角色。Master表示主服务器,从服务器设置为BACKUP
interface ens33 #指定监测网卡
virtual_router_id 35
priority 100 #优先级,BACKUP机器上的优先级要小于这个值
advert_int 1 #设置主备之间的检查时间,单位为s
authentication { #定义验证类型和密码
auth_type PASS
auth_pass Pass1234
}
virtual_ipaddress { #VIP地址,可以设置多个:
192.168.55.55
}
}
--------------------------------------------------------------------------------

启动 HAproxy 时可能会出现 cannot bind socket 的异常,这是因为 HAproxy 配置中使用了 VIP,但此时还没有启动 Keepalived,那么就还没有 VIP 绑定。

这时需要在 /etc/sysctl.conf 文件中配置如下内容:

net.ipv4.ip_nonlocal_bind = 1 # 意思是启动haproxy的时候,允许忽视VIP的存在
net.ipv4.ip_forward = 1 # 打开内核的转发功能

posted @ 2019-02-18 13:39  UnlimitedBlade  阅读(314)  评论(0)    收藏  举报