Kubernetes YAML Kustomize and CRDs

Kustomize overlays, patches, generators, and CRD/CR authoring patterns.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Kustomize Basics

Declarative YAML customization without templates.

Minimal kustomization.yaml

List resource YAML files managed together.

yamlANYkustomizebasics
yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - deployment.yaml
  - service.yaml
  - ingress.yaml
Notes

Kustomize customizes Kubernetes objects through a kustomization file and is supported by kubectl.

Apply a kustomization directory

Use kubectl's built-in Kustomize support.

bashANYkustomizekubectl
bash
kubectl apply -k overlays/prod

Render kustomization output

See final YAML before applying.

bashANYkustomizerender
bash
kubectl kustomize overlays/prod

Set namespace and common labels

Apply shared metadata to all resources.

yamlANYkustomizelabelsnamespace
yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: prod
commonLabels:
  app.kubernetes.io/part-of: storefront

Rewrite image tags in overlay

Update images without editing base manifests.

yamlANYkustomizeimages
yaml
images:
  - name: ghcr.io/example/api
    newTag: 2.1.0

Patches and Generators

Overlay environment-specific changes.

Patch replicas via overlay

Set environment-specific replica count.

yamlANYkustomizepatch
yaml
patches:
  - target:
      kind: Deployment
      name: api
    patch: |
      - op: replace
        path: /spec/replicas
        value: 5

Generate ConfigMap from literals

Create config objects as part of the build.

yamlANYkustomizeconfigmapgenerator
yaml
configMapGenerator:
  - name: app-config
    literals:
      - APP_ENV=production
      - LOG_LEVEL=info

Generate Secret from literals

Generate a Secret from string values.

yamlANYkustomizesecretgenerator
yaml
secretGenerator:
  - name: app-secrets
    literals:
      - API_KEY=replace-me

CRDs and Custom Resources

Define your own APIs and manifest shapes.

Basic CustomResourceDefinition YAML

Define a namespaced custom API.

yamlANYcrdapi-extension
yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: widgets.example.com
spec:
  group: example.com
  names:
    plural: widgets
    singular: widget
    kind: Widget
    shortNames: ["wdg"]
  scope: Namespaced
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                size:
                  type: string
Notes

CRDs let you extend the Kubernetes API with your own resource types and versions.

Custom resource instance YAML

Create an object for the custom API.

yamlANYcustom-resourcecrd
yaml
apiVersion: example.com/v1
kind: Widget
metadata:
  name: blue-widget
spec:
  size: large

Explain a CRD schema path

Inspect CRD structure from kubectl.

bashANYcrdkubectlexplain
bash
kubectl explain widgets.spec --api-version=example.com/v1

Recommended next

No recommendations yet.