Writing Kubernetes Manifests
This lesson explains the structure of a manifest and the Deployment, Service, ConfigMap and Secret objects that a typical application uses.
Learning outcomes
After completing this lesson, you will be able to:
- Identify the four top-level fields of a manifest.
- Explain how labels and selectors connect objects.
- Describe resource requests, limits and probes.
- Compare ConfigMaps, Secrets and Key Vault through the CSI driver.
The structure of a manifest
Every Kubernetes object in a manifest has the same four top-level fields. apiVersion names the API group and version, such as apps/v1 for Deployments or v1 for Services. kind names the object type. metadata holds the name, the namespace and labels, which are key-value pairs used to select objects. spec describes the desired state, and its contents depend on the kind. Several objects can be placed in one file, separated by a line containing three dashes.
Labels and selectors connect objects. A Deployment labels the Pods it creates, for example app: orders-api, and a Service selects Pods with that label. If the labels and the selector do not match, the objects are not connected, which is one of the most common configuration errors.
Deployments
A Deployment manages a stateless application. Its spec contains a replica count, a selector and a Pod template that defines the containers. The Deployment creates a ReplicaSet, which keeps the requested number of Pods running, and replaces the ReplicaSet gradually when the template changes. The following manifest runs three replicas of an API.
apiVersion: apps/v1
kind: Deployment
metadata:
name: orders-api
namespace: orders
labels:
app: orders-api
spec:
replicas: 3
selector:
matchLabels:
app: orders-api
template:
metadata:
labels:
app: orders-api
spec:
containers:
- name: api
image: REGISTRY.azurecr.io/orders-api:7f3c2ab
ports:
- containerPort: 8000
envFrom:
- configMapRef:
name: orders-config
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: orders-secrets
key: db-password
resources:
requests:
cpu: 250m
memory: 256Mi
limits:
memory: 512Mi
readinessProbe:
httpGet:
path: /healthz
port: 8000
periodSeconds: 10
livenessProbe:
httpGet:
path: /healthz
port: 8000
initialDelaySeconds: 15Several parts of the template deserve attention. Resource requests tell the scheduler how much CPU and memory to reserve, so a Pod is placed only on a node with that capacity; limits cap what the container may use, and a container that exceeds its memory limit is killed and restarted. The readiness probe keeps a Pod out of the Service's endpoints until it responds, which matters for containers that load a model at start-up; the liveness probe restarts a container that stops responding. Images are referenced by unique tag so that every replica runs the same code.
Inside a Deployment
Services and ingress
Pods come and go, so clients reach them through a Service, which provides a stable virtual IP address and DNS name and load-balances across the Pods that its selector matches. The Service type decides where it is reachable.
| Service type | Reachable from | Typical use |
|---|---|---|
| ClusterIP (default) | Inside the cluster only, at NAME.NAMESPACE.svc.cluster.local | Internal APIs and back ends |
| NodePort | A port on every node's IP address | Rarely used directly; a building block for load balancers |
| LoadBalancer | An Azure Load Balancer IP address, public or internal | Exposing a TCP or HTTP service outside the cluster |
apiVersion: v1
kind: Service
metadata:
name: orders-api
namespace: orders
spec:
type: ClusterIP
selector:
app: orders-api
ports:
- port: 80
targetPort: 8000The Service listens on port 80 and forwards to targetPort 8000 on the Pods. For HTTP applications, an Ingress object (or the newer Gateway API) routes requests for host names and paths to Services through an ingress controller, such as the managed NGINX controller that the AKS application routing add-on installs, so that many services share one public IP address and TLS certificate.
The following diagram compares the Service's selector with the labels on the Pods.

Exposing an application
ConfigMaps and Secrets
A ConfigMap holds non-confidential configuration as key-value pairs, and a Secret holds confidential values. Both are injected into containers as environment variables (envFrom or valueFrom) or mounted as files. Keeping configuration out of the image lets the same image run in every environment.
Kubernetes Secrets are only base64-encoded in their manifests, and they are stored in the cluster's etcd database, so a Secret manifest must never be committed to source control with real values. Production clusters usually source secrets from Azure Key Vault through the Azure Key Vault provider for Secrets Store CSI Driver, which mounts Key Vault secrets into Pods and can sync them to Kubernetes Secrets, using workload identity to authenticate.
ConfigMaps and Secrets
A Service with no endpoints
A developer applies a Deployment whose Pod template has the label app: orders and a Service whose selector is app: orders-api. The Pods are Running, but requests to the Service time out, and kubectl get endpoints orders-api shows no addresses.
The Service selects Pods by label, and no Pod carries app: orders-api, so the Service has no endpoints to forward to. Changing the Service selector, or the Pod template's labels, so that they match, fixes the problem; the endpoints appear as soon as matching Pods are ready.
Increasing the replica count would add more Pods with the same wrong label. Changing the Service type to LoadBalancer would publish an address that still has nowhere to send traffic. Restarting the Pods would not change their labels.
What happens to a container that uses more memory than its limit, and what happens if it uses more CPU than its limit?
Show answer
Exceeding the memory limit gets the container killed (OOMKilled) and restarted. Exceeding the CPU limit only throttles it.
Exam Tips
Remember for the exam
- Every manifest has apiVersion, kind, metadata and spec.
- Services find Pods through label selectors; a mismatch leaves no endpoints.
- Requests reserve capacity for scheduling; exceeding a memory limit kills the container.
- Kubernetes Secrets are base64-encoded, not encrypted; use Key Vault through the Secrets Store CSI Driver.