Get-EventLog -LogName System -Source Microsoft-Windows-Hyper-V-VmSwitch -After '2021-09-22 23:07:00' | Select-Object -Property Index,EntryType,EventID,TimeGenerated,Source,Message | Export-CSV -Path c:\q1autoops\temp\Hyper-v.csv -NoClobber
windows
Change user home directory in Windows 10
- Open Windows Registry
- Located at “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\”
- Find your user SID (such as S-x-x-xxxxxxxxxxxx)
- Change the value of “ProfileImagePath”
Two ways to retrieve process id while startup a process via Windows command line
一个特殊的机缘, 需要通过cmd.exe启动一个进程, 并且获取该启动后进程的Process ID, 搜罗到两种方法:
1. 通过wmic process call create
- wmic如果创建进程成功,将返回一个ReturnValue为0类JSON结构的输出, 从中获取ProcessId:
C:\app>cmd.exe /C wmic process call create "c:\app\cluster\GatewayServer.exe start -id 6", "c:\app"
Executing (Win32_Process)->Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ProcessId = 19420;
ReturnValue = 0;
};
- wmic如果创建进程失败, 将返回一个ReturnValue非0的输出:
C:\app>wmic process call create "cluster\GatewayServer.exe start -id 6", "c:\app"
Executing (Win32_Process)->Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ReturnValue = 9;
};
但wmic有一个很大的问题: 不使用当前用户上下文和系统的环境变量.
2. 通过powershell的Start-Process启动进程, 然后取Start-Process返回对像的Id属于得到Process ID:
C:\app>cmd.exe /C powershell -Command "try{$app = Start-Process -PassThru -FilePath \"cluster\GatewayServer.exe\" -WorkingDirectory \"C:\app\" -ArgumentList \"start -id 5\";echo $app.Id} catch {throw}"
How to install specific hotfix on Windows Server
Windows容器环境有个特点, Host与Container的OS Builder Number必须匹配, 有点场景甚至要求Revision Number匹配, 所以经常要为K8s Node安装指定Revision 的hotfix, 用powershell在线安装时下载过程缓慢而不可控, 体验最好的路径还是直接找到相应Revision Number的msu安装包,直接安装:
1. 从Windows Update History网站找到版本对应的KB. 如: Windows Server 1809 OS Build 10.0.17763.1158
https://support.microsoft.com/en-us/help/4549949
2. 在Windows Update Catelog按KB搜索: https://www.catalog.update.microsoft.com/
找到相应的下载包. 如17763.1158对应的KB4549949: https://www.catalog.update.microsoft.com/Search.aspx?q=KB4549949
3. 下载msu安装包后使用wusa指令安装即可:
wusa windows10.0-kb4549949-x64_90e8805e69944530b8d4d4877c7609b9a9e68d81.msu
附:
为了防止Windows Node版本变更, 还要关闭Windows Auto Update, 防止Node OS自己变更版本:
a). 查看Auto Update 状态:
%systemroot%\system32\Cscript %systemroot%\system32\scregedit.wsf /AU /v
b). 禁用 Windows Auto Update:
Net stop wuauserv %systemroot%\system32\Cscript %systemroot%\system32\scregedit.wsf /AU 1 Net start wuauserv
PS: 可使用wmic qfe list查看已安装的hostfix
Reference:
https://docs.microsoft.com/en-us/windows-server/administration/server-core/server-core-servicing
For Windows Container, you need to set –image-pull-progress-deadline for kubelet
Windows镜像动则几个G, 基于Windows Server Core的镜像5~10G, Windows节点上的kubelet在下载镜像的时候经常会cancel掉:
Failed to pull image "XXX": rpc error: code = Unknown desc = context canceled
造成这个问题的原因是因为默认的image pulling progress deadline是1分钟, 如果1分钟内镜像下载没有任何进度更新, 下载动作就会取消, 比较大的镜像就无法成功下载. 见官方文档:
If no pulling progress is made before this deadline, the image pulling will be cancelled. This docker-specific flag only works when container-runtime is set to docker. (default 1m0s)
解决方法是为kubelet配置–image-pull-progress-deadline参数, 比如指定为30分钟:
"c:/k/kubelet.exe ... --image-pull-progress-deadline=30m"
对于Windows服务, 使用sc指令修改kubelet的binPath:
sc config kubelet binPath= " --image-pull-progress-deadline=30m
然后重启kubelet及依赖服务:
sc stop kubeproxy && sc stop kubelet && sc start kubelet && sc start kubeproxy && sc query kubelet && sc query kubeproxy
Refer to: https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet/
DOS中通过chcp修改console code page
UTF-8格式的日志文件, DOS中通过type命令查看时显示乱码:
C:\app\Manager\Log\RootManagerServer>type Root_20200527_135730_Trace.html 1590559051 0 0 Trace 0 0 Bind Port:Listen:8800<br> 1590559051 0 0 Trace 0 0 鏈嶅姟鍣ㄧ洃鍚鍙?8800<br> 1590559051 0 0 Trace 0 0 Bind Port:Listen:9800<br> 1590559051 0 0 Trace 0 0 瀹㈡埛绔洃鍚鍙?9800<br>
通过chcp命令修改code page为65001后显示正常:
C:\app\Manager\Log\RootManagerServer>type Root_20200527_135730_Trace.html 1590559051 0 0 Trace 0 0 Bind Port:Listen:8800<br> 1590559051 0 0 Trace 0 0 服务器监听端口:8800<br> 1590559051 0 0 Trace 0 0 Bind Port:Listen:9800<br> 1590559051 0 0 Trace 0 0 客户端监听端口:9800<br>
DOS命令行中支持的Code Page:
代码页 | 国家/地区或语言 |
---|---|
437 | United States |
850 | 多语言 (拉丁文我) |
852 | 西里尔语 (俄语) |
855 | 西里尔语 (俄语) |
857 | 土耳其语 |
860 | 葡萄牙语 |
861 | 冰岛语 |
863 | 加拿大法语 |
865 | 北欧 |
866 | 俄语 |
869 | 现代希腊语 |
65001 | UTF-8 |
chcp文档: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/chcp
Using diskpart to create partition in Windows
DISKPART DISKPART> list disk 磁盘 ### 状态 大小 可用 Dyn Gpt -------- ------------- ------- ------- --- --- 磁盘 0 联机 50 GB 0 B 磁盘 1 脱机 50 GB 50 GB DISKPART> select disk 1 DISKPART> online disk 1 DISKPART> list disk 磁盘 ### 状态 大小 可用 Dyn Gpt -------- ------------- ------- ------- --- --- 磁盘 0 联机 50 GB 0 B * 磁盘 1 联机 50 GB 50 GB DISKPART> create partition primary DiskPart 遇到错误: 介质受写入保护。 有关详细信息,请参阅系统事件日志。 DISKPART> ATTRIBUTES DISK CLEAR READONLY 已成功清除磁盘属性。 DISKPART> create partition primary DiskPart 成功地创建了指定分区。 DISKPART> list disk 磁盘 ### 状态 大小 可用 Dyn Gpt -------- ------------- ------- ------- --- --- 磁盘 0 联机 50 GB 0 B * 磁盘 1 联机 50 GB 0 B DISKPART> list partition 分区 ### 类型 大小 偏移量 ------------- ---------------- ------- ------- * 分区 1 主要 49 GB 1024 KB DISKPART> FORMAT FS=NTFS QUICK 100 百分比已完成 DiskPart 成功格式化该卷。 DISKPART> list volume 卷 ### LTR 标签 FS 类型 大小 状态 信息 ---------- --- ----------- ----- ---------- ------- --------- -------- 卷 0 D DVD-ROM 0 B 无介质 卷 1 系统保留 NTFS 磁盘分区 549 MB 正常 系统 卷 2 C NTFS 磁盘分区 49 GB 正常 启动 * 卷 3 NTFS 磁盘分区 49 GB 正常 DISKPART> select volume 0 卷 0 是所选卷。 DISKPART> assign letter=e DiskPart 成功地分配了驱动器号或装载点。 DISKPART> select volume 3 卷 3 是所选卷。 DISKPART> assign letter=d DiskPart 成功地分配了驱动器号或装载点。 DISKPART> list volume 卷 ### LTR 标签 FS 类型 大小 状态 信息 ---------- --- ----------- ----- ---------- ------- --------- -------- 卷 0 E DVD-ROM 0 B 无介质 卷 1 系统保留 NTFS 磁盘分区 549 MB 正常 系统 卷 2 C NTFS 磁盘分区 49 GB 正常 启动 * 卷 3 D NTFS 磁盘分区 49 GB 正常
Implementing Graceful Shutdown in Windows Container
Kubernetes Linux Pod中,当通过kubectl删除一个Pod或rolling update一个Pod时, 每Terminating的Pod中的每个Container中PID为1的进程会收到SIGTERM信号, 通知进程进行资源回收并准备退出. 如果在Pod spec.terminationGracePeriodSeconds指定的时间周期内进程没有退出, 则Kubernetes接着会发出SIGKILL信号KILL这个进程。
通过 kubectl delete –force –grace-period=0 … 的效果等同于直接发SIGKILL信号.
但SIGTERM和SIGKILL方式在Windows Container中并不工作, 目前Windows Container的表现是接收到Terminating指令5秒后直接终止。。。
- V1.Pod.terminationGracePeriodSeconds – this is not fully implemented in Docker on Windows, see: reference. The behavior today is that the ENTRYPOINT process is sent CTRL_SHUTDOWN_EVENT, then Windows waits 5 seconds by default, and finally shuts down all processes using the normal Windows shutdown behavior. The 5 second default is actually in the Windows registry inside the container, so it can be overridden when the container is built.
基于社区的讨论结果及多次尝试, 目前Windows容器中行之有效的Graceful Shutdown方法是:
1. Build docker image时通过修改注册表延长等待时间
...
RUN reg add hklm\system\currentcontrolset\services\cexecsvc /v ProcessShutdownTimeoutSeconds /t REG_DWORD /d 300 && \
reg add hklm\system\currentcontrolset\control /v WaitToKillServiceTimeout /t REG_SZ /d 300000 /f
...
上面两个注册表位置, 第1个单位为秒, 第2个为毫秒
2. 在应用程序中注册kernel32.dll中的SetConsoleCtrlHandler函数捕获CTRL_SHUTDOWN_EVENT事件, 进行资源回收
以一个.net framework 的Console App为例说明用法:
using System; using System.Runtime.InteropServices; using System.Threading; namespace Q1.Foundation.SocketServer { class Program { internal delegate bool HandlerRoutine(CtrlType CtrlType); private static HandlerRoutine ctrlTypeHandlerRoutine = new HandlerRoutine(ConsoleCtrlHandler); private static bool cancelled = false; private static bool cleanupCompleted = false; internal enum CtrlType { CTRL_C_EVENT = 0, CTRL_BREAK_EVENT = 1, CTRL_CLOSE_EVENT = 2, CTRL_LOGOFF_EVENT = 5, CTRL_SHUTDOWN_EVENT = 6 } [DllImport("Kernel32")] internal static extern bool SetConsoleCtrlHandler(HandlerRoutine handler, bool add); static void Main() { var result = SetConsoleCtrlHandler(handlerRoutine, true); // INITIAL AND START APP HERE while (true) { if (cancelled) break; } // DO CLEANUP HERE ... cleanupCompleted = true; } private static bool ConsoleCtrlHandler(CtrlType type) { cancelled = true; while (!cleanupCompleted) { // Watting for clean-up to be completed... } return true; } } }
代码解释:
- 引入Kernel32并声明extern函数SetConsoleCtrlHandler
- 创建static的HandlerRoutine.
- 调用SetConsoleCtrlHandler注册处理函数进行事件捕获
- 捕获后在HandlerRoutine应用程序中进行资源清理
- 清理完成后在HandlerRoutine中返回true允许应用程序退出
上述两个步骤即完成了Graceful Shutdown.
需要注意的点是:
1. 传统.net Console App中的事件捕获( 比如: Console.CancelKeyPress
, SystemEvents.SessionEnding
)在容器中都不会生效,AppDomain.CurrentDomain.ProcessExit
的触发时间又太晚, 只有SetConsoleCtrlHandler可行. 更多的尝试代码请参见: https://github.com/moby/moby/issues/25982#issuecomment-250490552
2. 要防止程序退出前HandlerRoutine实例被回收, 所以上面示例中使用了static的HandlerRoutine. 这点很重要, 如果HandlerRoutine在应用程序未结束的时候被回收掉, 就会引发错误, 看如下代码:
static void Main()
{
// Initialize here
...
using
{
var sysEventHandler = new HandlerRoutine(type =>
{
cancelled = true;
while (!cleanCompleted)
{
// Watting for clean-up to be completed...
}
return true;
});
var sysEventSetResult = SetConsoleCtrlHandler(sysEventHandler, true);
...
}
...
// Cleanup here
}
在应用程序退出前, HandlerRoutine实例已经被回收掉了,在CTRL_SHUTDOWN_EVENT 被触发时就会引发NullReferenceException, 具体错误信息如下:
Managed Debugging Assistant 'CallbackOnCollectedDelegate': A callback was made on a garbage collected delegate of type 'Program+HandlerRoutine::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.
类似场景: CallbackOnCollectedDelegate was detected
关于SetConsoleCtrlHandler的使用参考:
SetConsoleCtrlHandler function
HandlerRoutine callback function
最后, 如果要处理的应用程序类型不是Console App, 而是图形化的界面应用,则要处理的消息应该是WM_QUERYENDSESSION, 参见文档:
https://docs.microsoft.com/en-us/windows/console/setconsolectrlhandler#remarks
Add File Extension to Windows IIS Container during image build
Let’s say: we need to add json file extension to the containerized IIS.
Dockerfile:
FROM {imageRegistry}/mcr.microsoft.com/dotnet/framework/aspnet:4.8-20200114-windowsservercore-ltsc2019 COPY . /inetpub/wwwroot WORKDIR /inetpub/wwwroot RUN C:\windows\system32\inetsrv\appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"fileExtensions.[fileExtension='json',allowed='True']" ENV ASPNETCORE_URLS http://+:80 EXPOSE 80/tcp
An error occurs during build docker image:
Step 1/6 : FROM repo.q1lan.k8s:9999/mcr.microsoft.com/dotnet/framework/aspnet:4.8-20200114-windowsservercore-ltsc2019 ---> a5bc996f06b3 Step 2/6 : COPY . /inetpub/wwwroot ---> bdb9536e506a Step 3/6 : WORKDIR /inetpub/wwwroot ---> Running in f7666a9ffd0b Removing intermediate container f7666a9ffd0b ---> c9fe76854f6c Step 4/6 : RUN C:\windows\system32\inetsrv\appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"fileExtensions.[fileExtension='json',allowed='True']" ---> Running in 1c74d16420c2 Failed to process input: The parameter 'Web' must begin with a / or - (HRESULT=80070057).
Try to escape all double-quotes in Dockerfile:
RUN C:\windows\system32\inetsrv\appcmd.exe set config \"Default Web Site\" -section:system.webServer/security/requestFiltering /+\"fileExtensions.[fileExtension='json',allowed='True']\"
It works like a charm:
Step 1/6 : FROM repo.q1lan.k8s:9999/mcr.microsoft.com/dotnet/framework/aspnet:4.8-20200114-windowsservercore-ltsc2019 ---> a5bc996f06b3 Step 2/6 : COPY . /inetpub/wwwroot ---> 646bbf3d5def Step 3/6 : WORKDIR /inetpub/wwwroot ---> Running in 584471c0524a Removing intermediate container 584471c0524a ---> 54f6a3ade821 Step 4/6 : RUN C:\windows\system32\inetsrv\appcmd.exe set config \"Default Web Site\" -section:system.webServer/security/requestFiltering /+\"fileExtensions.[fileExtension='json',allowed='True']\" ---> Running in f84c38da656a Applied configuration changes to section "system.webServer/security/requestFiltering" for "MACHINE/WEBROOT/APPHOST/Default Web Site" at configuration commit path "MACHINE/WEBROOT/APPHOST/Default Web Site" Removing intermediate container f84c38da656a ---> 7dfffe2d9813 Step 5/6 : ENV ASPNETCORE_URLS http://+:80 ---> Running in dff81c8282f1 Removing intermediate container dff81c8282f1 ---> cbd697556dd7 Step 6/6 : EXPOSE 80/tcp ---> Running in d10903bec188 Removing intermediate container d10903bec188 ...