Gitlab and Kubernetes CI/CD flowchart

Gitlab and Gitlab CI construction refer to previous articles

  • Github.com/yangpeng14/…
  • Github.com/yangpeng14/…

Helm Installation (installed on GitLab Runner machine)

  • Install the latest helm version 2.16.0
$wget https://get.helm.sh/helm-v2.16.0-linux-amd64.tar.gz $tar -zvf helm-v2.16.0-linux-amd64.tar.gz $cd linux-amd64/
$ cp helm /usr/local/bin
Copy the code
  • Verify the Helm
helm version
Client: &version.Version{SemVer:"v2.16.0", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.16.0", itTreeState:"clean"}
Copy the code
  • Initialize the Helm
$ helm init --client-only
$ helm plugin install https://github.com/chartmuseum/helm-push
Copy the code
  • Update repO to Ariyuan
$ helm repo remove stable
$ helm repo add ali https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
Copy the code

Gitlab CI file configuration example

stages:
  - test
  - deploy

# test job
job 1:
  stage: test
  only: 
    - master
  tags:
    - tag-test
  script: echo "Unit tests"

The # deploy phase puts docker build and K8S deployment together in one phase
job 2:
  stage: deploy
  only: 
    - master
  tags:
    - tag-test
  script:
    - echo "Deploy ..."
    # deploy python deployment script written by myself
    Yaml helm values configuration file
    # Product-line
    $CI_COMMIT_REF_NAME = $CI_COMMIT_REF_NAME = $CI_COMMIT_REF_NAME
    # $(date "+%Y%m%d%H%M%S")-${CI_COMMIT_SHA:0:6} docker tag
    # Dockerfile Build project Dockerfile
    - deploy -f helm.yaml product-line project-name $(date "+%Y%m%d%H%M%S") -${CI_COMMIT_SHA:0:6} Dockerfile 
  when: manual
Copy the code

Deploy deployment scripts

  • Here is the deploy part of the code, get all the script code, please pay attention to mineYP stationPublic number and replyGet the deploy code
#! /usr/bin/env python3

import sys
import os
import getopt
import yaml
import pykube

def usage(a):
    print("Usage: %s [ -b | --build-arg | -f | --file ] product-line project-name docker_images_version dockerfile_name --no-cache" % sys.argv[0]."\n"."or"."Usage: %s [ -b | --build-arg | -f | --file ] product-line project-name docker_images_version dockerfile_name" % sys.argv[0]."\n\n"."-b, --build-arg declare environment variables in Dockerfile. Parameter can be specified multiple times, example A=b,"."\n".-f, --file specifies the Helm values.yaml file.)

def check_item_exists(project, server_name, docker_images_version, *args):
    api = pykube.HTTPClient(pykube.KubeConfig.from_file("k8s config"))
    deploy = pykube.Deployment.objects(api).filter(namespace=project)
    os.environ['project'] = str(project)
    os.environ['server_name'] = str(server_name)
    os.environ['docker_images_version'] = str(docker_images_version)
    os.environ['s_name'] = str(server_name)
    if args:
        os.environ['helm_values_file'] = str(args[0])

    if os.system('helm status $server_name 1> /dev/null 2>&1') = =0:
        os.system("Echo '\033[1;32;40m'")
        os.system('helm upgrade --timeout 300 --atomic --install $server_name --namespace $project \ --set serverName=$server_name --set image.project=$project \ --set serverFeatureNameReplace=$server_name \ --set image.tag=$docker_images_version "Helm warehouse address" $helm_values_file')
        return
    else:
        # Compatibility in the past without Helm deployment
        for deployment in deploy:
            if server_name == str(deployment):
                # deployment Rolling upgrade
                os.system('kubectl set image deployment/$server_name \ $server_name=harbor.example.com/$project/$server_name:$docker_images_version \ --namespace=$project')
                return

    # Deploy new projects
    Deploy the service using the HELM template
    os.system('helm install --name $server_name --namespace $project \ --set serverName=$server_name --set image.project=$project \ --set serverFeatureNameReplace=$server_name \ --set image.tag=$docker_images_version "Helm warehouse address" $helm_values_file')
    return
Copy the code

reference

Blog.csdn.net/ygqygq2/art…

Pay attention to my

We welcome you to share automated operations, DevOps, Kubernetes, Service Mesh and Cloud Native regularly