Operations 38 min read

Build an Enterprise‑Grade DevOps Pipeline in 7 Days: Hands‑On Guide + Ready‑to‑Use Scripts

This step‑by‑step guide shows how to create a full‑stack, enterprise‑level DevOps CI/CD pipeline—from environment setup and Docker installation to Jenkins pipeline scripts, Kubernetes deployments, monitoring, security hardening, and cost‑optimisation—enabling teams to reduce release cycles from days to minutes within a week.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Build an Enterprise‑Grade DevOps Pipeline in 7 Days: Hands‑On Guide + Ready‑to‑Use Scripts

Introduction

"Why does our release cycle take three days from code commit to production? Can we shrink it to one hour?" This common demand from business stakeholders drives the need for an automated DevOps pipeline. Traditional manual releases are slow and error‑prone; a single mis‑configured file can cause production incidents. Drawing on experience from multiple enterprises, this article presents a practical method to build an enterprise‑grade DevOps pipeline that boosts release efficiency by more than tenfold and cuts failure rates by 80%.

Technical Background: Evolution and Value of DevOps Pipelines

What is a DevOps Pipeline?

A DevOps pipeline (CI/CD) is an automated software delivery methodology and toolchain that links code development, testing, and deployment, enabling Continuous Integration and Continuous Delivery.

Traditional Release Pain Points

Manual operations : compilation, packaging, and deployment require human intervention, leading to delays and errors.

Environment inconsistency : differences between dev, test, and prod cause "works on my machine" issues.

Difficult rollbacks : recovering to a previous stable version is time‑consuming.

Lack of quality gates : missing code quality and security scans.

Low collaboration efficiency : high communication overhead between development and operations.

Core Components of a DevOps Pipeline

A complete enterprise pipeline typically includes:

Code management (Git/GitLab/GitHub): version control and collaboration.

Continuous integration (Jenkins/GitLab CI/GitHub Actions): automated build and test.

Artifact management (Nexus/Harbor): centralized storage of binaries and images.

Quality gates (SonarQube): code quality and security scanning.

Containerization (Docker/Kubernetes): standardized deployment.

Configuration management (Ansible/Consul): environment configuration and service discovery.

Monitoring & alerting (Prometheus/Grafana): end‑to‑end observability.

Quantifying Enterprise‑Level DevOps Value

Release frequency : from 1‑2 times per month to 10+ times per day.

Delivery cycle : from several days to minutes.

Change failure rate : from 15‑20% down to 0‑5%.

Mean time to recovery : from hours to under 15 minutes.

Labor cost savings : a 5‑person ops team can support 50+ micro‑services.

During a major e‑commerce promotion, a company achieved 120 daily releases with zero incidents—unthinkable under the traditional model.

Hands‑On: Building the Enterprise DevOps Pipeline

Overall Architecture Design

We will build a pipeline based on Jenkins, GitLab, Docker, and Kubernetes:

开发提交代码 → GitLab(代码仓库)
    ↓
GitLab Webhook触发 → Jenkins(CI/CD引擎)
    ↓
Jenkins Pipeline执行:
    1. 代码检出
    2. 代码扫描(SonarQube)
    3. 单元测试
    4. 编译构建
    5. Docker镜像构建
    6. 推送到Harbor
    7. 部署到K8s(测试环境)
    8. 自动化测试
    9. 人工审批
    10. 部署到生产环境
    ↓
监控告警(Prometheus + Grafana)

Environment Preparation Checklist

3 servers (minimum 4‑core/8 GB RAM, production recommended 8‑core/16 GB).

OS: CentOS 7.9 or Ubuntu 20.04.

Network: all nodes can communicate and reach the internet.

Phase 1: Basic Environment Setup (Day 1‑2)

1. Install Docker

All nodes require Docker:

#!/bin/bash
# install_docker.sh - Docker installation script
set -e

echo "=== Starting Docker installation ==="
# Remove old versions
sudo yum remove -y docker docker-client docker-client-latest \
    docker-common docker-latest docker-latest-logrotate \
    docker-logrotate docker-engine
# Install dependencies
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Add Docker repo
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Install Docker
sudo yum install -y docker-ce docker-ce-cli containerd.io
# Configure daemon
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<'EOF'
{
  "registry-mirrors": ["https://mirror.ccs.tencentyun.com","https://registry.docker-cn.com"],
  "log-driver": "json-file",
  "log-opts": {"max-size": "100m","max-file": "3"},
  "storage-driver": "overlay2",
  "exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
# Start Docker
sudo systemctl daemon-reload
sudo systemctl enable docker
sudo systemctl start docker

docker --version
docker info

echo "=== Docker installation complete ==="

2. Deploy GitLab Repository

GitLab provides full code management and CI/CD capabilities:

#!/bin/bash
# install_gitlab.sh - GitLab installation script
set -e

GITLAB_HOME="/data/gitlab"
GITLAB_HOSTNAME="gitlab.company.com" # modify to your domain

echo "=== Starting GitLab installation ==="
# Create data directories
sudo mkdir -p ${GITLAB_HOME}/{config,logs,data}
# Run GitLab container
docker run -d \
  --hostname ${GITLAB_HOSTNAME} \
  --name gitlab \
  --restart always \
  --publish 443:443 \
  --publish 80:80 \
  --publish 22:22 \
  --volume ${GITLAB_HOME}/config:/etc/gitlab \
  --volume ${GITLAB_HOME}/logs:/var/log/gitlab \
  --volume ${GITLAB_HOME}/data:/var/opt/gitlab \
  --shm-size 256m \
  gitlab/gitlab-ce:latest

echo "=== GitLab is starting, wait 3‑5 minutes ==="
for i in {1..30}; do
  if docker logs gitlab 2>&1 | grep -q "gitlab Reconfigured"; then
    echo "GitLab started successfully!"
    break
  fi
  echo "Waiting... ($i/30)"
  sleep 10
done

# Show initial password and access URL
echo "=== GitLab initial admin password ==="
docker exec gitlab cat /etc/gitlab/initial_root_password | grep Password:

echo "Access URL: http://${GITLAB_HOSTNAME}"
echo "Admin user: root"

3. Deploy Harbor Image Registry

Harbor stores Docker images:

#!/bin/bash
# install_harbor.sh - Harbor installation script
set -e

HARBOR_VERSION="v2.9.0"
HARBOR_DOMAIN="harbor.company.com" # modify to your domain
HARBOR_ADMIN_PASSWORD="Harbor12345" # change admin password

echo "=== Starting Harbor installation ==="
# Install docker‑compose
sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# Download Harbor
cd /opt
wget https://github.com/goharbor/harbor/releases/download/${HARBOR_VERSION}/harbor-offline-installer-${HARBOR_VERSION}.tgz
tar xzvf harbor-offline-installer-${HARBOR_VERSION}.tgz
cd harbor
cp harbor.yml.tmpl harbor.yml
sed -i "s/hostname: reg.mydomain.com/hostname: ${HARBOR_DOMAIN}/g" harbor.yml
sed -i "s/harbor_admin_password: Harbor12345/harbor_admin_password: ${HARBOR_ADMIN_PASSWORD}/g" harbor.yml
# Disable HTTPS for testing (enable in production)
sed -i '/^https:/,+8 s/^/#/' harbor.yml
# Install Harbor
sudo ./install.sh

echo "=== Harbor installation complete ==="
echo "Access URL: http://${HARBOR_DOMAIN}"
echo "Admin user: admin"
echo "Admin password: ${HARBOR_ADMIN_PASSWORD}"

4. Deploy Jenkins CI/CD Engine

#!/bin/bash
# install_jenkins.sh - Jenkins installation script
set -e

JENKINS_HOME="/data/jenkins"

echo "=== Starting Jenkins installation ==="
# Create data directory
sudo mkdir -p ${JENKINS_HOME}
sudo chown -R 1000:1000 ${JENKINS_HOME}
# Run Jenkins container
docker run -d \
  --name jenkins \
  --restart always \
  --user root \
  -p 8080:8080 \
  -p 50000:50000 \
  -v ${JENKINS_HOME}:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v /usr/bin/docker:/usr/bin/docker \
  jenkins/jenkins:lts

echo "=== Waiting for Jenkins to start (≈2 minutes) ==="
sleep 120

# Show initial admin password
echo "=== Jenkins initial admin password ==="
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword

echo "Access URL: http://YOUR_IP:8080"

Phase 2: Jenkins Pipeline Configuration (Day 3‑4)

1. Jenkins Basic Setup

After logging into Jenkins, install required plugins: Git, Docker, Kubernetes, Pipeline, GitLab, SonarQube Scanner.

2. Create Jenkinsfile

The core Jenkinsfile implements the full CI/CD flow:

// Jenkinsfile - Enterprise pipeline script
pipeline {
    agent any

    environment {
        PROJECT_NAME = 'demo-app'
        GIT_REPO = 'http://gitlab.company.com/dev/demo-app.git'
        GIT_CREDENTIALS = 'gitlab-credentials'
        HARBOR_URL = 'harbor.company.com'
        HARBOR_PROJECT = 'production'
        HARBOR_CREDENTIALS = 'harbor-credentials'
        IMAGE_NAME = "${HARBOR_URL}/${HARBOR_PROJECT}/${PROJECT_NAME}"
        K8S_NAMESPACE_TEST = 'test'
        K8S_NAMESPACE_PROD = 'production'
        K8S_CREDENTIALS = 'k8s-credentials'
        SONAR_URL = 'http://sonarqube.company.com'
        BUILD_TAG = "${env.BUILD_NUMBER}-${env.GIT_COMMIT?.take(8)}"
    }

    stages {
        stage('代码检出') {
            steps {
                script {
                    echo "=== 从GitLab检出代码 ==="
                    git branch: 'master', credentialsId: "${GIT_CREDENTIALS}", url: "${GIT_REPO}"
                    env.GIT_COMMIT_MSG = sh(script: 'git log -1 --pretty=%B', returnStdout: true).trim()
                    env.GIT_AUTHOR = sh(script: 'git log -1 --pretty=%an', returnStdout: true).trim()
                }
            }
        }
        stage('代码扫描') {
            steps {
                script {
                    echo "=== 执行SonarQube代码扫描 ==="
                    withSonarQubeEnv('SonarQube') {
                        sh """sonar-scanner -Dsonar.projectKey=${PROJECT_NAME} -Dsonar.sources=. -Dsonar.host.url=${SONAR_URL} -Dsonar.login=${SONAR_TOKEN}"""
                    }
                }
            }
        }
        stage('质量门禁') {
            steps {
                script {
                    echo "=== 等待SonarQube质量门禁结果 ==="
                    timeout(time: 5, unit: 'MINUTES') {
                        def qg = waitForQualityGate()
                        if (qg.status != 'OK') {
                            error "代码质量不达标: ${qg.status}"
                        }
                    }
                }
            }
        }
        stage('单元测试') {
            steps {
                script {
                    echo "=== 执行单元测试 ==="
                    sh """# Java example
mvn clean test
# Node.js example
# npm test
# Python example
# pytest --cov=./ --cov-report=xml"""
                }
            }
            post { always { junit '**/target/surefire-reports/*.xml' } }
        }
        stage('编译构建') {
            steps { script { echo "=== 编译项目代码 ==="; sh """# Java
mvn clean package -DskipTests
# Node.js
# npm run build
# Go
# go build -o app main.go""" } }
        }
        stage('构建镜像') {
            steps { script { echo "=== 构建Docker镜像 ==="; sh """cat > Dockerfile <<'EOF'
FROM openjdk:11-jre-slim
WORKDIR /app
COPY target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT [\"java\", \"-jar\", \"app.jar\"]
EOF
"""
                    docker.build("${IMAGE_NAME}:${BUILD_TAG}")
                    docker.build("${IMAGE_NAME}:latest")
                } }
            }
        }
        stage('推送镜像') {
            steps { script { echo "=== 推送镜像到Harbor ==="; docker.withRegistry("http://${HARBOR_URL}", "${HARBOR_CREDENTIALS}") { docker.image("${IMAGE_NAME}:${BUILD_TAG}").push(); docker.image("${IMAGE_NAME}:latest").push() } } } }
        stage('部署到测试环境') {
            steps { script { echo "=== 部署到Kubernetes测试环境 ==="; withKubeConfig([credentialsId: "${K8S_CREDENTIALS}"]) { sh """kubectl set image deployment/${PROJECT_NAME} ${PROJECT_NAME}=${IMAGE_NAME}:${BUILD_TAG} -n ${K8S_NAMESPACE_TEST}
kubectl rollout status deployment/${PROJECT_NAME} -n ${K8S_NAMESPACE_TEST} --timeout=5m""" } } } }
        stage('自动化测试') {
            steps { script { echo "=== 执行接口自动化测试 ==="; sh """sleep 30
# newman run tests/api-tests.json
# pytest tests/integration/
echo "自动化测试通过""" } } }
        stage('人工审批') {
            when { branch 'master' }
            steps { script { echo "=== 等待人工审批发布到生产环境 ==="; def userInput = input(id: 'userInput', message: '是否发布到生产环境?', parameters: [booleanParam(defaultValue: false, description: '确认发布', name: 'DEPLOY_TO_PROD')]); if (!userInput) { error "用户取消了生产环境发布" } } } }
        stage('部署到生产环境') {
            when { branch 'master' }
            steps { script { echo "=== 部署到Kubernetes生产环境 ==="; withKubeConfig([credentialsId: "${K8S_CREDENTIALS}"]) { sh """kubectl annotate deployment/${PROJECT_NAME} kubernetes.io/change-cause="Build ${BUILD_TAG} by ${GIT_AUTHOR}: ${GIT_COMMIT_MSG}" -n ${K8S_NAMESPACE_PROD}
kubectl set image deployment/${PROJECT_NAME} ${PROJECT_NAME}=${IMAGE_NAME}:${BUILD_TAG} -n ${K8S_NAMESPACE_PROD}
kubectl rollout status deployment/${PROJECT_NAME} -n ${K8S_NAMESPACE_PROD} --timeout=10m""" } } } }
        stage('健康检查') {
            when { branch 'master' }
            steps { script { echo "=== 生产环境健康检查 ==="; sh """SERVICE_URL=$(kubectl get svc ${PROJECT_NAME} -n ${K8S_NAMESPACE_PROD} -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
for i in {1..10}; do
  HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://$SERVICE_URL/health)
  if [ "$HTTP_CODE" == "200" ]; then echo "健康检查通过"; exit 0; fi
  echo "等待服务就绪... ($i/10)"
  sleep 10
done
echo "健康检查失败,准备回滚"
exit 1""" } } }
    }
    post {
        success { script { echo "=== 流水线执行成功 ==="; sh """curl -X POST 'https://oapi.dingtalk.com/robot/send?access_token=YOUR_TOKEN' -H 'Content-Type: application/json' -d '{"msgtype":"markdown","markdown":{"title":"发布成功","text":"### ${PROJECT_NAME} 发布成功

- 构建编号: ${BUILD_NUMBER}
- 镜像版本: ${BUILD_TAG}
- 提交人: ${GIT_AUTHOR}
- 提交信息: ${GIT_COMMIT_MSG}"}}'""" } }
        failure { script { echo "=== 流水线执行失败 ==="; withKubeConfig([credentialsId: "${K8S_CREDENTIALS}"]) { sh """if [ "${env.BRANCH_NAME}" == "master" ]; then
  echo "执行自动回滚"
  kubectl rollout undo deployment/${PROJECT_NAME} -n ${K8S_NAMESPACE_PROD}
fi""" }
            sh """curl -X POST 'https://oapi.dingtalk.com/robot/send?access_token=YOUR_TOKEN' -H 'Content-Type: application/json' -d '{"msgtype":"text","text":{"content":"【告警】${PROJECT_NAME} 发布失败! 构建编号: ${BUILD_NUMBER}, 请及时处理!"},"at":{"isAtAll":true}}'""" } }
        always { cleanWs() }
    }
}

Phase 3: Kubernetes Deployment Configuration (Day 5‑6)

Kubernetes Deployment Manifests

# k8s/deployment.yaml - Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-app
  namespace: production
  labels:
    app: demo-app
spec:
  replicas: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: demo-app
  template:
    metadata:
      labels:
        app: demo-app
    spec:
      containers:
      - name: demo-app
        image: harbor.company.com/production/demo-app:latest
        ports:
        - containerPort: 8080
          name: http
        env:
        - name: JAVA_OPTS
          value: "-Xms512m -Xmx1024m"
        - name: SPRING_PROFILES_ACTIVE
          value: "production"
        resources:
          requests:
            memory: "512Mi"
            cpu: "500m"
          limits:
            memory: "1Gi"
            cpu: "1000m"
        livenessProbe:
          httpGet:
            path: /actuator/health/liveness
            port: 8080
          initialDelaySeconds: 60
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 3
        readinessProbe:
          httpGet:
            path: /actuator/health/readiness
            port: 8080
          initialDelaySeconds: 30
          periodSeconds: 5
          timeoutSeconds: 3
          failureThreshold: 3
        volumeMounts:
        - name: config
          mountPath: /app/config
        - name: logs
          mountPath: /app/logs
      volumes:
      - name: config
        configMap:
          name: demo-app-config
      - name: logs
        emptyDir: {}
      imagePullSecrets:
      - name: harbor-secret
---
apiVersion: v1
kind: Service
metadata:
  name: demo-app
  namespace: production
spec:
  type: LoadBalancer
  selector:
    app: demo-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: demo-app-config
  namespace: production
data:
  application.yaml: |
    server:
      port: 8080
    spring:
      datasource:
        url: jdbc:mysql://mysql:3306/demo
        username: demo_user
        password: ${DB_PASSWORD}
    logging:
      level:
        root: INFO

Deployment Script

#!/bin/bash
set -e

NAMESPACE="production"
APP_NAME="demo-app"

echo "=== 开始部署到Kubernetes ==="
# Create namespace if not exists
kubectl create namespace ${NAMESPACE} --dry-run=client -o yaml | kubectl apply -f -
# Create Harbor pull secret
kubectl create secret docker-registry harbor-secret \
  --docker-server=harbor.company.com \
  --docker-username=admin \
  --docker-password=Harbor12345 \
  --namespace=${NAMESPACE} \
  --dry-run=client -o yaml | kubectl apply -f -
# Apply manifests
kubectl apply -f k8s/deployment.yaml

echo "等待Deployment就绪..."
kubectl rollout status deployment/${APP_NAME} -n ${NAMESPACE} --timeout=5m

kubectl get pods -n ${NAMESPACE} -l app=${APP_NAME}
kubectl get svc -n ${NAMESPACE} ${APP_NAME}

echo "=== 访问地址 ==="
kubectl get svc ${APP_NAME} -n ${NAMESPACE} -o jsonpath='{.status.loadBalancer.ingress[0].ip}'

Phase 4: Monitoring & Alerting (Day 7)

Prometheus Configuration

# prometheus-config.yaml - ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: monitoring
data:
  prometheus.yml: |
    global:
      scrape_interval: 15s
      evaluation_interval: 15s
    alerting:
      alertmanagers:
      - static_configs:
        - targets:
          - alertmanager:9093
    rule_files:
      - /etc/prometheus/rules/*.yml
    scrape_configs:
      - job_name: 'jenkins'
        metrics_path: '/prometheus'
        static_configs:
        - targets:
          - jenkins:8080
      - job_name: 'kubernetes-pods'
        kubernetes_sd_configs:
        - role: pod
        relabel_configs:
        - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
          action: keep
          regex: true
        - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
          action: replace
          target_label: __metrics_path__
          regex: (.+)
        - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
          action: replace
          regex: ([^:]+)(?::\d+)?;(\d+)
          replacement: $1:$2
          target_label: __address__

Grafana Dashboard Deployment

# Deploy Prometheus and Grafana (simplified)
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      containers:
      - name: prometheus
        image: prom/prometheus:latest
        ports:
        - containerPort: 9090
        volumeMounts:
        - name: config
          mountPath: /etc/prometheus
        - name: data
          mountPath: /prometheus
      volumes:
      - name: config
        configMap:
          name: prometheus-config
      - name: data
        emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: prometheus
  namespace: monitoring
spec:
  type: NodePort
  selector:
    app: prometheus
  ports:
  - port: 9090
    targetPort: 9090
    nodePort: 30090
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      containers:
      - name: grafana
        image: grafana/grafana:latest
        ports:
        - containerPort: 3000
        env:
        - name: GF_SECURITY_ADMIN_PASSWORD
          value: "admin123"
---
apiVersion: v1
kind: Service
metadata:
  name: grafana
  namespace: monitoring
spec:
  type: NodePort
  selector:
    app: grafana
  ports:
  - port: 3000
    targetPort: 3000
    nodePort: 30030
EOF

echo "=== Monitoring system deployed ==="
echo "Prometheus: http://YOUR_IP:30090"
echo "Grafana: http://YOUR_IP:30030 (admin/admin123)"

Case Study: End‑to‑End DevOps Adoption in an Internet Company

Project Background

A online‑education company operated 20+ micro‑services with a manual release process taking 2‑3 hours per release and 1‑2 monthly production incidents caused by human error. The goal was to cut release time to under 30 minutes and reduce failure rate by 80%.

Implementation Process

Week 1: Infrastructure Setup

Day 1‑2: Procure three cloud servers, install Docker, GitLab, Harbor, Jenkins.

Day 3: Migrate code to GitLab, define branch strategy.

Day 4‑5: Build a Kubernetes cluster (1 master, 2 workers).

Day 6‑7: Deploy monitoring (Prometheus + Grafana).

Week 2: Pipeline Development

Select two representative micro‑services as pilots.

Write Jenkinsfiles to automate build, test, and deployment.

Integrate SonarQube for code scanning.

Add automated test suites.

Week 3: Canary Release

Validate pipeline in test environment.

Conduct team training.

First service goes live using the pipeline.

Collect feedback and iterate.

Week 4: Full Rollout

All 20 micro‑services adopt the pipeline.

Establish release calendar and approval workflow.

Define emergency rollback procedures.

Key Configuration Highlights

Multi‑environment management

# Environment mapping
def envConfig = [
  'dev': [namespace:'development', replicas:1, approvalRequired:false],
  'test': [namespace:'testing', replicas:2, approvalRequired:false],
  'prod': [namespace:'production', replicas:3, approvalRequired:true]
]

def targetEnv = env.BRANCH_NAME == 'master' ? 'prod' : (env.BRANCH_NAME == 'develop' ? 'test' : 'dev')

Canary deployment strategy

# Canary Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-app-canary
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo-app
      version: canary
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-app-stable
spec:
  replicas: 3
  selector:
    matchLabels:
      app: demo-app
      version: stable

Automatic rollback script

#!/bin/bash
rollback_deployment() {
  local APP=$1
  local NS=$2
  echo "Detected failure, performing automatic rollback"
  kubectl rollout undo deployment/${APP} -n ${NS}
  kubectl rollout status deployment/${APP} -n ${NS}
  # Send alert (placeholder)
}
if ! check_health; then
  rollback_deployment "demo-app" "production"
fi

Results

Metric

Before

After

Improvement

Release frequency

2 times/week

10 times/day

35×

Release duration

2‑3 hours

15 minutes

90%

Success rate

85%

99%

+14%

MTTR

2 hours

10 minutes

92%

Ops headcount

3 people

0.5 person

83%

Business value

Product iteration speed increased threefold.

Ops cost reduced by 70%.

Service stability and customer satisfaction improved.

Collaboration efficiency between dev and ops markedly increased.

Challenges & Solutions

Challenge 1: Developer resistance

Provide detailed documentation and training.

Assign technical champions to guide teams.

Demonstrate efficiency gains to win buy‑in.

Challenge 2: Legacy system migration

Adopt phased migration, modernize new services first.

Run legacy apps in VMs while refactoring.

Maintain a technical debt backlog with a repayment plan.

Challenge 3: Permission and security management

Implement RBAC for fine‑grained access control.

Require approval for production releases.

Enable audit logging for all operations.

Best Practices & Advanced Optimisation

Core DevOps Principles

Automation first: never perform manual steps that can be scripted.

Fast feedback: keep pipeline execution under 15 minutes.

Built‑in quality: integrate all quality checks into the pipeline.

Shift‑left security: run security scans during development.

Observability: end‑to‑end logs, metrics, and tracing.

Rapid recovery: automatic rollback and downgrade mechanisms.

Advanced Extensions

Multi‑cloud deployment

stage('Multi‑cloud') {
  parallel {
    stage('Deploy to AWS') { steps { sh 'kubectl --context=aws-prod apply -f k8s/' } }
    stage('Deploy to Alibaba') { steps { sh 'kubectl --context=aliyun-prod apply -f k8s/' } }
  }
}

Blue‑Green Deployment

#!/bin/bash
# blue_green_deploy.sh
blue_green_deploy() {
  local NEW_VERSION=$1
  # Deploy green environment
  kubectl apply -f deployment-green.yaml
  kubectl set image deployment/app-green app=app:${NEW_VERSION}
  kubectl rollout status deployment/app-green
  # Verify
  if smoke_test "http://app-green"; then
    kubectl patch service app -p '{"spec":{"selector":{"version":"green"}}}'
    echo "Switch to green successful"
  else
    echo "Verification failed, keep blue"
    exit 1
  fi
}

Automated Performance Testing

stage('Performance Test') {
  steps {
    sh '''
      # JMeter load test
      jmeter -n -t test-plan.jmx -l result.jtl -j jmeter.log
      # Analyse results
      python analyze_perf.py result.jtl
    '''
  }
}

Security Hardening Recommendations

1. Secret Management

# Create encrypted secret
kubectl create secret generic db-credentials \
  --from-literal=username=admin \
  --from-literal=password=SecurePassword123

2. Image Vulnerability Scanning

stage('Image Security Scan') {
  steps {
    sh '''
      trivy image --severity HIGH,CRITICAL ${IMAGE_NAME}:${BUILD_TAG}
    '''
  }
}

3. Network Isolation

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: app-network-policy
spec:
  podSelector:
    matchLabels:
      app: demo-app
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          role: frontend
    ports:
    - protocol: TCP
      port: 8080

Cost Optimisation Strategies

Resource quotas per namespace.

Horizontal pod autoscaling based on load.

Night‑time scaling down of non‑production environments.

Periodic Harbor image cleanup.

#!/bin/bash
# Harbor cleanup script
harbor_cleanup() {
  curl -u admin:password -X POST "http://harbor.company.com/api/v2.0/system/gc/schedule" \
    -H "Content-Type: application/json" \
    -d '{"schedule":{"type":"Daily","cron":"0 2 * * *"},"parameters":{"delete_untagged":true,"dry_run":false}}'
}

Conclusion & Outlook

This article walked through the complete process of building an enterprise‑grade DevOps pipeline—from environment preparation to production deployment—demonstrating how to achieve fully automated code‑to‑production flows that dramatically improve delivery speed and system stability.

Key Takeaways

Iterative approach: start with a minimal viable pipeline and refine.

Tool selection should match team size and tech stack.

DevOps is as much cultural change as tooling.

Continuously review pipeline performance and eliminate bottlenecks.

Security must be integrated from the beginning.

Future Trends

AIOps – intelligent operations with ML‑driven fault prediction.

GitOps – Git‑centric infrastructure as code and PR‑based approvals.

Serverless integration – combining FaaS with containers.

Service mesh – Istio, Linkerd for traffic management and security.

Full cloud‑native adoption – Kubernetes as the de‑facto standard across multi‑cloud environments.

Further Learning Resources

《持续交付2.0》 – 乔梁

The DevOps Handbook

CNCF official docs and certifications (CKA/CKAD)

DevOps community blogs and meetups

All scripts have been tested on CentOS 7.9 and Ubuntu 20.04. Adjust configurations as needed for your environment. The full code repository is available on GitHub.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Dockerci/cdAutomationKubernetesDevOpsInfrastructureJenkins
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.