-
Notifications
You must be signed in to change notification settings - Fork 27
/
Vagrantfile
143 lines (125 loc) · 4.79 KB
/
Vagrantfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.require_version ">= 1.6.0"
# Library to pass in parameters
require 'getoptlong'
MOUNT_OPT='--mount'
NETWORK_OPT='--network'
DOCKER_USERNAME_OPT='--docker-username'
DOCKER_PASSWORD_OPT='--docker-password'
cmd_opts = GetoptLong.new(
# The path on the host that will be mounted on the nodes under /data
[ MOUNT_OPT, GetoptLong::OPTIONAL_ARGUMENT ],
# The network driver to user (weave or flannel)
[ NETWORK_OPT, GetoptLong::OPTIONAL_ARGUMENT ],
# The dockerhub credentials
[ DOCKER_USERNAME_OPT, GetoptLong::OPTIONAL_ARGUMENT ],
[ DOCKER_PASSWORD_OPT, GetoptLong::OPTIONAL_ARGUMENT ]
)
options = {
#:kubernetes => "1.9.1",
:kubernetes => "latest",
:pod_network_cidr => "10.244.0.0/16",
:kubeadm_token => "54c315.78a320e33baaf27d",
:host_mount => nil,
:guest_mount => "/data",
:docker_username => nil,
:docker_password => nil,
:network => "weave" # or "flannel"
}
cmd_opts.each do |opt, arg|
case opt
when MOUNT_OPT
options[:host_mount]=arg
puts "Mount local folder #{arg} --> /data"
when NETWORK_OPT
options[:network]=arg
puts "Use Network driver #{arg}"
when DOCKER_USERNAME_OPT
options[:docker_username]=arg
when DOCKER_PASSWORD_OPT
options[:docker_password]=arg
end
end
boxes = [
{
:name => "k8smaster",
:eth1 => "192.168.8.10",
:mem => "2048",
:cpu => "1",
:is_master => true
},
{
:name => "k8sworker",
:eth1 => "192.168.8.11",
:mem => "4096",
:cpu => "2"
}
]
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
# Validate the nodes
masterCount = boxes.select { |box| box[:is_master] }.count
raise "No master defined in the boxes" if masterCount == 0
raise "You must only have one master" if masterCount > 1
# Configure the nodes
boxes.each do |opts|
config.vm.define opts[:name] do |config|
config.vm.hostname = opts[:name]
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", opts[:mem]]
v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
end
config.vm.network :private_network, ip: opts[:eth1]
end
end
# Install the nodes
boxes.each do |box|
config.vm.define box[:name], primary: box[:is_master] == true do |node|
node.ssh.forward_agent = true
# Mount an additional shared folder if specified as a command-line argument
if options[:host_mount]
node.vm.synced_folder "#{options[:host_mount]}", "#{options[:guest_mount]}"
end
# setup the node with kubernetes requirements
node.vm.provision "shell", path: "./scripts/setup-node.sh", args: [options[:kubernetes], box[:name], box[:eth1]]
# setup the node depending on its role: master or worker
if box[:is_master]
node.vm.provision "shell", args: [options[:kubernetes]], inline: <<-SHELL
set -e -x
KUBE_VERSION=$1
KUBEADM_VERSION=
# Create the master node
#kubeadm init --apiserver-advertise-address #{box[:eth1]} --pod-network-cidr #{options[:pod_network_cidr]} --kubernetes-version #{options[:kubernetes]} --token #{options[:kubeadm_token]}
if [ "$KUBE_VERSION" != "latest" ]; then
KUBEADM_VERSION="--kubernetes-version v$KUBE_VERSION"
fi
kubeadm init --apiserver-advertise-address #{box[:eth1]} --pod-network-cidr #{options[:pod_network_cidr]} --token #{options[:kubeadm_token]} $KUBEADM_VERSION
# Copy Kube config into our shared Vagrant folder
cp -rf /etc/kubernetes/admin.conf /vagrant/kubeconfig/
SHELL
else # it is a worker
master = boxes.select { |box| box[:is_master] }.first
raise "Could not find master box" if master == nil
node.vm.provision "shell", inline: <<-SHELL
set -e -x
# Add a worker node to the cluster
kubeadm join --ignore-preflight-errors=all --discovery-token-unsafe-skip-ca-verification --token #{options[:kubeadm_token]} #{master[:eth1]}:6443
SHELL
end
# if the user provided its credentials for his DockerHub account, then do the login for each node.
if options[:docker_username] && options[:docker_password] then
node.vm.provision "shell", env: {"USERNAME" => options[:docker_username], "PASSWORD" => options[:docker_password]}, inline: <<-SHELL
set -e
echo "Log into Dockerhub with user $USERNAME"
docker login -u $USERNAME -p $PASSWORD
SHELL
end
# Run post install script only in the last box
isLastBox = boxes.last[:name] == box[:name]
if isLastBox
node.vm.provision "shell", path: "./scripts/post-install.sh", args: [options[:network]]
end
end
end
end