首页 > 代码库 > Docker分离部署MySQL、Nginx+Tomcat复制共享
Docker分离部署MySQL、Nginx+Tomcat复制共享
防伪码:失去只是一种姿势,得到并不等同于幸福
项目需求:
1、nginx容器作为整个架构中前端服务器监听80端口接收用户的jsp页面请求,并将用户的jsp请求分发给tomcat web容器,tomcat容器需要连接mysql数据库容器。
2、nginx容器做为前端服务器可以直接响应用户的静态页面请求,jsp动态页面请求交给tomcat容器处理(静动分离)
3、通过session复制共享:session replication,实现tomcat服务器之间同步session,使session保持一致。
注:http://yw666.blog.51cto.com/11977292/1888747,session复制共享在前文详细讲解过,此处不再赘述。
如图所示:
制作nginx镜像
from docker.io/centos:centos6
add nginx-1.6.0 /nginx-1.6.0
run yum -y install gcc pcre pcre-devel zlib-devel make
run useradd nginx -s /sbin/nologin
run cd /nginx-1.6.0 && ./configure --prefix=/nginx --user=nginx --group=nginx && make && make install
run echo "daemon off;" >> /nginx/conf/nginx.conf
run echo ‘ip1=$(cat /etc/hosts | grep tomcat1 | awk ‘"‘{print "‘$1‘"}‘"‘)‘ >> /7.sh
run echo ‘ip=$(cat /etc/hosts | grep tomcat2 | awk ‘"‘{print "‘$1‘"}‘"‘)‘ >> /7.sh
run echo "sed -i -e ‘33 i"‘\u‘"pstream backend {‘ -e ‘33 i"‘\s‘"erver ""‘"‘$ip‘"‘"":8080 weight=1;‘ -e ‘33 i"‘\s‘"erver ""‘"‘$ip1‘"‘"":8080 weight=1;}‘ -e ‘46 i"‘\p‘"roxy_pass http://backend;‘ /nginx/conf/nginx.conf" >> /7.sh
run echo "/nginx/sbin/nginx" >> /7.sh
cmd source /7.sh
用docker build 生成nginx镜像
制作tomcat镜像
from docker.io/centos:centos6
add apache-tomcat-7.0.54 /apache-tomcat-7.0.54
add jdk1.7.0_65 /jdk1.7.0_65
add 123.sh /123.sh
add profile /profile
run mv /jdk1.7.0_65 /java
run mv /apache-tomcat-7.0.54 /tomcat7
add server.xml /tomcat7/conf/server.xml
add context.xml /tomcat7/conf/context.xml
run cp -rf /profile /etc/profile
run echo ‘ip1=$(ifconfig | grep Bcast | awk ‘"‘{print "‘$2‘"}‘"‘ | awk -F : ‘"‘{print "‘$2‘"}‘)" >> /8.sh
run echo ‘ip2=$(cat /etc/hosts | grep mysql | awk ‘"‘{print "‘$1‘"}‘"‘)‘ >> /8.sh
run echo "sed -i ‘118 "‘i\a‘"ddress="‘"‘"‘"‘$ip1‘"‘"‘"‘"‘"" /tomcat7/conf/server.xml" >> /8.sh
run echo "sed -i ‘23 i"‘\u‘"rl="‘"‘jdbc:mysql://"‘"‘$ip2‘"‘":3306/javatest‘"‘"/>‘ /tomcat7/conf/context.xml" >> /8.sh
add mysql-connector-java-5.1.22-bin.jar /tomcat7/lib/mysql-connector-java-5.1.22-bin.jar
add 456.sh /456.sh
cmd source /456.sh
编写tomcat守护进程脚本
program="/tomcat7/bin/startup.sh"
progress="tomcat"
while true;
do
sleep 10
progremflag=`ps -ef |grep $progress|wc -l`
echo $progremflag
if [ $progremflag -le 10 ];then
$program >/dev/null 2>&1 &
fi
done
编写cmd启动时要执行的脚本
用docker build 生成tomcat镜像
制作mysql镜像
from docker.io/centos:centos6
add cmake-2.8.12 /cmake-2.8.12
add mysql-5.5.38 /mysql-5.5.38
run yum -y install ncurses-devel gcc gcc-c++
run cd /cmake-2.8.12 && ./configure && gmake && gmake install
run cd /mysql-5.5.38 && cmake -DCMAKE_INSTALL_PREFIX=/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -SYSCONFDIR=/etc && make && make install
run rm -rf /etc/my.cnf
run cp /mysql-5.5.38/support-files/my-medium.cnf /etc/my.cnf
run cp /mysql-5.5.38/support-files/mysql.server /mysqld
run chmod 777 /mysqld
run groupadd mysql
run useradd -M -s /sbin/nologin mysql -g mysql
run chown -R mysql:mysql /mysql
run /mysql/scripts/mysql_install_db --user=mysql --basedir=/mysql/ --datadir=/mysql/data/
run ./mysqld start && cd /mysql/bin && echo "grant all privileges on *.* to ‘root‘@‘%.%.%.%‘ identified by ‘123456‘;" | ./mysql -u root && echo "create database javatest;" | ./mysql -u root && echo "create table javatest.yw(id int);" | ./mysql -u root
cmd cd /mysql/bin && ./mysqld_safe
用docker build 生成mysql镜像
至此,镜像都已经做好。
下面开始启动容器
先启动mysql
启动tomcat连接mysql
再启动nginx连接两台tomcat
Docker ps 查看容器启动状态
开始测试
验证tomcat连接mysql
谢谢观看,真心的希望能帮到您!
本文出自 “一盏烛光” 博客,谢绝转载!
Docker分离部署MySQL、Nginx+Tomcat复制共享