Use PowerShell on Windows nodes
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:
-
A Windows virtual node added to the cluster. For more information, see Create a Windows workload in an ACS cluster.
-
(For kubectl) Connected to a Kubernetes cluster using kubectl. For more information, see Connect to a Kubernetes cluster using kubectl.
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
-
Log in to the ACS console. In the left navigation pane, click Clusters.
-
On the Cluster List page, click the name of your cluster. In the left navigation pane, choose Workloads > Jobs.
-
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" -
After the Job is created, click View Details in the dialog box that appears. Confirm that the pod status is Running.
kubectl
-
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" -
Create the Job.
kubectl apply -f windows-job-powershell.yaml -
Confirm that the pod is running.
kubectl get job windows-job-powershellExpected output:
NAME STATUS COMPLETIONS DURATION AGE windows-job-powershell Running 0/1 8m50s 8m50s
Verify the script execution results
Console
-
On the cluster details page, in the left navigation pane, choose Workloads > Jobs.
-
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