一、制作tomcat镜像:
基本架构图:
1、制作JDK镜像
基于官方提供的centos7.2.1511基础镜像构建JDK和tomcat镜像,先构建JDK镜像,然后再基于JDK镜像构建tomcat镜像。
JDK下载路径:https://www.oracle.com/technetwork/java/javase/downloads/index.html
选择架构版本:
(1)先下载基础centos镜像
[[email protected] ~]# docker pull centos
(2)搭建JDK镜像
[[email protected] ~]# mkdir /opt/dockerfile/{web/{nginx,tomcat,jdk,apache},system/{centos,ubuntu,redhat}} -p # 先创建存放镜像目录
(3)切换到指定的centos系统目录下,安装一些常用命令
[[email protected] centos]# cd /opt/dockerfile/system/centos
(4)创建一个Dockerfile文件
[[email protected] centos]# cat Dockerfile FROM centos RUN yum install epel-release -y && yum install vim iotop bc gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel zip unzip zlib-devel net-tools lrzsz tree telnet lsof tcpdump wget libevent libevent-devel bc systemd-devel bash-completion traceroute -y && useradd nginx -u 2019 && useradd tomcat -u 2020 && rm -rf /etc/localtime && ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
(5)创建一个build脚本
[[email protected] centos]# cat build-command.sh #!/bin/bash docker build -t centos-base:7.6.1810 .
(6)执行脚本,创建一个安装基础命令的centos系统镜像
[[email protected] centos]# bash build-command.sh
(7)查看创建好的centos系统镜像
(8)切换到jdk目录下,创建指定的jdk版本目录,制作profile环境变量文件,并制作JDK镜像
切换到指定的jdk目录下,并将下载下来的jdk文件传到/opt/dockerfile/web/jdk/8u92目录下
[[email protected] dockerfile]# cd /opt/dockerfile/web/jdk/ [[email protected] jdk]# mkdir 8u92
创建profile文件
[[email protected] 8u92]# cat profile #在当前目录下创建profile环境变量文件 # /etc/profile # System wide environment and startup programs, for login setup # Functions and aliases go in /etc/bashrc # It‘s NOT a good idea to change this file unless you know what you # are doing. It‘s much better to create a custom.sh shell script in # /etc/profile.d/ to make custom changes to your environment, as this # will prevent the need for merging in future updates. pathmunge () { case ":${PATH}:" in *:"$1":*) ;; *) if [ "$2" = "after" ] ; then PATH=$PATH:$1 else PATH=$1:$PATH fi esac } if [ -x /usr/bin/id ]; then if [ -z "$EUID" ]; then # ksh workaround EUID=`/usr/bin/id -u` UID=`/usr/bin/id -ru` fi USER="`/usr/bin/id -un`" LOGNAME=$USER MAIL="/var/spool/mail/$USER" fi # Path manipulation if [ "$EUID" = "0" ]; then pathmunge /usr/sbin pathmunge /usr/local/sbin else pathmunge /usr/local/sbin after pathmunge /usr/sbin after fi HOSTNAME=`/usr/bin/hostname 2>/dev/null` HISTSIZE=1000 if [ "$HISTCONTROL" = "ignorespace" ] ; then export HISTCONTROL=ignoreboth else export HISTCONTROL=ignoredups fi export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL # By default, we want umask to get set. This sets it for login shell # Current threshold for system reserved uid/gids is 200 # You could check uidgid reservation validity in # /usr/share/doc/setup-*/uidgid file if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then umask 002 else umask 022 fi for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do if [ -r "$i" ]; then if [ "${-#*i}" != "$-" ]; then . "$i" else . "$i" >/dev/null fi fi done unset i unset -f pathmunge export JAVA_HOME=/usr/local/jdk export TOMCAT_HOME=/apps/tomcat export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$TOMCAT_HOME/bin:$PATH export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
创建Dockerfile文件和build脚本
[[email protected] 8u92]# cat Dockerfile FROM centos-base:7.6.1810 MAINTAINER [email protected] ADD jdk-8u192-linux-x64.tar.gz /usr/local/src RUN ln -sv /usr/local/src/jdk1.8.0_192 /usr/local/jdk # 创建软链接之前需要解压文件,查看解压后的目录是什么,再进行软链接 ADD profile /etc/profile ENV JAVA_HOME /usr/local/jdk ENV JRE_HOME $JAVA_HOME/jre ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/ ENV PATH $PATH:$JAVA_HOME/bin [[email protected] 8u92]# cat build-command.sh #!/bin/bash docker build -t jdk-base:1.8.0.192 .
(9)执行命令,创建jdk镜像:
[[email protected] 8u92]# bash build-command.sh
(10)在容器中测试jdk镜像:
[[email protected] 8u92]# docker images #先查看创建好的jdk镜像 REPOSITORY TAG IMAGE ID CREATED SIZE jdk-base 1.8.0.192 830c0d4f0279 6 minutes ago 903MB centos-base 7.6.1810 d12613615be1 23 minutes ago 507MB centos 7.6.1810 0f3e07c0138f 3 months ago 220MB centos latest 0f3e07c0138f 3 months ago 220MB You have new mail in /var/spool/mail/root [[email protected] 8u92]# docker run -it --rm jdk-base:1.8.0.192 bash [[email protected] /]# java -version #然后看看java版本 java version "1.8.0_192" Java(TM) SE Runtime Environment (build 1.8.0_192-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.192-b12, mixed mode)
此时jdk镜像制作完成
2、制作tomcat镜像
tomcat下载:https://archive.apache.org/dist/tomcat/tomcat-8/
1、切换到创建的tomcat目录下,并创建一个tomcat-base目录
[[email protected] tomcat]#cd /opt/dockerfile/web/tomcat [[email protected] tomcat]# mkdir tomcat-base [[email protected] tomcat]# cd tomcat-base/
2、然后在tomcat-base目录下创建Dockerfile和build脚本
[[email protected] tomcat-base]# cat Dockerfile # 创建Dockerfile文件 FROM jdk-base:1.8.0.192 MAINTAINER [email protected] ADD apache-tomcat-8.5.37.tar.gz /apps RUN ln -sv /apps/apache-tomcat-8.5.37 /apps/tomcat [[email protected] tomcat-base]# cat build-command.sh # 创建build脚本 #!/bin/bash docker build -t tomcat-base:8.5.37 . # 执行的脚本是创建一个tomcat-base:8.5.37版本的tomcat
执行build-command.sh脚本
# bash build-command.sh
3、将Dockerfile、build脚本与要执行的tomcat脚本进行分离,因此在/opt/dockerfile/web/tomcat/目录下创建一个目录:tomcat-app1
[[email protected] tomcat]# mkdir tomcat-app1 [[email protected] tomcat]# cd tomcat-app1
4、如果宿主机安装好了tomcat文件,然后修改里边的sever.xml文件,指定index.html文件路径。
vim /etc/tomcat/conf/server.xml #修改本地的server.xml文件并传到此目录下 <Host name="localhost" appBase="/data/tomcat/webapps" unpackWARs="true" autoDeploy="true"> # 指定存放代码路径到 /data/tomcat/webapps目录下
5、创建一个index.html测试页面
[[email protected] tomcat-app1]# cat index.html tomcat web app1 page [[email protected] tomcat-app1]# tar zcvf code.tar.gz index.html #并将指定的代码进行压缩 index.html
6、创建Dockerfile文件和run_tomcat.sh脚本
[[email protected] tomcat-app1]# cat Dockerfile # 创建Dockerfile文件 FROM tomcat-base:8.5.37 maintainer [email protected] ADD code.tar.gz /data/tomcat/webapps/app1 # webapps默认下面访问的是ROOT目录下的文件,如果创建的是ROOT目录,访问网页时,不需要指定ROOT目录,都是缺省路径,如果访问app1目录代码,需要将代码放在app1目录下 ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh ADD server.xml /apps/tomcat/conf RUN chown -R tomcat.tomcat /apps/apache-tomcat-8.5.37 /apps/tomcat /data/tomcat # 修改tomcat相关文件的权限 EXPOSE 8080 8443 CMD ["/apps/tomcat/bin/run_tomcat.sh"] #调用run_tomcat.sh脚本 [[email protected] tomcat-app1]# cat run_tomcat.sh # 创建run_tomcat.sh脚本 #!/bin/bash source /etc/profile echo "1.2.1.3 www.google.net" >> /etc/hosts # 可以修改 su - tomcat -c "/apps/tomcat/bin/catalina.sh start" #以后台运行 #su - tomcat -c "/apps/tomcat/bin/catalina.sh run" #以前台运行 tail -f /etc/hosts # 为了测试拉起容器,执行脚本时,可以运行起tomcat,没有此命令,就会运行完容器又会退出tomcat服务 [[email protected] tomcat-app1]# cat build-command.sh # 创建build脚本 #!/bin/bash docker build -t tomcat-app1:v1 .
7、执行build-command.sh脚本
# chmod +x run_tomcat.sh # 给脚本加上执行权限 # bash build-command.sh
8、创建的tomcat-app1目录下的文件:
[[email protected] tomcat-app1]# ls build-command.sh code.tar.gz Dockerfile index.html run_tomcat.sh server.xml
9、启动docker容器里的tomcat-app1:v1的镜像
[[email protected] tomcat-app1]# docker images #查看所有的镜像 REPOSITORY TAG IMAGE ID CREATED SIZE tomcat-app1 v1 98e44ecbf074 2 hours ago 931MB tomcat-base 8.5.37 21ab497620f2 2 hours ago 917MB jdk-base 1.8.0.192 830c0d4f0279 3 hours ago 903MB centos-base 7.6.1810 d12613615be1 3 hours ago 507MB centos 7.6.1810 0f3e07c0138f 3 months ago 220MB centos latest 0f3e07c0138f 3 months ago 220MB [[email protected] tomcat-app1]# docker run -it --rm -p 8080:8080 tomcat-app1:v1 bash # 映射8080端口,才能启动tomcat服务 [[email protected] /]# su - tomcat -c "/apps/tomcat/bin/catalina.sh start" # 启动tomcat服务 Using CATALINA_BASE: /apps/tomcat Using CATALINA_HOME: /apps/tomcat Using CATALINA_TMPDIR: /apps/tomcat/temp Using JRE_HOME: /usr/local/jdk Using CLASSPATH: /apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jar Tomcat started.
10、查看测试页面,此时就可以访问了
此时JAVA业务镜像制作成功!!!!
如果想创建第二个服务怎么办?
答:方法很简单,只需要将tomcat-app1目录复制一份,修改里边的build-command.sh、index.html文件即可
[[email protected] tomcat-app1]# pwd /opt/dockerfile/web/tomcat/tomcat-app1 [[email protected] tomcat-app1]# cd .. [[email protected] tomcat]# ls tomcat-app1 tomcat-base [[email protected] tomcat]# cp -r tomcat-app1/ tomcat-app2 # 复制 tomcat-app1为tomcat-app2 [[email protected] tomcat]# cd tomcat-app2
修改index.html文件
# vim index.html tomcat web app2 page
修改build-command.sh文件,修改后执行此脚本创建的tomcat镜像就是tomcat-app2:v1
#!/bin/bash docker build -t tomcat-app2:v1 .
将index.html文件打包
[[email protected] tomcat-app2]# tar -zcvf code.tar.gz index.html index.html
执行build-command.sh脚本
# basn build-command.sh
进入容器启动tomcat-app2:v1镜像,启动tomcat服务
[[email protected] /]# su - tomcat -c "/apps/tomcat/bin/catalina.sh start" Using CATALINA_BASE: /apps/tomcat Using CATALINA_HOME: /apps/tomcat Using CATALINA_TMPDIR: /apps/tomcat/temp Using JRE_HOME: /usr/local/jdk Using CLASSPATH: /apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jar Tomcat started.
访问网页测试效果,访问成功!!!
此时不同的tomcat镜像制作成功!!!!
实战二、源码编译安装haproxy
1、官网下载haproxy源码包:https://www.haproxy.org/
[[email protected] haproxy]# cd /opt/dockerfile/web/haproxy #切换到此目录下 [[email protected] haproxy]# cat Dockerfile #创建Dockerfile文件 FROM centos-base:7.6.1810 #从上面创建好的centos基础镜像进行调用 MAINTAINER liu [email protected] ADD haproxy-1.8.17.tar.gz /usr/local/src #解压下载的haproxy包 RUN yum groupinstall "development tools" -y # 安装开发包组 RUN yum install systemd-devel -y && cd /usr/local/src/haproxy-1.8.17 && make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy && make install PREFIX=/usr/local/haproxy && cp haproxy /usr/sbin/ && mkdir /usr/local/haproxy/run ADD haproxy.cfg /etc/haproxy/haproxy.cfg # 添加自己需要修改的配置文件 ADD run_haproxy.sh /usr/bin/run_haproxy.sh # 指定脚本放在哪个目录下 EXPOSE 80 9999 CMD ["/usr/bin/run_haproxy.sh"] [[email protected] haproxy]# cat built-command.sh #创建built-command.sh脚本 #!/bin/bash docker build -t centos-haproxy:v1.8.17 . #创建centos-haproxu:v1.8.17tag的镜像 [[email protected] haproxy]# #!/bin/bash [[email protected] haproxy]# cat run_haproxy.sh #创建run_haproxy.sh脚本 #!/bin/bash haproxy -f /etc/haproxy/haproxy.cfg # 执行此脚本,运行haproxy tail -f /etc/hosts
2、需要将指定的配置文件都存放在此目录下
[[email protected] haproxy]# pwd /opt/dockerfile/web/haproxy [[email protected] haproxy]# ls built-command.sh Dockerfile haproxy-1.8.17.tar.gz haproxy.cfg run_haproxy.sh #指定需要的文件
3、执行build-command.sh脚本,创建haproxy镜像
# chmod +x run_haproxy.sh #将 run_haproxu,sh加上执行权限,否则无法在容器里运行镜像 # bash build-command.sh
4、运行docker容器里的镜像
[[email protected] haproxy]# docker run -it --rm -p 80:80 -p 9999:9999 centos-haproxy:v1.8.17 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.2 a0dcecc6a36a
5、进行网页测试
此时haproxy源码编译的镜像完成!!!
实战三、实现一次构建,到处运行的镜像
架构图:
环境准备:
类型 | IP地址 | 服务器角色 |
A | 192.168.7.100 | tomcat-app1 |
B | 192.168.7.101 | tomcat-app2 |
C | 192.168.7.102 | HAProxy调度器 |
1、在A主机将以上的实验tomcat、haproxy镜像导出来
[[email protected] ~]# docker save tomcat-app1:v1 > /opt/tomcat-app1.tar.gz [[email protected] ~]# docker save tomcat-app2:v1 > /opt/tomcat-app2.tar.gz [[email protected] ~]# docker save centos-haproxy:v1 > /opt/haproxy.tar.gz
2、导出来的镜像传到其他B和C主机,并在其他主机进行导入制作好的镜像
[[email protected] ~]# scp /opt/tomcat-app1.tar.gz 192.168.7.101: [[email protected] ~]# scp /opt/haproxy.tar.gz 192.168.7.101:
3、在B和C主机上安装docker-ce的yum源仓库,然后再进行安装docker-ce
[[email protected] haproxy-1.8.17]# cd /etc/yum.repos.d/ #切换到yum仓库 [[email protected] yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #下载阿里云官网docker-ce的yum源 [[email protected] haproxy-1.8.17]# systemctl start docker 启动docker [[email protected] haproxy-1.8.17]# systemctl enable docker
4、将传递过来的镜像导入到docker容器中
[[email protected] ~]# docker load -i tomcat-app2.tar.gz
5、启动tomcat-app1和tomcat-app2的镜像服务
[[email protected] opt]# docker run -d -it --rm -p 8801:8080 tomcat-app1:v1 # 启动A主机的tomcat服务 [[email protected] ~]# docker run -it -d --rm -p 8802:8080 tomcat-app2:v1 # 启动B主机的tomcat服务 9b4a5defaffecccf7a269b95850cb514b001cada6cbe468fdf895efb4567476a
6、修改A主机的haproxy配置文件,指向两个后端的Tomcat服务器
vim /opt/dockerfile/web/haproxy/haproy.cfg
global maxconn 100000 chroot /usr/local/haproxy #stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin uid 99 gid 99 daemon #nbproc 2 #cpu-map 1 0 #cpu-map 2 1 pidfile /usr/local/haproxy/run/haproxy.pid log 127.0.0.1 local3 info defaults option http-keep-alive option forwardfor maxconn 100000 mode http timeout connect 300000ms timeout client 300000ms timeout server 300000ms listen stats mode http bind 0.0.0.0:9999 stats enable log global stats uri /haproxy-status stats auth haadmin:123456 listen web_port bind 0.0.0.0:80 mode http log global server web1 192.168.7.100:8801 check inter 3000 fall 2 rise 5 # 指向后端的服务器 server web2 192.168.7.101:8802 check inter 3000 fall 2 rise 5 # 指向后端服务器
7、A主机重新制作haproxy镜像
# bash build-command.sh
8、将A主机的haproxy镜像传到C服务器上,然后倒入到容器中
# scp centos-haproxy.tar.gz 192.168.7.102: # docker load -i centos-haproxy.tar.gz # 将文件传到容器中
9、启动C主机的haproxy服务
[[A[[email protected] haproxy]# docker run -it -d --rm -p 80:80 -p 9999:9999 centos-haproxy:v1.8.17 c1c4f85050b3d4ee8ab5323536d7636d511ba9835b858ee7814f5b218f699e1d
访问测试页面,此时就实现了基本的轮询调度访问后端tomcat服务,此实验完成!!!
原文地址:https://www.cnblogs.com/struggle-1216/p/12193479.html