Search code examples
powershellkuberneteskubernetes-pod

powershell pod failing in kubernetes cluster


I need to run powershell as an container in kubernetes

I am using following deployment file sample.yaml

apiVersion: v1
kind: Pod
metadata:
  name: powershell
spec:
  containers:
  - name: powershell
    image: mcr.microsoft.com/powershell:latest

When I run kubectl apply -f sample.yaml

I get the following error on kubectl get pods

powershell        0/1     CrashLoopBackOff   3 (50s ago)   92s

I did check the log kubectl logs powershell

PowerShell 7.2.6
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS /> ←[?1h

But when i run same image as a docker container with following command its working

docker run --rm -it mcr.microsoft.com/powershell:latest

Solution

  • If you want to keep a container for running, you should write like this yaml..

    apiVersion: v1
    kind: Pod
    metadata:
      name: powershell
    spec:
      containers:
      - name: powershell
        image: mcr.microsoft.com/powershell:latest
        command: ["pwsh"]
        args: ["-Command", "Start-Sleep", "3600"]
    
    
    [root@master1 ~]# kubectl get pod powershell
    NAME         READY   STATUS    RESTARTS   AGE
    powershell   1/1     Running   0          3m32s
    [root@master1 ~]# kubectl exec -it powershell -- pwsh
    PowerShell 7.2.6
    Copyright (c) Microsoft Corporation.
    
    https://aka.ms/powershell
    Type 'help' to get help.
    
    PS /> date
    Thu Oct 13 12:50:24 PM UTC 2022
    PS />