<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nahla Thabet</title>
    <description>The latest articles on DEV Community by Nahla Thabet (@nahla_thabet_54e011e4cb76).</description>
    <link>https://dev.to/nahla_thabet_54e011e4cb76</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3310640%2F5fcdeeec-df74-4659-869c-3d9a63850593.jpg</url>
      <title>DEV Community: Nahla Thabet</title>
      <link>https://dev.to/nahla_thabet_54e011e4cb76</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nahla_thabet_54e011e4cb76"/>
    <language>en</language>
    <item>
      <title>Kustomization Tutorial</title>
      <dc:creator>Nahla Thabet</dc:creator>
      <pubDate>Tue, 28 Oct 2025 20:56:22 +0000</pubDate>
      <link>https://dev.to/nahla_thabet_54e011e4cb76/kustomization-tutorial-4m32</link>
      <guid>https://dev.to/nahla_thabet_54e011e4cb76/kustomization-tutorial-4m32</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;What is Kustomize?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Kustomize is a configuration management solution that leverages layering to preserve the base settings of your applications and components by overlaying declarative yaml artifacts (called patches) that selectively override default settings without actually changing the original files.&lt;/p&gt;

&lt;p&gt;Say that you have 3 env you want to deploy the same application on them but every env has different number of replica, for example this wordoress deployment, and I have staging and prod environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Kustomize: A simple guide
&lt;/h2&gt;

&lt;p&gt;first we should create folder for &lt;em&gt;&lt;strong&gt;base&lt;/strong&gt;&lt;/em&gt; manifests file and create folders for every env under &lt;strong&gt;&lt;em&gt;overlay&lt;/em&gt;&lt;/strong&gt; section.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;my-app/&lt;br&gt;
   ├── base/&lt;br&gt;
   │   ├── kustomization.yaml&lt;br&gt;
   │   └── deployment.yaml&lt;br&gt;
   |    &lt;br&gt;
   └── overlays/&lt;br&gt;
      └── staging/&lt;br&gt;
      |   ├── kustomization.yaml&lt;br&gt;
      |    └── replica-patch.yaml&lt;br&gt;
      |&lt;br&gt;
      |___prod/&lt;br&gt;
          ├── kustomization.yaml&lt;br&gt;
          └── deployment-patch.yaml&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Kustomize Patching Strategies&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;we've set up our directory structure, let's explore the different ways to patch our WordPress deployment for different environments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- base/deployment.yaml&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  replicas: 2
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - name: wordpress
        image: wordpress:apache  
        ports:
        - containerPort: 80
        env:
        - name: WORDPRESS_DB_HOST
          value: cluster1-haproxy.mysql-cluster.svc.cluster.local:3306
        - name: WORDPRESS_DB_NAME
          value: wordpress
        - name: WORDPRESS_DB_USER
          value: wp
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: wp-secret
              key: wp-password
        resources:
          requests:
            memory: "256Mi"
            cpu: "200m"
          limits:
            memory: "512Mi"
            cpu: "400m"
        volumeMounts:
        - name: wordpress-data
          mountPath: /var/www/html
      volumes:
      - name: wordpress-data
        persistentVolumeClaim:
          claimName: wordpress-pvc

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;- base/kustomization.yaml&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - deployment.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Method 1: Strategic Merge Patch&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Strategic Merge Patch is the simplest approach. You define only the fields you want to change, and Kustomize intelligently merges them with the base configuration.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;overlays/staging/replica-patch.yaml
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
spec:
  replicas: 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;overlays/staging/kustomization.yaml
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - ../../base

patchesStrategicMerge:
  - replica-patch.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;For production&lt;/strong&gt;&lt;/em&gt;, you might want more replicas and different resource limits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;overlays/prod/deployment-patch.yaml
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
spec:
  replicas: 5
  template:
    spec:
      containers:
      - name: wordpress
        resources:
          requests:
            memory: "512Mi"
            cpu: "500m"
          limits:
            memory: "1Gi"
            cpu: "1000m"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;overlays/prod/kustomization.yaml
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - ../../base

patchesStrategicMerge:
  - deployment-patch.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Method 2: JSON 6902 Patches&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JSON 6902 patches provide more precise control over modifications using JSON Patch operations (add, remove, replace, move, copy, test).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;- overlays/prod/deployment-patch.yaml&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- op: replace
  path: /spec/replicas
  value: 5
- op: replace
  path: /spec/template/spec/containers/0/resources/requests/memory
  value: "512Mi"
- op: replace
  path: /spec/template/spec/containers/0/resources/limits/memory
  value: "1Gi"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;- overlays/prod/kustomization.yaml&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - ../../base

patches:
  - target:
      group: apps
      version: v1
      kind: Deployment
      name: wordpress
    path:  deployment-patch.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Install Kustomize
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;run the following:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"  | bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Move Kustomize to your path, so that it can be accessed system wide:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo mv kustomize /usr/local/bin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Kustomize testing and applying&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Testing Kustomize transformations&lt;/strong&gt;&lt;br&gt;
Always be sure to test Kustomize patches and transformations before applying them. Two useful commands for this are using &lt;code&gt;kubectl apply -k&lt;/code&gt;&lt;br&gt;
 with the &lt;code&gt;--dry-run=client -o yaml&lt;/code&gt; flag as well as and &lt;code&gt;kubectl diff&lt;/code&gt;plugin.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example usage:&lt;/em&gt;&lt;br&gt;
&lt;code&gt;kubectl apply -k &amp;lt;path-to-your-kustomization-directory&amp;gt; --dry-run=client -o yaml&lt;/code&gt;&lt;br&gt;
This command outputs the YAML rendered by combining the base resources and applying the patches defined in the &lt;code&gt;&amp;lt;path-to-your-kustomization-directory&amp;gt;&lt;/code&gt; directory, without actually applying any changes. You can inspect the output to ensure the patches were applied correctly.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;kubectl diff&lt;/code&gt; to compare the changes between your live cluster resources and the new configurations generated by Kustomize.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example usage:&lt;/em&gt;&lt;br&gt;
&lt;code&gt;kubectl diff -k &amp;lt;path-to-your-kustomization-directory&amp;gt;&lt;/code&gt;&lt;br&gt;
This will compare the current state of your cluster with the resources that Kustomize would apply and show a diff of the differences.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Apply Patches&lt;/strong&gt;&lt;br&gt;
Once you have confirmed that your overlays are correct and choose which env you want to apply the changes on it, use the &lt;code&gt;kubectl apply -k overlays/&amp;lt;env-name&amp;gt;&lt;/code&gt; command to apply the the settings to your cluster:&lt;br&gt;
&lt;code&gt;kubectl apply -k  overlays/staging&lt;/code&gt;&lt;br&gt;
&lt;code&gt;kubectl apply -k  overlays/prod&lt;/code&gt;&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>kustomization</category>
      <category>kustomize</category>
      <category>helm</category>
    </item>
  </channel>
</rss>
