部署和管理Nano Server

Nano Server是一种可远程管理的服务器操作系统,专门优化用于运行在云和数据中心中,类似Windows Server的Server Core模式,但是更加小很多,甚至连本地的PowerShell界面都不提供。不能本地登录,也不支持远程桌面。仅仅占用很少的磁盘空间(根据所安装的模块多少,从300MB到600MB不等),安装速度也非常快。更加重要的是相对与普通的Windows Server甚至Server Core模式的Windows Server,都大大减少了补丁的数量,从而很少需要重新启动。

可以看出Nano Server是下面这些场景理想的操作系统:

  • 计算节点(运行Hyper-V,可以是群集)
  • 存储节点(Scale-Out File Server,可以是群集)
  • 云端的Container或者虚拟机的操作系统

这篇博客不是一个有关Nano Server的详尽的部署手册,只是用于分享一些部署和管理的经验。完整的文档见https://technet.microsoft.com/en-us/library/mt126167.aspx?f=255&MSPPError=-2147217396 。这里还经实测修正了一些命令。

 

步骤一:创建可以引导的Windows Server Nano 2016 VHD文件

1. 下载下面两个PowerShell脚本。
    Convert-WindowsImage.ps1 https://gallery.technet.microsoft.com/scriptcenter/Convert-WindowsImageps1-0fe23a8f
    New-NanoServerVHD.ps1 https://gallery.technet.microsoft.com/scriptcenter/Create-a-New-Nano-Server-61f674f1/view/Reviews

2. 创建一个文件夹,我这里用的是F:\NanoServer。将上面两个脚本复制到这个文件夹。

3. 下载Windows Server 2016 Technical Preview ISO (https://www.microsoft.com/en-us/evalcenter/evaluate-windows-server-technical-preview) 到本地某个文件夹,我这里用的是D:\ISO

4. 以管理员权限打开一个PowerShell窗口,进入上面的创建的文件夹(F:\NanoServer)。

5. 运行命令
.\New-NanoServerVHD.ps1 -serveriso D:\ISO\10074.0.150424-1350.fbl_impressive_SERVER_OEMRET_X64FRE_EN-US.ISO-destvhd f:\nanoserver\Nano-msg8-3.vhd -computername nano-msg8-3 -AdministratorPassword 'pass@word1' -Packages 'Storage','OEM-Drivers','Guest'
同脚本说明中的范例不同,我这里没有指定IP地址,因为在我这里是通过DHCP分配IP地址的。

 

步骤二:向VHD中添加额外的安装包(这里仅用于说明如何添加,你可以根据自己需要在上面创建VHD的时候就添加,也可以通过下面的步骤添加)

1. 在F:\NanoServer下再创建一个子文件夹DISM,然后从Windows Server 2016 Technical Preview的光盘中的Source书目录中将下面的文件复制到新建的DISM目录中。

  • api*downlevel*.dll
  • *dism*
  • *provider*

2. 复制光盘上的Packages目录到F:\NanoServer

3. 运行下面的命令给上面创建的VHD添加相应的安装包。

cd \NanoServer

md mountdir

dism\dism /Mount-Image /ImageFile:F:\NanoServer\Nano-msg8-3.vhd /Index:1 /MountDir:.\mountdir

dism\dism /Add-Package /PackagePath:.\packages\Microsoft-NanoServer-Compute-Package.cab /Image:.\mountdir

dism\dism /Add-Package /PackagePath:.\packages\en-us\Microsoft-NanoServer-Compute-Package.cab /Image:.\mountdir

dism\dism /Add-Package /PackagePath:.\packages\Microsoft-NanoServer-OEM-Drivers-Package.cab /Image:.\mountdir

dism\dism /Add-Package /PackagePath:.\packages\en-US\Microsoft-NanoServer-OEM-Drivers-Package.cab /Image:.\mountdir

 

 步骤三:配置第一次启动时执行的命令

1. 创建一个启动脚本"SetupComplete.cmd"包含下面的内容。我这里的脚本手工地给两块网卡指定了DNS服务器(DHCP分配的DNS服务器没有我稍后要加入域的信息),并且关闭了防火墙,同时还在启动后显示当前的IP地址信息。

netsh interface ip set dnsservers name="Ethernet" static 10.190.165.99 primary
netsh interface ip add dnsservers name="Ethernet" 10.156.51.2 index=2
netsh interface ip set dnsservers name="Ethernet 2" static 10.190.165.99 primary
netsh interface ip add dnsservers name="Ethernet 2" 10.156.51.2 index=2
netsh advfirewall set domainprofile state off
netsh advfirewall set privateprofile state off
netsh advfirewall set publicprofile state off
netsh advfirewall set currentprofile state off
ipconfig

 2. 运行下面的命令将上面的脚本放入VHD中

md .\mountdir\Windows\Setup
md .\mountdir\Windows\Setup\Scripts
copy .\SetupComplete.cmd .\mountdir\Windows\Setup\Scripts

3. 卸载VHD并保存更改

 dism\dism /Unmount-Image /MountDir:.\MountDir /Commit

 

步骤四:从上面的VHD启动

1. 在目标服务器上运行下面的命令创建另一个VHD引导记录。

bcdedit /copy {current} /d "VHD: Windows Server 2016 Nano"
bcdedit /set {GUID generated from the first command} device vhd=[F:]\NANOSERVER\NANO-MSG8-3.vhd
bcdedit /set {GUID generated from the first command} osdevice vhd=[F:]\NANOSERVER\NANO-MSG8-3.vhd
bcdedit /set {GUID generated from the first command} detecthal on
bcdedit /set {GUID generated from the first command} hypervisorlaunchtype auto

2. 将上面的引导设置为默认引导记录

3. 重新启动服务器

 

步骤五:加入域

1. 在一个已经加入域的Windows Server 2016服务器上,打开一个命令行窗口,执行下面的命令获取一个数据文件。
djoin.exe /provision /domain red.cloud /machine nano-msg8-3 /savefile \odjblob

2. 将上面的odjblob文件复制到Nano Server上。
net use Z: \\nano-msg8-3\c$ pass@word1 /USER:nano-msg8-3\administrator
md z:\temp
copy odjblob z:\temp

3. 运行下面的命令将Nano Server加入到可信任的主机列表中。
Set-Item WSMan:\localhost\Client\TrustedHosts "nano-msg8-3"

4. 打开一个远程PowerShell会话
Enter-PSSession -ComputerName nano-msg8-3 -Credential nano-msg8-3\Administrator

5. 将Nano Server加入域并重启
djoin /requestodj /loadfile c:\Temp\odjblob /windowspath c:\windows /localos
shutdown /r /t 5
Exit-PSSession 

 

步骤六:启用远程管理

1. Nano Server重启以后,再次进入远程PowerShell会话
Enter-PSSession -ComputerName nano-msg8-3 -Credential nano-msg8-3\Administrator

2. 此时可以将一个域用户添加到本地管理员组中
chcp 65001
Winrs –r:nano-msg8-3 –u:nano-msg8-3\Administrator –p:pass@word1 net localgroup Administrators /add red\administrator

3. 启用CredSSP
winrm s winrm/config/service/auth @{CredSSP="true"} -r:nano-msg8-3 -u:red\administrator /p:pass@word1

 

此时域用户已经可以通过远程管理工具(如服务器管理器,Hyper-V管理器等)来管理这台Nano Server了。