debian

Debian Jenkins如何配置多节点

小樊
47
2025-09-23 15:33:58
栏目: 智能运维

Prerequisites
Before configuring multi-node Jenkins on Debian, ensure the following:

Step 1: Install Jenkins on Master and Agent Nodes

  1. Add Jenkins Repository: On both master and agent nodes, add the official Jenkins APT repository to ensure access to stable builds.
    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
    
  2. Install Jenkins: Run sudo apt install jenkins on both nodes. This installs Jenkins and starts the service automatically.
  3. Start/Enable Services: Verify Jenkins is running with sudo systemctl status jenkins (should show “active (running)”). Enable auto-start on boot: sudo systemctl enable jenkins.

Step 2: Configure Jenkins Master Node

  1. Access Web Interface: Open a browser and navigate to http://<master-node-ip>:8080. Unlock Jenkins using the initial admin password found in /var/lib/jenkins/secrets/initialAdminPassword.
  2. Install Essential Plugins: Go to Manage Jenkins > Manage Plugins. Install critical plugins:
    • SSH Build Agents (for secure agent communication).
    • Pipeline (if using Declarative/Scripted Pipelines).
    • Node and Label Parameter (for dynamic node selection).
  3. Adjust Master Configuration: Edit /etc/default/jenkins to:
    • Set HTTP_PORT=8080 (or a custom port if needed).
    • Ensure JENKINS_HOME=/var/lib/jenkins (default location for jobs/workspaces).
      Restart Jenkins to apply changes: sudo systemctl restart jenkins.

Step 3: Add Agent Nodes to Master

  1. Create Agent Node in Master:
    • Navigate to Manage Jenkins > Manage Nodes and Clouds > New Node.
    • Enter a Node Name (e.g., debian-agent-01).
    • Select Permanent Agent and click OK.
    • Configure node details:
      • Remote Root Directory: Set a unique directory for the agent (e.g., /home/jenkins/agent).
      • Labels: Add descriptive labels (e.g., linux, debian) to group similar agents.
      • Usage: Choose “Use this node as much as possible” (default) or restrict to specific jobs.
      • Launch Method: Select Launch agents via SSH (recommended for security).
  2. Configure SSH Credentials:
    • In the “Launch agents via SSH” section, click Add under “Credentials”.
    • Choose SSH Username with private key, enter the username (e.g., jenkins) for the agent node, and paste the private key (generated in Step 4).
  3. Save Node Configuration: Click Save to finalize the agent setup.

Step 4: Configure SSH Authentication (Master → Agent)

  1. Generate SSH Key Pair on Master:
    • Log in to the master node and run ssh-keygen -t rsa -b 4096. Press Enter to accept defaults (no passphrase for automation).
    • The private key is stored in ~/.ssh/id_rsa, and the public key in ~/.ssh/id_rsa.pub.
  2. Copy Public Key to Agent Node:
    • Use ssh-copy-id jenkins@<agent-node-ip> to copy the master’s public key to the agent’s ~/.ssh/authorized_keys file.
    • Enter the agent node’s password when prompted. This enables password-less SSH login from master to agent.
  3. Test SSH Connection: Run ssh jenkins@<agent-node-ip>. You should log in without a password.

Step 5: Verify Agent Connection

  1. Check Agent Status in Master: Go to Manage Jenkins > Manage Nodes and Clouds. The agent node (e.g., debian-agent-01) should appear with a Connected status.
  2. Run a Test Job:
    • Create a new Freestyle job in Jenkins.
    • Under Restrict where this project can be run, enter the agent’s label (e.g., linux).
    • Add a build step (e.g., Execute shell with echo "Hello from agent").
    • Save and run the job. Check the console output to confirm it executed on the agent node.

Optional: Set Up Load Balancing for High Availability
To distribute traffic across multiple master nodes (for high availability):

  1. Install HAProxy: On a separate Debian node (or one of the masters), run sudo apt install haproxy.
  2. Configure HAProxy: Edit /etc/haproxy/haproxy.cfg to include:
    frontend jenkins
        bind *:8080
        default_backend jenkins_servers
    backend jenkins_servers
        balance roundrobin
        server jenkins1 <master1-ip>:8080 check
        server jenkins2 <master2-ip>:8080 check
    
  3. Restart HAProxy: sudo systemctl restart haproxy.
  4. Access Jenkins via HAProxy: Use the HAProxy node’s IP/hostname (e.g., http://<haproxy-ip>:8080) to access Jenkins. Requests will be load-balanced across the configured masters.

Troubleshooting Tips

0
看了该问题的人还看了