2016-05-08 23:11:09 +08:00
|
|
|
# -*- mode: ruby -*-
|
|
|
|
# # vi: set ft=ruby :
|
|
|
|
|
2018-11-16 22:28:29 +08:00
|
|
|
# For help on using kubespray with vagrant, check out docs/vagrant.md
|
|
|
|
|
2016-05-08 23:11:09 +08:00
|
|
|
require 'fileutils'
|
|
|
|
|
2017-12-20 22:40:35 +08:00
|
|
|
Vagrant.require_version ">= 2.0.0"
|
2016-05-08 23:11:09 +08:00
|
|
|
|
|
|
|
CONFIG = File.join(File.dirname(__FILE__), "vagrant/config.rb")
|
|
|
|
|
2017-06-04 23:31:39 +08:00
|
|
|
COREOS_URL_TEMPLATE = "https://storage.googleapis.com/%s.release.core-os.net/amd64-usr/current/coreos_production_vagrant.json"
|
|
|
|
|
2018-01-12 02:28:33 +08:00
|
|
|
# Uniq disk UUID for libvirt
|
|
|
|
DISK_UUID = Time.now.utc.to_i
|
|
|
|
|
2017-06-03 06:53:47 +08:00
|
|
|
SUPPORTED_OS = {
|
2018-11-16 22:28:29 +08:00
|
|
|
"coreos-stable" => {box: "coreos-stable", user: "core", box_url: COREOS_URL_TEMPLATE % ["stable"]},
|
|
|
|
"coreos-alpha" => {box: "coreos-alpha", user: "core", box_url: COREOS_URL_TEMPLATE % ["alpha"]},
|
|
|
|
"coreos-beta" => {box: "coreos-beta", user: "core", box_url: COREOS_URL_TEMPLATE % ["beta"]},
|
|
|
|
"ubuntu1604" => {box: "generic/ubuntu1604", user: "vagrant"},
|
|
|
|
"ubuntu1804" => {box: "generic/ubuntu1804", user: "vagrant"},
|
|
|
|
"centos" => {box: "centos/7", user: "vagrant"},
|
|
|
|
"centos-bento" => {box: "bento/centos-7.5", user: "vagrant"},
|
|
|
|
"fedora" => {box: "fedora/28-cloud-base", user: "vagrant"},
|
|
|
|
"opensuse" => {box: "opensuse/openSUSE-42.3-x86_64", user: "vagrant"},
|
|
|
|
"opensuse-tumbleweed" => {box: "opensuse/openSUSE-Tumbleweed-x86_64", user: "vagrant"},
|
2017-06-03 06:53:47 +08:00
|
|
|
}
|
|
|
|
|
2016-05-08 23:11:09 +08:00
|
|
|
# Defaults for config options defined in CONFIG
|
|
|
|
$num_instances = 3
|
|
|
|
$instance_name_prefix = "k8s"
|
|
|
|
$vm_gui = false
|
2017-10-03 04:16:39 +08:00
|
|
|
$vm_memory = 2048
|
2016-05-08 23:11:09 +08:00
|
|
|
$vm_cpus = 1
|
|
|
|
$shared_folders = {}
|
|
|
|
$forwarded_ports = {}
|
|
|
|
$subnet = "172.17.8"
|
2018-10-08 18:40:20 +08:00
|
|
|
$os = "ubuntu1804"
|
2017-10-03 18:16:13 +08:00
|
|
|
$network_plugin = "flannel"
|
2018-11-04 17:07:38 +08:00
|
|
|
# Setting multi_networking to true will install Multus: https://github.com/intel/multus-cni
|
|
|
|
$multi_networking = false
|
2017-02-17 04:46:04 +08:00
|
|
|
# The first three nodes are etcd servers
|
|
|
|
$etcd_instances = $num_instances
|
2017-10-03 18:16:13 +08:00
|
|
|
# The first two nodes are kube masters
|
2017-02-17 04:46:04 +08:00
|
|
|
$kube_master_instances = $num_instances == 1 ? $num_instances : ($num_instances - 1)
|
2017-10-03 18:16:13 +08:00
|
|
|
# All nodes are kube nodes
|
|
|
|
$kube_node_instances = $num_instances
|
2018-01-12 02:28:33 +08:00
|
|
|
# The following only works when using the libvirt provider
|
|
|
|
$kube_node_instances_with_disks = false
|
|
|
|
$kube_node_instances_with_disks_size = "20G"
|
|
|
|
$kube_node_instances_with_disks_number = 2
|
2019-02-20 22:37:18 +08:00
|
|
|
$override_disk_size = false
|
|
|
|
$disk_size = "20GB"
|
2018-01-12 02:28:33 +08:00
|
|
|
|
2018-08-11 01:42:48 +08:00
|
|
|
$playbook = "cluster.yml"
|
|
|
|
|
2016-05-08 23:11:09 +08:00
|
|
|
host_vars = {}
|
|
|
|
|
|
|
|
if File.exist?(CONFIG)
|
|
|
|
require CONFIG
|
|
|
|
end
|
|
|
|
|
2017-06-04 23:31:39 +08:00
|
|
|
$box = SUPPORTED_OS[$os][:box]
|
2016-05-08 23:11:09 +08:00
|
|
|
# if $inventory is not set, try to use example
|
2018-11-16 22:28:29 +08:00
|
|
|
$inventory = "inventory/sample" if ! $inventory
|
|
|
|
$inventory = File.absolute_path($inventory, File.dirname(__FILE__))
|
2016-05-08 23:11:09 +08:00
|
|
|
|
2018-11-16 22:28:29 +08:00
|
|
|
# if $inventory has a hosts.ini file use it, otherwise copy over
|
|
|
|
# vars etc to where vagrant expects dynamic inventory to be
|
|
|
|
if ! File.exist?(File.join(File.dirname($inventory), "hosts.ini"))
|
|
|
|
$vagrant_ansible = File.join(File.dirname(__FILE__), ".vagrant", "provisioners", "ansible")
|
2016-05-08 23:11:09 +08:00
|
|
|
FileUtils.mkdir_p($vagrant_ansible) if ! File.exist?($vagrant_ansible)
|
|
|
|
if ! File.exist?(File.join($vagrant_ansible,"inventory"))
|
2018-02-07 23:50:05 +08:00
|
|
|
FileUtils.ln_s($inventory, File.join($vagrant_ansible,"inventory"))
|
2016-05-08 23:11:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-29 05:32:56 +08:00
|
|
|
if Vagrant.has_plugin?("vagrant-proxyconf")
|
|
|
|
$no_proxy = ENV['NO_PROXY'] || ENV['no_proxy'] || "127.0.0.1,localhost"
|
|
|
|
(1..$num_instances).each do |i|
|
|
|
|
$no_proxy += ",#{$subnet}.#{i+100}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-08 23:11:09 +08:00
|
|
|
Vagrant.configure("2") do |config|
|
2018-11-16 22:28:29 +08:00
|
|
|
|
2016-05-18 10:52:57 +08:00
|
|
|
config.vm.box = $box
|
2017-06-04 23:31:39 +08:00
|
|
|
if SUPPORTED_OS[$os].has_key? :box_url
|
|
|
|
config.vm.box_url = SUPPORTED_OS[$os][:box_url]
|
|
|
|
end
|
2017-06-03 07:51:09 +08:00
|
|
|
config.ssh.username = SUPPORTED_OS[$os][:user]
|
2018-11-16 22:28:29 +08:00
|
|
|
|
2016-05-08 23:11:09 +08:00
|
|
|
# plugin conflict
|
|
|
|
if Vagrant.has_plugin?("vagrant-vbguest") then
|
|
|
|
config.vbguest.auto_update = false
|
|
|
|
end
|
|
|
|
|
2018-11-16 22:28:29 +08:00
|
|
|
# always use Vagrants insecure key
|
|
|
|
config.ssh.insert_key = false
|
2016-10-29 05:32:56 +08:00
|
|
|
|
2019-02-20 22:37:18 +08:00
|
|
|
if ($override_disk_size)
|
|
|
|
unless Vagrant.has_plugin?("vagrant-disksize")
|
|
|
|
system "vagrant plugin install vagrant-disksize"
|
|
|
|
end
|
|
|
|
config.disksize.size = $disk_size
|
|
|
|
end
|
|
|
|
|
2018-11-16 22:28:29 +08:00
|
|
|
(1..$num_instances).each do |i|
|
|
|
|
config.vm.define vm_name = "%s-%01d" % [$instance_name_prefix, i] do |node|
|
2016-05-08 23:11:09 +08:00
|
|
|
|
2018-11-16 22:28:29 +08:00
|
|
|
node.vm.hostname = vm_name
|
|
|
|
|
|
|
|
if Vagrant.has_plugin?("vagrant-proxyconf")
|
|
|
|
node.proxy.http = ENV['HTTP_PROXY'] || ENV['http_proxy'] || ""
|
|
|
|
node.proxy.https = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ""
|
|
|
|
node.proxy.no_proxy = $no_proxy
|
2016-05-08 23:11:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
["vmware_fusion", "vmware_workstation"].each do |vmware|
|
2018-11-16 22:28:29 +08:00
|
|
|
node.vm.provider vmware do |v|
|
2016-05-08 23:11:09 +08:00
|
|
|
v.vmx['memsize'] = $vm_memory
|
|
|
|
v.vmx['numvcpus'] = $vm_cpus
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-16 22:28:29 +08:00
|
|
|
node.vm.provider :virtualbox do |vb|
|
2016-05-08 23:11:09 +08:00
|
|
|
vb.memory = $vm_memory
|
|
|
|
vb.cpus = $vm_cpus
|
2018-11-16 22:28:29 +08:00
|
|
|
vb.gui = $vm_gui
|
|
|
|
vb.linked_clone = true
|
2019-02-20 22:35:21 +08:00
|
|
|
vb.customize ["modifyvm", :id, "--vram", "8"] # ubuntu defaults to 256 MB which is a waste of precious RAM
|
2016-05-08 23:11:09 +08:00
|
|
|
end
|
|
|
|
|
2018-11-16 22:28:29 +08:00
|
|
|
node.vm.provider :libvirt do |lv|
|
|
|
|
lv.memory = $vm_memory
|
|
|
|
lv.cpus = $vm_cpus
|
|
|
|
lv.default_prefix = 'kubespray'
|
|
|
|
# Fix kernel panic on fedora 28
|
|
|
|
if $os == "fedora"
|
|
|
|
lv.cpu_mode = "host-passthrough"
|
|
|
|
end
|
|
|
|
end
|
2017-10-28 00:57:12 +08:00
|
|
|
|
2018-01-12 02:28:33 +08:00
|
|
|
if $kube_node_instances_with_disks
|
|
|
|
# Libvirt
|
|
|
|
driverletters = ('a'..'z').to_a
|
2018-11-16 22:28:29 +08:00
|
|
|
node.vm.provider :libvirt do |lv|
|
2018-01-12 02:28:33 +08:00
|
|
|
# always make /dev/sd{a/b/c} so that CI can ensure that
|
|
|
|
# virtualbox and libvirt will have the same devices to use for OSDs
|
|
|
|
(1..$kube_node_instances_with_disks_number).each do |d|
|
|
|
|
lv.storage :file, :device => "hd#{driverletters[d]}", :path => "disk-#{i}-#{d}-#{DISK_UUID}.disk", :size => $kube_node_instances_with_disks_size, :bus => "ide"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-16 22:28:29 +08:00
|
|
|
if $expose_docker_tcp
|
|
|
|
node.vm.network "forwarded_port", guest: 2375, host: ($expose_docker_tcp + i - 1), auto_correct: true
|
|
|
|
end
|
|
|
|
|
|
|
|
$forwarded_ports.each do |guest, host|
|
|
|
|
node.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
|
|
|
|
end
|
|
|
|
|
|
|
|
node.vm.synced_folder ".", "/vagrant", disabled: false, type: "rsync", rsync__args: ['--verbose', '--archive', '--delete', '-z'] , rsync__exclude: ['.git','venv']
|
|
|
|
$shared_folders.each do |src, dst|
|
|
|
|
node.vm.synced_folder src, dst, type: "rsync", rsync__args: ['--verbose', '--archive', '--delete', '-z']
|
|
|
|
end
|
|
|
|
|
|
|
|
ip = "#{$subnet}.#{i+100}"
|
|
|
|
node.vm.network :private_network, ip: ip
|
|
|
|
|
|
|
|
# Disable swap for each vm
|
|
|
|
node.vm.provision "shell", inline: "swapoff -a"
|
|
|
|
|
|
|
|
host_vars[vm_name] = {
|
|
|
|
"ip": ip,
|
2019-01-27 05:38:37 +08:00
|
|
|
"flannel_interface": "eth1",
|
2018-11-16 22:28:29 +08:00
|
|
|
"kube_network_plugin": $network_plugin,
|
|
|
|
"kube_network_plugin_multus": $multi_networking,
|
|
|
|
"docker_keepcache": "1",
|
|
|
|
"download_run_once": "True",
|
|
|
|
"download_localhost": "False"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Only execute the Ansible provisioner once, when all the machines are up and ready.
|
2016-05-08 23:11:09 +08:00
|
|
|
if i == $num_instances
|
2018-11-16 22:28:29 +08:00
|
|
|
node.vm.provision "ansible" do |ansible|
|
2018-08-11 01:42:48 +08:00
|
|
|
ansible.playbook = $playbook
|
2019-02-01 15:46:52 +08:00
|
|
|
$ansible_inventory_path = File.join( $inventory, "hosts.ini")
|
|
|
|
if File.exist?($ansible_inventory_path)
|
|
|
|
ansible.inventory_path = $ansible_inventory_path
|
2016-05-08 23:11:09 +08:00
|
|
|
end
|
2017-12-12 18:47:04 +08:00
|
|
|
ansible.become = true
|
2016-05-08 23:11:09 +08:00
|
|
|
ansible.limit = "all"
|
|
|
|
ansible.host_key_checking = false
|
2018-11-16 22:28:29 +08:00
|
|
|
ansible.raw_arguments = ["--forks=#{$num_instances}", "--flush-cache", "--ask-become-pass"]
|
2016-05-08 23:11:09 +08:00
|
|
|
ansible.host_vars = host_vars
|
2016-05-18 12:30:01 +08:00
|
|
|
#ansible.tags = ['download']
|
2016-05-08 23:11:09 +08:00
|
|
|
ansible.groups = {
|
2018-11-16 22:28:29 +08:00
|
|
|
"etcd" => ["#{$instance_name_prefix}-[1:#{$etcd_instances}]"],
|
|
|
|
"kube-master" => ["#{$instance_name_prefix}-[1:#{$kube_master_instances}]"],
|
|
|
|
"kube-node" => ["#{$instance_name_prefix}-[1:#{$kube_node_instances}]"],
|
2016-05-08 23:11:09 +08:00
|
|
|
"k8s-cluster:children" => ["kube-master", "kube-node"],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|