DevOps - CI/CD Tutorial - Part 6
DevOps - CI/CD Tutorial - Part 6
Learn secure Kubernetes delivery, rollouts, and rollbacks from registry images.
1Part 5 Recap
Part 5 covered CI pipelines, Docker image builds, and HTTP smoke tests. This part delivers that artifact into a cluster safely.
2Continuous Delivery Flow
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
kubectl config current-contextandkubectl get nsfirst identify the target cluster and namespace.- Have an already-pushed image and its immutable digest in the registry.
- Use a deployment identity with write access only to the required namespace and resource types.
- 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
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
6Tag and Digest Pinning
A tag is convenient but mutable. An immutable digest in repo@sha256:... identifies one exact image manifest.
# 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
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.
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
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.
No comments :
Post a Comment
Please Write a Message for Programming or something else 🙏