Quick Fix: Using Kubectl Patch On Windows PowerShell

When I first started learning Kubernetes and using Kubectl I did most of my study on a Linux box. One of the commands I learnt to use was the patch command. This patch command allows you to change part of a resource specification from the CLI. Having to only specify what you want to change as opposed to a command like apply where it reads a file and makes the required changes.

I’ve since switched to using Windows and PowerShell as my main desktop. For the most part I’ve found the transition to using Kubernetes and Kubectl on Windows fairly seamless for my work. That is with the exception of using the patch command. Recently I was following a guide to modify an Ingress deployment which made reference to using the patch command. When I went to run the command under Windows PowerShell I received the following error.

kubectl patch deployment nginx-ingress --patch ‘{“spec”: {“template”: {“metadata”: {“labels”: {“app”: “nginx”}}}}}’

Error from server (BadRequest): invalid character ‘s’ looking for beginning of object key string

I tried changing the single and double quotes and trying a few variations but that didn’t seem to help. It can be tricky sometimes to tell if you have all the correct curly braces and quotes. So I took the command over to Linux and sure enough it worked fine.

Digging into the issue, the solution, while I guess visually messy, is actually very simple. It turns out under Windows you need to escape each double quote in the command with a backslash.

So the following command

kubectl patch deployment nginx1 --patch ‘{“spec”: {“template”: {“metadata”: {“labels”: {“test”: “test123”}}}}}’


kubectl patch deployment nginx1 --patch ‘{\”spec\”: {\”template\”: {\”metadata\”: {\”labels\”: {\”test\”: \”test123\”}}}}}’

Only the double quotes need the backslash in front of them. The single quote around the data that you are wanting to add does not need it.

1 thought on “Quick Fix: Using Kubectl Patch On Windows PowerShell”

Leave a Reply

Your email address will not be published. Required fields are marked *