一. 创建容器
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 登录共享的问题即单点登录的问题
四. Redis 存储对象
五.
ubuntu16.04 tomcat 集群
来源: http://www.bubuko.com/infodetail-2488771.html