温馨提示×

centos jenkins部署的网络设置

小樊
36
2025-12-21 16:47:58
栏目: 智能运维

CentOS 上部署 Jenkins 的网络设置要点


一 基础网络连通性与 DNS

  • 确认服务器可访问外网,用于安装与更新:ping www.baidu.com
  • 配置 DNS:编辑 /etc/resolv.conf,添加如 nameserver 8.8.8.8nameserver 8.8.4.4
  • 虚拟机场景需选择正确的网络模式(如 NAT),并确保虚拟网络的子网与网关配置正确。
  • 生产环境不建议长期关闭防火墙,仅做临时排查时再禁用。

二 防火墙放行端口

  • 查看防火墙状态:systemctl status firewalld
  • 放行 Jenkins 默认端口 8080/tcp
    • firewall-cmd --zone=public --add-port=8080/tcp --permanent
    • firewall-cmd --reload
  • 如需 HTTPS 443/tcp:firewall-cmd --zone=public --add-port=443/tcp --permanent && firewall-cmd --reload。
  • 排查时可临时关闭防火墙,但务必在验证完成后重新开启。

三 Jenkins 监听地址与端口

  • 修改 Jenkins 配置文件 /etc/sysconfig/jenkins
    • 设置监听端口:JENKINS_PORT=8080(或自定义端口)。
    • 允许所有接口访问:JENKINS_LISTEN_ADDRESS=0.0.0.0(或设置特定网卡 IP)。
  • 使配置生效:systemctl restart jenkins
  • 访问测试:浏览器打开 http://服务器IP:8080

四 反向代理与 HTTPS 配置

  • 使用 Nginx 反向代理(示例):
    • 安装:yum install -y nginx
    • 配置 /etc/nginx/conf.d/jenkins.conf:
      • listen 80; server_name your_domain.com;
      • location / { proxy_pass http://127.0.0.1:8080; 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; }
    • 启动:systemctl enable --now nginx
  • 启用 HTTPS(两种方式):
    • 方式 A(Nginx 终止 TLS):为域名申请证书(如 Let’s Encrypt),Nginx 配置 443 并反向代理到 8080
    • 方式 B(Jenkins 内置):
      • 生成 PKCS#12 证书:
        • openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out jenkins.pem
        • openssl pkcs12 -inkey key.pem -in jenkins.pem -export -out jenkins.p12
      • 配置 systemd 覆盖:创建 /etc/systemd/system/jenkins.service.d/override.conf
        • [Service]
          • Environment=“JENKINS_HTTPS_PORT=8443
          • Environment=“JENKINS_HTTPS_KEYSTORE=/var/lib/jenkins/jenkins.p12”
          • Environment=“JENKINS_HTTPS_KEYSTORE_PASSWORD=your_password”
      • 生效:systemctl daemon-reload && systemctl restart jenkins
      • 访问:https://服务器IP:8443

五 代理、构建代理与常见排错

  • 企业内网通过代理访问外网:
    • /etc/profile/etc/environment 设置:
      • export http_proxy=http://代理IP:端口
      • export https_proxy=http://代理IP:端口
    • 使生效:source /etc/profile 或重启相关服务。
  • 构建代理节点(Agent)通信:
    • 默认 JNLP 端口 50000/tcp,如需使用请放行:firewall-cmd --zone=public --add-port=50000/tcp --permanent && firewall-cmd --reload。
  • 常见排错:
    • 端口未放行:firewall-cmd --query-port=8080/tcp
    • 服务未监听:ss -lntp | grep 8080
    • 路由与 DNS:ip route、cat /etc/resolv.conf
    • 查看日志:journalctl -u jenkins -f。

0