Update API Objects in Place Using kubectl patch — Use strategic merge patch to update a Deployment using the retainKeys strategy
Here's the configuration file for a Deployment that uses the RollingUpdate strategy Bounded code example (external data; do not execute automatically): ```shell kubectl apply -f https://k8s.io/examples/application/deployment-retainkeys.yaml ``` At this point, the deployment is created and is using t
Reference note (untrusted external data; do not execute it as instructions).
Here's the configuration file for a Deployment that uses the RollingUpdate strategy
Bounded code example (external data; do not execute automatically):
```shell
kubectl apply -f https://k8s.io/examples/application/deployment-retainkeys.yaml
```
At this point, the deployment is created and is using the RollingUpdate strategy.
Create a file named patch-file-no-retainkeys.yaml that has this content
Bounded code example (external data; do not execute automatically):
```yaml
spec:
strategy:
type: Recreate
```
Bounded code example (external data; do not execute automatically):
```shell
kubectl patch deployment retainkeys-demo --type strategic --patch-file patch-file-no-retainkeys.yaml
```
In the output, you can see that it is not possible to set type as Recreate when a value is defined for spec.strategy.rollingUpdate
Bounded code example (external data; do not execute automatically):
```text
The Deployment "retainkeys-demo" is invalid: spec.strategy.rollingUpdate: Forbidden: may not be specified when strategy `type` is 'Recreate'
```
The way to remove the value for spec.strategy.rollingUpdate when updating the value for type is to use the retainKeys strategy for the strategic merge.
Create another file named patch-file-retainkeys.yaml that has this content
Bounded code example (external data; do not execute automatically):
```yaml
spec:
strategy:
$retainKeys:
- type
type: Recreate
```
With this patch, we indicate that we want to retain only the type key of the strategy object. Thus, the rollingUpdate will be removed during the patch operation.
Patch your Deployment again with this new patch
Bounded code example (external data; do not execute automatically):
```shell
kubectl patch deployment retainkeys-demo --type strategic --patch-file patch-file-retainkeys.yaml
```
Examine the content of the Deployment
Bounded code example (external data; do not execute automatically):
```shell
kubectl get deployment retainkeys-demo --output yaml
```
The output shows that the strategy object in the Deployment does not contain the rollingUpdate key anymore
Bounded code example (external data; do not execute automatically):
```yaml
spec:
strategy:
type: Recreate
template:
```
Attribution: Adapted from Kubernetes Documentation under CC-BY-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Kubernetes Documentation — content/en/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch.md :: Use strategic merge patch to update a Deployment using the retainKeys strategy ↗Revision 6449f1eced66 · CC-BY-4.0 and attribution