写点什么

极客时间运维进阶训练营第四周作业

作者:9527
  • 2022-11-19
    美国
  • 本文字数:2372 字

    阅读完需:约 8 分钟

Mandatory Assignment

Jenkins master monitoring

  • Install "prometheus metrics plugin"

  • Install "CloudBees Disk Simple"

Jenkins distributed env

  • Multi slaves


Jenkins install slave

Master config

  • On the Jenkins master, install "SSH Build Agent" plugin, restart Jenkins master

Slave config

  • Install Java11

$ amazon-linux-extras install java-openjdk11...Installed:  java-11-openjdk.x86_64 1:11.0.16.0.8-1.amzn2.0.1
$ java --versionopenjdk 11.0.16 2022-07-19 LTSOpenJDK Runtime Environment (Red_Hat-11.0.16.0.8-1.amzn2.0.1) (build 11.0.16+8-LTS)OpenJDK 64-Bit Server VM (Red_Hat-11.0.16.0.8-1.amzn2.0.1) (build 11.0.16+8-LTS, mixed mode, sharing)
复制代码
  • Create a user on the agent to be used by Jenkins.

$ useradd -d /var/lib/jenkins jenkins$ id jenkinsuid=1001(jenkins) gid=1001(jenkins) groups=1001(jenkins)
复制代码
  • Generate an ssh key for Jenkins user

$ sudo su jenkins$ cd ~$ pwd/var/lib/jenkins$ ssh-keygen -m PEM -t rsa -b 4096...
$ ls -al .sshtotal 8drwx------ 2 jenkins jenkins 38 Nov 15 15:57 .drwx------ 3 jenkins jenkins 74 Nov 15 15:57 ..-rw------- 1 jenkins jenkins 3247 Nov 15 15:57 id_rsa-rw-r--r-- 1 jenkins jenkins 762 Nov 15 15:57 id_rsa.pub

复制代码
  • Add the public key to the authorized_keys file of the Jenkins user on the agent node.

$ cat ~/.ssh/id_rsa.pub >> /var/lib/jenkins/.ssh/authorized_keys$ chmod 600 /var/lib/jenkins/.ssh/authorized_keys$ ls -al ~/.sshtotal 12drwx------ 2 jenkins jenkins   61 Nov 15 16:06 .drwx------ 3 jenkins jenkins   95 Nov 15 16:05 ..-rw------- 1 jenkins jenkins  762 Nov 15 16:06 authorized_keys-rw------- 1 jenkins jenkins 3247 Nov 15 15:57 id_rsa-rw-r--r-- 1 jenkins jenkins  762 Nov 15 15:57 id_rsa.pub
复制代码
  • On the Jenkins slave, you can test ssh login

$ sudo su jenkins$ ssh xx.xx.xx.xx
You should be able to log in successfully
复制代码
  • Now we’re ready to finish setting up the node via the Jenkins UI. In Jenkins Master, go to Manage Jenkins, then Manage Nodes, then click New Node. Here you can give your agent node a name, then select Permanent Agent and click OK. There are a variety of options you can use here to customize your node. All we care about right now is the Launch Method.

  • Select Launch Slave Agents via SSH for Launch Method.

  • Enter the hostname or IP address of your agent node in the Host field.

  • Click the Add button next to Credentials and select the Jenkins scope.

  • For the credential, set Kind to SSH username with private key.

  • Enter jenkins for the username.

  • For the private key, select Enter directly. Then, copy the contents of your private key file (~/.ssh/id_rsa by default) and paste it into the Key box.

  • If you used a passphrase when generating the key, enter that for Passphrase, otherwise, leave it blank.

  • Enter a descriptive id and description, then click Add.

  • Click Save to save the new node.

  • Now you should be able to see the new Jenkins agent

  • Config a test job to use new slave

  • Trigger the job

  • You should see it run successfully

Jekinsfile Syntax

  • Sample Jenkinsfile

pipeline {    agent none     stages {        stage('Example Build') {            agent { docker 'maven:3.8.1-adoptopenjdk-11' }             steps {                echo 'Hello, Maven'                sh 'mvn --version'            }        }        stage('Example Test') {            agent { docker 'openjdk:8-jre' }             steps {                echo 'Hello, JDK'                sh 'java -version'            }        }    }}
复制代码
  • Official Pipeline documentation: https://www.jenkins.io/doc/book/pipeline/syntax/

Install Sonarqube

  • Optimize system kernel parameters:

$ vi /etc/sysctl.conf# Addvm.max_map_count=524288fs.file-max=131072
$ sysctl -pvm.max_map_count=524288fs.file-max=131072
复制代码
  • Install with docker-compose

version: "3"services:  sonarqube:    image: sonarqube:community    hostname: sonarqube    container_name: sonarqube    depends_on:      - db    environment:      SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar      SONAR_JDBC_USERNAME: sonar      SONAR_JDBC_PASSWORD: sonar    volumes:      - sonarqube_data:/opt/sonarqube/data      - sonarqube_extensions:/opt/sonarqube/extensions      - sonarqube_logs:/opt/sonarqube/logs    ports:      - "9000:9000"  db:    image: postgres:12    hostname: postgresql    container_name: postgresql    environment:      POSTGRES_USER: sonar      POSTGRES_PASSWORD: sonar      POSTGRES_DB: sonar    volumes:      - postgresql:/var/lib/postgresql      - postgresql_data:/var/lib/postgresql/data
volumes: sonarqube_data: sonarqube_extensions: sonarqube_logs: postgresql: postgresql_data:
复制代码
  • Run docker-compose command:

$ docker-compose up -d
复制代码


  • Log into SonarQube

  • Dashboard


Optional Assignment


Jenkins install SonarQube Scanner Plugin

  • Go to Jenkins master, "manage Jenkins", "plugins"

  • Configure test job to run "SonarQube Scanner"

  • Job output



发布于: 刚刚阅读数: 5
用户头像

9527

关注

还未添加个人签名 2020-04-22 加入

还未添加个人简介

评论

发布
暂无评论
极客时间运维进阶训练营第四周作业_9527_InfoQ写作社区