Ansible部署Rabbitmq集群
# Ansible部署Rabbitmq集群
节点 | IP | 主机名 |
---|---|---|
ansible | 172.25.253.137 | ansible |
node1 | 172.25.253.138 | node1 |
node2 | 172.25.253.139 | node2 |
node3 | 172.25.253.140 | node3 |
# 查看当前文件树
[root@ansible ansible]# tree rabbitmq/
rabbitmq/
├── hosts
├── local.repo
├── playbook.yaml
├── rabbitmq.sh
└── scp.sh
# 查看ansible主机清单
[root@ansible rabbitmq]# cat /etc/ansible/hosts
[server]
172.25.253.138 hostname=node1
172.25.253.140 hostname=node3
172.25.253.139 hostname=node2
# ansible部署rabbitmq集群
- 使用playbook编写一键部署集群
- 配置好所有机器的ssh免密
- 更改好hosts配置文件、local.repo、以及编写两个脚本
- 编写playbook
# 1.配置hosts文件
- 配置三个节点的映射以及解析主机名
[root@ansible rabbitmq]# cat hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.253.138 node1
172.25.253.139 node2
172.25.253.140 node3
# 2.配置yum源
- 可以使用file本地yum源也可以使用http源
- 这里为了方便使用http源
[root@ansible rabbitmq]# cat local.repo
[rabbitmq-repo]
name=rabbitmq-repo
baseurl=http://172.25.253.144/file/rabbitmq-repo/
gpgcheck=0
# 3.编写scp.sh
- scp传送文件的时候用主机名会需要输入密码 ip地址不会显示输入密码
[root@ansible rabbitmq]# cat scp.sh
#!/bin/bash
scp /var/lib/rabbitmq/.erlang.cookie root@172.25.253.139:/var/lib/rabbitmq/.erlang.cookie
scp /var/lib/rabbitmq/.erlang.cookie root@172.25.253.140:/var/lib/rabbitmq/.erlang.cookie
# 4.编写rabbitmq.sh
- 使用shell脚本自动化加入集群
- 停止集群状态
- 加入磁盘节点node1
- 启动集群状态
[root@ansible rabbitmq]# cat rabbitmq.sh
#!/bin/bash
rabbitmqctl stop_app
rabbitmqctl join_cluster --ram rabbit@node1
rabbitmqctl start_app
# 5.编写playbook
- 使用copy模块传输文件
- 使用yum和service模块安装和开启服务的状态
- 使用script模块运行脚本
- 标记好更改配置文件用notify,使用handlers重启服务 利用一个item的循环
[root@ansible rabbitmq]# cat playbook.yaml
- name: first tasks
hosts: server
remote_user: root
tasks:
- name: copy hosts
copy: src=hosts dest=/etc/hosts
- name: delete repo
shell: rm -rf /etc/yum.repos.d/*
- name: copy repo
copy: src=local.repo dest=/etc/yum.repos.d/local.repo
- name: Install packages
yum: name=rabbitmq-server state=present
- name: start rabbitmq
service: name=rabbitmq-server state=started
- name: run sh
script: scp.sh
when: ansible_fqdn == "node1"
- name: Install UI
shell: rabbitmq-plugins enable rabbitmq_management
notify: restart service3
handlers:
- name: restart service3
service: name=rabbitmq-server state=restarted
- name: two tasks
hosts: server
remote_user: root
tasks:
- name: run rabbitmq.sh
script: rabbitmq.sh
when: ansible_fqdn == {{ "item" }}
with_items:
- node2
- node3
# 6.执行脚本
[root@ansible rabbitmq]# ansible-playbook playbook.yaml
PLAY [first tasks] ******************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************
ok: [172.25.253.138]
ok: [172.25.253.139]
ok: [172.25.253.140]
TASK [copy hosts] *******************************************************************************************************************************
ok: [172.25.253.138]
ok: [172.25.253.140]
ok: [172.25.253.139]
·
·
·
PLAY RECAP **************************************************************************************************************************************
172.25.253.138 : ok=10 changed=5 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.25.253.139 : ok=10 changed=5 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
172.25.253.140 : ok=10 changed=5 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
# 7.检查集群状态
[root@ansible rabbitmq]# ansible server -m shell -a "rabbitmqctl cluster_status"
172.25.253.138 | CHANGED | rc=0 >>
Cluster status of node rabbit@node1 ...
[{nodes,[{disc,[rabbit@node1]},{ram,[rabbit@node3,rabbit@node2]}]},
{running_nodes,[rabbit@node2,rabbit@node3,rabbit@node1]},
{cluster_name,<<"rabbit@node1">>},
{partitions,[]}]
...done.
172.25.253.140 | CHANGED | rc=0 >>
Cluster status of node rabbit@node3 ...
[{nodes,[{disc,[rabbit@node1]},{ram,[rabbit@node3,rabbit@node2]}]},
{running_nodes,[rabbit@node1,rabbit@node2,rabbit@node3]},
{cluster_name,<<"rabbit@node1">>},
{partitions,[]}]
...done.
172.25.253.139 | CHANGED | rc=0 >>
Cluster status of node rabbit@node2 ...
[{nodes,[{disc,[rabbit@node1]},{ram,[rabbit@node3,rabbit@node2]}]},
{running_nodes,[rabbit@node1,rabbit@node3,rabbit@node2]},
{cluster_name,<<"rabbit@node1">>},
{partitions,[]}]
...done.
# 8.记录Rabbitmq集群的更新:
这里我使用的是fetch和file模块对rabbitmq集群的密钥进行传输
[root@ansible rabbit]# vim rabbit.yaml
- hosts: rabbit
remote_user: root
tasks:
- name: rm repofile
shell: rm -rf /etc/yum.repos.d/*
- name: copy repofile
copy:
src: local.repo
dest: /etc/yum.repos.d/
backup: true
- name: tar repo.tar
unarchive:
src: rabbitmq-repo.tar.gz
dest: /opt/
remote_src: no
- name: install rabbitmq
yum:
name: rabbitmq-server
state: latest
- name: service
service:
name: rabbitmq-server
state: started
- name: fetch
fetch:
dest: /root/rabbit/
src: /var/lib/rabbitmq/.erlang.cookie
flat: yes
when: ansible_fqdn == "node-1"
- name: copy erlang.conf
copy:
src: /root/rabbit/.erlang.cookie
dest: /var/lib/rabbitmq/.erlang.cookie
when: ansible_fqdn == {{ "item" }}
with_items:
- node-2
- node-3
- name: chmod
file:
path: /var/lib/rabbitmq/.erlang.cookie
owner: rabbitmq
group: rabbitmq
上次更新: 2023/11/28, 22:03:59