极客时间运维进阶训练营第五周作业

1、完全基于 Pipeline 实现完整的代码部署流水线

#!groovy
pipeline {
    agent any
    // agent { label 'jenkins-slave01' }
    options {
        buildDiscarder(logRotator(numToKeepStr: '5'))
        disableConcurrentBuilds()
        timeout(time: 5, unit: 'MINUTES') //任务执行超时时间,默认单位小时
        timestamps()  //在控制台显示命令执行时间,格式10:33:29
        retry(2) //流水线构建失败后重置次数为2次
    }
    // 声明环境变量
    environment {
        // def GIT_URL = 'https://gitlab.iclinux.com/linux/app1.git'
        // def HARBOR_URL = 'harbor.iclinux.com'
        // def IMAGE_PROJECT = 'app'
        // def IMAAGE_NAME = 'nginx'
        def IMAGE_BUILD_NODE="192.168.56.201"
        def DOCKER_NODE="192.168.56.202"
        def DATE = sh(script: "date +%F_%H-%M-%S", returnStdout: true).trim()

    }
    // 参数定义
    parameters {
        string(name: 'BRANCH', defaultValue: 'develop', description: 'branck select')
        choice(name: 'DEPLOY_ENV', choices: ['develop', 'production'], description: 'deploy env')
    }
    // 流水线开始工作
    stages {
        stage('code clone'){
            // agent { label 'master' }
            steps {
                deleteDir()  //清空目录
                script {
                    if ( env.BRANCH == 'main' ){
                        git branch: 'main', credentialsId: 'root', url: 'https://gitlab.iclinux.com/linux/app1.git'

                    } else if ( env.BRANCH == 'develop' ) {
                        git branch: 'develop', credentialsId: 'root', url: 'https://gitlab.iclinux.com/linux/app1.git'

                    } else {
                        echo 'BRANCH ERROR, please check it.'
                    }
                GIT_COMMIT_TAG = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
                }
                // echo "${GIT_COMMIT_TAG}"
            }
        }

        stage('sonarqube-scanner'){
            // agent { label 'master' }
            steps{
                sh 'pwd'
                dir('/var/lib/jenkins/workspace/pipeline-groovy-test') {
                    // some block
                    sh '/apps/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=iclinx -Dsonar.projectName=iclinux-python-app3 -Dsonar.projectVersion=1.3 -Dsonar.sources=./src -Dsonar.language=py -Dsonar.sourceEncoding=UTF-8'
                }
            }
        }

        stage('code build'){
            steps{
                dir('/var/lib/jenkins/workspace/pipeline-groovy-test'){
                    sh 'tar cvzf frontend.tar.gz ./index.html ./images'
                }
            }
        }

        stage('file sync'){
            steps {
                echo "file sync"
                sh "scp frontend.tar.gz root@${IMAGE_BUILD_NODE}:/opt/dockerfiles/app/web1"
            }

        }

        stage("image build"){
            steps{
                echo "sync file to docker images server and make docker image"
                sh """
                    ssh root@${IMAGE_BUILD_NODE} "cd /opt/dockerfiles/app/web1 && bash build-command.sh ${GIT_COMMIT_TAG}-${DATE}"
                """
            }
        }

        stage('docker-compose image update'){
            steps{
                sh """
                    #ssh [email protected] "echo 123"
                    ssh root@${DOCKER_NODE} "cd /opt/iclinux-web && sed -i  's#image: harbor.iclinux.com/app/iclinux-web1:.*#image: harbor.iclinux.com/app/iclinux-web1:${GIT_COMMIT_TAG}-${DATE}#' docker-compose.yml && sleep 1 && docker-compose pull"
                    """

            }
        }

        stage('docker-compose app update'){
            steps{
                echo 'update app'
                sh """
                    ssh root@${DOCKER_NODE} "cd /opt/iclinux-web && docker-compose  up -d"
                """
            }
        }

        stage('send email'){
            steps {
                sh 'echo send email'
            }
            post {
                always{
                    script {
                    mail to: '[email protected]',
                    subject: "Pipeline Name: ${currentBuild.fullDisplayName}",
                    body: " ${env.JOB_NAME} -Build Number-${env.BUILD_NUMBER}\n 点击链接 ${env.BUILD_URL} 查看详情"
                    }

                }
            }
        }

    }
}

  


2、熟悉 ELK 各组件的功能、Elasticsearch 的节点角色类型


3、熟悉索引、doc、分片与副本的概念


4、掌握不同环境的 ELK 部署规划,基于 deb 或二进制部署 Elasticsearch 集群


5、了解 Elasticsearch API 的简单使用,安装 head 插件管理 ES 的数据


6、安装 Logstash 收集不同类型的系统日志并写入到 ES 的不同 index


7、安装 Kibana、查看 ES 集群的数据

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章