一.创建容器
1.Dockerfile 版本指定7.0
from tomcat:7.0.82
COPY sources.list /etc/apt/sources.list
EXPOSE 8080
2.sources.list 源
# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable
3.生成tomcat镜像
docker build -t pic:latest .
4.生成两个tomcat容器
#pic1 容器
docker run -v /home/eva/src1:/usr/local/tomcat/webapps --name pci1 -d -p 18080:8080 pic:latest
#pic2容器
docker run -v /home/eva/src2:/usr/local/tomcat/webapps --name pci2 -d -p 28080:8080 pic:latest
然后把项目war包,分别拷贝到src1和src2目录下
5.生成redis容器
docker run --name redis -d -p 6379:6379 redis:latest
6.查看所有正在运行的容器,停止,启动,重启
root@ubuntuserver:/home/eva# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dd60175f3224 pic:latest "catalina.sh run" 9 seconds ago Up 9 seconds 0.0.0.0:28080->8080/tcp pci2
81e115052997 pic:latest "catalina.sh run" 39 seconds ago Up 39 seconds 0.0.0.0:18080->8080/tcp pci1
9ea719f54b31 redis:latest "docker-entrypoint.s…" 10 hours ago Up 14 minutes 0.0.0.0:6379->6379/tcp redis
root@ubuntuserver:/home/eva# docker stop dd6
dd6
root@ubuntuserver:/home/eva# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dd60175f3224 pic:latest "catalina.sh run" 19 seconds ago Exited (143) 2 seconds ago pci2
81e115052997 pic:latest "catalina.sh run" 49 seconds ago Up 48 seconds 0.0.0.0:18080->8080/tcp pci1
9ea719f54b31 redis:latest "docker-entrypoint.s…" 10 hours ago Up 14 minutes 0.0.0.0:6379->6379/tcp redis
root@ubuntuserver:/home/eva# docker start dd6
dd6
root@ubuntuserver:/home/eva# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dd60175f3224 pic:latest "catalina.sh run" 26 seconds ago Up 1 second 0.0.0.0:28080->8080/tcp pci2
81e115052997 pic:latest "catalina.sh run" 56 seconds ago Up 55 seconds 0.0.0.0:18080->8080/tcp pci1
9ea719f54b31 redis:latest "docker-entrypoint.s…" 10 hours ago Up 14 minutes 0.0.0.0:6379->6379/tcp redis
root@ubuntuserver:/home/eva# docker restart 81e11
81e11
root@ubuntuserver:/home/eva# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dd60175f3224 pic:latest "catalina.sh run" 34 seconds ago Up 10 seconds 0.0.0.0:28080->8080/tcp pci2
81e115052997 pic:latest "catalina.sh run" About a minute ago Up 1 second 0.0.0.0:18080->8080/tcp pci1
9ea719f54b31 redis:latest "docker-entrypoint.s…" 10 hours ago Up 15 minutes 0.0.0.0:6379->6379/tcp redis
root@ubuntuserver:/home/eva#
二.nginx 负载均衡配置
1.在nginx.conf中的server{}节点上面添加
#和 sever节点同级
include ttt/*.conf;
2.在nginx.conf 同级创建ttt目录,里面添加aslan.conf配置文件
upstream www.aslan.com{
server 127.0.0.1:18080 weight=1;
server 127.0.0.1:28080 weight=1;
}
server {
listen 80;
server_name wwww.aslan.com;
location / {
proxy_pass http://www.aslan.com;
}
}
3.检查nginx配置文件是否有问题
root@ubuntuserver:/usr/local/nginx/sbin# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
4.重启nginx
root@ubuntuserver:/usr/local/nginx/sbin# ./nginx -s reload
root@ubuntuserver:/usr/local/nginx/sbin#
5.访问nginx
负载均衡没问题
三.Cookie的使用
(1)在用户登录的时候,生成名为aslan_login_token的cookie。
(2)在获取用户信息的时候,检查名为aslan_login_token的cookie的值。如果有值解析出来对于的值(暂为user)
比如用户登录时在pic1服务器,在获取用户信息的时候被分配到了pic2服务器。通过使用aslan_login_token解决session登录共享的问题。即单点登录的问题。