6.3. Windows 容器工作负载部署示例

在有 Windows 计算节点可用后,您可以将 Windows 容器工作负载部署到集群中。

注意

这个示例部署仅供参考。

Service 对象示例

apiVersion: v1
kind: Service
metadata:
  name: win-webserver
  labels:
    app: win-webserver
spec:
  ports:
    # the port that this service should serve on
  - port: 80
    targetPort: 80
  selector:
    app: win-webserver
  type: LoadBalancer

Deployment 对象示例

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: win-webserver
  name: win-webserver
spec:
  selector:
    matchLabels:
      app: win-webserver
  replicas: 1
  template:
    metadata:
      labels:
        app: win-webserver
      name: win-webserver
    spec:
      containers:
      - name: windowswebserver
        image: mcr.microsoft.com/windows/servercore:ltsc2019 1
        imagePullPolicy: IfNotPresent
        command:
        - powershell.exe 2
        - -command
        - $listener = New-Object System.Net.HttpListener; $listener.Prefixes.Add('http://*:80/'); $listener.Start();Write-Host('Listening at http://*:80/'); while ($listener.IsListening) { $context = $listener.GetContext(); $response = $context.Response; $content='<html><body><H1>Red Hat OpenShift + Windows Container Workloads</H1></body></html>'; $buffer = [System.Text.Encoding]::UTF8.GetBytes($content); $response.ContentLength64 = $buffer.Length; $response.OutputStream.Write($buffer, 0, $buffer.Length); $response.Close(); };
        securityContext:
          runAsNonRoot: false
          windowsOptions:
            runAsUserName: "ContainerAdministrator"
      os:
        name: "windows"
      runtimeClassName: windows2019 3

1
指定要使用的容器镜像:mcr.microsoft.com/powershell:<tag>mcr.microsoft.com/windows/servercore:<tag>。容器镜像必须与节点上运行的 Windows 版本匹配。
  • 对于 Windows 2019,使用 ltsc2019 标签。
  • 对于 Windows 2022,使用 ltsc2022 标签。
2
指定要在容器上执行的命令。
  • 对于 mcr.microsoft.com/powershell:<tag> 容器镜像,您必须将命令定义为 pwsh.exe
  • 对于 mcr.microsoft.com/windows/servercore:<tag> 容器镜像,您必须将该命令定义为 powershell.exe
3
指定您为集群中的 Windows 操作系统变体创建的运行时类。