暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

应用自动化部署篇

希里安 2023-04-16
328

关注“希里安”,get更多有用干货!




截止昨天已经将应用容器化并部署到k8s平台上,但是每次都要手动部署肯定不现实,所以有一个可自动部署的平台或功能是很重要的,这样就能实现随时开发随时部署了。那么有什么办法可以实现自动部署呢?






自动化部署这边往高级了说就是CICD,就是持续集成和持续部署的意思,就是实现了自动化构建、测试和部署的过程,这边先了解下有哪些CI/CD工具:

1. Jenkins - 一个开源自动化服务器,可用于构建和测试您的软件项目,并将其部署到生产环境。它是一个基于Java开发的工具,可运行在服务器上,就是装好jdk,直接运行jar就可以了。

那么这里给一个部署应用到K8s的自动部署jenkinsfile文件:

    pipeline {
    agent any
    stages {
    stage('Checkout') {
    steps {
    echo 'Checking out...'
    checkout scm
    }
    }
    stage('Build') {
    steps {
    echo 'Building...'
    sh 'npm install'
    }
    }
    stage('Test') {
    steps {
    echo 'Testing...'
    sh 'npm test'
    }
    }
    stage('Lint') {
    steps {
    echo 'Linting...'
    sh 'npm run lint'
    }
    }
    stage('Build Image') {
    steps {
    echo 'Building Docker Image...'
    sh 'docker build -t hello-world:latest .'
    }
    }
    stage('Push Image') {
    steps {
    echo 'Pushing Docker Image...'
    sh 'docker push hello-world:latest'
    }
    }
    stage('Deploy') {
    steps {
    echo 'Deploying...'
    sh 'kubectl apply -f deployment.yaml'
    }
    }
    }
    }
    这个流水线pipeline中主要有三个阶段:
    (1)build,就是构建编译过程,安装依赖项目
    (2)test,就是测试过程
    (3)deploy,就是打包容器镜像,然后直接运行命令行kubectl apply来部署deployment.yaml清单文件(昨天已经编写了,直接用就行)

    2. GitLab CI/CD - GitLab内置的CI/CD工具。它直接集成在GitLab中,可以自动构建、测试和部署GitLab中托管的项目也就是我们今天要介绍的工具。

    步骤和jenkins都大同小异:
      image: node:latest
      stages:
      - install
      - test
      - lint
      - build
      - deploy
      install:
      stage: install
      script:
      - npm install
      test:
      stage: test
      script:
      - npm test
      lint:
      stage: lint
      script:
      - npm run lint
      build:
      stage: build
      script:
      - docker build -t frontend:latest .
      deploy_job:
      stage: deploy
      script:
      - docker push frontend:latest
      - kubectl apply -f deployment.yaml
      deploy:
      stage: deploy
      script:
      - kubectl apply -f deployment.yaml
      主要还是三部分:构建、测试、打包镜像部署到k8s

      3. GitHub Actions - GitHub的CI/CD工具。它直接在GitHub中运行,可以自动构建、测试和部署GitHub仓库中的代码。

        name: CI/CD Workflow
        on: push
        jobs:
        install:
        name: Install dependencies
        runs-on: ubuntu-latest
        steps:
        - name: Checkout code
        uses: actions/checkout@v2
        - name: Install npm dependencies
        run: npm install
        test:
        name: Run tests
        runs-on: ubuntu-latest
        steps:
        - name: Checkout code
        uses: actions/checkout@v2
        - name: Run tests
        run: npm test
        lint:
        name: Run linter
        runs-on: ubuntu-latest
        steps:
        - name: Checkout code
        uses: actions/checkout@v2
        - name: Run linter
        run: npm run lint
        build:
        name: Build docker image
        runs-on: ubuntu-latest
        steps:
        - name: Checkout code
        uses: actions/checkout@v2
        - name: Build docker image
        run: docker build -t frontend:latest .
        deploy:
        name: Deploy to Kubernetes
        runs-on: ubuntu-latest
        steps:
        - name: Checkout code
        uses: actions/checkout@v2
        - name: Deploy to Kubernetes
        run: |
        docker push frontend:latest
        kubectl apply -f deployment.yaml

        4. Azure Pipelines - Microsoft Azure的CI/CD工具。它可以为Azure DevOps和GitHub中的项目自动化构建、测试、部署和发布管道。
        暂时没用过,就不举例了。


        5. Argo CD - 一个声明式的GitOps CD工具,可以自动部署和同步储存在 GitHub、GitLab、 Bitbucket 和 Argoproj 等仓库中的kubenetesmainfest文件。和k8s联系比较紧密,可以实时监控应用程序清单文件,并自动部署到K8s集群中。大佬们都强烈建议使用Argo CD来管理K8s的应用部署。

        这是一个简单的argo cd的yaml文件
          apiVersion: argoproj.io/v1alpha1
          kind: Application
          metadata:
          name: example
          spec:
          project: default
          source:
          repoURL: https://github.com/argoproj/argocd-example-apps.git
          targetRevision: HEAD
          path: example
          destination:
          server: https://kubernetes.default.svc
          namespace: argocd-example-apps
          syncPolicy:
          automated: true
          这里启用自动同步策略,相关清单文件一更改就会自动部署到集群。







          我这边主要还是用gitlab比较多,所以看看具体在哪设置:


          在这里可以找到注册gitlab-runner相关信息



          如何安装gitlab-runner



          可以二进制安装,也可以helm安装,直接搜索helm官方网站就可,gitlab也给出了对应不同系统的安装方式包括kubernetes





          这里我使用helm3 安装gitlabrunner,命令如上所示:



          这里给下我的两种方式的ci文件

            build-job: # This job runs in the build stage,
            which runs first.
            stage: build
            script:
            -
            echo "Compiling the code..."
            -
            npm install
            -
            npm run build
            -
            docker build -t cops-fe .
            -
            echo "Compile complete."
            deploy-job: # This job runs in the deploy stage.
            stage: deploy # It only runs when
            *both* jobs in the test stage complete successfully.
            environment: kubesphere-1
            script:
            -
            echo "Deploying application..."
            -
            kubectl apply -f cops-fe.yaml
            -
            echo "Application successfully deployed."
            only:
            -
            master

            Helm方式

            yaml

              image: node:latest
              variables:
              DOCKER_HOST: tcp://docker.example.com:2375
              CI_REGISTRY: registry.example.com
              CI_REGISTRY_USER: gitlab-ci-token
              CI_REGISTRY_PASSWORD: $CI_JOB_TOKEN
              CI_ENVIRONMENT_SLUG: production
              build:
              stage: build
              script:
              -
              npm install
              -
              npm run build
              -
              docker build -t $CI_REGISTRY/my-group/my-project:$CI_COMMIT_REF_SLUG .
              -
              docker push $CI_REGISTRY/my-group/my-project:$CI_COMMIT_REF_SLUG
              deploy:
              stage: deploy
              environment:
              name: $CI_ENVIRONMENT_SLUG
              script:
              -
              helm upgrade my-release ./helm-charts --set image.tag=$CI_COMMIT_REF_SLUG

              rollback:
              stage: rollback
              environment:
              name: $CI_ENVIRONMENT_SLUG
              script:
              - helm rollback
              my-release 1

              这期间会遇到类似的问题:




              这个错误意味着GitLab Runner没有权限创建Kubernetes Secrets,执行授权即可。

                kubectl create
                clusterrolebinding gitlab-runner \
                --clusterrole=cluster-admin \
                --serviceaccount=gitlab-runner:default







                好了,关于自动化部署的内容今天就到这了,感兴趣的朋友别忘了点赞关注!


                文章转载自希里安,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                评论