Windows 应用容器化

背景

在这个时间点,咱们可能已经对 Linux 容器使用已经达到熟练掌握的程度,由于 Docker 与 Kubernetes 都是最先为 Linux 平台设计。当咱们从容器这项技术中体会到种种收益,对于咱们的 windows 的应用是否也能利用容器技术简化咱们的开发运维?对于大型的企业来讲,Windows 系列的开发程序也会占必定的比例,这个时候领导可能会有一个指示下来:“咱们 .Net 应用也要上容器云”。html

好的,任务拿到之后咱们首先要解决的第一件事就是 Windows 应用容器化,虽然咱们知道 .Net Core 是一个能够跨平台运行,但仍然有不少使用 .Net Framwork 编写的应用仍在运行和迭代,因此 Docker on Windows 是一条必需要走的路,好在微软和 Docker 在这方面有足够的投入。git

小贴士:
  对于企业来讲,转型并非把原来全部的资产所有抛弃,是利用能利用的原有资产和新的技术继续向前进

Windows 容器类型

虽然咱们常说 Container 的实现方案不只只有 Docker, 但咱们在实际使用上用的最最最多仍是 Docker。这里心疼 Docker 三秒钟😭。在 Windows 容器化的实现上分为两类:github

  • Hyper-V 容器
    • 相似于 Docker on Mac, Docker on Windows 也经历了经过基于 Hypervisor 的虚拟化技术来实现非原生 Linux 平台上的容器方案。 Mac 上使用的是 hyperkit ,Windows 上有 Hyper-V
    • 这就至关于每一个容器运行在一个被高度优化过的虚拟机里,他们之间不共享操做系统内核,好处是会有更好的安全隔离性,以及在操做系统的内核上有更多的选择性。
  • Native 容器
    • 相似于咱们在 Linux 上使用的容器,基于 process 和 namespace 的隔离。

这两种不一样的容器类型,从操做角度上是一致的,像Build、Push、Run 等等,不一样的是它是 Windows 环境,须要使用 powershell 或者 cmd 去写 Dockerfile, 固然这个对于 Windows 的运维人员没什么问题。web

Windows Dockerfile 示例

看一个简单的例子:docker

FROM microsoft/windowsservercore:1803

COPY ConsoleTest.exe C:/

ENTRYPOINT C:/ConsoleTest.exe

咱们注意到这个 Dockerfile 的 base 镜像是 windowsservercore:1803 ,意味着这个镜像是能够和 windowsserver 1803 兼容的 Docker 镜像, 这里提到到了一个 Windows Host OS 与 容器 OS 的版本兼容性:shell

Container OS version Host OS Version
Windows Server 2016 Builds: 14393. Windows 10 1609, 1703 Builds: 14393., 15063. Windows Server version 1709 Builds 16299. Windows 10 Fall Creators Update Builds 16299. Windows Server version 1803 Builds 17134. Windows 10 version 1803 Builds 17134.
Windows Server 2016 Builds: 14393. Supports processorhypervisolation Supports Onlyhypervisolation Supports Onlyhypervisolation Supports Onlyhypervisolation Supports Onlyhypervisolation Supports Onlyhypervisolation
Windows Server version 1709 Builds 16299. Not supported Not supported Supports processorhypervisolation Supports Onlyhypervisolation Supports Onlyhypervisolation Supports Onlyhypervisolation
Windows Server version 1803 Builds 17134. Not supported Not supported Not supported Not supported Supports processorhypervisolation Supports Onlyhypervisolation

翻译过是:windows

  1. 相同的 OS 版本能够支持 native container 和 hyperv container
  2. Host OS 版本高,Container OS 版本低,能够用 hyperv container
  3. Container OS 比 Host OS 高? 那就不行了。

再看一个例子:安全

buildapp.ps1app

# Remove existing default web site files
remove-item C:\inetpub\wwwroot\iisstart.*

# Ensure write permissions over web app project files
icacls C:\inetpub\wwwroot\WebTest /grant Everyone:F /t /q

# Import necessary IIS modules then set app project folder as web application
Import-Module IISAdministration
Import-Module WebAdministration

New-Item 'IIS:\Sites\Default Web Site\WebTest' -Type Application -PhysicalPath 'C:\inetpub\wwwroot\WebTest'
Set-WebConfigurationProperty -p 'MACHINE/WEBROOT/APPHOST' -fi 'system.applicationHost/log' -n 'centralLogFileMode' -v 'CentralW3C'; `
Set-WebConfigurationProperty -p 'MACHINE/WEBROOT/APPHOST' -fi 'system.applicationHost/log/centralW3CLogFile' -n 'truncateSize' -v 4294967295; `
Set-WebConfigurationProperty -p 'MACHINE/WEBROOT/APPHOST' -fi 'system.applicationHost/log/centralW3CLogFile' -n 'period' -v 'MaxSize'; `
Set-WebConfigurationProperty -p 'MACHINE/WEBROOT/APPHOST' -fi 'system.applicationHost/log/centralW3CLogFile' -n 'directory' -v 'c:\iislog'

runapp.ps1运维

Start-Service W3SVC; `
Invoke-WebRequest http://localhost -UseBasicParsing | Out-Null; `
netsh http flush logbuffer | Out-Null; `
Get-Content -path 'c:\iislog\W3SVC\u_extend1.log' -Tail 1 -Wait

Dockerfile

FROM microsoft/dotnet-framework:4.7.2-sdk-20180814-windowsservercore-1803

# WebTest.NET dependencies
RUN dism.exe /online /enable-feature /all /featurename:iis-webserver /NoRestart
RUN powershell add-windowsfeature web-asp-net45

# Configure Web App
COPY runapp.ps1 buildapp.ps1 WebTest.zip C:/

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
RUN  powershell -Command { Expand-Archive -Path C:\WebTest.zip -DestinationPath C:\inetpub\wwwroot\WebTest }
RUN  powershell -Command { Remove-Item C:\WebTest.zip -Force }

RUN powershell.exe C:/buildapp.ps1
EXPOSE 80

ENTRYPOINT ["powershell", "C:/runapp.ps1"]

上面的例子作了一件事是把 iis 的文件日志输出经过 tail 的方式转换成了标准输出,这样 docker logs 就能看到日志输出了

提问😂

  1. 什么状况下用 ContainerOS 使用 latest 的 tag?
  2. 若是是在 Kubernetes 的环境下除了经过转换成标准输出,还能怎样采集 iis 的文件日志?

下一篇: 快速搭建 Windows Kubernetes 环境


Ref:

相关文章
相关标签/搜索