CodeBlanks Logo Icon CodeBlanks

Web Deployment

Write a Deployment for the web application.

The web and API Deployments

The API runs with two replicas, takes its settings from a ConfigMap and the password from a Secret, and has a readiness check. Web (nginx) has three replicas and forwards requests to http://api:8000:

# 20-api.yaml (summary)
spec:
  replicas: 2
  template:
    spec:
      containers:
        - name: api
          image: ghcr.io/ali/magaza-api:1.0
          envFrom:
            - configMapRef: {name: api-ayar}
          env:
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef: {name: db-gizli, key: POSTGRES_PASSWORD}
          readinessProbe:
            httpGet: {path: /saglik, port: 8000}

Exposing the web service

Only web is exposed to the outside world. In Minikube, NodePort is enough; in the cloud, a LoadBalancer or (later) an Ingress is used:

apiVersion: v1
kind: Service
metadata:
  name: web
  namespace: magaza
spec:
  type: NodePort
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30080

minikube service web -n magaza --url

Practice The API deployment

Write a Deployment YAML: name api, namespace magaza, replicas: 2; selector and template label app: api; container named api, image ghcr.io/ali/magaza-api:1.0; the api-ayar ConfigMap under envFrom.

Starter code

terminal
# buraya yaz

Sample solution

terminal
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
  namespace: magaza
spec:
  replicas: 2
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
        - name: api
          image: ghcr.io/ali/magaza-api:1.0
          envFrom:
            - configMapRef:
                name: api-ayar

Expected output

deployment.apps/api created

Questions and answers

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

1 If the readiness check fails as long as the API can't connect to the database, what happens?

  • Web crashes
  • The API pods get no traffic but aren't restarted Correct answer
  • The API pods are deleted
  • The database is restarted

Explanation: A readiness failure only removes it from the Service endpoints.

2 Take a single key from a Secret.

Question
              valueFrom:
                ____: {name: db-gizli, key: POSTGRES_PASSWORD}
Answer
              valueFrom:
                secretKeyRef: {name: db-gizli, key: POSTGRES_PASSWORD}

Explanation: secretKeyRef selects a single key.

3 Match.

Term Meaning
api replicas: 2 Redundant API
readinessProbe No traffic until ready
web NodePort Single external entry
{name: x} YAML inline map

All lessons in Intermediate