Rabbitmq镜像模式+HA+LB
拓扑图:
一、搭建rabbitmq集群
准备:三台机器
ip | hostname | 版本 |
---|---|---|
192.168.166.130 | rabbitmq1 | Centos7.4 |
192.168.166.134 | rabbitmq2 | Centos7.4 |
192.168.166.135 | rabbitmq3 | Centos7.4 |
防火墙 selinux 关闭 配置好本地解析
ANSIBLE 搭配 SHELL脚本部署 RABBITMQ集群
脚本:
#!/bin/bash
user=root
pass=123
account(){
rabbitmqctl add_user admin admin
rabbitmqctl set_user_tags admin administrator
rabbitmqctl add_user user_monitoring passwd_monitor
rabbitmqctl set_user_tags user_monitoring monitoring
rabbitmqctl set_user_tags user_monitoring management
rabbitmqctl list_users
}
check(){
if [ $? -eq 0 ];then
echo "succesful"
else
echo "failed"
exit 9
fi
}
echo "即将安装ansible"
yum install -y ansible &> /dev/null
check && echo " Successful!!! "
sed -ri '/^#host_key/c\host_key_checking=false/' /etc/ansible/ansible.cfg
read -p "请输入节点数量: " num
echo "[rabbitmqs]" >> /etc/ansible/hosts
for i in `seq 1 $num`
do
read -p "请输入第 $i 个的ip: " ip
echo "ip$i $ip ansible_ssh_user='"$user"' ansible_ssh_pass='"$pass"'" >> /root/ip.txt
get=`grep "ip$i" /root/ip.txt |awk '{print $2,$3,$4}'`
echo "$get hostname=rabbitmq$i" >> /etc/ansible/hosts
hosts=`awk -v i=$i 'NR==i{print $2}' /root/ip.txt`
arr[$i]=$hosts
echo ${arr[$i]} " rabbitmq$i" >> /etc/hosts
done
echo "[rabbitmq23]" >> /etc/ansible/hosts
for i in `seq 2 $num`
do
ll=`awk -v i=$i 'NR==i{print $2}' ./ip.txt`
echo $ll >> /etc/ansible/hosts
done
grep "rabbitmq*" /etc/hosts > /root/hosts.txt
yum install -y expect
echo "开始生成密钥"
sleep 1
echo "正在生成密钥"
rm -rf ~/.ssh/{known_hosts,id_rsa*}
ssh-keygen -f ~/.ssh/id_rsa -t rsa -N '' &>/dev/null
if [ $? -eq 0 ];then
echo "密钥生成成功"
else
echo "密钥生成失败"
fi
ansible-playbook /root/rabbitmq01.yaml
for i in `seq 1 $num`
do
ip=`awk -v i=$i 'NR==i{print $2}' /root/ip.txt`
echo "[rabbitmq$i]" >> /etc/ansible/hosts
echo $ip >> /etc/ansible/hosts
done
echo "即将设置本地解析和主机名,请等待"
for i in `seq 1 $num`
do
#ip=`awk -v i=$i 'NR==i{print $1}' /root/hosts.txt`
#ssh root@$ip "cat /root/hosts.txt >> /etc/hosts"
ansible rabbitmq$i -m hostname -a "name=rabbitmq$i"
done
check
ansible-playbook /root/rabbitmq02.yaml
check
ansible-playbook /root/rabbitmq03.yaml
check
echo "部署成功"
yaml1:
---
- name: "安装rabbitmq集群"
hosts: rabbitmqs
gather_facts: no
remote_user: root
vars:
soft:
- erlang
- socat
- rabbitmq-server
tasks:
- name: "传送公钥"
authorized_key:
user: root
key: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
- name: "安装应用"
yum:
name: "{{ item }}"
state: present
loop: "{{ soft }}"
- name: "拷贝hosts文件"
copy: src=/root/hosts.txt dest=/root/
- name: "启动应用"
systemd: name="rabbitmq-server.service" state="started"
- name: "设置开机自启"
systemd: name="rabbitmq-server.service" enabled="yes"
yaml2:
---
- name: "一般模式集群配置"
hosts: rabbitmq1
gather_facts: no
remote_user: root
tasks:
- name: "创建账号配置"
shell: rabbitmqctl add_user admin admin && rabbitmqctl set_user_tags admin administrator && rabbitmqctl add_user user_monitoring passwd_monitor && rabbitmqctl set_user_tags user_monitoring monitoring && rabbitmqctl set_user_tags user_monitoring management
- name: "插件加载"
shell: rabbitmq-plugins enable rabbitmq_management
- name: "创建用户"
shell: rabbitmqctl add_user superrd superrd && rabbitmqctl set_permissions -p / admin '.*' '.*' '.*' && rabbitmqctl set_permissions -p / superrd ".*" ".*" ".*" && rabbitmqctl set_user_tags superrd administrator
- name: "修改权限"
shell: chmod 600 /var/lib/rabbitmq/.erlang.cookie
yaml3:(有偷懒,自动化的话请在shell脚本里ssh<重定向指定脚本来执行命令,因为指定的主机组需要用变量获取)
---
- name: "配置其它节点"
hosts: rabbitmq23
gather_facts: no
remote_user: root
tasks:
- name: "传送erlang.cookie"
copy: src=/var/lib/rabbitmq/.erlang.cookie dest=/var/lib/rabbitmq/
- name: "重启服务"
systemd: name=rabbitmq-server state=restarted
- name: "Rabbitmq集群添加节点"
shell: rabbitmqctl stop_app && rabbitmqctl join_cluster --ram rabbit@rabbitmq1 && rabbitmqctl start_app
- name: "等待添加"
shell: rabbitmq-plugins enable rabbitmq_management && systemctl restart rabbitmq-server.service
运行中的部分状态:
查看集群:
二、keepalived部署
1.环境
- 两台虚拟机或者选择集群中的任意两个节点配置
- keepalived1:192.168.166.131
- keepalived2:192.168.166.132
- VIP地址:192.168.166.160
2.安装keepalived
3、修改配置文件
1、keepalived1 配置
[root@localhost log]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id directory1
}
vrrp_script check_run {
script "/etc/keepalived/keepalived_check_haproxy.sh"
interval 5
}
vrrp_instance VI_1 {
state MASTER
nopreempt
interface ens33
virtual_router_id 80
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_run
}
virtual_ipaddress {
192.168.166.160/24
}
}
1234567891011121314151617181920212223242526272829
2、keepalived2 配置
[root@localhost log]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id directory2
}
vrrp_script check_run {
script "/etc/keepalived/keepalived_check_haproxy.sh"
interval 5
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface ens33
virtual_router_id 80
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_run
}
virtual_ipaddress {
192.168.166.160/24
}
}
1234567891011121314151617181920212223242526272829
3、健康检测脚本 haproxy_chk.sh
[root@localhost keepalived]# cat keepalived_check_haproxy.sh
#!/bin/bash
systemctl status haproxy &>/dev/null
if [ $? -ne 0 ];then
/usr/bin/systemctl stop keepalived
fi
1234567
4、开启服务验证是VIP
可以看到vip在192.168.166131keepalived上
而192.168.166.132上没有vip
三、部署haproxy
1、安装 haproxy 主192.168.166.131 备192.168.166.132
主:
备:
2、创建 haproxy.conf
主:
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
defaults
mode http
log global
option dontlognull
retries 3
maxconn 3000
contimeout 50000
clitimeout 50000
srvtimeout 50000
listen stats
bind *:8888
stats enable
stats hide-version
stats uri /haproxy
stats realm Haproxy\ stats
stats auth admin:admin
stats admin if TRUE
frontend web
option httplog
option http-server-close
option forwardfor except 127.0.0.0/8
mode http
bind *:5672
default_backend rabbitmqs
backend rabbitmqs
balance roundrobin
server rabbitmq1 192.168.166.130:5672 weight 1 check maxconn 2000
server rabbitmq2 192.168.166.134:5672 weight 1 check maxconn 2000
server rabbitmq3 192.168.166.135:5672 weight 1 check maxconn 2000
备:
[root@localhost log]# cat /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
defaults
mode http
log global
option dontlognull
retries 3
maxconn 3000
contimeout 50000
clitimeout 50000
srvtimeout 50000
listen stats
bind *:8888
stats enable
stats hide-version
stats uri /haproxy
stats realm Haproxy\ stats
stats auth admin:admin
stats admin if TRUE
frontend web
option httplog
option http-server-close
option forwardfor except 127.0.0.0/8
mode tcp
bind *:5672
default_backend rabbitmqs
backend rabbitmqs
balance roundrobin
server rabbitmq1 192.168.166.130:5672 weight 1 check maxconn 2000
server rabbitmq2 192.168.166.134:5672 weight 1 check maxconn 2000
server rabbitmq3 192.168.166.135:5672 weight 1 check maxconn 2000
3、Haproxy rsyslog 日志配置
主备配置相同:
[root@localhost log]# vim /etc/rsyslog.conf
#开启接收功能
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514
#添加在这个下边#### RULES ####
local2.* /var/log/haproxy.log
#重启rsyslog服务
[root@localhost ~]# systemctl restart rsyslog
123456789101112131415161718
#可以看到我们在指定的目录下的haproxy.log日志
[root@localhost log]# ls
anaconda boot.log chrony dmesg firewalld haproxy.log maillog rhsm spooler tuned yum.log
audit btmp cron dmesg.old grubby_prune_debug lastlog messages secure tallylog wtmp
#查看日志内容
[root@localhost log]# cat haproxy.log
Sep 18 17:43:07 localhost haproxy[1589]: Proxy stats started.
Sep 18 17:43:07 localhost haproxy[1589]: Proxy web started.
Sep 18 17:43:07 localhost haproxy[1589]: Proxy redisservers started.
Sep 18 17:46:37 localhost haproxy[1590]: Connect from 192.168.166.1:64428 to 192.168.166.131:8888 (stats/HTTP)
Sep 18 17:46:37 localhost haproxy[1590]: Connect from 192.168.166.1:64429 to 192.168.166.131:8888 (stats/HTTP)
Sep 18 17:47:09 localhost haproxy[1590]: Connect from 192.168.166.1:64435 to 192.168.166.131:8888 (stats/HTTP)
Sep 18 17:47:09 localhost haproxy[1590]: Connect from 192.168.166.1:64436 to 192.168.166.131:8888 (stats/HTTP)
1234567891011121314
结果:访问vip及定义端口