前面已经说明如何制作镜像并且上传到阿里镜像仓库,这篇文章的主要目的是使用docker-compose部署应用。
在上篇文章中,我们将java应用,h5项目,后台web管理项目都打包成了docker镜像,应用的准备工作已经做好了,但是java应用还需要连接mysql,redis来工作,客户端的请求也需要通过nginx代理。所以我们有以下工作需要完成:
- 1.启动mysql的docker容器(基于docker-compose)
- 2.启动redis的docker容器(基于docker-compose)
- 3.启动nginx的docker容器(基于docker-compose)
- 4.启动后台应用的docker容器(基于docker-compose)
- 5.启动h5应用的docke容器(基于docker-compose)
- 6.启动web管理后台应用的docke容器(基于docker-compose)
从本质上来说我们采用一个docker-compose.yml就可以完成以上工作,但是为了清晰,我们会将mysql,redis,nginx用一个docker-compose.yml来启动,其他的应用分开启动。
1.服务器环境安装
Linux服务器 : ubuntu18.04
1.1安装docker
- 步骤1: 查看linux服务器内核版本,确认是3.10以上
uname -a
- 步骤2:安装docker
sudo apt-get update
sudo apt-get install -y docker.io
- 步骤3:开启docker
sudo systemctl start docker
- 步骤4:设置开机启动
systemctl enable docker
- 步骤5: 查看是否安装成功
docker version
会发现报错如下:
解决办法如下
#添加组
sudo groupadd docker
#把当前用户加入docker组
sudo gpasswd -a ${USER} docker
#重启docker
sudo service docker restart
#切换当前会话到新 group
newgrp - docker
- 步骤6: 添加镜像加速地址
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://vaorvenp.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docke
1.2 安装docker-compose
- 下载docker-compose
sudo curl -L https://github.com/docker/compose/releases/download/1.17.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
- 授权
sudo chmod +x /usr/local/bin/docker-compose
- 查看安装版本
docker-compose --version
2.部署说明
- 下面为各个服务的docker内部网络IP地址以及端口
服务 | 容器内部IP | 端口 |
---|---|---|
MYSQL | 172.2.0.10 | 3306 |
REDIS | 172.2.0.11 | 6379 |
NGINX | 172.2.0.12 | 80 |
后台 | 172.2.0.100 | 8080 |
H5 | 172.2.0.201 | 81 |
Admin | 172.2.0.202 | 82 |
- mysql账号密码
账号 | 密码 |
---|---|
root | 123456abc |
pitaya | 123456abc |
- redis密码
123456abc
3.部署基础服务
基础服务包括mysql,redis, nginx
3.1 docker创建网络
docker network create --subnet 172.2.0.0/24 pitaya_network
3.2 部署mysql,redis以及nginx
- 创建目录 ~/app/basic
#创建目录
mkdir ~/app/basic
#创建docker-compose文件
touch docker-compose.yml
并且在 ~/app/basic目录下创建如下目录结构
- basic-compose.yml内容
version: '3'
services:
mysql:
image: mysql:5.7.16
container_name: mysql
volumes:
- ./mysql/db:/var/lib/mysql/
- ./mysql/conf/my.cnf:/etc/mysql/my.cnf
- ./mysql/init:/docker-entrypoint-initdb.d/
networks:
pitaya_network:
ipv4_address: 172.2.0.10
ports:
- 3406:3306
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --max_connections=3000
environment:
MYSQL_ROOT_PASSWORD: 123456abc
MYSQL_USER: pitaya
MYSQL_PASSWORD: 123456abc
TZ: Asia/Shanghai
redis:
image: redis:5.0.9
container_name: redis
restart: always
networks:
pitaya_network:
ipv4_address: 172.2.0.11
ports:
- 6379:6379
volumes:
- ./redis/conf/redis.conf:/etc/redis/redis.conf:rw
- ./redis/db:/data:rw
command:
redis-server /etc/redis/redis.conf --appendonly yes
environment:
TZ: Asia/Shanghai
nginx:
restart: always
image: nginx
container_name: nginx
networks:
pitaya_network:
ipv4_address: 172.2.0.12
ports:
- 80:80
- 443:443
volumes:
- ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/html:/usr/share/nginx/html
- ./nginx/ca:/etc/nginx/ca
environment:
TZ: Asia/Shanghai
networks:
pitaya_network:
external: true
- my.cnf文件内容
[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
skip_host_cache
skip-name-resolve=1
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
- nginx.conf内容(注意nginx.conf.http和nginx.conf.https是备份文件,可以不处理)
worker_processes 1;
error_log /var/log/nginx/error.log;
error_log /var/log/nginx/error.log notice;
error_log /var/log/nginx/error.log info;
user root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream api_upstream {
server 172.2.0.100:8080;
}
upstream h5_upstream {
server 172.2.0.201:81;
}
upstream admin_upstream {
server 172.2.0.202:82;
}
server {
listen 80;
server_name www.pitaya.com;
location / {
root /usr/share/nginx/html/;
index index.html;
}
location ~ ^/h5/ {
proxy_pass http://h5_upstream;
}
location ~ ^/admin/ {
proxy_pass http://admin_upstream;
}
location ~ ^/api/ {
proxy_pass http://api_upstream;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_cache_bypass $http_upgrade;
proxy_http_version 1.1;
proxy_redirect off;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_connect_timeout 5s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_buffers 8 32k;
proxy_buffer_size 64k;
}
}
}
- redis.conf
requirepass 123456abc
#daemonize yes
bind 0.0.0.0
appendonly yes
其他目录都为空目录即可
- 启动服务
cd ~/app/basic
docker-compose up -d
4.其他应用启动
后台,h5,web管理后台均分别上传上篇文章中的docker-compose.yml文件后,在每个所对应的目录下执行
docker-compose up -d
服务即可启动成功
5. 总结
docker-compose虽然在k8s面前显得相对简单,但是对于启动这样的不多的服务,个人认为正好合适!