docker-compose部署 rabbit
2023-06-30 本文已影响0人
coderTG
1、docker-ce安装
[root@localhost ~]# vim /etc/yum.repos.d/docker-ce.repo
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://download.docker.com/linux/centos/$releasever/$basearch/stable
enabled=1
gpgcheck=0
[root@SPHQOPENAPIMQ01 ~]# yum install docker-ce -y
2、docker-compose安装
[root@localhost ~]# yum install -y python-pip
[root@localhost ~]# pip install -U docker-compose
[root@localhost ~]# docker-compose version
docker-compose version 1.21.0, build 5920eb0
docker-py version: 3.2.1
CPython version: 2.7.5
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
3、下载rabbitmq镜像
[root@localhost ~]# docker pull rabbitmq:3.8-management
1.
4、编写docker-compose.yml
[root@localhost ~]# mkdir /data/rabbitmq
[root@localhost ~]# cd /data/rabbitmq
[root@localhost rabbitmq]# vim docker-compose.yml
version: '3'
services:
rabbitmq01:
image: rabbitmq:3.8-management
container_name: rabbitmq01
ports:
- "15673:15672"
- "5673:5672"
hostname: rabbitmq01
environment:
- RABBITMQ_ERLANG_COOKIE=rabbitcookie
volumes:
- /data/rabbitmq/rabbitmq01:/var/lib/rabbitmq
- /etc/localtime:/etc/localtime
rabbitmq02:
image: rabbitmq:3.8-management
container_name: rabbitmq02
ports:
- "15674:15672"
- "5674:5672"
hostname: rabbitmq02
environment:
- RABBITMQ_ERLANG_COOKIE=rabbitcookie
volumes:
- /data/rabbitmq/rabbitmq02:/var/lib/rabbitmq
- /etc/localtime:/etc/localtime
rabbitmq03:
image: rabbitmq:3.8-management
container_name: rabbitmq03
ports:
- "15675:15672"
- "5675:5672"
hostname: rabbitmq03
environment:
- RABBITMQ_ERLANG_COOKIE=rabbitcookie
volumes:
- /data/rabbitmq/rabbitmq03:/var/lib/rabbitmq
- /etc/localtime:/etc/localtime
5、创建docker-compose中定义的目录
[root@localhost ~]# mkdir /data/rabbitmq/{rabbitmq01,rabbitmq02,rabbitmq03}
[root@localhost ~]# mkdir /data/nginx
1.
2.
6、启动rabbitmq镜像集群
[root@localhost ~]# cd /data/rabbitmq
[root@localhost rabbitmq]# docker-compose up -d
[root@localhost rabbitmq]# vim init_rabbitmq.sh
#!/bin/bash
#reset first node
echo "Reset first rabbitmq node."
docker exec rabbitmq01 /bin/bash -c 'rabbitmqctl stop_app'
docker exec rabbitmq01 /bin/bash -c 'rabbitmqctl reset'
docker exec rabbitmq01 /bin/bash -c 'rabbitmqctl start_app'
#build cluster
echo "Starting to build rabbitmq cluster with two ram nodes."
docker exec rabbitmq02 /bin/bash -c 'rabbitmqctl stop_app'
docker exec rabbitmq02 /bin/bash -c 'rabbitmqctl reset'
docker exec rabbitmq02 /bin/bash -c 'rabbitmqctl join_cluster --ram rabbit@rabbitmq01'
docker exec rabbitmq02 /bin/bash -c 'rabbitmqctl start_app'
docker exec rabbitmq03 /bin/bash -c 'rabbitmqctl stop_app'
docker exec rabbitmq03 /bin/bash -c 'rabbitmqctl reset'
docker exec rabbitmq03 /bin/bash -c 'rabbitmqctl join_cluster --ram rabbit@rabbitmq01'
docker exec rabbitmq03 /bin/bash -c 'rabbitmqctl start_app'
#check cluster status
echo "Check cluster status:"
docker exec rabbitmq01 /bin/bash -c 'rabbitmqctl cluster_status'
docker exec rabbitmq02 /bin/bash -c 'rabbitmqctl cluster_status'
docker exec rabbitmq03 /bin/bash -c 'rabbitmqctl cluster_status'
[root@localhost rabbitmq]# chmod +x init_rabbitmq.sh
[root@localhost rabbitmq]# sh init_rabbitmq.sh #初始化镜像集群
6、配置nginx反向代理到镜像集群通信
[root@localhost ~]# cd /data/nginx
[root@localhost nginx]# vim nginx.conf
user nginx;
worker_processes 2;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 5s;
proxy_send_timeout 5s;
proxy_read_timeout 5s;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
#rabbitmq管理界面
upstream rabbitManage {
server 192.168.100.159:15673;
server 192.168.100.159:15674;
server 192.168.100.159:15675;
}
server {
listen 15672;
server_name 192.168.100.159;
location / {
proxy_pass http://rabbitManage;
index index.html index.htm;
}
}
}
# rabbitmq通信
stream{
upstream rabbitTcp{
server 192.168.100.159:5673;
server 192.168.100.159:5674;
server 192.168.100.159:5675;
}
server {
listen 5672;
proxy_pass rabbitTcp;
}
}
[root@localhost nginx]# docker run -it -d --name nginxRabbitmq -v /data/nginx/nginx.conf:/etc/nginx/nginx.conf --privileged --net=host nginx:1.20.2
7、部署portainer容器界面化管理
[root@localhost nginx]# docker run -itd -p 9000:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --name portainer docker.io/portainer/portainer
1.
访问: http://192.168.100.159:9000
8、访问rabbitmq的管理页面
访问: http://192.168.100.159:15672 默认账号密码: guest/guest