在很多公司都同时存在Windows+Linux两种操作系统,在Linux上跑Docker很方便各种资料文档齐全,但是在Windows上跑Docker相对比较少,同时也受到一些限制,如Windows上docker网络问题。下面就介绍下利用Windows IIS跑Web应用,同时使用Haproxy作为负载均衡器。


方案采用Windows原生container、docker swarmkit、负载均衡器采用Haproxy+Etcd+Confd

wKiom1mHyN_AwZdnAAC-39ZeiTg794.jpg

流程说明:

  1. 开发及运维修改dockerfile及代码提交至SVN服务器,同时触发SVN钩子进行镜像构建(Windows项目在Windows构建、Linux项目在Linux构建),并push至我们的Docker私有仓库


  2. 推送镜像之后,钩子同时触发Docker Swarmkit集群的leader服务器进行服务创建或者更新


  3. 启动IIS容器,需要提前启动haproxy+etcd+confd容器,以便服务注册


Windows Docker Swarm初始化集群集群,不可以只使用docker swarm init,而不指定IP及端口,否则就会一直卡在初始化的进程下,正确的姿势如下:

1
docker swarm init --advertise-addr 192.168.2.20:2377  --listen-addr 192.168.2.20:2377

新建overlay网络用于容器之间互联

1
docker network create -d overlay myspace

如果存在测试环境/正式环境,就需要在不同的环境使用不同的overlay网络,同时指定DNS


说明事项:

1.docker swarm的endpoint-mode必须为dnsrr(--endpoint-mode=dnsrr),主要为Windows暂时不支持路由VIP模式

1
docker service create --name=iis --replicas=1 --network=myspace  --endpoint-mode=dnsrr --constraint  "Node.Platform.OS==windows"  microsoft /iis


2.启动IIS服务时必须要指定服务器的OS为windows(--constraint "Node.Platform.OS==windows"),而Linux服务指向Linux


3.服务名称不能包含点(.),否则可能报Error response from daemon: rpc error: code = 3 desc = name must be valid as a DNS name component


4.Windows Docker机器一定要打开相关防火墙接口

1
2
3
4
5
netsh advfirewall firewall add rule name= "swm 2377"  dir = in  action=allow protocol=TCP localport=2377 
netsh advfirewall firewall add rule name= "swm 7946"  dir = in  action=allow protocol=TCP localport=7946
netsh advfirewall firewall add rule name= "swm 7946udp"  dir = in  action=allow protocol=UDP localport=7946
netsh advfirewall firewall add rule name= "swm 4789"  dir = in  action=allow protocol=TCP localport=4789
netsh advfirewall firewall add rule name= "swm 4789udp"  dir = in  action=allow protocol=UDP localport=4789


5.Windows退出swarm集群后再加入swarm集群,将无法再进行工作,原因是HNS无法加载新集群的网络信息,暂未找到处理办法,找到处理办法的同学留言回复下


6.自动注册: 在机器启动的时候,通过curl向etcd服务器添加配置信息

1
2
3
4
5
6
7
8
原版:
for  /f  "tokens=2* delims==:"  %%a  in  ( 'ipconfig^|find /i "10.0"' do 
curl.exe -XPUT http: //haproxy :2379 /v2/keys/web/ %site_name%/%computername% -d value= "%%a:80"
)
BUG修复版:主要是解决主机重复
for  /f  "tokens=2* delims==:"  %%a  in  ( 'ipconfig^|find /i "10.0"' do 
for  /f  "tokens=3,4 delims=."  %%b  in  ( "%%a" do  curl.exe -XPUT http: //haproxy :2379 /v2/keys/web/ %site_name%/%%b%%c -d value= "%%a:80"
)

7.服务回收: 由于服务注册是利用容器启动进行注册,当容器删除后服务不自动回收,这里利用监听Haproxy状态对DOWN 5分中后的容器进行自动回收

1
for  in  ` echo  "show stat"  | socat stdio unix-connect: /var/run/haproxy .sock | grep  - v  "BACKEND" | awk  -F,  '/DOWN/{if ( $25 >300 ) print $1,$2}' | sed  's#_cluster #/#g' `; do  curl -XDELETE http: //127 .0.0.1:2379 /v2/keys/web/ $a; done

8.如果采用hyper-v Container,则Windows Server 2016数据中心版支持不限量容器运行,但是Windows Server 2016标准版只支持2个容器。但是对于上面的操作系统层的容器则无限量支持。


9. IIS Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
FROM microsoft /iis
#install ASP.NET 4.5
RUN dism  /online  /enable-feature  /all  /featurename :NetFx4  /featurename :IIS-ApplicationInit  /featurename :IIS-ASPNET45  /NoRestart
#enable windows eventlog
RUN powershell.exe - command  Set-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\WMI\Autologger\EventLog-Application Start 1
ENTRYPOINT [ "C:\\SetHostsAndStartMonitoring.cmd" ]
RUN  for  %s  in  (TermService,SessionEnv,iphlpsvc,RemoteRegistry,WinHttpAutoProxySvc,SENS,WinRM,LanmanWorkstation,Winmgmt)  do  ( sc config %s start= disabled) && reg add  "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\InetStp\Configuration"  /v  "MaxWebConfigFileSizeInKB"  /t  reg_dword -d  "0"  /f  && reg add  "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp\Configuration"  /v  "MaxWebConfigFileSizeInKB"  /t  reg_dword -d  "0"  /f
VOLUME [ "c:/inetpub/logs/LogFiles" ]
ADD curl.exe  /windows/system32/curl .exe
ENV site_version 4
ENV site_name fengwan.blog.51cto.com
RUN  /windows/system32/inetsrv/appcmd .exe delete site  "Default Web Site/"  &&  /windows/system32/inetsrv/appcmd .exe add apppool  /name : "%site_name%"   /managedRuntimeVersion : ""   /managedPipelineMode :Integrated -queueLength:65535 &&  /windows/system32/inetsrv/appcmd .exe add site  /name : "%site_name%"  /physicalPath : "c:\wwwroot\%site_name%"  -serverAutoStart: true  /bindings :http: // %site_name%:80 &&  /windows/system32/inetsrv/appcmd .exe  set  site  /site .name: "%site_name%"  /[path= '/' ].applicationPool: "%site_name%"
ADD SetHostsAndStartMonitoring.cmd \SetHostsAndStartMonitoring.cmd
ADD webroot C: /wwwroot/fengwan .blog.51cto.com

SetHostsAndStartMonitoring.cmd

1
2
3
4
5
6
::服务注册
for  /f  "tokens=2* delims==:"  %%a  in  ( 'ipconfig^|find /i "10.0"' do 
for  /f  "tokens=3,4 delims=."  %%b  in  ( "%%a" do  curl.exe -XPUT http: //haproxy :2379 /v2/keys/web/ %site_name%/%%b%%c -d value= "%%a:80"
)
::启动IIS服务
c:\ServiceMonitor.exe w3svc