mac+docker+jenkins+gitlab+k8s+springboot Build cicd from 0

This article uses jenkins installed by brew; docker uses gitlab installed by mirror; docker uses local private mirror warehouse built by mirror; and k8s to build a set of jenkins pipeline to trigger the deployment of micro-services by local submission code.

1. Installation of Jenkins

jenkins can be installed in a variety of ways, either by brew install jenkins on a mac system, by docker image, or by k8s.

  • How homebrew works:
    brew install jenkins #install
    brew services start jenkins #start-up

    Enter http://localhost:8080/or http://native ip:8080/in your browser to verify that jenkins is ok. A page will pop up where you can enter your password and the password will be found:

  •  cat secrets/initialAdminPassword

    Enter pipeline in the Jenkins->Manage jenkins->Available->search search search box to install the pipeline plug-in and gitlab plug-in, or install an additional blue ocean so that the ui of the pipeline will look better and the time to install the plug-in will be shorter and will require patience.

  • After the plug-in is installed, click New Pipeline, as shown, which means the installation of jenkins and the plug-in process is complete.

  •  

    2. Installation of gitlab

This is installed using docker+image:

$     docker pull gitlab/gitlab-ce #Pull mirror
$ sudo docker run -d \
    --hostname xxxx.xxxx.xx \
    --name gitlab \
    --restart always \
    --publish 30001:22 --publish 30000:80 --publish 30002:443 \
    --volume $HOME/gitlab/data:/var/opt/gitlab \
    --volume $HOME/gitlab/logs:/var/log/gitlab \
    --volume $HOME/gitlab/config:/etc/gitlab \
    gitlab/gitlab-ce #Function

After running successfully, you need to enter http://localhost:30000 on your browser to access the built gitlab, at which point you can create a super administrator user and a project

3. Installation of Private Mirror Warehouse

$ docker pull registry #

$docker run-d-v/Users/hfguan/self/docker/registry:/var/lib/registry-p 5000:5000 -- restart=always -- name private-registry registry #Run

$curl http://127.0.0.1:5000/v2/_catalog Verify the success of the private warehouse:

{"repositories":[]}

Modify the docker's daemon.json file, where 192.168.1.8 is the native ip, before push is mirrored in the private repository

Modify the daemon.json file
{
    "insecure-registries":["192.168.1.8:5000"],
}
docker tag registry 192.168.1.8:5000/registry:v1 #tag
docker push 192.168.1.8:5000/registry:v1 #Push the image of the tag you just typed into the private mirror warehouse
$curl http://127.0.0.1:5000/v2/_catalog #Visit the repository to see if the image you just saw was successfully pushed into it


{"repositories":["registry"]}

4. Association of gitlab and jenkins with webhook hooks

4.1 Add a new pipeline to jenkins

4.2 Generate access token in gitlab

Enter the name of the token in User Settings->Access tokens->Check permissions, click Create personal access token, and save the token after it is generated to prepare for the following jenkins configuration.

4.3 jenkins configures access credentials to access gitlab

In Jenkins->Manage jenkins->configure system->gitlab

After adding gitlab api token, select the token previously added in the Gitlab settings interface "Credentials", and then click "Test Connection" to test for success.Save after the test succeeds.


 

jenkins accessed gitlab, which is a single line. Next, you need to configure gitlab to trigger the unimpeded configuration of jenkins in this direction.

4.4 gitlab configuration webhook

Click advanced,

Add a new webhook, the project just created in gitlab - > Setting - > webhooks

Click test after successful creation to test connectivity

Back to 200, the representative is through, and jenkins triggers a pipeline

5. pipeline creation

5.1 pipeline s can be written in the pipeline module of jenkins, roughly like this:

pipeline{
    agent any
    stages{
        stage('Build'){
            steps{
                echo 'building...'
            }
        }
        stage('Test'){
            steps{
                echo 'Test...'
            }
        }
        stage('Deploy'){
            steps{
                echo 'Deploy...'
            }
        }
    }
}

#To represent the entire process of compiling, testing, and deploying

5.2 You can also add a new jenkinsfile to the microservice, pipelining real-time pull, which can be changed in code projects without changing the configuration of pipelining in real time.

6. Building k8s cluster

Here, minikube is used to build a master-only k8s cluster with a single node


$ brew install minikube #Install minikube
//Install virtual machine virtualbox
$ minikube start --vm-driver=virtulbox #Starting a k8s single node cluster with minikube
$ minikube status #View cluster status
m01
host: Running
kubelet: Running
apiserver: Running

kubeconfig: Configured
//Command line tool kubectl to install k8s

brew install kubectl
kubectl version #Check whether the installation was successful or not

7. Create a springboot project and write a dockerfile

Setting up a microservice is easy. Leave out the tables here and create a Dockerfile under the root path of the microservice with the project address: https://github.com/gholly/docker-scaffold.git

For information on how to write a dockerfile, refer to: How to write a Dockerfile

# Openjdk:lastestable to find the mirror you want from https://hub.docker.com/official mirror repository
FROM  openjdk:11
COPY ./build/libs/scaffold-1.0-SNAPSHOT.jar docker-scaffold.jar
#JAVA_OPTS is a variable used to set JVM-related operational parameters
ENV JAVA_OPTS="-Dserver.port=8081"
EXPOSE 8081
ENTRYPOINT exec java $JAVA_OPTS -jar docker-scaffold.jar

5. Writing of jenkinsfile

Create a Jenkinsfile file file in a microservice to manage what is done at each stage of the pipeline, referring specifically to pipeline syntax learning: pipeline Grammar Learning

Here is a preliminary version that includes compiling, mirroring, push mirroring, and creating a resource with k8s for optimization:

pipeline {
 agent any

 stages {
    stage('Build'){
        steps{
             sh './gradlew clean build'
        }
    }

    stage('Build Docker image'){
            steps{
          sh '/usr/local/bin/docker build -f ./Dockerfile .  -t 192.168.1.8:5000/scaffold:v5'
          }
    }

    stage('Docker push image'){
            steps{
          sh '/usr/local/bin/docker push 192.168.1.8:5000/scaffold:v4'
          }
    }

    stage('kubectl create pod'){
             steps{
              sh '/usr/local/bin/kubectl apply -f ./deploy/application.yaml '
              }
    }




 }
}

9. k8s yaml file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: backnd
  labels:
    name: backnd
spec:
  replicas: 2
  selector:
    matchLabels:
      name: backnd

  template:
    metadata:
      labels:
        name: backnd
    spec:
      containers:
        - name: scaffold
          image: 192.168.1.8:5000/scaffold:v5
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8081
      imagePullSecrets:
        - name: regsecret
---
apiVersion: v1
kind: Service
metadata:
  name: scaffold
spec:
  type: NodePort
  ports:
    - port: 8081
      nodePort: 30011
      targetPort: 8081
  selector:
    name: backnd

10. Joint adjustment:

Bind the locally created project to the project just created on gitlab, submit the code, and then check in jenkins to see if the pipeline is triggered:

Binding relationship:

git remote rename origin old-origin01
   git remote add origin http://localhost:30000/gt/pt.git
  git push -u origin --all
  git push -u origin --tags

At this point the pipeline has been successfully built~

10. Introducing elk log collection system

To be perfected

11. introducing sonar to improve

I.

Tags: Programming GitLab jenkins Docker brew

Posted on Fri, 24 Apr 2020 09:25:16 -0700 by evanluke