• About Us
  • Privacy & Policy
HowTo's
  • Home
  • Commands
  • Linux
  • SCM
  • Git
  • Database
  • MySQL
  • Kubernetes
  • Docker
No Result
View All Result
  • Home
  • Commands
  • Linux
  • SCM
  • Git
  • Database
  • MySQL
  • Kubernetes
  • Docker
No Result
View All Result
HowTo's
No Result
View All Result
Home Kubernetes

Patching Kubernetes Resources with kubectl patch

June 26, 2024
in Kubernetes, Kubernetes Commands Examples, Kubernetes Commands Tutorial, Kubernetes Tutorial
A A
0
18
SHARES
163
VIEWS
Share on FacebookShare on Twitter

The kubectl patch command in Kubernetes is used to update fields of a resource using strategic merge patch, JSON patch, or merge patch. It allows for making precise modifications to existing resources without requiring a full replacement.

Here are several examples demonstrating the usage of kubectl patch:

Example 1: Patching a Deployment to update its image:

kubectl patch deployment nginx-deployment -p '{"spec":{"template":{"spec":{"containers":[{"name":"nginx","image":"nginx:1.19.10"}]}}}}'
        

This command updates the image of the container named ‘nginx’ in the ‘nginx-deployment’ Deployment to version 1.19.10 of nginx.
To verify, you can use:

kubectl get deployment nginx-deployment -o=jsonpath='{.spec.template.spec.containers[0].image}'
        

Expected output: nginx:1.19.10

Example 2: Patching a Service to change its type to NodePort:

kubectl patch service my-service -p '{"spec":{"type":"NodePort"}}'
        

This command modifies the type of the ‘my-service’ Service to NodePort.
To verify, you can use:

kubectl get service my-service -o=jsonpath='{.spec.type}'
        

Expected output: NodePort

Example 3: Applying a strategic merge patch to update labels:

kubectl patch pod mypod --type='json' -p='[{"op": "replace", "path": "/metadata/labels", "value": {"new-label": "value"}}]'
        

This updates the labels of the ‘mypod’ Pod to include a new label named ‘new-label’.
To verify, you can use:

kubectl get pod mypod -o=jsonpath='{.metadata.labels.new-label}'
        

Expected output: value

Example 4: Patching a ConfigMap to add a new data entry:

kubectl patch configmap my-config --type='json' -p='[{"op": "add", "path": "/data/new-key", "value": "new-value"}]'
        

This adds a new entry ‘new-key: new-value’ to the ‘my-config’ ConfigMap.
To verify, you can use:

kubectl get configmap my-config -o=jsonpath='{.data.new-key}'
        

Expected output: new-value

Example 5: Patching a Namespace to update its annotations:

kubectl patch namespace my-namespace -p '{"metadata":{"annotations":{"new-annotation":"value"}}}'
        

This updates the annotations of the ‘my-namespace’ Namespace to include a new annotation ‘new-annotation’.
To verify, you can use:

kubectl get namespace my-namespace -o=jsonpath='{.metadata.annotations.new-annotation}'
        

Expected output: value

Example 6: Patching a StatefulSet to modify its replicas:

kubectl patch statefulset my-statefulset -p '{"spec":{"replicas":3}}'
        

This adjusts the number of replicas for the ‘my-statefulset’ StatefulSet to 3.
To verify, you can use:

kubectl get statefulset my-statefulset -o=jsonpath='{.spec.replicas}'
        

Expected output: 3

Example 7: Patching a PersistentVolumeClaim to update storage size:

kubectl patch pvc my-pvc -p '{"spec":{"resources":{"requests":{"storage":"10Gi"}}}}'
        

This command updates the storage request of the PersistentVolumeClaim ‘my-pvc’ to 10Gi.
To verify, you can use:

kubectl get pvc my-pvc -o=jsonpath='{.spec.resources.requests.storage}'
        

Expected output: 10Gi

Example 8: Patching a ServiceAccount to add a new secret:

kubectl patch serviceaccount my-sa -p '{"secrets": [{"name": "new-secret"}]}'
        

This adds a new secret ‘new-secret’ to the ‘my-sa’ ServiceAccount.
To verify, you can use:

kubectl get serviceaccount my-sa -o=jsonpath='{.secrets[?(@.name=="new-secret")].name}'
        

Expected output: new-secret

Example 9: Patching a Job to update its backoffLimit:

kubectl patch job my-job -p '{"spec":{"backoffLimit":5}}'
        

This updates the backoffLimit of the ‘my-job’ Job to 5.
To verify, you can use:

kubectl get job my-job -o=jsonpath='{.spec.backoffLimit}'
        

Expected output: 5

Example 10: Patching a NetworkPolicy to update its podSelector:

kubectl patch networkpolicy my-network-policy -p '{"spec":{"podSelector":{"matchLabels":{"role":"frontend"}}}}'
        

This updates the podSelector of the ‘my-network-policy’ NetworkPolicy to select pods labeled with ‘role: frontend’.
To verify, you can use:

kubectl get networkpolicy my-network-policy -o=jsonpath='{.spec.podSelector.matchLabels.role}'
        

Expected output: frontend

Also check similar articles.

Applying Kubernetes Configurations with kubectl apply
Diffing Kubernetes Configurations with kubectl diff
Listing Kubernetes Events with kubectl events
Troubleshooting Kubernetes with kubectl debug
Managing Kubernetes Authorization with kubectl auth

Tags: KubernetesKubernetes Commands ExamplesKubernetes Commands TutorialKubernetes Tutorial
Previous Post

Applying Kubernetes Configurations with kubectl apply

Next Post

Replace Kubernetes Resources with kubectl replace

Related You may like!

howto

Interacting with Kubernetes Plugins using kubectl plugin

June 26, 2024
howto

Configuring kubectl and kubeconfig Files

June 26, 2024

Exploring Kubernetes API Versions with kubectl api-versions

June 26, 2024

Understanding Kubernetes API Resources with kubectl api-resources

June 26, 2024

Generating Shell Completion Code with kubectl completion

June 26, 2024

Managing Kubernetes Annotations with kubectl annotate

June 26, 2024
Next Post
howto

Replace Kubernetes Resources with kubectl replace

howto

Waiting for Kubernetes Resources with kubectl wait

howto

Building Kubernetes Customizations with kubectl kustomize

Discussion about this post

Latest Updated

howto

How to Use -iname for Case-Insensitive Filename Searches in find

August 21, 2024
howto

Search for Files with Case-Insensitive Pattern Matching Using -ilname in find

August 21, 2024
howto

Find Files by Group Name with -group in find Command

August 21, 2024
howto

Locate Files by Group ID Using -gid in find Command

August 21, 2024
howto

How to Search for Filesystems with -fstype in find Command

August 21, 2024

Trending in Week

  • howto

    Using BTRFS Subvolume for User Home Directory in Linux

    22 shares
    Share 9 Tweet 6
  • Downloading Docker Images from a Registry

    13 shares
    Share 5 Tweet 3
  • Configuring SSL Connection Mode in mysqldump

    17 shares
    Share 7 Tweet 4
  • Omit Tablespace Information in mysqldump Output

    13 shares
    Share 5 Tweet 3
  • Setting MySQL Dump Compatibility Mode

    18 shares
    Share 7 Tweet 5
  • Setting Network Buffer Length in mysqldump

    13 shares
    Share 5 Tweet 3
  • Logging out from Docker Registries

    13 shares
    Share 5 Tweet 3
  • Scheduling Nodes in Kubernetes with kubectl uncordon

    12 shares
    Share 5 Tweet 3
  • Managing Default User Creation Settings in Linux

    15 shares
    Share 6 Tweet 4
  • Using Extended INSERT Syntax in mysqldump

    12 shares
    Share 5 Tweet 3
  • About Us
  • Privacy & Policy

© 2024 All Rights Reserved. Howto.swebtools.com.

No Result
View All Result

© 2024 All Rights Reserved. Howto.swebtools.com.