这是一个关于Vagrant的学习系列,包含如下文章:
1.Vagrant基本使用入门 https://www.linuxidc.com/Linux/2018-04/151772.htm
2.创建自己的Vagrant box https://www.linuxidc.com/Linux/2018-04/151773.htm
3.用Vagrant搭建Jenkins构建环境 https://www.linuxidc.com/Linux/2018-04/151774.htm
4.用Vagrant和Ansible搭建持续交付平台 https://www.linuxidc.com/Linux/2018-04/151775.htm
在上一篇文章中,我们讲到了如何创建自己的vagrant box。本文中,我们将使用Vagrant一次性创建3台虚拟机以组成一个Jenkins的master-slave构建环境,其中包含1台Jenkins master机器,2台slave机器。我们将采用Shell作为provision工具,当然你也可以使用像Ansible、Chef等。
在Vagrantfile中,通过config.vm.define来定义多台机器,通过引用外部的Shell脚本进行provision,Vagrantfile如下:
Vagrant.configure("2") do |config|
config.vm.box = "Ubuntu/trusty64"
config.vm.define "master" do |master|
master.vm.network "private_network", ip: "192.168.2.2"
master.vm.provision "shell", path: "master_provision.sh"
master.vm.provider "virtualbox" do |v|
v.name = "jenkins-master"
end
end
config.vm.define "slave1" do |slave1|
slave1.vm.network "private_network", ip: "192.168.2.3"
slave1.vm.provision "shell", path: "slave_provision.sh"
slave1.vm.provider "virtualbox" do |v|
v.name = "jenkins-slave1"
end
end
config.vm.define "slave2" do |slave2|
slave2.vm.network "private_network", ip: "192.168.2.4"
slave2.vm.provision "shell", path: "slave_provision.sh"
slave2.vm.provider "virtualbox" do |v|
v.name = "jenkins-slave2"
end
end
end
在每一个config.vm.define的配置内部,我们可以使用与其外部相同的配置项,内部配置会覆盖外部配置。这样,我们可以在外部配置中设置一些所有机器都共享的配置信息,然后在每个config.vm.define内部中针对性地配置某一台机器。
我们使用private network组成一个局域网,分别为各台机器指定静态的IP地址,此时其实使用的Virtualbox的Host-only网络类型。
对于master机器而言,我们首先需要安装JDK,然后安装Jenkins server,此时的master_provision.sh如下:
#! /bin/bash
#change apt source to be inside China to speed up apt
sudo mv /etc/apt/sources.list /etc/apt/sources.list.old
sudo cp /vagrant/sources.list /etc/apt/sources.list
#prepare for jenkins installation
wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c 'echo deb binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
#install openjdk
sudo apt-get -y install default-jdk
#install jenkins
sudo apt-get -y install jenkins
请注意,在provision一开始,我们更新了apt的源以提高下载速度。此时的sources.list包含以下阿里云的源:
deb trusty main multiverse restricted universe
deb trusty-backports main multiverse restricted universe
deb trusty-proposed main multiverse restricted universe
deb trusty-security main multiverse restricted universe
deb trusty-updates main multiverse restricted universe
deb-src trusty main multiverse restricted universe
deb-src trusty-backports main multiverse restricted universe
deb-src trusty-proposed main multiverse restricted universe
deb-src trusty-security main multiverse restricted universe
deb-src trusty-updates main multiverse restricted universe
对于两台slave机器来说,我们只需要安装JDK即可(其他软件,比如SSH等都已经包含在ubuntu/trusty64 box中了),对两台slave执行同一份provision文件provision_slave.sh:
#! /bin/bash
#change apt source to be inside China to speed up apt
sudo mv /etc/apt/sources.list /etc/apt/sources.list.old
sudo cp /vagrant/sources.list /etc/apt/sources.list
sudo apt-get update
#install openjdk
sudo apt-get -y install default-jdk
运行“vagrant up”,在Virtualbox中将出现三台虚拟机,他们与host机器同属于一个局域网,相互可连通。
在host机器中打开:8080(即Jenkins master),开始初始化配置Jenkins: