[Microservice Deployment] 1. Use docker-compose to deploy Jenkins, SonarQube, and PostgreSQL

1. Installation

1. Write docker-compose to deploy the yml file jenkins-compose.yml for Postgres, SonarQube, and Jenkins.
  • Postgres: as database storage for SonarQube
  • SonarQube: Code Quality Check
  • Jenkins: jenkins/jenkins:lts image, jenkinsci/blueocean image lacks the node runtime environment, causing node to fail to run.
Key configuration instructions (host: Docker container):
  • /data/docker/ci/postgresql/data:/var/lib/postgresql/data # Postgres data storage is mapped to the host directory
  • /etc/localtime:/etc/localtime:ro # The docker container takes the date of the host machine, :ro(read-only) means read-only, that is, the docker container can only read the host machine's /etc/localtime file and cannot modify it.
  • /usr/lib64/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7 # Run the host docker command in the docker container, if the host does not have this file, you can use the following command Install.
  • Here, the memory of the Docker container is set to 10G, because the memory we need to set for NodeJS packaging is 8G.
yum install libtool-ltdl-2.4.2-22.el7_3.x86_64

The complete content of jenkins-compose.yml is as follows:

version: '3'
networks:
  prodnetwork:
    driver: bridge
services:
  sonardb:
    image: postgres:12.2
    restart: always
    ports:
      - "5433:5432"
    networks:
      - prodnetwork
    volumes:
      - /data/docker/ci/postgresql:/var/lib/postgresql
      - /data/docker/ci/postgresql/data:/var/lib/postgresql/data
      - /etc/localtime:/etc/localtime:ro
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
      - POSTGRES_DB=sonar
      - TZ=Asia/Shanghai
  sonar:
    image: sonarqube:10.1.0-community
    restart: always
    ports:
    - "19000:9000"
    - "19092:9092"
    networks:
      - prodnetwork
    depends_on:
      - sonardb
    volumes:
      - /data/docker/ci/sonarqube/conf:/opt/sonarqube/conf
      - /data/docker/ci/sonarqube/data:/opt/sonarqube/data
      - /data/docker/ci/sonarqube/logs:/opt/sonarqube/logs
      - /data/docker/ci/sonarqube/extensions:/opt/sonarqube/extensions
      - /data/docker/ci/sonarqube/bundled-plugins:/opt/sonarqube/lib/bundled-plugins
    environment:
      - TZ=Asia/Shanghai
      - SONARQUBE_JDBC_URL=jdbc:postgresql://sonardb:5432/sonar
      - SONARQUBE_JDBC_USERNAME=sonar
      - SONARQUBE_JDBC_PASSWORD=sonar
  jenkins:
    image: jenkins/jenkins:lts
    user: root
    privileged: true
    restart: always
    ports:
      - "18080:8080"
    networks:
      - prodnetwork
    volumes:
      - /usr/bin/docker:/usr/bin/docker
      - /var/run/docker.sock:/var/run/docker.sock
      - /etc/docker/daemon.json:/etc/docker/daemon.json
      - /usr/lib64/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7
      - /etc/localtime:/etc/localtime:ro
      - $HOME/.ssh:/root/.ssh
      - /data/docker/ci/jenkins/lib:/var/lib/jenkins/
      - /data/docker/ci/jenkins/home:/var/jenkins_home
    depends_on:
      - sonar
    environment:
      - TZ=Asia/Shanghai
      - NEXUS_PORT=8081
      - SONAR_PORT=9000
      - SONAR_DB_PORT=5432
      - JAVA_OPTS=-Xms512m -Xmx10240m
    deploy:
      resources:
         limits:
            cpus: "2.00"
            memory: 10G
         reservations:
            memory: 200M
    cap_add:
      - ALL

  Because not every project needs its own Maven private server, the deployment of Maven private server Nexus is removed here. If necessary, just add it directly to the jenkins-compose.yml file:

  nexus:
    image: sonatype/nexus3
    restart: always
    ports:
      - "18081:8081"
    networks:
      - prodnetwork
    volumes:
      - /data/docker/ci/nexus:/nexus-data

  In Jenkins depends_on: add

    depends_on:
      - nexus
      - sonar
2. Create a host mounting directory and grant permissions
mkdir -p /data/docker/ci/nexus /data/docker/ci/jenkins/lib /data/docker/ci/jenkins/home /data/docker/ci/sonarqube /data/docker/ci/postgresql /data/docker/ci/postgresql/data

chmod -R 777 /data/docker/ci/nexus /data/docker/ci/jenkins/lib /data/docker/ci/jenkins/home /data/docker/ci/sonarqube /data/docker/ci/postgresql /data/docker/ci/postgresql/data
3. Execute the installation startup command in the directory where the jenkins-compose.yml file is located.
  • start command
docker-compose -f jenkins-compose.yml up -d
  • stop order
docker-compose -f jenkins-compose.yml down
  • Recreate a container command
docker-compose -f jenkins-compose.yml up --force-recreate --no-deps 服务名称
  • Check the running status of the container
[root@root server]# docker ps
CONTAINER ID   IMAGE                        COMMAND                  CREATED        STATUS        PORTS                                              NAMES
b28f1878ee08   jenkins/jenkins:lts          "/sbin/tini -- /usr/…"   20 hours ago   Up 19 hours   50000/tcp, 0.0.0.0:18080->8080/tcp                 docker-jenkins-1
cff7c0d88150   sonarqube:10.1.0-community   "/opt/sonarqube/dock…"   20 hours ago   Up 20 hours   0.0.0.0:19000->9000/tcp, 0.0.0.0:19092->9092/tcp   docker-sonar-1
90dacc85efb1   postgres:12.2                "docker-entrypoint.s…"   20 hours ago   Up 20 hours   0.0.0.0:5433->5432/tcp                             docker-sonardb-1

  • View the running log of a certain container docker logs -f container id
[root@root server]# docker logs -f b28f1878ee08
  • Restart a container docker restart container id
[root@root server]# docker restart  b28f1878ee08
4. Access Jenkins through the interface, find the initial password through the prompted path and enter it, and then proceed to the next step.

Jenkins initial interface

[root@ZvRiIw4706 docker]# cd /data/docker/ci/jenkins/home/secrets
[root@ZvRiIw4706 secrets]# ls
filepath-filters.d  initialAdminPassword  jenkins.model.Jenkins.crumbSalt  master.key  org.jenkinsci.main.modules.instance_identity.InstanceIdentity.KEY  slave-to-master-security-kill-switch  whitelisted-callables.d
[root@ZvRiIw4706 secrets]# cat initialAdminPassword

5. Choose to install the recommended plug-ins.

Recommended plug-ins

6. The plug-in is being installed

Plugin installation

7. Some plug-ins failed to install. According to the background log, some plug-ins cannot be installed because the jenkins version is too low. Here, go to the next step first. After upgrading the jenkins version in the system, continue to install the plug-ins. (jenkins/jenkins:lts image does not integrate the latest jenkins, but when downloading the plug-in, it is the latest plug-in downloaded, which leads to incompatibility, so it cannot be downloaded)

installation failed

Error log:

2023-08-09 07:44:36.664+0000 [id=72]	INFO	h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading github-branch-source
2023-08-09 07:44:36.685+0000 [id=72]	SEVERE	h.model.UpdateCenter$DownloadJob#run: Failed to install github-branch-source
java.io.IOException: Failed to load: GitHub Branch Source Plugin (1696.v3a_7603564d04)
 - Failed to load: GitHub plugin (1.34.5)
 - Jenkins (2.346.1) or higher required
	at hudson.PluginWrapper.resolvePluginDependencies(PluginWrapper.java:1016)
	at hudson.PluginManager.dynamicLoad(PluginManager.java:917)
Caused: java.io.IOException: Failed to install github-branch-source plugin
	at hudson.PluginManager.dynamicLoad(PluginManager.java:930)
	at hudson.model.UpdateCenter$InstallationJob._run(UpdateCenter.java:2179)
Caused: java.io.IOException: Failed to dynamically deploy this plugin
	at hudson.model.UpdateCenter$InstallationJob._run(UpdateCenter.java:2183)
	at hudson.model.UpdateCenter$DownloadJob.run(UpdateCenter.java:1846)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at hudson.remoting.AtmostOneThreadExecutor$Worker.run(AtmostOneThreadExecutor.java:121)
	at java.base/java.lang.Thread.run(Thread.java:829)

2. Configuration

8. After logging in to Jenkins, first enter system management and then upgrade the Jenkins version.

Upgrade Jenkins
Upgrade Jenkins

9. Then install the required plug-ins, System Management > Plug-in Management
  • Blue Ocean : An aggregation tool designed by Jenkins Pipeline to graphically visualize the Pipeline process and make the pipeline more intuitive.
  • SonarQube Scanner for Jenkins : Code quality scanning tool, Jenkins scans the code and sends the results to SonarQube Server.
  • NodeJS Plugin : A plug-in required when packaging a NodeJs project, used to run the NodeJs environment.
  • Maven Integration plugin : Maven project packaging plugin.
  • Publish Over SSH : Send the packaged file to the remote server.
10. System Management > System Configuration
  • Maven parameter configuration, configure it here according to the configuration of your own server
-Xms256m -Xmx512m -XX:MaxPermSize=128m -XX:ReservedCodeCacheSize=64m

Maven parameter

  • SonarQube servers configuration, where we configure SonarQube deployed by docker-compose, Server authentication token is required, this will explain how to get generated token from SonarQube in the next steps.
    SonarQube servers
  • Publish over SSH configuration, here configure the server we need to deploy to. Multiple deployments can be made. Click "Advanced" on the lower side and fill in the server password and port.
    Publish over SSH
11. System Management > Global Tool Configuration. Here we mainly install the software environment required by some required plug-ins. The jenkins/jenkins:lts image is equipped with openjdk and git by default. If there are no special requirements, we can not install it. We install SonarQube Scanner here , Maven, NodeJS.
  • SonarQube Scanner installation
    SonarQube Scanner installation

  • For Maven installation, it is best to download it yourself and then upload it to the server, because using the server to download and install is very slow.
    Maven installation

  • Maven global configuration, configure Maven configuration file
    Maven global configuration

  • Modify the jar package storage path in the configuration file in the Maven global configuration, otherwise the jar package will be re-downloaded for different task builds.
    jar package storage path

  • NodeJS installation, select the version you need, and if there are other toolkits, you can fill in the following. For example, we use pnpm, and the mirror source used is https://registry.npm.taobao.org, you can fill in the following:

pnpm -registry=https://registry.npm.taobao.org

NodeJS installation

GitEgg-Cloud is an enterprise-level microservice application development framework based on Spring Cloud integration. The open source project address is:

Gitee: https://gitee.com/wmz1930/GitEgg

GitHub: https://github.com/wmz1930/GitEgg

Guess you like

Origin blog.csdn.net/wmz1932/article/details/132575302