CodeBlanks Logo Icon CodeBlanks

Deployment YAML

Learn the Deployment YAML structure.

Deployment YAML

A Deployment's spec has three important parts: replicas, the selector determining which pods belong to it, and the template, the mould for new pods. The labels in the template must match the selector:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: nginx
          image: nginx:1.27
          ports:
            - containerPort: 80

Resource requests and limits

In production, resources are written for each container. requests are the guaranteed amount the scheduler reserves; limits are the ceiling that can't be exceeded (a container exceeding its memory limit is OOMKilled):

          resources:
            requests:
              cpu: 100m
              memory: 128Mi
            limits:
              cpu: 500m
              memory: 256Mi

100m = 0.1 CPU core; Mi = mebibytes.

Practice Write the deployment YAML

Write a Deployment YAML: name web, replicas: 3; selector and template label app: web; container named nginx, image nginx:1.27, containerPort: 80.

Starter code

terminal
# buraya yaz

Sample solution

terminal
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: nginx
          image: nginx:1.27
          ports:
            - containerPort: 80

Expected output

deployment.apps/web created

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 If selector.matchLabels is app: web but the template label is app: api, what happens?

  • It works fine
  • The selector is fixed automatically
  • The API server rejects the object Correct answer
  • Pods are created but not counted

Explanation: You get a "selector does not match template labels" error.

2 Write the field defining the mould for new pods.

Question
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  ____:
    metadata:
      labels:
        app: web
Answer
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web

Explanation: template is a pod definition (metadata + spec).

3 Match.

Term Meaning
replicas Replica count
selector Which pods are mine
template Pod mould
requests Guaranteed resources

All lessons in Intermediate