Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 37 ยท Projects and Workloads

YAML Manifests, oc apply and Templates

Manage OpenShift objects declaratively with YAML, oc apply, diff and dry-run, organize environments with Kustomize, and meet OpenShift Templates and parameters.

Typing oc new-app is great for learning, but real teams keep their application description in files under version control. This is called working declaratively: you edit YAML, apply it, and the cluster converges to what the file says. It gives you history, review, repeatability and the ability to recreate an environment from scratch.

YAML in five minutes

YAML is a human-friendly data format. For OpenShift you need only these rules:

  • Indentation with spaces (never tabs) shows nesting; two spaces is the convention.
  • key: value is a mapping; a line starting with - is a list item.
  • Strings usually need no quotes, but quote values that could be read as other types ("true", "8080" for a string, "1.0").
  • | starts a multi-line text block; # starts a comment.
  • Several objects in one file are separated by a line with ---.
YAML
apiVersion: v1
kind: Service
metadata:
  name: web
  labels:
    app: web
spec:
  selector:
    app: web
  ports:
    - name: http
      port: 8080
      targetPort: 8080
---
apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: web
spec:
  to:
    kind: Service
    name: web
  port:
    targetPort: http
  tls:
    termination: edge

create, apply, replace, delete

Terminal
oc create -f app.yaml
oc apply -f app.yaml
oc apply -f manifests/
oc apply -R -f manifests/
oc delete -f app.yaml
  • oc create fails if the object already exists.
  • oc apply creates or updates. It remembers your last-applied configuration (and with server-side apply, who owns which field) so it can merge changes. This is the everyday command.
  • oc delete -f removes what the file describes.

Before applying, look at what would change:

Terminal
oc apply -f app.yaml --dry-run=client -o yaml
oc apply -f app.yaml --dry-run=server
oc diff -f app.yaml

--dry-run=server sends the object to the API server for validation and admission without saving it, so quota and security errors show early. oc diff prints the difference between your file and the live object.

Add --server-side to let the API server track field ownership and detect conflicts, a good option when several tools (you and a GitOps controller, for example) manage one object.

Generating YAML from commands

You do not write every file from scratch. Let the CLI draft it:

Terminal
oc create deployment web --image=quay.io/example/web:1.0 --replicas=2 --dry-run=client -o yaml > web.yaml
oc create configmap web-config --from-literal=A=1 --dry-run=client -o yaml
oc get deployment web -o yaml

When saving exported YAML from a live object, remove server-added fields (status, metadata.uid, resourceVersion, creationTimestamp, managedFields) before committing; they belong to the cluster, not to your source.

Organizing files with Kustomize

You usually need the same app with small differences per environment: replicas, host names, image tags. Kustomize is built into oc (oc apply -k). A folder structure:

TEXT
shop/
  base/
    deployment.yaml
    service.yaml
    kustomization.yaml
  overlays/
    dev/kustomization.yaml
    prod/kustomization.yaml
YAML
# base/kustomization.yaml
resources:
  - deployment.yaml
  - service.yaml

# overlays/prod/kustomization.yaml
resources:
  - ../../base
namePrefix: prod-
replicas:
  - name: web
    count: 4
images:
  - name: quay.io/example/web
    newTag: "1.4.2"
Terminal
oc kustomize overlays/prod
oc apply -k overlays/prod

The overlay patches the base without copying it. GitOps with Argo CD later builds on this layout.

OpenShift Templates

Before Helm and Kustomize were common, OpenShift introduced Templates: a list of objects with ${PARAMETER} placeholders.

YAML
apiVersion: template.openshift.io/v1
kind: Template
metadata:
  name: web-template
parameters:
  - name: NAME
    required: true
  - name: REPLICAS
    value: "2"
objects:
  - apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: ${NAME}
    spec:
      replicas: ${{REPLICAS}}
      selector:
        matchLabels: { app: ${NAME} }
      template:
        metadata:
          labels: { app: ${NAME} }
        spec:
          containers:
            - name: web
              image: quay.io/example/web:1.0

Use ${NAME} for string substitution and ${{REPLICAS}} when the result must be a number. Instantiate with:

Terminal
oc process -f web-template.yaml -p NAME=shop -p REPLICAS=3 | oc apply -f -
oc new-app --template=web-template -p NAME=shop
oc process --parameters -f web-template.yaml

Templates still work and ship in the cluster catalog, but many teams now prefer Helm charts or Kustomize because those are portable to any Kubernetes.

Stand-in: template parameter substitution

This stand-in mimics oc process on a string template. It is much simpler than the real implementation, which understands JSON and the ${{ }} number form.

Python Stand-in: process a template

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Stand-in: a tiny kustomize overlay

Python Stand-in: overlay patches a base

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Habits that pay off

  • One resource per logical file, or one file per application with --- separators.
  • Put manifests in Git and review changes like code.
  • Always oc diff or --dry-run=server before touching production.
  • Never hand-edit objects that a GitOps tool manages; edit Git instead.
  • Pin image tags (or digests); avoid latest.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the difference between oc create -f and oc apply -f?

    oc apply is declarative and idempotent, so you can re-run it as your files change.
  2. 2. How do you preview what an apply would change in the cluster?

    oc diff compares your file with the live objects. --dry-run=server also validates against admission.
  3. 3. In an OpenShift Template, how do you substitute a parameter as a number?

    ${{NAME}} substitutes the value without quotes so it stays a number; ${NAME} yields a string.
  4. 4. What is Kustomize used for?

    Kustomize layers patches on a base without copying the base files.

Summary

  • Keep manifests in Git and use oc apply, oc diff and --dry-run=server.
  • Generate drafts with --dry-run=client -o yaml, and strip server-added fields from exports.
  • Use Kustomize (oc apply -k) for per-environment overlays.
  • OpenShift Templates provide parameterized object lists; Helm and Kustomize are the portable alternatives.

Next: BuildConfigs and Build Strategies