在Windows节点上部署.NET IIS服务

更新时间:
复制为 MD 格式

ACS集群的Windows节点上运行基于.NET框架的IIS(Internet Information Services)Web服务时,需要创建Windows Pod并配置合适的资源规格。本文介绍如何创建Windows Server Pod、验证IIS服务可用性,以及测试跨Pod网络通信(包括Linux PodWindows Pod之间的通信)。

前提条件

创建Windows Server Pod

创建运行IIS服务的Windows Pod,并配置nodeSelector确保Pod调度到Windows节点。

控制台

  1. 登录容器计算服务控制台,在左侧导航栏选择集群列表

  2. 集群列表页面,单击目标集群名称,然后在左侧导航栏,选择工作负载 > 无状态

  3. 无状态页面,单击使用YAML创建资源。将以下YAML内容复制到编辑器中,然后单击创建

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: windows-iis-net-server
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: windows-iis-net-server
      template:
        metadata:
          labels:
            app: windows-iis-net-server
            alibabacloud.com/acs: "true"
        spec:
          nodeSelector:
            kubernetes.io/os: windows  # 确保Pod调度到Windows节点
          containers:
          - name: windows-container
            image: registry-cn-hangzhou.ack.aliyuncs.com/test/myiis_net:v1
            ports:
            - name: http
              containerPort: 80
              protocol: TCP
            resources:
              requests:
                cpu: "4"
                memory: "8Gi"
              limits:
                cpu: "4"
                memory: "8Gi"
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: windows-iis-svc
    spec:
      type: LoadBalancer
      selector:
        app: windows-iis-net-server   # 要和Deploymentlabels一致
      ports:
      - name: http
        port: 80          # 对外端口
        targetPort: 80    # Pod端口
        protocol: TCP
  4. 创建完成后,在弹出的对话框中单击查看,确认Pod状态为Running

kubectl

  1. 已使用kubectl连接Kubernetes集群。具体操作,请参见获取集群kubeconfig并通过kubectl工具连接集群

  2. 将以下YAML内容保存为windows-iis-deployment.yaml文件。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: windows-iis-net-server
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: windows-iis-net-server
      template:
        metadata:
          labels:
            app: windows-iis-net-server
        spec:
          nodeSelector:
            kubernetes.io/os: windows  # 确保Pod调度到Windows节点
          containers:
          - name: windows-container
            image: registry-cn-hangzhou.ack.aliyuncs.com/test/myiis_net:v1
            ports:
            - name: http
              containerPort: 80
              protocol: TCP
            resources:
              requests:
                cpu: "4"
                memory: "8Gi"
              limits:
                cpu: "4"
                memory: "8Gi"
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: windows-iis-svc
    spec:
      type: LoadBalancer
      selector:
        app: windows-iis-net-server   # 要和Deploymentlabels一致
      ports:
      - name: http
        port: 80          # 对外端口
        targetPort: 80    # Pod端口
        protocol: TCP
  3. 执行以下命令创建应用。

    kubectl apply -f windows-iis-deployment.yaml
  4. 执行以下命令确认Pod状态为Running

    kubectl get pods -l app=windows-iis-net-server

    预期输出:

    NAME                                        READY   STATUS    RESTARTS   AGE
    windows-iis-net-server-77bcb7cdd7-nhp72     1/1     Running   0          23h

验证IIS服务可用性

  1. 集群列表页面,单击目标集群名称,然后在左侧导航栏,选择网络 > 服务

  2. 服务列表页面,找到windows-iis-svc服务,复制外部 IP 地址(External IP)列地址EXTERNAL_IP

  3. 替换EXTERNAL_IP,在浏览器访问以下地址。

    http://EXTERNAL_IP:80/Default.aspx

    预期输出:image

验证跨Pod网络通信

创建Linux PodWindows客户端Pod,验证它们能够通过Pod IP访问Windows Server Pod提供的IIS服务。

步骤一:创建Linux测试Pod

控制台

无状态页面,单击使用YAML创建资源。将以下YAML内容复制到编辑器中,然后单击创建

apiVersion: apps/v1
kind: Deployment
metadata:
  name: linux-test-deployment
  labels:
    app: linux-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: linux-test
  template:
    metadata:
      labels:
        app: linux-test
    spec:
      containers:
      - name: alinux3
        image: alibaba-cloud-linux-3-registry.cn-hangzhou.cr.aliyuncs.com/alinux3/alinux3:latest
        command: ["/bin/bash", "-c", "while true; do sleep 3600; done"]

kubectl

  1. 将以下YAML内容保存为linux-test-deployment.yaml文件。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: linux-test-deployment
      labels:
        app: linux-test
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: linux-test
      template:
        metadata:
          labels:
            app: linux-test
        spec:
          containers:
          - name: alinux3
            image: alibaba-cloud-linux-3-registry.cn-hangzhou.cr.aliyuncs.com/alinux3/alinux3:latest
            command: ["/bin/bash", "-c", "while true; do sleep 3600; done"]
    
  2. 执行以下命令创建工作负载。

    kubectl apply -f linux-test-deployment.yaml

步骤二:获取Windows Server PodIP地址

控制台

  1. 容器组页面,找到名为windows-iis-net-server-xxxPod,在Pod IP列查看IP地址。

kubectl

执行以下命令获取Pod IP。

kubectl get pod -l app=windows-iis-net-server -o jsonpath='{.items[0].status.podIP}'

步骤三:从Linux Pod访问Windows Server Pod

控制台

  1. 容器组页面,找到linux-test-deployment-xxx,单击操作列的终端,选择alinux3进入。

  2. 在终端中执行以下命令(将WINDOWS_POD_IP替换为实际IP)。

    curl http://WINDOWS_POD_IP:80/Default.aspx

    预期输出:

    <!DOCTYPE html>
    <html>
    <head><title>ASP.NET on IIS</title></head>
    <body>
        <h1>OK - ASP.NET (.NET Framework) on IIS</h1>
        <p>Time: 2026-01-23 17:13:39</p>
        <p>Machine: 2760XXXXXXX</p>
    </body>
    </html>

kubectl

执行以下命令(将WINDOWS_POD_IP替换为实际IP)。

kubectl exec -it deployment/linux-test-deployment -- curl http://{{WINDOWS_POD_IP}}:80/Default.aspx

预期输出:

<!DOCTYPE html>
<html>
<head><title>ASP.NET on IIS</title></head>
<body>
    <h1>OK - ASP.NET (.NET Framework) on IIS</h1>
    <p>Time: 2026-01-23 17:13:39</p>
    <p>Machine: 2760XXXXXXX</p>
</body>
</html>

步骤四:(可选)创建Windows客户端Pod验证Windows间通信

如需验证Windows Pod之间的网络通信,可创建另一个Windows Pod作为客户端。

  1. 将以下YAML内容保存为windows-client-deployment.yaml文件。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: windows-client-deployment
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: windows-client
      template:
        metadata:
          labels:
            app: windows-client
            alibabacloud.com/acs: "true"
        spec:
          nodeSelector:
            kubernetes.io/os: windows
          containers:
          - name: windows-container
            image: registry-cn-hangzhou.ack.aliyuncs.com/test/myiis_net:v1
            command: ["powershell", "-Command", "while ($true) { Start-Sleep -Seconds 3600 }"]
            resources:
              requests:
                cpu: "2"
                memory: "4Gi"
              limits:
                cpu: "2"
                memory: "4Gi"
  2. 执行以下命令创建工作负载。

    kubectl apply -f windows-client-deployment.yaml
  3. 等待Pod状态变为Running后,执行以下命令验证通信(将WINDOWS_POD_IP替换为Server Pod的实际IP)。

    kubectl exec -it deployment/windows-client-deployment -- cmd /c curl WINDOWS_POD_IP:80/Default.aspx

    预期输出:

    <!DOCTYPE html>
    <html>
    <head><title>ASP.NET on IIS</title></head>
    <body>
        <h1>OK - ASP.NET (.NET Framework) on IIS</h1>
        <p>Time: 2026-01-23 17:13:39</p>
        <p>Machine: 2760XXXXXXX</p>
    </body>
    </html>

资源清理

测试完成后,清理相关资源。

kubectl delete service windows-iis-svc
kubectl delete deployment windows-iis-net-server linux-test-deployment windows-client-deployment