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
Minimal kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - deployment.yaml
  - service.yaml
  - ingress.yaml

# List resource YAML files managed together.

Apply a kustomization directory
kubectl apply -k overlays/prod

# Use kubectl's built-in Kustomize support.

Render kustomization output
kubectl kustomize overlays/prod

# See final YAML before applying.

Set namespace and common labels
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: prod
commonLabels:
  app.kubernetes.io/part-of: storefront

# Apply shared metadata to all resources.

Rewrite image tags in overlay
images:
  - name: ghcr.io/example/api
    newTag: 2.1.0

# Update images without editing base manifests.

## Patches and Generators
Patch replicas via overlay
patches:
  - target:
      kind: Deployment
      name: api
    patch: |
      - op: replace
        path: /spec/replicas
        value: 5

# Set environment-specific replica count.

Generate ConfigMap from literals
configMapGenerator:
  - name: app-config
    literals:
      - APP_ENV=production
      - LOG_LEVEL=info

# Create config objects as part of the build.

Generate Secret from literals
secretGenerator:
  - name: app-secrets
    literals:
      - API_KEY=replace-me

# Generate a Secret from string values.

## CRDs and Custom Resources
Basic CustomResourceDefinition 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

# Define a namespaced custom API.

Custom resource instance YAML
apiVersion: example.com/v1
kind: Widget
metadata:
  name: blue-widget
spec:
  size: large

# Create an object for the custom API.

Explain a CRD schema path
kubectl explain widgets.spec --api-version=example.com/v1

# Inspect CRD structure from kubectl.

Recommended next

No recommendations yet.