Centos7 offline install RabbitMQ, and configure a cluster

Centos7 offline install RabbitMQ, and configure a cluster

Disclaimer: This article is a blogger original article, follow the  CC 4.0 by-sa  copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Alger_magic/article/details/82868267

Objective: Centos7 offline install RabbitMQ, and configure a cluster

surroundings:

系统:Centos 7.5
环境:
  • 1
  • 2

First, install RabbitMQ:

1. dependent download and installation package, Rabbitmq two installation depends rpm, erlang and socat

  1. Download erlang, select the corresponding version of the page in https://packagecloud.io/rabbitmq/erlang erlang the rpm installation package (centos7 requires 19.3 or later.
    Here Insert Picture Description

  2. Download socat, in http://www.rpmfind.net/linux/rpm2html/search.php?query=socat(x86-64) version of the page to download centos7 socat the rpm installation package.
    Here Insert Picture Description

  3. Download Rabbitmq, in http://www.rabbitmq.com/install-rpm.html page to download the installation package version centos7
    Here Insert Picture Description

ps: you can also directly download resources https://download.csdn.net/download/alger_magic/10716820

2. rpm installation

  1. Rpm files will be downloaded to the copy into centos7, placed into a custom folder.

  2. Use the rpm command to install, you can use yum install * .rpm installation.
    RPM -ivh Erlang-19.3.6.11-2.el6.x86_64.rpm the sudo
    the sudo RPM -ivh socat-1.7.3.2-2.el7.x86_64.rpm
    the sudo RPM -ivh RabbitMQ-Server-3.7.8-1.el7. noarch.rpm

    ps:
    using windows centos7, use the mouse directly drag rpm of a virtual machine installed copy into centos, during installation may be prompted to packet errors (copy guess is that the process may be missing).
    The solution is to use winscp upload or past use u disk copy.
    Here Insert Picture Description

3. start rabbitmq

  1. Performing sudo service rabbitmq-server start
    prompt indicates a successful start shown in FIG.
    Here Insert Picture Description

  2. Configuration management rabbitmq account.
    Run rabbitmqctl add_user admin admin, set the account password is admin admin
    Here Insert Picture Description

  3. Run rabbitmqctl set_user_tags admin administrator, administrator privileges to set admin
    Here Insert Picture Description

  4. Run rabbitmq-plugins enable rabbitmq_management, open rabbitmq web management.
    Here Insert Picture Description

  5. Management links for: HTTP: // localhost: 15672 , login account password is set admin admin
    Here Insert Picture Description

  6. 登陆管理端,并打开访问权限。
    a.登陆后台
    Use the user login background
    b.查看用户权限,默认状态下权限是不允许访问(此时程序访问5672端口是连接被拒绝)。View user permissions, default permissions are not allowed to access
    c.点击用户名,进入用户页面,直接点击设置权限。此时刷新页面回到Users页面,权限变成可访问。
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
    PS:常见问题
    一. http://ip:15672不能访问,确认两点:1. 添加用户、给用户设置管理员权限、rabbitmq-plugins这三步是否执行成功。2. 使用firewall打开5672/15672端口。具体步骤如下:

sudo service firewalld start
firewall-cmd --zone=public --add-port=5672/tcp --permanent
firewall-cmd --zone=public --add-port=15672/tcp --permanent
sudo service firewalld start(如果系统不要求开启防火墙,可以在设置完以后再关闭它)
备注:即使防火墙处于关闭状态,也应该先打开端口再关闭,否则在有些机器上会仍然端口不通。

 

二、RabbitMQ集群部署:

1.按照前面的方式在想要集群的多台主机上安装好RabbitMQ实例

2.集群配置

1.更改主机/etc/hosts,加入主机节点。 如下所示三台主机均在/etc/hosts中添加,然后三台主机相互之间可以ping通rabbit-node

vim /etc/hosts
添加内容:
192.168.1.201 rabbit-node1
192.168.1.202 rabbit-node2
192.168.1.203 rabbit-node3

2. 由于RabbitMQ集群的主机需要使用同一个erlang.cookie。所以需要将多台主机的erlang.cookie设置为同一个。

  1. erlang.cookie位置: 如果是使用rpm包安装的rabbitmq, erlang.cookie的位置在/var/lib/rabbitmq/.erlang.cookie;如果是源码编译安装,erlang.cookie文件位置在~/.erlang.cookie

  2. 例如上面的三台主机操作流程如下:如果以rabbit-node1的erlang.cookie作为集群的erlang.cookie.

1. 在node1的主机上 chmod 777 /var/lib/rabbitmq/.erlang.cookie.(改变只读权限,让node2,node3可以获取)
2. 在node2主机上执行:
    chmod 777 /var/lib/rabbitmq/.erlang.cookie   
    scp -r rabbitmq-node1:/var/lib/rabbitmq/.erlang.cookie /var/lib/rabbitmq/.erlang.cookie
    输入yes
    输入node1的密码
    提示: .erlang.cookie  100%  20,即执行成功
3. 同样在node3主机上执行:
    chmod 777 /var/lib/rabbitmq/.erlang.cookie   
    scp -r rabbitmq-node1:/var/lib/rabbitmq/.erlang.cookie /var/lib/rabbitmq/.erlang.cookie
    输入yes
    输入node1的密码
    提示: .erlang.cookie  100%  20,即执行成功
 4. 在三台主机上使用cat /var/lib/rabbitmq/.erlang.cookie查看erlang.cookie是否统一,如果已统一,再分别执行chmod 400 /var/lib/rabbitmq/.erlang.cookie,恢复原本权限

 

3. 配置集群

  1. 在三台主机上停止当前Rabbitmq-server, 分别执行service rabbitmq-server stop,如有问题,可以使用ps查看rabbitmq进程号,在kill -9 杀掉。
  2. 然后执行service rabbitmq-server start 开启服务.
  3. 分别在三台主机上执行rabbitmqctl cluster_status查看集群状况,此时只有本机的记录。打印输出如下:
Cluster status of node rabbit@ rabbit-node ...
[{nodes,[{disc,[rabbit@ rabbit-node]}]},
 {running_nodes,[rabbit@ rabbit-node]},
 {cluster_name,<<"rabbit@ rabbit-node">>},
 {partitions,[]},
 {alarms,[{rabbit@ rabbit-node,[]}]}] 
  1. 将 rabbit-node1, rabbit-node2, rabbit-node3组成集群。
1.在 rabbit-node2上执行:
    rabbitmqctl stop_app
    rabbitmqctl join_cluster rabbit@rabbit-node1
    rabbitmqctl start_app

2.同样在 rabbit-node3上执行:
	rabbitmqctl stop_app
	rabbitmqctl join_cluster rabbit@rabbit-node1
	rabbitmqctl start_app
3.集群就建立完成了(rabbit-node2, rabbit-node3之间会自动建立连接)。
4.在三台主机上分别执行rabbitmqctl cluster_status查看集群状态,结果如下:
	Cluster status of node rabbit@rabbit-node1 ...
	[{nodes,[{disc,['rabbit@rabbit-node1','rabbit-node2',
              	  'rabbit@rabbit-node3']}]},
	 {running_nodes,['rabbit@rabbit-node1','rabbit@rabbit-node2',
                	 'rabbit@rabbit-node3']},
	 {cluster_name,<<"rabbit@rmq-cluster-1">>},
	 {partitions,[]},
	 {alarms,[{'rabbit@rabbit-node1',[]},
	          {'rabbit@rabbit-node2',[]},
	          {'rabbit@rabbit-node3',[]}]}]
表示集群创建成功,从任意一个节点的rabbitmq web管理端也可以看到各个节点状态

如图:
Here Insert Picture Description

三、为RabbitMQ安装配置负载均衡器HAProxy:

1.下载安装

1.下载地址http://www.rpmfind.net/linux/centos/7.5.1804/os/x86_64/Packages/haproxy-1.5.18-7.el7.x86_64.rpm
2. 在集群以外的一台主机上安装HAProxy(如主机不足也可以安装在其中一台,示例安装在rabbit-node1), rpm -ivh haproxy-1.5.18-7.el7.x86_64.rpm.

2. 配置负载均衡

1. HAProxy程序的配置文件默认在/etc/haproxy/haproxy.cfg
2. vim /etc/haproxy/haproxy.cfg,在末尾添加,如已有负载均衡配置,注释掉旧的配置

####################################################################
listen http_front
        bind 0.0.0.0:8100           #监听端口  
        stats refresh 30s           #统计页面自动刷新时间  
        stats uri /haproxy?stats            #统计页面url  
        stats realm Haproxy Manager #统计页面密码框上提示文本  
        stats auth admin:admin      #统计页面用户名和密码设置  
        #stats hide-version         #隐藏统计页面上HAProxy的版本信息

#####################我把RabbitMQ的管理界面也放在HAProxy后面了###############################
listen rabbitmq_admin 
    bind 0.0.0.0:8101
    server node1 192.168.1.201:15672
    server node2 192.168.1.202:15672
    server node3 192.168.1.203:15672
####################################################################
listen rabbitmq_cluster 
    bind 0.0.0.0:5671
    option tcplog
    mode tcp
    timeout client  3h
    timeout server  3h
    option          clitcpka
    balance roundrobin      #负载均衡算法(#banlance roundrobin 轮询,balance source 保存session值,支持static-rr,leastconn,first,uri等参数)
    #balance url_param userid
    #balance url_param session_id check_post 64
    #balance hdr(User-Agent)
    #balance hdr(host)
    #balance hdr(Host) use_domain_only
    #balance rdp-cookie
    #balance leastconn
    #balance source //ip
    server   node1 192.168.1.201:5672 check inter 5s rise 2 fall 3   #check inter 2000 是检测心跳频率,rise 2是2次正确认为服务器可用,fall 3是3次失败认为服务器不可用
    server   node2 192.168.1.202:5672 check inter 5s rise 2 fall 3
    server   node3 192.168.1.203:5672 check inter 5s rise 2 fall 3

 

3. 重启HAProxy, sudo service haproxy restart
4. 在浏览器输入http://rabbit-node1:8100/haproxy?stats, 查看HAProxy状态,如下图
Here Insert Picture Description

5. 安装已完成,可使用HAProxy配置的端口(示例中5671)进行rabbitmq操作,测试负载均衡效果

 

6、rabbitmq常用命令

    add_user        <UserName> <Password>

    delete_user    <UserName>

    change_password <UserName> <NewPassword>

    list_users

    add_vhost    <VHostPath>

    delete_vhost <VHostPath>

    list_vhostsset_permissions  [-p <VHostPath>] <UserName> <Regexp> <Regexp> <Regexp>

    clear_permissions [-p <VHostPath>] <UserName>

    list_permissions  [-p <VHostPath>]

    list_user_permissions <UserName>

    list_queues    [-p <VHostPath>] [<QueueInfoItem> ...]

    list_exchanges [-p <VHostPath>] [<ExchangeInfoItem> ...]

    list_bindings  [-p <VHostPath>]

    list_connections [<ConnectionInfoItem> ...]

Guess you like

Origin www.cnblogs.com/mayhh/p/11425532.html