将云梦公司开发的应用商城Mall重新实现全容器化部署,原应用商城系统各模块功能及系统架构如下:
*模块* | *使用技术* | *备注* |
---|---|---|
mall.sql | MySQL | 网站的数据库 |
dist目录 | Nginx | 网站的前端项目 |
mall-shopping | web项目 | 8081端口,商品/购物车/首页渲染等交互 |
mall-user | 8082端口,提供用户相关的交互,如登录、注册、个人中心等 | |
user-provider | 后端服务 | 提供用户相关服务 |
shopping-provider | 提供购物车、推荐商品、商品等服务 |
1.编写Dockerfile制作数据库MySQL镜像
镜像名为mall-mysql:v1.1,使用centos:7.9.2009基础镜像,安装mariadb数据库,
设置数据库支持UTF-8编码,设定root用户的密码为123456,并设置服务开机自启。
Dockerfile
FROM 192.168.121.21/library/centos:centos7.5.1804
MAINTAINER Chinaskill
RUN rm -rvf /etc/yum.repos.d/*
ADD ftp.repo /etc/yum.repos.d/
ADD gpmall.sql /root/
RUN yum install -y mariadb-server
RUN sed -i "13cport = 8066" /etc/my.cnf.d/server.cnf
ADD init.sh /root/init.sh
RUN chmod +x /root/init.sh
RUN /root/init.sh
ENV LC_ALL en_US.UTF-8
EXPOSE 3306
EXPOSE 8066
CMD ["mysqld_safe"]
init.sh
#!/bin/bash
mysql_install_db --user=mysql
mysqld_safe &
sleep 3s
mysqladmin -u root password '123456'
sleep 3s
mysql -uroot -p123456 -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456'"
sleep 3s
mysql -uroot -p123456 -e "create database gpmall;use gpmall;"
mysql -uroot -p123456 gpmall < /root/gpmall.sql
ftp.repo
[gpmall]
name=gpmall
baseurl=ftp://192.168.121.21/ChinaskillMall/gpmall-repo
gpgcheck=0
nabled=1
[centos]
name=centos
baseurl=ftp://192.168.121.21/centos
gpgcheck=0
enabled=1
创建镜像
docker build -t mall-xxxx:v1.1 .
2.编写Dockerfile制作Redis镜像
镜像名为mall-redis:v1.1,使用centos:centos7.9.2009基础镜像,安装Redis服务,
设置Redis免密,并关闭保护模式,开放端口6379,并设置服务开机自启。
Dockerfile
FROM 192.168.121.21/library/centos:centos7.5.1804
MAINTAINER Chinaskill
RUN rm -rvf /etc/yum.repos.d/*
COPY ftp.repo /etc/yum.repos.d/local.repo
RUN yum clean all
RUN yum list
RUN yum -y install 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@protected-mode yes@protected-mode no@g' /etc/redis.conf
RUN sed -i -e 's@daemonize yes@daemonize no@g' /etc/redis.conf
EXPOSE 6379
ENTRYPOINT redis-server /etc/redis.conf
CMD ["redis-server"]
创建镜像
docker build -t mall-xxxx:v1.1 .
3.编写Dockerfile制作Kafka镜像
镜像名为mall-kafka:v1.1,使用centos:centos7.9.2009基础镜像,
安装Kafka服务,开放端口9092,并设置服务开机自启。
Dockerfile
FROM 192.168.121.21/library/centos:centos7.5.1804
MAINTAINER Chinaskill
RUN rm -rvf /etc/yum.repos.d/*
COPY ftp.repo /etc/yum.repos.d/local.repo
RUN yum -y install java-1.8.0
ADD kafka_2.11-1.1.1.tgz /usr/local
ENV KAFKA_HOME /usr/local/kafka_2.11-1.1.1
RUN sed -i 's/localhost:2181/zk1.mall:2181,zk2.mall:2181,zk3.mall:2181/g' \
/usr/local/kafka_2.11-1.1.1/config/server.properties
EXPOSE 9092
ENTRYPOINT [ "/usr/local/kafka_2.11-1.1.1/bin/kafka-server-start.sh", \
"/usr/local/kafka_2.11-1.1.1/config/server.properties" ]
创建镜像
docker build -t mall-xxxx:v1.1 .
4.编写Dockerfile制作ZooKeeper镜像
镜像名为mall-zookeeper:v1.1,使用centos:7.9.2009基础镜像,
安装ZooKeeper服务,开放端口2181;并设置服务开机自启。
Dockerfile
FROM 192.168.121.21/library/centos:centos7.5.1804
MAINTAINER Chinaskill
RUN rm -rvf /etc/yum.repos.d/*
COPY ftp.repo /etc/yum.repos.d/local.repo
RUN yum -y install java-1.8.0
ADD zookeeper-3.4.14.tar.gz /usr/local
ENV ZOOKEEPER_HOME /usr/local/zookeeper-3.4.14
ENV PATH $PATH:$JAVA_HOME/bin:$JRE_HOME/bin:$ZOOKEEPER_HOME/bin
EXPOSE 2181
EXPOSE 2888
EXPOSE 3888
CMD $ZOOKEEPER_HOME/bin/zkServer.sh start-foreground
创建镜像
docker build -t mall-xxxx:v1.1 .
5.编写Dockerfile制作nginx镜像
镜像名为mall-nginx:v1.1,使用centos:7.9.2009基础镜像,
安装nginx服务,开放端口2181;并设置服务开机自启。
Dockerfile
FROM 192.168.121.21/library/centos:centos7.5.1804
MAINTAINER Chinaskill
RUN rm -rvf /etc/yum.repos.d/*
ADD ftp.repo /etc/yum.repos.d/
RUN yum install nginx -y
RUN sed -i '12ilocation /user {proxy_pass http://jar:8082;}' /etc/nginx/conf.d/default.conf
RUN sed -i '12ilocation /shopping {proxy_pass http://jar:8081;}' /etc/nginx/conf.d/default.conf
RUN rm -rf /usr/share/nginx/html/*
ADD dist/ /usr/share/nginx/html/
EXPOSE 80
CMD ["nginx","-g","daemon off;"]
创建镜像
docker build -t mall-xxxx:v1.1 .
6.编写Dockerfile制作jar包运行环境镜像
Dockerfile
FROM 192.168.121.21/library/centos:centos7.5.1804
MAINTAINER Chinaskill
RUN rm -rvf /etc/yum.repos.d/*
ADD ftp.repo /etc/yum.repos.d/
#RUN yum install -y java-1.8.0
ADD jdk-8u77-linux-x64.tar.gz /opt
ENV JAVA_HOME=/opt/jdk1.8.0_77
ENV JRE_HOME=${JAVA_HOME}/jre
ENV CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
ENV PATH=${JAVA_HOME}/bin:$PATH
ADD gpmall-shopping-0.0.1-SNAPSHOT.jar /opt/
ADD gpmall-user-0.0.1-SNAPSHOT.jar opt/
ADD shopping-provider-0.0.1-SNAPSHOT.jar /opt/
ADD user-provider-0.0.1-SNAPSHOT.jar /opt/
ADD start.sh /start.sh
RUN chmod +x /start.sh
EXPOSE 8081
EXPOSE 8082
EXPOSE 20880
EXPOSE 20881
EXPOSE 9090
CMD ["bash","/start.sh"]
start.sh
#!/bin/bash
sleep 5
nohup java -jar /opt/shopping-provider-0.0.1-SNAPSHOT.jar &
sleep 30
nohup java -jar /opt/user-provider-0.0.1-SNAPSHOT.jar &
sleep 30
nohup java -jar /opt/gpmall-shopping-0.0.1-SNAPSHOT.jar &
sleep 30
nohup java -jar /opt/gpmall-user-0.0.1-SNAPSHOT.jar &
sleep 30
while [[ true ]]; do
sleep 1
done
创建镜像
docker build -t mall-xxxx:v1.1 .
7.编写docker-compose.yaml文件,使用上述镜像编排部署Mall应用商城。
docker-compose.yml
version: '2'
services:
zk1.mall:
image: 192.168.1.10/library/mall-xxxx:v1.1
restart: always
expose:
- 2181
- 2888
- 3888
volumes:
- ./zookeeper1/conf:/usr/local/zookeeper-3.4.14/conf
- ./zookeeper1/data:/data
zk2.mall:
image: 192.168.1.10/library/mall-xxxx:v1.1
restart: always
expose:
- 2181
- 2888
- 3888
volumes:
- ./zookeeper2/conf:/usr/local/zookeeper-3.4.14/conf
- ./zookeeper2/data:/data
zk3.mall:
image: 192.168.1.10/library/mall-xxxx:v1.1
restart: always
expose:
- 2181
- 2888
- 3888
volumes:
- ./zookeeper3/conf:/usr/local/zookeeper-3.4.14/conf
- ./zookeeper3/data:/data
kafka1.mall:
image: 192.168.1.10/library/mall-xxxx:v1.1
restart: always
expose:
- 9092
links:
- zk1.mall
- zk2.mall
- zk3.mall
redis.mall:
image: 192.168.1.10/library/mall-xxxx:v1.1
expose:
- 6379
mysql.mall:
image: 192.168.1.10/library/mall-xxxx:v1.1
expose:
- 8066
nginx:
image: 192.168.1.10/library/mall-xxxx:v1.1
restart: always
ports:
- "80:80"
links:
- jar
jar:
image: 192.168.1.10/library/mall-xxxx:v1.1
links:
- kafka1.mal
- mysql.mall
- redis.mall
- zk1.mall
- zk2.mall
- zk3.mall
expose:
- 8081
- 8082
- 20880
- 20881
- 9090