Docker + redis + nginx + tomcat
环境
pc机
机器
192.168.1.242
系统
[[email protected] ~]# uname -a
Linux hdp-gp-dk02 2.6.32-504.el6.x86_64 #1 SMP Tue Sep 16 01:56:35 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux
[[email protected] ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.6 (Santiago)
简介
一个Web应用,使用Docker容器来部署应用,并将Session交给Redis来存储和管理,涉及到Docker/Redis/Tomcat/Nginx/Spring Web/Spirng Web等技术。其中:
Docker——容器技术或虚拟化技术,可以将我们的application及相关依赖打包到一个容器内,方便移植、集群部署,容器完全使用沙箱机制,容器之间互不影响完全独立。下文所有的server都是部署在Docker中。
安装包(jar包)
jdk-7u79-linux-x64.tar.gz
redis-3.0.6.tar.gz
apache-tomcat-7.0.56.tar.gz
nginx-1.8.0.tar.gz
commons-pool-1.5.4.jar
commons-pool2-2.4.1.jar
jedis-2.6.2.jar
tomcat-juli.jar
tomcat-juli-adapters.jar
tomcat-redis-session-manager1.2.jar
Docker镜像基础环境 centos
安装docker
省略,前面已经给出;见我的博客:http://daxionglaiba.blog.51cto.com/11790757/1830289
下载centos镜像
Docker pull centos
Docker安装tomcat 并制作成镜像
安装jdk,配置环境变量
1. 下载jdk-7u79-linux-x64.tar.gz
2. 上传jdk-7u79-linux-x64.tar.gz
3. 解压tar zxvf jdk-7u79-linux-x64.tar.gz
4. docker容器中移动解压目录,修改环境变量
mv jdk1.7.0_79 /usr/local
vim .bash_profile
export JAVA_HOME=/usr/local/jdk1.7.0_79
export JAVA_BIN=$JAVA_HOME/bin
export JRE_HOME=$JAVA_HOME/jre
export PATH=$JAVA_HOME/bin:$PATH
#export CLASSPATH=./:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JRE_HOME/lib/tools.jar
5. 更新bash_profile
source .bash_profile
Docker安装tomcat
1. 下载apache-tomcat-7.0.56.tar.gz
2. 上传apache-tomcat-7.0.56.tar.gz
3. 解压tar zxvf apache-tomcat-7.0.56.tar.gz
4. tomcat开启和关闭
开启tomcat
/usr/local/tomcat/apache-tomcat-7.0.56/bin/shutdown.sh
关闭tomcat
/usr/local/tomcat/apache-tomcat-7.0.56/bin/startup.sh
将tomcat容器制作成镜像
1. 生成镜像:
docker commit(tomcat容器id) tomcat_gbicc/v1
2. 查看镜像:
docker images
3. 导出镜像:
docker save tomcat_gbicc/v1 | bzip2 -9 -c>tomcat_gbicc_v1.tar.bz2
Docker安装nginx 并制作成镜像
安装jdk,配置环境变量
1. 下载jdk-7u79-linux-x64.tar.gz
2. 上传jdk-7u79-linux-x64.tar.gz
3. 解压tar zxvf jdk-7u79-linux-x64.tar.gz
4. docker容器中移动解压目录,修改环境变量
mv jdk1.7.0_79 /usr/local
vim .bash_profile
export JAVA_HOME=/usr/local/jdk1.7.0_79
export JAVA_BIN=$JAVA_HOME/bin
export JRE_HOME=$JAVA_HOME/jre
export PATH=$JAVA_HOME/bin:$PATH
#export CLASSPATH=./:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JRE_HOME/lib/tools.jar
5. 更新bash_profile
source .bash_profile
Docker安装nginx
1. 下载nginx-1.8.0.tar.gz
2. 上传nginx-1.8.0.tar.gz
3. 解压tar zxvf nginx-1.8.0.tar.gz
4. 安装所需要的包
yum -y install gcc* automake autoconf libtool make
安装openssl
wget http://www.openssl.org/source/openssl-1.0.1c.tar.gztar -zxvf openssl-1.0.1c.tar.gz
cd openssl-1.0.1c/
./configmake make install
安装zlib
wget http://zlib.net/zlib-1.2.8.tar.gztar -zxvf zlib-1.2.8.tar.gzcd zlib-1.2.8./configuremake make install
进去nginx-1.8.0
./configure --prefix=/home/nginx/nginx$ make$ make install
5. nginx开启和关闭
安装完成之后生成一个nginx的文件夹,cd进去并切换到sbin目录
开启nginx
./nginx
关闭nginx
./nginx -s stop
重启nginx
./nginx -s reload
将nginx容器制作成镜像
1. 生成镜像:
docker commit(nginx容器id) nginx_gbicc/v1
2. 查看镜像:
docker images
3. 导出镜像:
docker save nginx_gbicc/v1 | bzip2 -9 -c>nginx_gbicc_v1.tar.bz2
Docker安装redis 并制作成镜像
安装jdk,配置环境变量
1. 下载jdk-7u79-linux-x64.tar.gz
2. 上传jdk-7u79-linux-x64.tar.gz
3. 解压tar zxvf jdk-7u79-linux-x64.tar.gz
4. docker容器中移动解压目录,修改环境变量
mv jdk1.7.0_79 /usr/local
vim .bash_profile
export JAVA_HOME=/usr/local/jdk1.7.0_79
export JAVA_BIN=$JAVA_HOME/bin
export JRE_HOME=$JAVA_HOME/jre
export PATH=$JAVA_HOME/bin:$PATH
#export CLASSPATH=./:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JRE_HOME/lib/tools.jar
5. 更新bash_profile
source .bash_profile
Docker安装redis
1. 下载redis-3.0.6.tar.gz
2. 上传redis-3.0.6.tar.gz
3. 解压tar zxvf redis-3.0.6.tar.gz
4. 安装gcc、c++环境等所需要的包
yum -y install gcc* automake autoconf libtool make gcc-c++
进去redis-3.0.6
$ make MALLOC=libc$ make install
修改配置文件
mkdir -p /usr/local/redis/log
mkdir -p /usr/local/redis/pid
mkdir -p /usr/local/redis/db
cp /usr/local/src/redis/redis.conf /usr/local/redis/redis.conf
vim /usr/local/redis/redis.conf
daemonize yes
pidfile /usr/local/redis/pid/redis.pid
logfile /usr/local/redis/log/redis.log
dir /usr/local/redis/db
timeout 60
添加redis服务开机启动:
vim /etc/init.d/redis
#!/bin/sh
# chkconfig: 2345 60 40
# Description: Start and Stop redis
# Provides: redis
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
REDIS_CLI=/usr/local/redis/bin/redis-cli
PIDFILE= /usr/local/redis/pid
CONF="/usr/local/redis/redis.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping Redis server..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis is stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
chmod a+x /etc/init.d/redis
chkconfig --add redis
chkconfig --level 2345 redis on
chkconfig --list | grep redis
5. redis开启和关闭
开启redis
/etc/init.d/redis start
关闭redis
/etc/init.d/redis stop
重启redis
/etc/init.d/redis restart
将redis容器制作成镜像
1. 生成镜像:
docker commit(redis容器id) redis_gbicc/v1
2. 查看镜像:
docker images
3. 导出镜像:
docker save redis_gbicc/v1 | bzip2 -9 -c>redis_gbicc_v1.tar.bz2
Redis管理Session+Nginx负载均衡+Docker+Tomcat
1. 修改tomcat的配置
要用Redis来存储和管理Session,我们需要修改content.xml,来改变Tomcat
的Session管理方式,如下:
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="172.17.0.25"
port="6379"
database="0"
maxInactiveInterval="60"/>
</Context>
Redis服务器地址和端口、Session有效时间按自己需要配置。
复制上面提到的jar文件到{Tomcat}/lib目录
commons-pool-1.5.4.jar
commons-pool2-2.4.1.jar
jedis-2.6.2.jar
tomcat-juli.jar
tomcat-juli-adapters.jar
tomcat-redis-session-manager1.2.jar
2. 修改nginx的配置
用Nginx做负载均衡;
在http段里面加上如下配置:
upstream 172.17.0.18 {
server 172.17.0.26:8080 max_fails=3 fail_timeout=3s weight=1;
server 172.17.0.30:8080 max_fails=3 fail_timeout=3s weight=2;
keepalive 65;
}
server {
listen 80;
server_name 172.17.0.18;
location / {
root html;
index index.html index.htm;
proxy_pass http://172.17.0.18;
# proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
总结:
上图为docker-容器的ip;使用所有的服务都需要映射出其端口;在开启容器的时候开启就行。