[Docker]使用Docker部署常见的中间件

image.png

本篇文章给大家介绍如何使用 Docker 部署在企业工作中常用的中间件服务。

Nginx

docker run -d \

    -p 80:80 --name nginx-server-conf \
    -v /opt/nginx-server-conf:/usr/share/nginx/html:ro \
    -v /opt/nginxcon/nginx.conf:/etc/nginx/nginx.conf:ro \
    nginx

设置宿主机 80 端口和容器的 80 端口映射,以及数据卷目录挂载与容器的配置文件所在目录映射。一个是 Nginx 的主页还有一个是服务器配置文件。

Tomat

docker run -d -p 8080:8080 \
    -v /opt/tomcat-server:/usr/local/tomcat/webapps/ROOT \
    tomcat:9.0

设置宿主机端口 8080 端口和容器 8080 端口映射,访问宿主机 IP 地址加上 8080 端口就可以查看 Tomcat 主页。

-v 参数设置的是 Tomcat 容器的 webapps 和宿主机的目录映射关系,在宿主机的目录下添加 war 包就可以正常访问你开发的 web app 了。

MySQL

单机部署

MySQL 长期支持版本有 5.7 和 8.0,这里演示的是 5.7。

docker run -d -p 3306:3306 \

    --name mysql \
    -v /opt/mysql/log:/var/log/mysql \
    -v /opt/mysql/data:/var/lib/mysql \
    -v /opt/mysql/conf:/etc/mysql \
    -e MYSQL_ROOT_PASSWORD=root \


    mysql:5.7

若要使用 MySQL 内置的客户端访问,输入以下命令:

docker exec -it mysql mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.37 MySQL Community Server (GPL)


Copyright (c) 2000, 2022, Oracle and/or its affiliates.





Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.



Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>



还可以通过在 docker host 上访问:

# yum -y install mariadb



# mysql -h 192.168.255.157 -uroot -proot -P 3306


Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MySQL connection id is 7
Server version: 5.7.37 MySQL Community Server (GPL)






Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.



Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.




MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

主从复制集群部署

部署主节点

docker run -d -p 3306:3306 \

    --name mysql-master \
    -v /opt/mysql-master/log:/var/log/mysql \
    -v /opt/mysql-master/data:/var/lib/mysql \
    -v /opt/mysql-master/conf:/etc/mysql \
    -e MYSQL_ROOT_PASSWORD=root \


    mysql:5.7

配置主节点

# vim /opt/mysql-master/conf/my.cnf
# cat /opt/mysql-master/conf/my.cnf
[client]

default-character-set=utf8




[mysql]

default-character-set=utf8






[mysqld]

init_connect='SET collation_connection = utf8_unicode_ci'

init_connect='SET NAMES utf8'

character-set-server=utf8

collation-server=utf8_unicode_ci

skip-character-set-client-handshake

skip-name-resolve




server_id=1
log-bin=mysql-bin

read-only=0
binlog-do-db=kubemsb_test



replicate-ignore-db=mysql

replicate-ignore-db=sys

replicate-ignore-db=information_schema

replicate-ignore-db=performance_schema

部署从节点

docker run -d -p 3307:3306 \
    --name mysql-slave \
    -v /opt/mysql-slave/log:/var/log/mysql \
    -v /opt/mysql-slave/data:/var/lib/mysql \
    -v /opt/mysql-slave/conf:/etc/mysql \
    -e MYSQL_ROOT_PASSWORD=root \


    --link mysql-master:mysql-master \
    mysql:5.7

配置从节点

# vim /opt/mysql-slave/conf/my.cnf
# cat /opt/mysql-slave/conf/my.cnf
[client]

default-character-set=utf8




[mysql]

default-character-set=utf8






[mysqld]

init_connect='SET collation_connection = utf8_unicode_ci'

init_connect='SET NAMES utf8'

character-set-server=utf8

collation-server=utf8_unicode_ci

skip-character-set-client-handshake

skip-name-resolve




server_id=2
log-bin=mysql-bin

read-only=1
binlog-do-db=kubemsb_test



replicate-ignore-db=mysql

replicate-ignore-db=sys

replicate-ignore-db=information_schema

replicate-ignore-db=performance_schema

集群配置

配置主节点

# mysql -h 192.168.255.157 -uroot -proot -P 3306
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.37 MySQL Community Server (GPL)



Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


MySQL [(none)]>
# 授权
MySQL [(none)]> grant replication slave on *.* to 'backup'@'%' identified by '123456';
# 重启容器,使用配置生效
# docker restart mysql-master


# 查看状态
MySQL [(none)]> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 154
     Binlog_Do_DB: kubemsb_test
 Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)

配置从节点

# docker restart mysql-slave



# mysql -h 192.168.255.157 -uroot -proot -P 3307


Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MySQL connection id is 2
Server version: 5.7.37 MySQL Community Server (GPL)






Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.



Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.




MySQL [(none)]>
MySQL [(none)]> change master to master_host='mysql-master', master_user='backup', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=154, master_port=3306;

MySQL [(none)]> start slave;

# 查看从节点状态
MySQL [(none)]> show slave status\G

Oracle

# oracle数据库连接信息
# port:49161
# sid:xe
# username:system
# password:oracle
# 49160 为ssh端口
# 49161 为sqlplus端口
# 49162 为oem端口
docker run -h oracle --name oracle -d 
    -p 49160:22 \
    -p 49161:1521 \
    -p 49162:8080 \
    oracleinanutshell/oracle-xe-11g

PostgreSQL

docker run -d \

	--name postgres \
	-e POSTGRES_PASSWORD=mysecretpassword \
	-e PGDATA=/var/lib/postgresql/data/pgdata \
	-v /custom/mount:/var/lib/postgresql/data \
	postgres:11.20-bullseye

ElasticSearch

单个部署

部署 ElasticSearch

docker pull elasticsearch:7.17.0



# 创建数据卷挂载目录
mkdir -p /opt/es/config
mkdir -p /opt/es/data

# 编写配置文件,使得可以远程访问
echo "http.host: 0.0.0.0" >> /opt/es/config/elasticsearch.yml


docker run -d --name elasticsearch \
    -p 9200:9200 \
    -p 9300:9300 \
    -e "discovery.type=single-node" \
    -e ES_JAVA_OPTS="-Xms64m -Xmx512m" \
    -v /opt/es/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
    -v /opt/es/data:/usr/share/elasticsearch/data \
    -v /opt/es/plugins:/usr/share/elasticsearch/plugins \
    elasticsearch:7.17.0

部署 Kibana

docker run -d --name kibana \
    -e ELASTICSEARCH_HOSTS=http://192.168.255.157:9200 \
    -p 5601:5601 \
    kibana:7.17.0

使用docker-compose部署

version: '3'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.17.0
    container_name: elasticsearch
    environment:
      - discovery.type=single-node
    ports:
      - "9200:9200"
      - "9300:9300"
    networks:
      - elk_network
  kibana:
    image: docker.elastic.co/kibana/kibana:7.17.0
    container_name: kibana
    environment:
      ELASTICSEARCH_HOSTS: "http://elasticsearch:9200"
    ports:
      - "5601:5601"
    networks:
      - elk_network

networks:
  elk_network:
# 启动容器
docker-compose up -d

Redis

单机部署

# 创建挂载的目录
mkdir -p /opt/redis/conf


# 创建配置文件
touch /opt/redis/conf/redis.conf


docker run -d -p 6379:6379 --name redis \
    -v /opt/redis/data:/data \
    -v /opt/redis/conf:/etc/redis \
    redis redis-server /etc/redis/redis.conf
# 使用redis-cli连接redis服务
# some-redis指的是容器名称
docker run -it --network some-network --rm redis redis-cli -h some-redis

集群部署

安装 redis-cluster:3 主 3 从方式,从为了同步备份,主进行 slot 数据分片。

redis-cluster.sh

# 编辑运行多个redis容器脚本文件
# vim redis-cluster.sh
# cat redis-cluster.sh
for port in $(seq 8001 8006); \
do \
    mkdir -p /mydata/redis/node-${port}/conf
    touch /mydata/redis/node-${port}/conf/redis.conf
    cat << EOF >/mydata/redis/node-${port}/conf/redis.conf
    port ${port}
    cluster-enabled yes
    cluster-config-file nodes.conf
    cluster-node-timeout 5000
    cluster-announce-ip 192.168.255.157
    cluster-announce-port ${port}
    cluster-announce-bus-port 1${port}
    appendonly yes
    EOF

    docker run -p ${port}:${port} -p 1${port}:1${port} --name redis-${port} \
        -v /mydata/redis/node-${port}/data:/data \
        -v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
        -d redis:5.0.7 redis-server /etc/redis/redis.conf; \
done

执行脚本:

sh redis-cluster.sh

登录 Redis 容器:

docker exec -it redis-8001 bash

创建 redis-cluster:

redis-cli --cluster create \
    192.168.255.157:8001 \
    192.168.255.157:8002 \
    192.168.255.157:8003 \
    192.168.255.157:8004 \
    192.168.255.157:8005 \
    192.168.255.157:8006 \
    --cluster-replicas 1

RabbitMQ

# 端口说明:
# 4369, 25672 (Erlang发现&集群端口)
# 5672, 5671 (AMQP端口)
# 15672 (web管理后台端口)
# 61613, 61614 (STOMP协议端口)
# 1883, 8883 (MQTT协议端口)
# 部署带有管理控制台的RabbitMQ
docker run -d --name rabbitmq \
    -p 5671:5671 \
    -p 5672:5672 \
    -p 4369:4369 \
    -p 25672:25672 \
    -p 15671:15671 \
    -p 15672:15672 \
    -v /opt/rabbitmq:/var/lib/rabbitmq \
    rabbitmq:management

© 版权声明
THE END
喜欢就支持一下吧
点赞0

Warning: mysqli_query(): (HY000/3): Error writing file '/tmp/MY9fCdZO' (Errcode: 28 - No space left on device) in /www/wwwroot/583.cn/wp-includes/class-wpdb.php on line 2345
admin的头像-五八三
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

图形验证码
取消
昵称代码图片