2016-05-08 23:11:09 +08:00
|
|
|
# -*- mode: ruby -*-
|
|
|
|
# # vi: set ft=ruby :
|
|
|
|
|
|
|
|
require 'fileutils'
|
|
|
|
|
|
|
|
Vagrant.require_version ">= 1.8.0"
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2017-06-03 06:53:47 +08:00
|
|
|
SUPPORTED_OS = {
|
2017-06-04 23:31:39 +08:00
|
|
|
"coreos-stable" => {box: "coreos-stable", bootstrap_os: "coreos", user: "core", box_url: COREOS_URL_TEMPLATE % ["stable"]},
|
|
|
|
"coreos-alpha" => {box: "coreos-alpha", bootstrap_os: "coreos", user: "core", box_url: COREOS_URL_TEMPLATE % ["alpha"]},
|
|
|
|
"coreos-beta" => {box: "coreos-beta", bootstrap_os: "coreos", user: "core", box_url: COREOS_URL_TEMPLATE % ["beta"]},
|
2017-06-03 07:51:09 +08:00
|
|
|
"ubuntu" => {box: "bento/ubuntu-16.04", bootstrap_os: "ubuntu", user: "ubuntu"},
|
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
|
2016-05-18 10:52:57 +08:00
|
|
|
$vm_memory = 1536
|
2016-05-08 23:11:09 +08:00
|
|
|
$vm_cpus = 1
|
|
|
|
$shared_folders = {}
|
|
|
|
$forwarded_ports = {}
|
|
|
|
$subnet = "172.17.8"
|
2017-06-03 07:03:43 +08:00
|
|
|
$os = "ubuntu"
|
2017-02-17 04:46:04 +08:00
|
|
|
# The first three nodes are etcd servers
|
|
|
|
$etcd_instances = $num_instances
|
|
|
|
# The first two nodes are masters
|
|
|
|
$kube_master_instances = $num_instances == 1 ? $num_instances : ($num_instances - 1)
|
|
|
|
# All nodes are kube nodes
|
|
|
|
$kube_node_instances = $num_instances
|
2017-02-25 05:41:27 +08:00
|
|
|
$local_release_dir = "/vagrant/temp"
|
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
|
|
|
|
$inventory = File.join(File.dirname(__FILE__), "inventory") if ! $inventory
|
|
|
|
|
|
|
|
# if $inventory has a hosts 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"))
|
|
|
|
$vagrant_ansible = File.join(File.dirname(__FILE__), ".vagrant",
|
|
|
|
"provisioners", "ansible")
|
|
|
|
FileUtils.mkdir_p($vagrant_ansible) if ! File.exist?($vagrant_ansible)
|
|
|
|
if ! File.exist?(File.join($vagrant_ansible,"inventory"))
|
|
|
|
FileUtils.ln_s($inventory, $vagrant_ansible)
|
|
|
|
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|
|
|
|
|
# always use Vagrants insecure key
|
|
|
|
config.ssh.insert_key = false
|
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]
|
2016-05-08 23:11:09 +08:00
|
|
|
# plugin conflict
|
|
|
|
if Vagrant.has_plugin?("vagrant-vbguest") then
|
|
|
|
config.vbguest.auto_update = false
|
|
|
|
end
|
|
|
|
|
|
|
|
(1..$num_instances).each do |i|
|
|
|
|
config.vm.define vm_name = "%s-%02d" % [$instance_name_prefix, i] do |config|
|
|
|
|
config.vm.hostname = vm_name
|
|
|
|
|
2016-10-29 05:32:56 +08:00
|
|
|
if Vagrant.has_plugin?("vagrant-proxyconf")
|
|
|
|
config.proxy.http = ENV['HTTP_PROXY'] || ENV['http_proxy'] || ""
|
|
|
|
config.proxy.https = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ""
|
|
|
|
config.proxy.no_proxy = $no_proxy
|
|
|
|
end
|
|
|
|
|
2016-05-08 23:11:09 +08:00
|
|
|
if $expose_docker_tcp
|
|
|
|
config.vm.network "forwarded_port", guest: 2375, host: ($expose_docker_tcp + i - 1), auto_correct: true
|
|
|
|
end
|
|
|
|
|
|
|
|
$forwarded_ports.each do |guest, host|
|
|
|
|
config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
|
|
|
|
end
|
|
|
|
|
|
|
|
["vmware_fusion", "vmware_workstation"].each do |vmware|
|
|
|
|
config.vm.provider vmware do |v|
|
|
|
|
v.vmx['memsize'] = $vm_memory
|
|
|
|
v.vmx['numvcpus'] = $vm_cpus
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-10 05:19:28 +08:00
|
|
|
$shared_folders.each do |src, dst|
|
|
|
|
config.vm.synced_folder src, dst
|
|
|
|
end
|
|
|
|
|
2016-05-08 23:11:09 +08:00
|
|
|
config.vm.provider :virtualbox do |vb|
|
|
|
|
vb.gui = $vm_gui
|
|
|
|
vb.memory = $vm_memory
|
|
|
|
vb.cpus = $vm_cpus
|
|
|
|
end
|
|
|
|
|
|
|
|
ip = "#{$subnet}.#{i+100}"
|
|
|
|
host_vars[vm_name] = {
|
2017-03-01 05:31:28 +08:00
|
|
|
"ip": ip,
|
|
|
|
"flannel_interface": ip,
|
|
|
|
"flannel_backend_type": "host-gw",
|
2017-02-25 05:41:27 +08:00
|
|
|
"local_release_dir" => $local_release_dir,
|
2017-03-01 05:31:28 +08:00
|
|
|
"download_run_once": "False",
|
|
|
|
# Override the default 'calico' with flannel.
|
|
|
|
# inventory/group_vars/k8s-cluster.yml
|
|
|
|
"kube_network_plugin": "flannel",
|
2017-06-03 06:53:47 +08:00
|
|
|
"bootstrap_os": SUPPORTED_OS[$os][:bootstrap_os]
|
2016-05-08 23:11:09 +08:00
|
|
|
}
|
|
|
|
config.vm.network :private_network, ip: ip
|
|
|
|
|
|
|
|
# Only execute once the Ansible provisioner,
|
|
|
|
# when all the machines are up and ready.
|
|
|
|
if i == $num_instances
|
|
|
|
config.vm.provision "ansible" do |ansible|
|
|
|
|
ansible.playbook = "cluster.yml"
|
|
|
|
if File.exist?(File.join(File.dirname($inventory), "hosts"))
|
|
|
|
ansible.inventory_path = $inventory
|
|
|
|
end
|
|
|
|
ansible.sudo = true
|
|
|
|
ansible.limit = "all"
|
|
|
|
ansible.host_key_checking = false
|
|
|
|
ansible.raw_arguments = ["--forks=#{$num_instances}"]
|
|
|
|
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 = {
|
2017-02-17 04:46:04 +08:00
|
|
|
"etcd" => ["#{$instance_name_prefix}-0[1:#{$etcd_instances}]"],
|
|
|
|
"kube-master" => ["#{$instance_name_prefix}-0[1:#{$kube_master_instances}]"],
|
|
|
|
"kube-node" => ["#{$instance_name_prefix}-0[1:#{$kube_node_instances}]"],
|
2016-05-08 23:11:09 +08:00
|
|
|
"k8s-cluster:children" => ["kube-master", "kube-node"],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|