> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Different Types of Patches

> This article explains methods for defining patches in Kubernetes using JSON 6902 and strategic merge patches, with options for inline or external file references.

This article explains two methods for defining patches in Kubernetes: JSON 6902 patches and strategic merge patches. You have the flexibility to define patches either inline within the kustomization.yaml file or by referencing external patch files. Inline patches consolidate everything in one file, while external files keep your kustomization.yaml concise, especially when managing numerous patches.

## JSON 6902 Patches

### Inline Definition in kustomization.yaml

Below is an example of an inline patch applied to the "api-deployment":

```yaml theme={null}
# Inline patch for api-deployment
patches:
  - target:
      kind: Deployment
      name: api-deployment
    patch: |-
      - op: replace
        path: /spec/replicas
        value: 5
```

### Referencing an External Patch File

Alternatively, you can reference a separate file. In this example, the external file (`replica-patch.yaml`) is used for the "nginx-deployment":

```yaml theme={null}
# Reference to an external file for nginx-deployment
patches:
  - path: replica-patch.yaml
    target:
      kind: Deployment
      name: nginx-deployment
```

In the external file (`replica-patch.yaml`), the patch is defined as follows:

```yaml theme={null}
- op: replace
  path: /spec/replicas
  value: 5
```

## Strategic Merge Patches

For strategic merge patches, you can similarly choose between inline definitions or referencing an external file.

### Inline Strategic Merge Patch in kustomization.yaml

The following example shows an inline strategic merge patch for the "api-deployment":

```yaml theme={null}
patches:
  - patch: |-
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: api-deployment
      spec:
        replicas: 5
```

### Using an External YAML File for Strategic Merge Patch

To reference a file for a strategic merge patch, simply include the file path in your kustomization.yaml:

```yaml theme={null}
patches:
  - path: replica-patch.yaml
```

<Callout icon="lightbulb" color="#1CB2FE">
  Both inline definitions and separate patch files are valid options. Choose the approach that best suits your project structure and maintenance preferences.
</Callout>

## Summary Table

| Patch Type            | Inline Example                                         | External File Example                          |
| --------------------- | ------------------------------------------------------ | ---------------------------------------------- |
| JSON 6902 Patch       | Inline definition in kustomization.yaml as shown above | Reference external file (`replica-patch.yaml`) |
| Strategic Merge Patch | Inline definition in kustomization.yaml as shown above | Reference external file (`replica-patch.yaml`) |

Using external patch files is particularly beneficial if you have a high number of patches or if you want to keep your kustomization.yaml lean and easy to manage.

For more in-depth information and best practices on patching in Kubernetes, explore the [Kubernetes Documentation](https://kubernetes.io/docs/).

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/certified-kubernetes-application-developer-ckad/module/2503ab74-a871-4b65-a677-7180c001d5c5/lesson/1b0aa541-7251-472c-89f3-8763fee58898" />
</CardGroup>
