在Ubuntu上实现Jenkins的负载均衡通常涉及以下几个步骤:
# 在Ubuntu上安装Jenkins
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.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 update
sudo apt install jenkins
/etc/default/jenkins
文件,修改 JENKINS_PORT
参数。# Jenkins instance 1
JENKINS_PORT="8080"
# Jenkins instance 2
JENKINS_PORT="8081"
sudo systemctl restart jenkins
你可以使用 Nginx 或 HAProxy 等负载均衡器来分发请求到不同的 Jenkins 实例。
sudo apt get install nginx
upstream jenkins {
server localhost:8080;
server localhost:8081;
}
server {
listen 80;
location / {
proxy_pass http://jenkins;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
sudo systemctl restart nginx
sudo apt get install haproxy
frontend jenkins_frontend
bind *:80
default_backend jenkins_backend
backend jenkins_backend
balance roundrobin
server jenkins1 localhost:8080 check
server jenkins2 localhost:8081 check
sudo systemctl restart haproxy
为了安全起见,你可以为 Jenkins 配置 SSL 证书。你可以使用 Let’s Encrypt 免费获取 SSL 证书。
# 安装 Certbot
sudo apt get install certbot python3-certbot-nginx
# 获取并安装 SSL 证书
sudo certbot --nginx -d yourdomain.com
Certbot 会自动更新 Nginx 配置文件以启用 SSL。
打开浏览器,访问你的域名或 IP 地址,确保请求被正确分发到不同的 Jenkins 实例。
通过以上步骤,你可以在 Ubuntu 上实现 Jenkins 的负载均衡,提高系统的可用性和性能。