theme: healer-readable
本篇文章给大家介绍如何使用 Docker 部署在企业工作中常用的中间件服务。
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 的主页还有一个是服务器配置文件。
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 长期支持版本有 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
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
[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
[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
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
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
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 ```
```shell
docker run -h oracle --name oracle -d -p 49160:22 \ -p 49161:1521 \ -p 49162:8080 \ oracleinanutshell/oracle-xe-11g ```
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
```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
```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 ```
```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
docker run -it --network some-network --rm redis redis-cli -h some-redis ```
安装 redis-cluster:3 主 3 从方式,从为了同步备份,主进行 slot 数据分片。
redis-cluster.sh:
```shell
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
```shell
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 ```