Lesson 2 of 3 Deploying Applications to Azure Kubernetes Service

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: 15

Several 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.

Match the pairs

Inside a Deployment

Exam skills 1.2.3

Match each part of a Deployment manifest with what it does.

Log in to try this activity Interactive activities are free with an account.

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 typeReachable fromTypical use
ClusterIP (default)Inside the cluster only, at NAME.NAMESPACE.svc.cluster.localInternal APIs and back ends
NodePortA port on every node's IP addressRarely used directly; a building block for load balancers
LoadBalancerAn Azure Load Balancer IP address, public or internalExposing 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: 8000

The 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.

Why the Service has no endpoints. The Service selects Pods with the label app: orders-api, but the Deployment's Pod template labels its Pods app: orders, so no Pod matches and requests time out. Making the selector and the Pod labels match creates the endpoints as soon as matching Pods are ready.
A Service forwards only to Pods whose labels match its selector.
Scenario questions

Exposing an application

Exam skills 1.2.3

Choose how to expose each service from an AKS cluster.

Log in to try this activity Interactive activities are free with an account.

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.

ConfigMap
Intended contentOrdinary settings
Stored inetcd, plain
Rotation and auditManual
Choose when Feature switches, URLs, sizes
Kubernetes Secret
Intended contentConfidential values
Stored inetcd, base64-encoded
Rotation and auditManual
Choose when Simple clusters and test environments
Key Vault via Secrets Store CSI Driver
Intended contentConfidential values
Stored inAzure Key Vault
Rotation and auditKey Vault versions, rotation and logs
Choose when Production secrets that must be audited and rotated
True or false

ConfigMaps and Secrets

Exam skills 1.2.3 · 4.1.1

Decide whether each statement about configuration in Kubernetes is true.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.