啟動docker服務(wù)斗忌,拉取tomcat觉啊,nginx鏡像
本文是基于 win10 環(huán)境進(jìn)行集成,至于怎么在win10環(huán)境上體驗docker,可自行百度鲫骗,本文不在闡述犬耻。
安裝好docker服務(wù)之后,啟動docker服務(wù)执泰,運(yùn)行如下命令枕磁,拉取tomcat,nginx鏡像:
docker pull hub.c.163.com/library/tomcat:latest
docker pull hub.c.163.com/library/nginx:latest
鏡像拉取成功之后术吝,運(yùn)行 docker images命令查看拉取的鏡像:
λ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest c5355f8853e4 4 weeks ago 107MB
hub.c.163.com/library/ubuntu latest ccc7a11d65b1 8 months ago 120MB
hub.c.163.com/library/tomcat latest 72d2be374029 8 months ago 292MB
hub.c.163.com/library/nginx latest 46102226f2fd 12 months ago 109MB
啟動tomcat容器计济,掛載本地目錄
docker run -it -p 58080:8080 -v E:local_data:/data --name tomcat_02 hub.c.163.com/library/tomcat /bin/bash
運(yùn)行上述命令,啟動容器排苍,將宿主機(jī)的 E:local_data 掛載到容器的 /data 目錄下沦寂。將打包好的war包放在 E:local_data目錄下,使用如下命令將war包復(fù)制到 tomcat 的 webapps 下淘衙,并啟動tomcat传藏。
cp /data/nginx_tomcat-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/sh /usr/local/tomcat/bin/startup.sh
重復(fù)上述操作,連續(xù)啟動三個tomcat容器彤守,如下所示:
λ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
175d6f2be242 hub.c.163.com/library/tomcat "/bin/bash" 7 hours ago Up 7 hours 0.0.0.0:48080->8080/tcp tomcat_03
11d5b4487c66 hub.c.163.com/library/tomcat "/bin/bash" 7 hours ago Up 7 hours 0.0.0.0:38080->8080/tcp tomcat_02
c77cd2b2a6e9 hub.c.163.com/library/tomcat "/bin/bash" 7 hours ago Up 7 hours 0.0.0.0:58080->8080/tcp tomcat_01
啟動nginx并配置
docker run -p 80:80 --privileged=true -v E:my_nginxconfig ginx.conf:/etc/nginx/nginx.conf --name mynginx hub.c.163.com/library/nginx
將nginx的80端口映射到宿主機(jī)的80端口毯侦,將宿主機(jī)的 E:my_nginxconfig ginx.conf 目錄掛載到容器的 /etc/nginx/nginx.conf 目錄下,用自定義 nginx.conf配置文件覆蓋默認(rèn)配置文件具垫。
配置文件如下所示:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
server {
listen 80;
location / {
proxy_pass http://blance;
}
}
upstream blance{
server 172.17.0.2:8080;
server 172.17.0.3:8080;
server 172.17.0.4:8080;
}
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
其中侈离,以下為在默認(rèn)配置內(nèi)容中新增的內(nèi)容
server {
listen 80;
location / {
proxy_pass http://blance;
}
}
upstream blance{
server 172.17.0.2:8080;
server 172.17.0.3:8080;
server 172.17.0.4:8080;
}
運(yùn)行啟動命令,啟動nginx容器做修。
查看所有啟動容器霍狰,測試是否均衡負(fù)載
運(yùn)行 docker ps 命令查看所有正在運(yùn)行的容器:
λ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b0d477164bed hub.c.163.com/library/nginx "nginx -g 'daemon of…" 35 minutes ago Up 35 minutes 0.0.0.0:80->80/tcp mynginx
175d6f2be242 hub.c.163.com/library/tomcat "/bin/bash" 7 hours ago Up 7 hours 0.0.0.0:48080->8080/tcp tomcat_03
11d5b4487c66 hub.c.163.com/library/tomcat "/bin/bash" 7 hours ago Up 7 hours 0.0.0.0:38080->8080/tcp tomcat_02
c77cd2b2a6e9 hub.c.163.com/library/tomcat "/bin/bash" 7 hours ago Up 7 hours 0.0.0.0:58080->8080/tcp tomcat_01
瀏覽器訪問 127.0.0.1:80 ,進(jìn)入tomcat 歡迎頁面饰及。
多次訪問 127.0.0.1/nginx_tomcat-0.0.1-SNAPSHOT/test/loadBalancing 鏈接蔗坯,會打印請求到的服務(wù)的ip及端口,
java代碼如下:
/**
- Created by Haiyoung on 2018/5/1.
*/@RestController@RequestMapping( value = "/test")public class LoadBalancing {
@RequestMapping( value = "/loadBalancing")
public String loadBalancingTest(HttpServletRequest request) throws UnknownHostException {
String ip = java.net.InetAddress.getLocalHost().getHostAddress();
int post = request.getLocalPort();
System.out.println(ip + ":" + post);
return ip + ":" + post;
}}
請求結(jié)果如下所示:
172.17.0.2:8080
172.17.0.3:8080
172.17.0.4:8080
172.17.0.2:8080
172.17.0.3:8080
172.17.0.4:8080
172.17.0.2:8080
可見燎含,nginx實現(xiàn)了對請求的負(fù)載均衡宾濒。