Powered by Blogger.

DevOps - CI/CD Tutorial - Part 6

No comments :

DevOps - CI/CD Tutorial - Part 6

Learn secure Kubernetes delivery, rollouts, and rollbacks from registry images.

By A143MK  |  DevOps Learning Series  |  Part 6

1Part 5 Recap

Part 5 covered CI pipelines, Docker image builds, and HTTP smoke tests. This part delivers that artifact into a cluster safely.

Continuous Delivery usually deploys to production after an approval or release gate. Continuous Deployment can automate that gate.

2Continuous Delivery Flow

CodeTest and merge
RegistryImmutable digest
Cluster policyRBAC / admission
RolloutHealth checks

A CD pipeline should not rebuild an image that was already verified. Promote an immutable digest from the registry so staging and production run the exact same artifact.

3Cluster and Registry Prerequisites

  1. kubectl config current-context and kubectl get ns first identify the target cluster and namespace.
  2. Have an already-pushed image and its immutable digest in the registry.
  3. Use a deployment identity with write access only to the required namespace and resource types.
  4. Configure a protected environment, reviewers, and restricted branch rules for production.
kubectl config current-context
kubectl auth can-i get pods -n a143mk-demo
kubectl auth can-i create deployments -n a143mk-demo
kubectl auth can-i update deployments -n a143mk-demo
kubectl -n a143mk-demo get deployment,service
Do not deploy to an unknown, shared, or production cluster. Never print credentials or kubeconfig contents in posts or logs.

4Deployment Manifest

This example uses a digest placeholder. A real workflow replaces the placeholder before applying the manifest.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: a143mk-api
  namespace: a143mk-demo
spec:
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 0
      maxSurge: 1
  selector:
    matchLabels:
      app: a143mk-api
  template:
    metadata:
      labels:
        app: a143mk-api
    spec:
      containers:
        - name: api
          image: ghcr.io/YOUR_ACCOUNT/a143mk-api@sha256:REPLACE_WITH_DIGEST
          ports:
            - containerPort: 8080
          readinessProbe:
            httpGet:
              path: /health
              port: 8080
            initialDelaySeconds: 2
            periodSeconds: 5
          livenessProbe:
            httpGet:
              path: /health
              port: 8080
            initialDelaySeconds: 10
            periodSeconds: 10
---

A liveness probe restarts the container; a readiness probe stops traffic. Probe failures may indicate an application problem. Configure a valid health endpoint and realistic startup time.

5CD Workflow Example

This is a design example, not a copy-and-run workflow. Adapt credentials, registry, cluster, and manifests for your environment and obtain organizational approval first.

name: Promote and deploy API

on:
  workflow_dispatch:
    inputs:
      image_digest:
        description: "Verified sha256 digest"
        required: true
        type: string

permissions:
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production
    timeout-minutes: 15
    steps:
      - name: Check out deployment manifests
        uses: actions/checkout@v4
        with:
            persist-credentials: false

      - name: Install a pinned kubectl
        run: echo "Configure the approved kubectl installation here"

      - name: Select only your deployment context
        run: |
            echo "Configure a short-lived kube credential before this step"
            kubectl config current-context

      - name: Substitute image digest
        run: |
            test "${{ inputs.image_digest }}" =~ ^sha256:[a-f0-9]{64}$
            sed -i "s|sha256:REPLACE_WITH_DIGEST|${{ inputs.image_digest }}|" k8s/deployment.yaml

      - name: Apply and verify
        run: |
            kubectl -n a143mk-demo apply -f k8s/deployment.yaml
            kubectl -n a143mk-demo rollout status deployment/a143mk-api --timeout=300s
Do not put a long-lived kubeconfig or static cluster-admin secret in a workflow. Use your organization's OIDC, short-lived credentials, or secure GitOps controller.

6Tag and Digest Pinning

A tag is convenient but mutable. An immutable digest in repo@sha256:... identifies one exact image manifest.

Mutable tagapi:v1.2
Registry manifestsha256 digest
Pinned rollout@sha256:...
# Ask the registry for the digest of an already-published image.
docker buildx imagetools inspect ghcr.io/YOUR_ACCOUNT/a143mk-api:v1.2

# Record the immutable digest in your release record.
# Example manifest:
# image: ghcr.io/YOUR_ACCOUNT/a143mk-api@sha256:64_HEX_CHARACTERS

7Rollout Verification

After apply, inspect rollout status and test the service's real HTTP health. kubectl rollout status waits for completion, while an endpoint test verifies behavior separately.

kubectl -n a143mk-demo rollout status deployment/a143mk-api --timeout=300s
kubectl -n a143mk-demo get deployment a143mk-api
kubectl -n a143mk-demo get pods -l app=a143mk-api
kubectl -n a143mk-demo rollout history deployment/a143mk-api

kubectl -n a143mk-demo run a143mk-curl \
  --image=curlimages/curl:latest \
  --restart=Never \
  --rm -i -- curl --fail --silent --show-error \
  http://a143mk-api:8080/health
Complete promotion only after deployment rollout and Pod readiness. Add external monitoring and synthetic checks too.

8Rollback Strategy

Roll back to a known-good digest or previous revision. Rollback changes only the Kubernetes workload; database schemas, external APIs, and data migrations do not revert automatically.

kubectl -n a143mk-demo rollout history deployment/a143mk-api
kubectl -n a143mk-demo rollout undo deployment/a143mk-api
kubectl -n a143mk-demo rollout status deployment/a143mk-api --timeout=300s
kubectl -n a143mk-demo get deployment a143mk-api -o wide

Define a clear release-halt rule: inspect health, metrics, and logs, then roll back according to an approved team decision.

9Credentials and Approval

  • Use a separate identity and least privilege for each environment.
  • Keep registry and Kubernetes credentials secret or short-lived through OIDC.
  • Use protected branches and environment approvals so only approved commits deploy.
  • Link commits, image digests, workflow runs, and approvers in a release record for audit.
Granting secrets to workflows from pull requests increases risk. Never give untrusted workflows trusted production credentials.

10GitOps and Helm

In GitOps, a Git repository is the source of desired state. A controller observes repository changes and reconciles Kubernetes.

Common structure:

gitops-repository/
  environments/
    staging/
      kustomization.yaml
      deployment-patch.yaml
    production/
      kustomization.yaml
      deployment-patch.yaml

Helm packages releases with templates, values, and charts. Raw manifests are simpler; use Helm when reusable templates are genuinely needed.

11Troubleshooting

  • ImagePullBackOff: Check the digest, registry authentication, and imagePullSecrets.
  • CrashLoopBackOff: Inspect logs and the previous container state.
  • Rollout stuck: Check available replicas, readiness, and resource limits.
  • HTTP failure: Verify the Service, endpoints, and Pod ports independently.
kubectl -n a143mk-demo describe deployment/a143mk-api
kubectl -n a143mk-demo get pods -l app=a143mk-api
kubectl -n a143mk-demo logs -l app=a143mk-api --tail=100
kubectl -n a143mk-demo get endpoints a143mk-api
kubectl -n a143mk-demo get events --sort-by=.lastTimestamp

12Quick Quiz

1. Why prefer a digest over a mutable tag?
2. Does rollback also revert a data migration?

13Final Checklist

  • The correct cluster, namespace, and identity are verified.
  • The image digest and rollback point are in the release record.
  • Readiness and liveness probes test real endpoints.
  • Rollout status, HTTP tests, logs, and metrics are checked.
  • Production approvals and short-lived credentials are enforced.
  • Database migration and configuration rollback are separately planned.
Next: observability, SLOs, and automated recovery

No comments :

Post a Comment

Please Write a Message for Programming or something else 🙏