基础环境准备:
- 至少需要两台装有docker的主机
- 关闭防火墙,selinux,iptables等功能
- 开启vsftpd功能和共享目录
- mall-repo文件已到共享目录下面
过程:
1.生成所需镜像
2.容器化部署前端服务
2.编写docker-compose文件文件结构:
看个人怎么安排文件的存放位置,我弄得比较随便下面展示我的
体系介绍:
云梦公司开发了一套基于SpringBoot+MyBatis微服务架构的ChinaSkillsMall电商系统,并实现全容器化部署,ChinaSkillsMall电商应用系统架构图如下:
模块 | 说明 |
---|---|
mall-monitor | 监控中心 |
mall-gateway | 微服务网关 |
mall-auth | 认证中心 |
mall-admin | 商城后台服务 |
mall-portal | 商城前台服务 |
1.构建mariadb镜像
题目要求是:
(1)基础镜像:centos:centos7.5.1804;
(2)作者:Chinaskill;
(3)安装并初始化mariadb,密码:root;
(4)设置数据库编码为UTF-8;
(5)开放端口:3306 #注意端口冲突
(6)设置mariadb开机自启。
# 在dockerfile中需要有yum的repo文件(local.repo文件)和用来初始化数据库并导入数据的命令(init.sh文件)
(1)编写mariadb的dockerfile
# vi dockerfile
FROM centos:centos7.5.1804
MAINTAINER jing
RUN rm -rf /etc/yum.repos.d/*
COPY local.repo /etc/yum.repos.d/
RUN yum install -y mariadb-server
COPY init.sh /root/
COPY mall.sql /root
RUN chmod +x /root/init.sh
ENV LC_ALL en_US.UTF-8
RUN /root/init.sh
EXPOSE 3066
CMD ["mysqld_safe","--user=root"]
(2)编写local.reop文件
在使用ftp时要保证在宿主机上vsftpd功能以开启且设置好了/opt的共享目录和已将文件解压传递到/opt/下面
# vi local.repo
[mall]
name=mall
baseurl=ftp://192.168.44.100/mall-repo
gpgcheck=0
enabled=1
(3)编写init.sh文件
vi init.sh
#!/bin/bash
mysql_install_db --user=root
mysqld_safe --user=root & #一定要加--user=root
sleep 8
mysqladmin -u root password 'root'
mysql -uroot -proot -e "grant all privileges on *.* to 'reader'@'%' identified by '123456' ;"
mysql -uroot -proot -e "create database mall;use mall;"
mysql -uroot -proot mall < /root/mall.sql
(4)构建镜像
[root@master mysql]# docker build -t mysql:1 .
........
2.构建redis镜像
题目要求:
(1)基础镜像:centos:centos7.5.1804;
(2)作者:Chinaskill;
(3)安装redis服务;
(4)修改配置文件中的bind 127.0.0.1为bind 0.0.0.0;
(5)设置redis免密,并关闭保护模式;
(6)开放端口:6379;
(7)设置服务开机自启。
(1)编写redis的dockfile
# vi dockerfile
FROM centos:centos7.5.1804
MAINTAINER jing
RUN rm -rf /etc/yum.repos.d/*
COPY local.repo /etc/yum.repos.d/
RUN yum install -y redis
RUN sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/g' /etc/redis.conf
RUN sed -i -e 's/peotected-mode yes/protected-mode no/g' /etc/redis.conf
RUN sed -i -e 's/daemonize yes/daemonize no/g' /etc/redis.conf
EXPOSE 6379
CMD ["/usr/bin/redis-server", "/etc/redis.conf"]
(2)编写local.repo 文件
local.repo 文件与构建mariadb文件一样这里不在展示
(3)构建redis镜像
[root@master mysql]# docker build -t redis:c1 .
.......
3.构建Nacos-Registry镜像
题目要求:
(1)基础镜像:centos:centos7.5.1804;
(2)作者:Chinaskill;
(3)安装jdk工具和nacos-server工具;
(4)开放端口:8848;
(5)设置nacos服务开机自启。
(1)编写nacos的dockerfile
# vi dockerfile
FROM centos:centos7.5.1804
MAINTAINER jing
RUN rm -rf /etc/yum.repos.d/*
COPY local.repo /etc/yum.repos.d/
ADD nacos-server-1.1.0.tar.gz /usr/local
ADD jdk-8u121-linux-x64.tar.gz /usr/local
ENV JAVA_HOME /usr/local/jdk1.8.0_121
ENV NACOS_HOME /usr/local/nacos
ENV PATH=$PATH:$JAVA_HOME/bin:$NACOS_HOME/bin
EXPOSE 8848
CMD startup.sh -m standalone && tail -f $NACOS_HOME/logs/start.out
(2)编写local.repo 文件
local.repo 文件与构建mariadb文件一样这里不在展示
(3)构建nacos镜像
[root@master mysql]# docker build -t nacos:c1 .
.......
4.构建rabbitmq镜像
题目要求:(这题在原题中没有要求这里为自己写的)
(1)基础镜像:centos:centos7.5.1804;
(2)作者:Chinaskill;
(3)安装rabbitmq-server;
(4)开放端口:5672, 15672;
(5)设置rabbitmq_manage开启
(1)编写rabbitmq的dockerfile
# vi dockerfile
FROM centos:centos7.5.1804
MAINTAINER jing
RUN rm -rf /etc/yum.repos.d/*
COPY local.repo /etc/yum.repos.d/
RUN yum install -y rabbitmq-server
COPY rabbitmq.sh /root/
RUN chmod +x /root/rabbitmq_start.sh
EXPOSE 5672 15672
CMD ["/bin/bash","/root/rabbitmq.sh"]
(2)编写local.repo 文件
local.repo 文件与构建mariadb文件一样这里不在展示
(3)编写rabbitmq_start.sh 文件
vi rabbitmq_start.sh
#!/bin/bash
rabbitmq=/usr/lib/rabbitmq/bin
$rabbitmq/rabbitmq-server start
sleep 8
$rabbitmq/rabbitmqctl add_vhost mall
$rabbitmq/rabbitmqctl add_user mall mall
$rabbitmq/rabbitmqctl add_user_tag mall admin
$rabbitmq/rabbitmqctl set_permissions -p mall mall '.*' '.*' '.*'
$rabbitmq/rabbitmq-plugins enable rabbitmq_management
$rabbitmq/rabbitmq-server restart
(4)构建rabbitmq镜像
[root@master mysql]# docker build -t rabbitmq:c1 .
.......
5.容器化部署前段服务
题目要求:
在master节点上安装node工具,对mall-admin-web项目进行打包,然后编写Dockerfile-nginx文件构建chinaskillmall-nginx:v1.1镜像,要求如下:
(1)基础镜像:centos:centos7.5.1804;
(2)作者:Chinaskill;
(3)安装nginx服务;
(4)将mall-admin-web项目打包后的文件拷贝到/usr/share/nginx/html目录下;
(5)设置nginx服务开机自启。
load镜像
(1)生成前端文件:
[root@master mall-swarm]# tar -zxvf mall-admin-web.tar.gz
[root@master mall-swarm]# cd mall-admin-web
[root@master mall-admin-web]# vi config/prod.env.js
'use strict'
module.exports = {
NODE_ENV: '"production"',
BASE_API: '"http://10.24.2.156:8201/mall-admin"' #修改为本机IP
}
(2)使用命令进行打包,生成dist目录:
[root@node mall-admin-web]# cd ../
[root@node mall-swarm]# tar zxvf node-v6.17.1-linux-x64.tar.gz
[root@node mall-swarm]# mv node-v6.17.1-linux-x64 /usr/local/node
[root@node mall-swarm]# vi /etc/profile
export NODE_HOME=/usr/local/node
export PATH=$NODE_HOME/bin:$PATH
[root@node mall-swarm]# source /etc/profile
[root@node mall-swarm]# node -v
v6.17.1
[root@node mall-swarm]# npm -v
3.10.10
[root@node mall-swarm]# cd mall-admin-web
[root@master mall-admin-web]# npm run build
......
[root@master mall-admin-web]# mv dist/ ../
[root@master mall-admin-web]# cd ../
(3)编写nginx的dockerfile文件
# vi dockerfile
FROM centos:centos7.5.1804
MAINTAINER jing
RUN rm -rf /etc/yum.repos.d/*
COPY local.repo /etc/yum.repos.d/
RUN yum install -y nginx
RUN rm -rf /usr/share/nginx/html/*
COPY dist/ /usr/share/nginx/html/
EXPOSE 80
CMD ["nginx","-g","daemon off;"]
(4)编写local.repo 文件
local.repo 文件与构建mariadb文件一样这里不在展示
(5)生成nginx镜像
[root@master mysql]# docker build -t nginx:c1 .
.......
6.编排部署ChinaSkillsMall商城
在master节点上编写/root/mall-swarm/docker-compose.yaml文件,具体要求如下:
(1)容器1名称:chinaskillmall-admin;镜像:mall/mall-admin:1.0-SNAPSHOT;端口映射:8080:8080;
(2)容器2名称:chinaskillmall-auth;镜像:mall/mall-auth:1.0-SNAPSHOT;端口映射:8401:8401;
(3)容器3名称:chinaskillmall-gateway;镜像:mall/mall-gateway:1.0-SNAPSHOT;端口映射:8201:8201;
(4)容器4名称:chinaskillmall-mysql;镜像:chinaskillmall-mariadb:v1.1;端口映射:3306:3306;
(5)容器5名称:chinaskillmall-nacos;镜像:chinaskillmall-nacos:v1.1;端口映射:8848:8848;
(6)容器6名称:chinaskillmall-nginx;镜像:chinaskillmall-nginx:v1.1;端口映射:8888:80;
(7)容器1名称:chinaskillmall-redis;镜像:chinaskillmall-redis:v1.1;端口映射:6379:6379。
完成后编排部署ChinaSkillsMall商城。
编写docker-compose文件
# vi docker-copose
version: '3'
services:
mysql:
image: mysql:x1
container_name: mysql
restart: always
ports:
- 3306:3306
redis:
image: redis:x1
container_name: redis
ports:
- 6379:6379
nginx:
image: nginx:x1
container_name: nginx
ports:
- 8888:80
rabbitmq:
image: rabbitmq:x1
container_name: rabbitmq
ports:
- 5672:5672
- 15672:15672
nacos-registry:
image: nacos:x1
container_name: nacos-registry
ports:
- 8848:8848
mall-admin:
image: mall/mall-admin:1.0-SNAPSHOT
container_name: mall-admin
ports:
- 8080:8080
links:
- mysql:db
mall-gateway:
image: mall/mall-gateway:1.0-SNAPSHOT
container_name: mall-gateway
ports:
- 8201:8201
links:
- redis:redis
- nacos-registry:nacos-registry
mall-auth:
image: mall/mall-auth:1.0-SNAPSHOT
container_name: mall-auth
ports:
- 8401:8401
links:
- nacos-registry:nacos-registry
来吧展示!!!
[root@master mymall]# docker-compose up