jenkins学习之master服务器安装

参考

https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+on+Ubuntu

https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins

Installation

1
2
3
4
5
6
7

wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins


Upgrade

Once installed like this, you can update to the later version of Jenkins (when it comes out) by running the following commands:

1
2
3
4

sudo apt-get update
sudo apt-get install jenkins

(aptitude or apt-get doesn’t make any difference.)

Jenkins will be launched as a daemon up on start. See /etc/init.d/jenkins for more details.
The 'jenkins' user is created to run this service.
Log file will be placed in /var/log/jenkins/jenkins.log. Check this file if you are troubleshooting Jenkins.
/etc/default/jenkins will capture configuration parameters for the launch like e.g JENKINS_HOME
By default, Jenkins listen on port 8080. Access this port with your browser to start configuration.

安装后的配置

Setting up an Apache Proxy for port 80 -> 8080

This configuration will setup Apache2 to proxy port 80 to 8080 so that you can keep Jenkins on 8080.

1
2
3
4
5
6
7
8
9

sudo aptitude install apache2 安装apache2 服务




sudo a2enmod proxy
sudo a2enmod proxy_http

Create a file called jenkins.conf in /etc/apache2/sites-available

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName ci.company.com
ServerAlias ci
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPreserveHost on
ProxyPass / http://localhost:8080/ nocanon
AllowEncodedSlashes NoDecode
</VirtualHost>


重启

1
2
sudo a2ensite jenkins
sudo apache2ctl restart

设置apache的反向代理,可以把8080端口重定向到80端口

主要是添加下面三行

1
2
3
ProxyPreserveHost on
ProxyPass / http://localhost:8080/ nocanon
AllowEncodedSlashes NoDecode

80 端口的转发 也可以使用 iptables的 nat 功能实现

1
2
3
4
5

# 80 端口 转发 80 端口
sudo iptables -t nat -A PREROUTING -i eno1 -p tcp --dport 80 -j REDIRECT --to-port 8080
sudo iptables -t nat -A PREROUTING -i eno2 -p tcp --dport 80 -j REDIRECT --to-port 8080

jenkins-master ufw 防火墙设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env bash
# jenkins-master ufw 防火墙设置
#
# awk -F " " '{print $2 ": " $12 " " $13 " " " " $22}' /var/log/ufw.log |sort|uniq

yes | sudo ufw enable

# ufw 默认是这样的
sudo ufw default deny incoming #禁用所有连接服务器的入
sudo ufw default allow outgoing # 允许服务器上所有的出

sudo ufw allow from 192.168.1.9 to any port 22 # 允许 ssh管理端口 管理端口


sudo ufw allow 8080 # 允许网页访问jenkins
sudo ufw allow 80 # 允许网页访问jenkins
sudo ufw allow 37743 # 允许 windows node



sudo ufw logging on
sleep 3
sudo ufw status numbered


更改jenkins的 HOME 目录

1
2
3

sudo usermod --home /work/jenkins jenkins # 这个命令是修改 linux 帐号的家目录

修改jenkins的JENKINS_HOME 目录

1
2
sudo vi /etc/default/jenkins

编辑这个文件把里面的 JENKINS_HOME 这行改为  JENKINS_HOME=/work/jenkins
默认安装好jenkins 这个2个变量都是/var/lib/jenkins这个值。这里根据具体情况来修改。
这里我们是把15TB的硬盘挂载到/work 下面了。所以这里修改为/work/jenkins 了。
这里也是为了路径的统一,方便脚本备份jenkins。

其他

上线下线 节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

import jenkins.model.*;
for (slave in Jenkins.instance.slaves) {
def comp=slave.computer;
if(comp.name.contains("bf-")){
println comp.name
comp.connect(true)
}

}


import jenkins.model.*;
for (slave in Jenkins.instance.slaves) {
def comp=slave.computer;
if(comp.name.contains("bf-")){
println comp.name
comp.disconnect()
}

}
1
2
3
4
5
6
7
8
9
10
for (aSlave in hudson.model.Hudson.instance.slaves) {
if(aSlave.name.contains("xxx-xx")){
println aSlave.name
computer = aSlave.getComputer();

computer.disconnect(null)
//computer.setTemporarilyOffline(true) // To Disable all Nodes
// computer.setTemporarilyOffline(false) -- To Enable all nodes.
}
}

设置这个 ping thread 的时间

1
2
3
4
5
6
7
8
9
10
11
12
13
// 设置这个 ping thread 的时间

import jenkins.util.SystemProperties;

System.setProperty("hudson.slaves.ChannelPinger.pingTimeoutSeconds","3600")
System.setProperty("hudson.slaves.ChannelPinger.pingIntervalSeconds","600")

def a = SystemProperties.getInteger("hudson.slaves.ChannelPinger.pingTimeoutSeconds", 0)
def b = SystemProperties.getInteger("hudson.slaves.ChannelPinger.pingIntervalSeconds", 0)

println(a)
println(b)