一:docker安装
二:基于Docker容器的web应用系统部署
基础环境准备:
- 至少需要两台装有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:c1
restart: always
container_name: mysql
ports:
- 8066:3306 #注意端口冲突
nginx:
image: nginx:c1
container_name: nginx
ports:
- 8888:80 #注意端口冲突
redis:
image: redis:c1
container_name: redis
ports:
- 6379:6379
rabbitmq:
image: rabbitmq:c1
container_name: rabbitmq
ports:
- 5672:5672
- 15672:15672
nacos:
image: nacos:c1
container_name: nacos
ports:
- 8848:8848
admin:
image: mall/mall-admin:1.0-SNAPSHOT
container_name: admin
ports:
- 8080:8080
links:
- mysql
auth:
image: mall/mall-auth:1.0-SNAPSHOT
container_name: auth
ports:
- 8401:8401
links:
- nacos
gateway:
image: mall/mall-gateway:1.0-SNAPSHOT
container_name: gateway
ports:
- 8201:8201
links:
- redis
- nacos
来吧展示!!!
[root@master mymall]# docker-compose up
在浏览器访问nginx暴露的端口可访问到mall(密码是:macro123)
也可访问nacos,通过ip+nacos暴露的端口加”/nacos”可访问到(账号密码都是nacos)
三:CICD部署
1.启动Jenkins
我采用docker-compose的方式启动,其他方法自行选择
在master节点上使用镜像jenkins/jenkins:2.262-centos部署Jenkins服务,具体要求如下:
(1)容器名称:jenkins;
(2)端口映射:8080:8080;
(3)使用root身份生成容器;
(4)离线安装Jenkins插件;
(5)设置Jenkins用户:chinaskill;密码:000000;
(6)在授权策略中配置“任何用户可以做任何事(没有任何限制)”。
(1)编写docker-compose文件
[root@k8s-master jenkins]# vi docker-compose.yaml
version: '3'
services:
jenkins:
image: jenkins/jenkins:2.262-centos
container_name: jenkins
restart: always
user: root
ports:
- "8080:8080"
volumes:
- /home/jenkins_home:/var/jenkins_home
- /usr/bin/docker:/usr/bin/docker
- /var/run/docker.sock:/var/run/docker.sock
- /usr/bin/kubectl:/usr/local/bin/kubectl
- /root/.kube:/root/.kube
(2)启动Jenkins
[root@k8s-master jenkins]# docker-compose up -d
Starting jenkins ... done
[root@k8s-master jenkins]# docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------------------------
jenkins /sbin/tini -- /usr/local/b ... Up 50000/tcp, 0.0.0.0:8080->8080/tcp,:::8080->8080/tcp
2.启动gitlab
在master节点上使用镜像gitlab/gitlab-ce:12.9.2-ce.0部署Gitlab服务,具体要求如下:
(1)容器名称:mygitlab;
(2)端口映射:1022:22、81:80、443:443;
(3)容器重启策略:always;
(4)设置root用户及密码;
(5)使用root用户登录Gitlab,密码:00000000;
(6)新建项目ChinaskillProject,将/opt/ChinaskillProject中的代码上传到ChinaskillProject项目中。
(1)编写gitlab的docker-compose文件
[root@k8s-master gitlab]# vi docker-compose.yaml
version: '3'
services:
gitlab:
image:
restart: always
container_name: mygitlab
ports:
- '1022:22'
- '443:443'
- '81:80'
volumes:
- /etc/gitlab/config:/etc/gitlab
- /etc/gitlab/log:/var/log/gitlab
- /etc/gitlab/opt:/var/opt/gitlab
(2)启动gitlab
[root@k8s-master gitlab]# docker-compose up -d
Creating network "gitlab_default" with the default driver
Creating mygitlab ... done
[root@k8s-master gitlab]# docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------------------------------------------------
mygitlab /assets/wrapper Up (health: starting) 0.0.0.0:1022->22/tcp,:::1022->22/tcp, 0.0.0.0:443->443/tcp,:::443->443/tcp,
0.0.0.0:81->80/tcp,:::81->80/tcp