生产环境sonarqube安装

生产环境sonarqube安装(单节点)

Install the Server
Install the Server as a Cluster

SonarQube三要素

three components

  1. SonarQube server运行如下进程

    • SonarQube用户界面webserver
    • Elasticsearch搜索服务
    • 负责分析代码报告并存入SonarQube数据库的compute engine
  2. 数据库存储下列数据

    • 代码质量和安全的metric与issue
    • SonarQube实例配置信息

主机与位置

出于性能考虑,SonarQube和数据库应该位于不同的机器上,并且SonarQube server应该独占的服务器,数据库应该位于相同的网络环境下。
所有的主机时间必须同步。

数据库安装

Linux downloads (Red Hat family)

# Install the repository RPM:
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# Install PostgreSQL:
yum install -y postgresql14-server

默认配置文件位置修改

# 默认数据位置
# /var/lib/pgsql

# 修改默认数据位置
# mv /var/lib/pgsql /export/
# vim /usr/lib/systemd/system/postgresql-14.service

# It's not recommended to modify this file in-place, because it will be
# overwritten during package upgrades.  It is recommended to use systemd
# "dropin" feature;  i.e. create file with suffix .conf under
# /etc/systemd/system/postgresql-14.service.d directory overriding the
# unit's defaults. You can also use "systemctl edit postgresql-14"
# Look at systemd.unit(5) manual page for more info.

# Note: changing PGDATA will typically require adjusting SELinux
# configuration as well.

# Note: do not use a PGDATA pathname containing spaces, or you will
# break postgresql-14-setup.
[Unit]
Description=PostgreSQL 14 database server
Documentation=https://www.postgresql.org/docs/14/static/
After=syslog.target
After=network.target

[Service]
Type=notify

User=postgres
Group=postgres

# Note: avoid inserting whitespace in these Environment= lines, or you may
# break postgresql-setup.

# Location of database directory
Environment=PGDATA=/export/pgsql/14/data/

# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog

# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj
Environment=PG_OOM_ADJUST_VALUE=0

ExecStartPre=/usr/pgsql-14/bin/postgresql-14-check-db-dir ${PGDATA}
ExecStart=/usr/pgsql-14/bin/postmaster -D ${PGDATA}
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT

# Do not set any timeout value, so that systemd will not kill postmaster
# during crash recovery.
TimeoutSec=0

# 0 is the same as infinity, but "infinity" needs systemd 229
TimeoutStartSec=0

TimeoutStopSec=1h

[Install]
WantedBy=multi-user.target

简单配置文件调整

# vim /export/pgsql/14/data/postgresql.conf
listen_addresses = '0.0.0.0'            # what IP address(es) to listen on;
max_connections = 1000                  # (change requires restart)
shared_buffers = 1024MB                 # min 128kB
dynamic_shared_memory_type = posix      # the default is the first option
max_wal_size = 30GB
min_wal_size = 80MB
log_destination = 'stderr'              # Valid values are combinations of
logging_collector = on                  # Enable capturing of stderr and csvlog
log_directory = 'log'                   # directory where log files are written,
log_filename = 'postgresql-%a.log'      # log file name pattern,
log_rotation_age = 1d                   # Automatic rotation of logfiles will
log_rotation_size = 0                   # Automatic rotation of logfiles will
log_truncate_on_rotation = on           # If on, an existing log file with the
log_line_prefix = '%m [%p] '            # special values:
log_timezone = 'Asia/Shanghai'
datestyle = 'iso, ymd'
timezone = 'Asia/Shanghai'
lc_messages = 'zh_CN.UTF-8'                     # locale for system error message
lc_monetary = 'zh_CN.UTF-8'                     # locale for monetary formatting
lc_numeric = 'zh_CN.UTF-8'                      # locale for number formatting
lc_time = 'zh_CN.UTF-8'                         # locale for time formatting
default_text_search_config = 'pg_catalog.simple'

初始化和启动

# Optionally initialize the database and enable automatic start:
/usr/pgsql-14/bin/postgresql-14-setup initdb
systemctl enable postgresql-14
systemctl start postgresql-14

rpm下载安装方式

POSTGRESQL COMMON REPOSITORY

Create an empty schema and a sonarqube user. Grant this sonarqube user permissions to create, update, and delete objects for this schema.

数据库创建用户和授权

# su - postgres
# psql
CREATE DATABASE sonar;
CREATE USER sonarqube WITH PASSWORD 'xxxxxx';
GRANT ALL PRIVILEGES ON DATABASE sonar TO sonarqube;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO sonarqube;

postgresql主从

Achieving PostgreSQL Master Slave Replication: 7 Easy Steps

zip文件安装SonarQube

useradd sonar
cd /usr/local
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.4.0.54424.zip
unzip sonarqube-9.4.0.54424.zip
mv sonarqube-9.4.0.54424 sonarqube
chown -R sonar.sonar sonarqube

内核参数设置

# vim /etc/sysctl.conf
vm.max_map_count=655360
# sysctl -p

数据库设置

# vim /usr/local/sonarqube/conf/sonar.properties
# Example for PostgreSQL
sonar.jdbc.username=sonarqube
sonar.jdbc.password=xxxxxx
sonar.jdbc.url=jdbc:postgresql://localhost/sonar

SonarQube已经提供除oracle以外的数据库驱动,不要改动。

设置Elasticsearch的存储路径

默认情况下,Elasticsearch的存储路径为$SONARQUBE-HOME/data,生产环境最好修改。

# mkdir -p /export/sonarqube/{data, temp}
# chown -R sonar.sonar /export/sonarqube
# vim /usr/local/sonarqube/conf/sonar.properties
sonar.path.data=/export/sonarqube/data
sonar.path.temp=/export/sonarqube/temp

启动webserver

默认端口是9000,路径是/,变更方式

# vim /usr/local/sonarqube/conf/sonar.properties
sonar.web.host=0.0.0.0
sonar.web.port=9000
sonar.web.context=/

启动方式

su - sonar
/usr/local/sonarqube/bin/linux-x86-64/sonar.sh start

变更java版本(可选)

修改$SONARQUBE-HOME/conf/wrapper.conf

# vim /usr/local/sonarqube/conf/wrapper.conf
wrapper.java.command=/path/to/my/jdk/bin/java

使用systemd管理SonarQube

Operating the Server

# vim /etc/systemd/system/sonarqube.service
[Unit]
Description=SonarQube service
After=syslog.target network.target

[Service]
Type=simple
User=sonar
Group=sonar
PermissionsStartOnly=true
ExecStart=/usr/bin/nohup /usr/bin/java -Xms1G -Xmx32G -Djava.net.preferIPv4Stack=true -jar /usr/local/sonarqube/lib/sonar-application-9.4.0.54424.jar
StandardOutput=syslog
LimitNOFILE=131072
LimitNPROC=8192
TimeoutStartSec=5
Restart=always
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

java进程内存

4种内存不足迹象
  • Your monitoring tools show one or more of the SonarQube processes is reaching its memory limit
  • Any of the SonarQube processes crashes and/or generates an out-of-memory error in the sonar.log file
  • A SonarQube background task fails with an out-of-memory error in the background task log
  • The store size of the Issues index of your Elasticsearch instance (visible in the System Info) is greater than or equal to the memory allocated to the Elasticsearch Java process

可以在$SONARQUBE-HOME/conf/sonar.properties中增加-Xmx内存

Java ProcessSonarQube PropertyNotes
Compute Enginesonar.ce.javaOpts
Elasticsearchsonar.search.javaOptsIt is recommended to set the min and max memory to the same value to prevent the heap from resizing at runtime, which diverts JVM resources and can greatly increase response times of in-flight requests.
Websonar.web.javaOpts
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
安装SonarQube 8.7,可以按照以下步骤进行操作: 1. 首先,确保你已经安装了Java运行时环境(JRE)或者Java开发工具包(JDK),版本要求为8或以上。 2. 访问SonarQube官方网站(https://www.sonarqube.org/downloads/),下载适用于你的操作系统的SonarQube 8.7版本。 3. 解压缩下载的文件到你希望安装SonarQube的目录。 4. 打开SonarQube目录中的`conf`文件夹,编辑`sonar.properties`文件。在文件中找到`sonar.jdbc.username`和`sonar.jdbc.password`两行,分别设置数据库的用户名和密码。 5. 在同一个文件中,找到并取消注释下面这一行:`#sonar.web.host=0.0.0.0`。这将允许从其他计算机访问SonarQube。 6. 打开终端或命令提示符,导航到SonarQube目录中的`bin`文件夹。 7. 执行以下命令启动SonarQube服务器: - 在Linux或Mac上:`./sonar.sh start` - 在Windows上:`StartSonar.bat` 8. 等待一段时间,直到看到类似于`SonarQube is up`的消息,表示服务器已经成功启动。 9. 使用浏览器访问`http://localhost:9000`(如果你在步骤5中更改了主机设置,请使用相应的主机名替换localhost)。 10. 在首次访问时,系统会要求你提供一个管理员凭证。按照指示完成管理员账户的创建。 11. 登录后,你可以开始使用SonarQube进行代码分析和质量管理。 请注意,以上步骤仅适用于在本地安装SonarQube。如果你想在生产环境中部署SonarQube,可能需要进行一些额外的配置和安全性措施。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值