一、docker,docker-compose 安装

1. 卸载依赖

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

2. 安装epel源

yum install -y vim wget epel-release

3. 安装所需的软件包

sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

4. 设置稳定的仓库(可选)

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

5. 安装 Docker Engine-Community 和 containerd

# 列出并排序您存储库中可用的版本,选择一个合适的版本进行安装
yum list docker-ce --showduplicates | sort -r
# 此处的VERSION自行选择稳定版本
sudo yum install -y docker-ce-<VERSION> docker-ce-cli-<VERSION> containerd.io

6. 配置Docker Hub 镜像加速器(可选)

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": [
        "https://1nj0zren.mirror.aliyuncs.com",
        "https://docker.mirrors.ustc.edu.cn",
        "http://f1361db2.m.daocloud.io",
        "https://registry.docker-cn.com"
    ]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

7. 安装docker命令补全工具

yum install -y bash-completion

8. 设置docker开机自启动(可选)

systemctl enable docker
# 到此docker安装完毕

9. 下载docker compose的当前稳定版本

# 目录可以自定义
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

10. 赋权

# 自定义目录按需更改
sudo chmod +x /usr/local/bin/docker-compose

11. 创建链接

# 自定义目录按需更改
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

12. 查看版本

docker-compose --versions

二、常用脚本

1. 安装mysql

1.1 启动脚本
#!/bin/sh
docker run \
--restart=always \
--name mysql \
-v /root/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-v /root/mysql/data:/var/lib/mysql \
-v /root/mysql/logs:/logs \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=${your_pwd} \
-d mysql
1.2 相关配置文件
1.2.1 my.cnf
[client]

#socket = /usr/mysql/mysqld.sock

default-character-set = utf8mb4

[mysqld]

#pid-file        = /var/run/mysqld/mysqld.pid

#socket          = /var/run/mysqld/mysqld.sock

datadir         = /var/lib/mysql

#socket = /usr/mysql/mysqld.sock

#pid-file = /usr/mysql/mysqld.pid

character_set_server = utf8mb4

collation_server = utf8mb4_bin

secure-file-priv= NULL

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

# Custom config should go here

!includedir /etc/mysql/conf.d/

2. 安装redis

2.1 启动脚本
#!/bin/sh
docker run -p 6379:6379 --name touyan_redis --restart=always -v /root/redis/redis.conf:/etc/redis/redis.conf  -v /root/redis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes

主要配置为 redis.conf,可以自行按需个人配置

3. 安装mongodb

3.1 启动脚本
#!/bin/bash
docker run -itd \
--name mongodb \
--restart=always \
--privileged \
-p 27017:27017 \
-v /home/docker/mongo/data:/data/db \
-v /home/docker/mongo/conf:/data/configdb \
-v /home/docker/mongo/logs:/data/log/ \
mongo:5.0.3 \
-f /data/configdb/mongod.conf
3.2 相关配置文件
3.2.1 mongod.conf

路径可以根据自己挂载路径动态改变

vim /home/docker/mongo/conf/mongod.conf
# 数据库文件存储位置
dbpath = /data/db
# log文件存储位置
logpath = /data/log/mongod.log
# 使用追加的方式写日志
logappend = true
# 是否以守护进程方式运行
# fork = true
# 全部ip可以访问
bind_ip = 0.0.0.0
# 端口号
port = 27017
# 是否启用认证
auth = true
# 设置oplog的大小(MB)
oplogSize=2048
3.3 相关后置操作
# 进入容器内部
docker exec -it mongodb /bin/bash
# 进入mongo
mongo
# 使用admin用户
use admin
# 创建用户
db.createUser({user:'mymongo',pwd:'mypwd.',roles:['root']});
# 使用创建的用户登录
db.auth('mymongo','mypwd.');
# 更新用户权限
db.updateUser("apiAdmin",{roles : [{"role" : "dbAdmin","db" : "mymongo"},{"role" : "readWrite","db" : "mymongo"}]});
# 切换对应的库,后续需要新建一张表才可以正常使用
use db;

另附spring-boot的application配置与之对应配置文件:

spring:
  data:
    mongodb:
      host: ${mongodb.host}
      port: ${mongodb.port}
      # 此处 username 与上述 "mymongo"对应
      username: ${mongodb.username}
      # 此处 password 与上述 "mypwd."对应
      password: ${mongodb.password}
      # 此处 database 与上述 "db"对应
      database: ${mongodb.database}

4. 安装nginx

4.1 启动脚本
#!/bin/bash
docker run \
--name nginx \
--restart=always \
-p 80:80 \
-v /home/docker/nginx/html/:/usr/share/nginx/html/ \
-d nginx

此处只挂载了部署的静态页面以及端口映射,关于nginx的其他配置可以自行修改后挂载即可。

5. 安装fastdfs

5.1 启动脚本

因为 fastdfs 服务分为 tracker、storage以及nginx,所以此处采用 docker-compose 启动,会方便很多。

以下配置文件注意修改 ip 以及对应目录

version: '2'
services:
    fastdfs-tracker:
        hostname: fastdfs-tracker
        container_name: fastdfs-tracker
        image: season/fastdfs:1.2
        network_mode: "host"
        command: tracker
        user: root:root
        privileged: true
        volumes:
          - ./tracker_data:/fastdfs/tracker/data
    fastdfs-storage:
        hostname: fastdfs-storage
        container_name: fastdfs-storage
        image: season/fastdfs:1.2
        network_mode: "host"
        user: root:root
        volumes:
          - ./storage_data:/fastdfs/storage/data
          - ./store_path:/fastdfs/store_path
        environment:
          - TRACKER_SERVER=192.168.1.156:22122
        command: storage
        privileged: true
        depends_on:
          - fastdfs-tracker
    fastdfs-nginx:
        hostname: fastdfs-nginx
        container_name: fastdfs-nginx
        image: season/fastdfs:1.2
        network_mode: "host"
        user: root:root
        privileged: true
        volumes:
          - ./nginx.conf:/etc/nginx/conf/nginx.conf
          - ./store_path:/fastdfs/store_path
        environment:
          - TRACKER_SERVER=192.168.1.156:22122
        command: nginx

nginx的挂载conf文件如下,可以按需自行更改:

#user  nobody;
worker_processes  1;
 
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       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  logs/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #gzip  on;
 
    server {
        listen       7003;
        server_name  localhost;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location /group1/M00 {
            root /fastdfs/storage/data;
            ngx_fastdfs_module;
        }
 
        #error_page  404              /404.html;
 
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 }
}

另附spring-boot的application配置与之对应配置文件:

fdfs:
  so-timeout: 50000
  connect-timeout: 50000
  # 此处 domain 为storage server上web server域名,通常仅针对单独部署的web server。
  # 例 http://192.168.1.156:7003,这里为什么是7003端口因为nginx中配置了7003端口监听转发,可以自行按需更改
  domain: ${fdfs.domain}
  base:
    # 数据目录地址,例 /home/ubgpu/fastdfs/data/${server.name}
    path: ${fdfs.base.path}
  thumb-image:             #缩略图生成参数
    width: 200
    height: 200
  tracker-list:
    # TRACKER_SERVER的暴露地址,与上述 TRACKER_SERVER 一致,例 192.168.1.156:22122
    - ${fdfs.tracker-list1}

6. 安装es

6.1 启动脚本

因为ES所需内存较大,所以本人专门选取了一台大内存的机器部署,vm配置可以自行按需修改

#!/bin/bash
docker run --name elasticsearch -p 9200:9200  -p 9300:9300 \
 -e "discovery.type=single-node" \
 -e ES_JAVA_OPTS="-Xms4096m -Xmx4096m" \
 -v /home/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
 -v /home/elasticsearch/data:/usr/share/elasticsearch/data \
 -v /home/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
 -d elasticsearch:7.6.2
6.2 相关配置文件
echo "http.host: 0.0.0.0" >> /home/elasticsearch/config/elasticsearch.yml

7. 安装kibana

7.1 启动脚本
#!/bin/bash
docker run --name kibana -v /home/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml -p 5601:5601 -d kibana:7.6.2 
7.2 相关配置文件
mkdir -p /home/kibana    # 创建挂载配置目录
chmod 777 /home/kibana   # 赋值读写执行权限
echo "server.host: 0.0.0.0" >> /home/kibana/kibana.yml
echo "elasticsearch.hosts: http://0.0.0.0:9200" >> /home/kibana/kibana.yml # 0.0.0.0 表示你的ip

8. 安装kafka

8.1 启动脚本

因为 kafka需要zookeeper选举,此外还安装了一个界面化工具kafka-manager,所以此处采用 docker-compose 启动,会方便很多。

version: '3'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    volumes:
      - /home/zookeeper/data:/data
    ports:
      - 2182:2181
  kafka:
    image: wurstmeister/kafka
    ports:
      - 9092:9092
    environment:
      KAFKA_BROKER_ID: 0
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://${宿主机IP}:9092
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
    volumes:
      - /home/kafka/kafka-logs:/kafka
    depends_on:
      - zookeeper
  kafka-manager:
    container_name: kafka-manager
    image: sheepkiller/kafka-manager
    ports:
      - 9000:9000
    environment:
      ZK_HOSTS: zookeeper:2181

三、docker-compose部署SpringCloud项目

假设目前的服务有4个,分别是 gateway(网关服务)、data(数据服务)、yewu1(业务服务1)、yewu2(业务服务2)

1. docker-compose.yaml

version: '3'
services:
  data:
    # 总是重启
    restart: always
    # 构建镜像目录
    build: ./data
    # 镜像名称与版本号
    image: "data:v0.1"
    # 挂载日志目录,第二个路径与 ${logging.file.path} 对应
    volumes:
      - /tmp/log/data:/log/data
    # 容器的主机名,此主机名与feign调用时的名称对应
    hostname: data
    # 属于指定网络
    networks:
      - my-net
    # 环境配置
    environment:
      - TZ=Asia/Shanghai
    # 依赖指定容器,被依赖容器启动后才会启动当前容器
    depends_on:
      - gateway
    # 暴露端口,未做主机映射
    expose:
      - 9002
  gateway:
    restart: always
    build: ./gateway
    image: "gateway:v0.1"
    volumes:
      - /tmp/log/gateway:/log/gateway
    networks:
      - my-net
    environment:
      - TZ=Asia/Shanghai
    # 映射主机端口到容器端口
    ports:
      - "9001:9001"
  yewu1:
    restart: always
    build: ./yewu1
    image: "yewu1:v0.1"
    volumes:
      - /tmp/log/yewu1:/log/yewu1
    networks:
      - my-net
    environment:
      - TZ=Asia/Shanghai
    depends_on:
      - gateway
    expose:
      - 9004
  yewu2:
    restart: always
    build: ./yewu2
    image: "yewu2:v0.1"
    volumes:
      - /tmp/log/yewu2:/log/yewu2
    networks:
      - my-net
    environment:
      - TZ=Asia/Shanghai
    depends_on:
      - gateway
    expose:
      - 9003
networks:
  my-net:
    driver: bridge   #以桥接模式创建自定义网络,否则会使用默认的网络

2. Dockerfile

以 data 服务为例,其他服务类似

# 基础镜像
FROM java:8
# 作者
MAINTAINER eason
# 把可执行jar包复制到基础镜像的根目录下
ADD *.jar /data.jar
# 镜像要暴露的端口,如要使用端口,在执行docker run命令时使用-p生效
EXPOSE 9002
# 挂载日志路径
VOLUME /log/data
# 在镜像运行后为容器执行的命令
ENTRYPOINT ["java","-jar","-Xms2G","-Xmx4G","-Dfile.encoding=utf-8","-Dspring.profiles.active=exp,commonDev","/data.jar"]

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐