Use PowerShell on Windows nodes

Updated at:

Run a custom PowerShell script on a Windows node in an Alibaba Cloud Container Service (ACS) cluster by creating a Kubernetes Job that targets a Windows node.

Prerequisites

Before you begin, ensure that you have:

Create a Windows Server pod

The following Job runs a PowerShell script that writes a line to the log. The Job targets a Windows node using nodeSelector and uses the powershell.exe command to run the script inline.

Console

  1. Log in to the ACS console. In the left navigation pane, click Clusters.

  2. On the Cluster List page, click the name of your cluster. In the left navigation pane, choose Workloads > Jobs.

  3. On the Jobs page, click Create Resources in YAML. Copy the following YAML into the editor, then click Create.

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: windows-job-powershell
    spec:
      backoffLimit: 0
      template:
        metadata:
          labels:
            job-name: windows-job-powershell
            alibabacloud.com/acs: "true"
        spec:
          nodeSelector:
            kubernetes.io/os: windows  # Ensure that the pod is scheduled to a Windows node
          restartPolicy: Never
          containers:
          - name: test
            image: registry-cn-hangzhou.ack.aliyuncs.com/test/servercore:ltsc2022
            command:
              - powershell.exe
              - -NoProfile
              - -ExecutionPolicy
              - Bypass
              - -Command
              - |
                Write-Host "Hello from Windows Job"
                Start-Sleep -Seconds 3600
            resources:
              requests:
                cpu: "4"
                memory: "8Gi"
              limits:
                cpu: "4"
                memory: "8Gi"
  4. After the Job is created, click View Details in the dialog box that appears. Confirm that the pod status is Running.

kubectl

  1. Save the following YAML as windows-job-powershell.yaml.

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: windows-job-powershell
    spec:
      backoffLimit: 0
      template:
        metadata:
          labels:
            job-name: windows-job-powershell
        spec:
          nodeSelector:
            kubernetes.io/os: windows  # Ensure that the pod is scheduled to a Windows node
          restartPolicy: Never
          containers:
          - name: test
            image: registry-cn-hangzhou.ack.aliyuncs.com/test/servercore:ltsc2022
            command:
              - powershell.exe
              - -NoProfile
              - -ExecutionPolicy
              - Bypass
              - -Command
              - |
                Write-Host "Hello from Windows Job"
                Start-Sleep -Seconds 3600
            resources:
              requests:
                cpu: "4"
                memory: "8Gi"
              limits:
                cpu: "4"
                memory: "8Gi"
  2. Create the Job.

    kubectl apply -f windows-job-powershell.yaml
  3. Confirm that the pod is running.

    kubectl get job windows-job-powershell

    Expected output:

    NAME                     STATUS    COMPLETIONS   DURATION   AGE
    windows-job-powershell   Running   0/1           8m50s      8m50s

Verify the script execution results

Console

  1. On the cluster details page, in the left navigation pane, choose Workloads > Jobs.

  2. On the Jobs page, click the name of the Job. On the details page, click the Logs tab. The following output confirms the script ran successfully.

    Hello from Windows Job

kubectl

Run the following command to view the pod logs.

kubectl logs job/windows-job-powershell

Expected output:

Hello from Windows Job

Clean up resources

After testing, run the following command to delete the Job.

kubectl delete job windows-job-powershell