【微服务部署】一、使用docker-compose部署Jenki

2023-10-10  本文已影响0人  全栈程序猿

一、安装

1、编写docker-compose部署Postgres、SonarQube、Jenkins的yml文件jenkins-compose.yml
关键配置说明( 宿主机 : Docker容器):
yum install libtool-ltdl-2.4.2-22.el7_3.x86_64

jenkins-compose.yml完整内容如下:

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

  因并不是每个项目都需要自己的Maven私服,所以这里去除了Maven私服Nexus的部署,如果需要,直接在jenkins-compose.yml文件中添加即可:

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

  在Jenkins的depends_on:添加

    depends_on:
      - nexus
      - sonar
2、创建宿主机挂载目录并赋权
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、在 jenkins-compose.yml 文件所在目录下执行安装启动命令
docker-compose -f jenkins-compose.yml up -d
docker-compose -f jenkins-compose.yml down
docker-compose -f jenkins-compose.yml up --force-recreate --no-deps 服务名称
[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

[root@root server]# docker logs -f b28f1878ee08
[root@root server]# docker restart  b28f1878ee08
4、通过界面访问Jenkins,通过提示的路径找到初始密码并输入,然后进行下一步
Jenkins初始界面
[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、选择安装推荐插件即可
推荐插件
6、插件安装中
插件安装中
7、部分插件安装失败,根据后台日志可知,部分插件因为jenkins版本太低,所以不能安装,这里先进行下一步,在系统中升级jenkins版本之后,再继续安装插件。(jenkins/jenkins:lts镜像里面集成的不是最新的jenkins,但在下载插件时,是下载的最新的插件,导致不兼容,所以无法下载)
安装失败

报错日志:

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)

二、配置

8、登录到Jenkins之后,首先进入系统管理然后升级Jenkins版本
升级Jenkins
升级Jenkins
9、然后安装所需插件,系统管理 > 插件管理
10、系统管理 > 系统配置
-Xms256m -Xmx512m -XX:MaxPermSize=128m -XX:ReservedCodeCacheSize=64m
Maven参数
11、系统管理 > 全局工具配置,这里主要安装一些所需插件需要的软件环境,jenkins/jenkins:lts镜像默认装有openjdk和git,如果没有特殊要求,我们可以不安装,我们在这里安装SonarQube Scanner、Maven、NodeJS。
pnpm -registry=https://registry.npm.taobao.org
NodeJS安装
GitEgg-Cloud是一款基于SpringCloud整合搭建的企业级微服务应用开发框架,开源项目地址:

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

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

上一篇下一篇

猜你喜欢

热点阅读