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


theme: healer-readable

highlight: xcode

[Docker]使用Docker部署常见的中间件_第1张图片

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

Nginx

shell 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

shell 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。

shell 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 内置的客户端访问,输入以下命令:

```shell 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 上访问:

```shell

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 | +--------------------+ | informationschema | | mysql | | performanceschema | | sys | +--------------------+ 4 rows in set (0.00 sec) ```

主从复制集群部署

部署主节点

shell 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

配置主节点

```shell

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] initconnect='SET collationconnection = utf8unicodeci' initconnect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8unicode_ci skip-character-set-client-handshake skip-name-resolve

serverid=1 log-bin=mysql-bin read-only=0 binlog-do-db=kubemsbtest

replicate-ignore-db=mysql replicate-ignore-db=sys replicate-ignore-db=informationschema replicate-ignore-db=performanceschema ```

部署从节点

shell 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

配置从节点

```shell

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] initconnect='SET collationconnection = utf8unicodeci' initconnect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8unicode_ci skip-character-set-client-handshake skip-name-resolve

serverid=2 log-bin=mysql-bin read-only=1 binlog-do-db=kubemsbtest

replicate-ignore-db=mysql replicate-ignore-db=sys replicate-ignore-db=informationschema replicate-ignore-db=performanceschema ```

集群配置

配置主节点

```shell

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)]> ```

```shell

授权

MySQL [(none)]> grant replication slave on . to 'backup'@'%' identified by '123456'; ```

```shell

重启容器,使用配置生效

docker restart mysql-master

查看状态

MySQL [(none)]> show master status\G ******** 1. row ******** File: mysql-bin.000001 Position: 154 BinlogDoDB: kubemsbtest BinlogIgnoreDB: ExecutedGtid_Set: 1 row in set (0.00 sec) ```

配置从节点

```shell

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)]> ```

```shell MySQL [(none)]> change master to masterhost='mysql-master', masteruser='backup', masterpassword='123456', masterlogfile='mysql-bin.000001', masterlogpos=154, masterport=3306;

MySQL [(none)]> start slave;

查看从节点状态

MySQL [(none)]> show slave status\G ```

Oracle

```shell

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

shell 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

```shell 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 ESJAVAOPTS="-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

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

使用docker-compose部署

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

networks: elk_network: ```

```shell

启动容器

docker-compose up -d ```

Redis

单机部署

```shell

创建挂载的目录

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 shell

使用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

```shell

编辑运行多个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 ```

执行脚本:

shell sh redis-cluster.sh

登录 Redis 容器:

shell docker exec -it redis-8001 bash

创建 redis-cluster:

shell 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

```shell

端口说明:

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 ```

你可能感兴趣的:(docker,中间件,容器,运维)