Skip to content

Commit

Permalink
vagrant lib files
Browse files Browse the repository at this point in the history
  • Loading branch information
marthanda93 committed May 27, 2021
1 parent 06bf232 commit 6b83bae
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 230 deletions.
129 changes: 15 additions & 114 deletions kubernetes/kubeadm/centos/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
# vi: set ft=ruby :

require 'yaml'
require 'open3'

k8s = YAML.load_file(File.join(File.dirname(__FILE__), 'config.yaml'))
ENV["LC_ALL"] = "en_US.UTF-8"

$msg = <<MSG
msg = <<MSG
------------------------------------------------------
Kubernetes up and running ✌ ☺ ✌
Expand All @@ -17,124 +19,23 @@ URLS:
MSG

Vagrant.configure(k8s['api_version']) do |config|
config.vm.define "#{k8s['cluster']['master']}" do |subconfig|
subconfig.vm.post_up_message = $msg
subconfig.vm.box = k8s['image']

subconfig.vm.hostname = "#{k8s['cluster']['master']}"
subconfig.vm.network :private_network, ip: "#{k8s['ip_part']}.10"

# Hostfile :: Master node
subconfig.vm.provision "master-hostfile", type: "shell" do |mhf|
mhf.inline = <<-SHELL
echo -e "127.0.0.1\t$2" | tee -a /etc/hosts; echo -e "$1\t$2" | tee -a /etc/hosts
SHELL
mhf.args = ["#{k8s['ip_part']}.10", "#{k8s['cluster']['master']}"]
end
# Hostfile :: Worker node
subconfig.vm.provision "Update hostfile and authorized_keys", type: "shell" do |whu|
whu.inline = <<-SHELL
for i in $(eval echo {1..$2}); do
echo -e "${3}.$((10 + $i))\t#{k8s['cluster']['node']}-${i}" | tee -a /etc/hosts
done
SHELL
whu.args = ["#{k8s['user']}", "#{k8s['resources']['node']['count']}", "#{k8s['ip_part']}"]
end
config.vm.boot_timeout = 600
# config.ssh.keep_alive = true

subconfig.vm.provider "virtualbox" do |vb|
vb.memory = k8s['resources']['master']['memory']
vb.cpus = k8s['resources']['master']['cpus']
end

subconfig.vm.provision "Restart VM", type: "shell" do |reboot|
reboot.privileged = true
reboot.inline = <<-SHELL
echo "----------------------------------|| Reboot to load all config"
SHELL
reboot.reboot = true
end

subconfig.vm.provision "#{k8s['cluster']['master']}-setup", type: "shell" do |mns|
mns.path = "script/bootstrap_master.sh"
mns.args = ["#{k8s['user']}", "#{k8s['ip_part']}", "10"]
end

subconfig.trigger.after :up do |trigger_local|
trigger_local.run = {inline: "/bin/bash -c 'vagrant ssh --no-tty -c \"cat /etc/kubernetes/admin.conf\" #{k8s['cluster']['master']} > admin.conf && rm -f \${HOME}/.kube/config 2>/dev/null; mkdir -p \${HOME}/.kube; cp -i admin.conf \${HOME}/.kube/config; rm -f admin.conf'"}
end
# Kubernetes Controller cluster
if File.exist?('lib/master.rb')
eval(IO.read('lib/master.rb'), binding)
end

(1..k8s['resources']['node']['count']).each do |i|
config.vm.define "#{k8s['cluster']['node']}-#{i}" do |subconfig|
subconfig.vm.box = k8s['image']

subconfig.vm.hostname = "#{k8s['cluster']['node']}-#{i}"
subconfig.vm.network :private_network, ip: "#{k8s['ip_part']}.#{i + 10}"

# Hostfile :: Master node
subconfig.vm.provision "master-hostfile", type: "shell" do |s|
s.inline = <<-SHELL
echo -e "$1\t$2" | tee -a /etc/hosts
firewall-cmd --permanent --add-port=10250/tcp
firewall-cmd --permanent --add-port=30000-32767/tcp
firewall-cmd --reload
SHELL
s.args = ["#{k8s['ip_part']}.10", "#{k8s['cluster']['master']}"]
end
# Hostfile :: Worker node
(1..k8s['resources']['node']['count']).each do |j|
if i != j
subconfig.vm.provision "other-worker-hostfile", type: "shell" do |supdate|
supdate.inline = <<-SHELL
echo -e "$1\t$2" | tee -a /etc/hosts
SHELL
supdate.args = ["#{k8s['ip_part']}.#{10 + j}", "#{k8s['cluster']['node']}-#{j}", "#{k8s['user']}", "#{i}"]
end
else
subconfig.vm.provision "self-worker-hostfile", type: "shell" do |supdate|
supdate.inline = <<-SHELL
echo -e "127.0.0.1\t$2" | tee -a /etc/hosts; echo -e "$1\t$2" | tee -a /etc/hosts
SHELL
supdate.args = ["#{k8s['ip_part']}.#{10 + j}", "#{k8s['cluster']['node']}-#{j}", "#{k8s['user']}", "#{i}"]
end
end
end

subconfig.vm.provider "virtualbox" do |vb|
vb.memory = k8s['resources']['node']['memory']
vb.cpus = k8s['resources']['node']['cpus']
end

subconfig.trigger.after :up do |trigger_local|
trigger_local.run = {inline: "/bin/bash -c 'wpub_key=$(vagrant ssh --no-tty -c \"cat /home/#{k8s['user']}/.ssh/id_rsa.pub\" #{k8s['cluster']['node']}-#{i}) && vagrant ssh --no-tty -c \"echo \${wpub_key} >> /home/#{k8s['user']}/.ssh/authorized_keys\" #{k8s['cluster']['master']}; mpub_key=$(vagrant ssh --no-tty -c \"cat /home/#{k8s['user']}/.ssh/id_rsa.pub\" #{k8s['cluster']['master']}) && vagrant ssh --no-tty -c \"echo \${mpub_key} >> /home/#{k8s['user']}/.ssh/authorized_keys\" #{k8s['cluster']['node']}-#{i}'"}
end

subconfig.trigger.after :up do |trigger_remote|
trigger_remote.run_remote = {inline: <<-SHELL
kube_join=\$(echo "ssh #{k8s['user']}@#{k8s['cluster']['master']} -o StrictHostKeyChecking=no '( cat /home/#{k8s['user']}/.bash_profile | grep KUBEADM_JOIN)'" | su - #{k8s['user']})
kube_join=\$(echo ${kube_join} | awk -F'"' '{print \$2}')
echo "sudo $kube_join" | su - #{k8s['user']}
echo "scp -o StrictHostKeyChecking=no #{k8s['user']}@#{k8s['cluster']['master']}:/etc/kubernetes/admin.conf /home/#{k8s['user']}/" | su - #{k8s['user']}
echo "mkdir -p /home/#{k8s['user']}/.kube" | su - #{k8s['user']}
echo "cp -i /home/#{k8s['user']}/admin.conf /home/#{k8s['user']}/.kube/config" | su - #{k8s['user']}
echo "sudo chown #{k8s['user']}:#{k8s['user']} -R /home/#{k8s['user']}/.kube" | su - #{k8s['user']}
echo "kubectl label nodes #{k8s['cluster']['node']}-#{i} kubernetes.io/role=#{k8s['cluster']['node']}-#{i}" | su - #{k8s['user']}
SHELL
}
end

subconfig.vm.provision "Restart VM", type: "shell" do |reboot|
reboot.privileged = true
reboot.inline = <<-SHELL
echo "----------------------------------|| Reboot to load all config"
SHELL
reboot.reboot = true
end
# Kubernetes Worker cluster
(1..k8s['resources']['node']['count']).each do |i|
if File.exist?('lib/node.rb')
eval(IO.read('lib/node.rb'), binding)
end
end

config.vm.provision "vm-setup", type: "shell" do |vms|
vms.path = "script/bootstrap.sh"
vms.args = ["#{k8s['user']}"]
# Exchange ssh keys to access each other, expect HA can access each of vm but not other vm cannot to access HA directly.
if File.exist?('lib/trigger.rb')
eval(IO.read('lib/trigger.rb'), binding)
end
end
46 changes: 46 additions & 0 deletions kubernetes/kubeadm/centos/lib/master.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
config.vm.define "#{k8s['cluster']['master']}" do |subconfig|
subconfig.vm.post_up_message = $msg
subconfig.vm.box = k8s['image']

subconfig.vm.hostname = "#{k8s['cluster']['master']}"
subconfig.vm.network :private_network, ip: "#{k8s['ip_part']}.10"

# Hostfile :: Master node
subconfig.vm.provision "master-hostfile", type: "shell" do |mhf|
mhf.inline = <<-SHELL
echo -e "127.0.0.1\t$2" | tee -a /etc/hosts; echo -e "$1\t$2" | tee -a /etc/hosts
SHELL
mhf.args = ["#{k8s['ip_part']}.10", "#{k8s['cluster']['master']}"]
end
# Hostfile :: Worker node
subconfig.vm.provision "Update hostfile and authorized_keys", type: "shell" do |whu|
whu.inline = <<-SHELL
for i in $(eval echo {1..$2}); do
echo -e "${3}.$((10 + $i))\t#{k8s['cluster']['node']}-${i}" | tee -a /etc/hosts
done
SHELL
whu.args = ["#{k8s['user']}", "#{k8s['resources']['node']['count']}", "#{k8s['ip_part']}"]
end

subconfig.vm.provider "virtualbox" do |vb|
vb.memory = k8s['resources']['master']['memory']
vb.cpus = k8s['resources']['master']['cpus']
end

subconfig.vm.provision "Restart VM", type: "shell" do |reboot|
reboot.privileged = true
reboot.inline = <<-SHELL
echo "----------------------------------|| Reboot to load all config"
SHELL
reboot.reboot = true
end

subconfig.vm.provision "#{k8s['cluster']['master']}-setup", type: "shell" do |mns|
mns.path = "script/bootstrap_master.sh"
mns.args = ["#{k8s['user']}", "#{k8s['ip_part']}", "10"]
end

subconfig.trigger.after :up do |trigger_local|
trigger_local.run = {inline: "/bin/bash -c 'vagrant ssh --no-tty -c \"cat /etc/kubernetes/admin.conf\" #{k8s['cluster']['master']} > admin.conf && rm -f \${HOME}/.kube/config 2>/dev/null; mkdir -p \${HOME}/.kube; cp -i admin.conf \${HOME}/.kube/config; rm -f admin.conf'"}
end
end
66 changes: 66 additions & 0 deletions kubernetes/kubeadm/centos/lib/node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
config.vm.define "#{k8s['cluster']['node']}-#{i}" do |subconfig|
subconfig.vm.box = k8s['image']

subconfig.vm.hostname = "#{k8s['cluster']['node']}-#{i}"
subconfig.vm.network :private_network, ip: "#{k8s['ip_part']}.#{i + 10}"

# Hostfile :: Master node
subconfig.vm.provision "master-hostfile", type: "shell" do |s|
s.inline = <<-SHELL
echo -e "$1\t$2" | tee -a /etc/hosts
firewall-cmd --permanent --add-port=10250/tcp
firewall-cmd --permanent --add-port=30000-32767/tcp
firewall-cmd --reload
SHELL
s.args = ["#{k8s['ip_part']}.10", "#{k8s['cluster']['master']}"]
end
# Hostfile :: Worker node
(1..k8s['resources']['node']['count']).each do |j|
if i != j
subconfig.vm.provision "other-worker-hostfile", type: "shell" do |supdate|
supdate.inline = <<-SHELL
echo -e "$1\t$2" | tee -a /etc/hosts
SHELL
supdate.args = ["#{k8s['ip_part']}.#{10 + j}", "#{k8s['cluster']['node']}-#{j}", "#{k8s['user']}", "#{i}"]
end
else
subconfig.vm.provision "self-worker-hostfile", type: "shell" do |supdate|
supdate.inline = <<-SHELL
echo -e "127.0.0.1\t$2" | tee -a /etc/hosts; echo -e "$1\t$2" | tee -a /etc/hosts
SHELL
supdate.args = ["#{k8s['ip_part']}.#{10 + j}", "#{k8s['cluster']['node']}-#{j}", "#{k8s['user']}", "#{i}"]
end
end
end

subconfig.vm.provider "virtualbox" do |vb|
vb.memory = k8s['resources']['node']['memory']
vb.cpus = k8s['resources']['node']['cpus']
end

subconfig.trigger.after :up do |trigger_local|
trigger_local.run = {inline: "/bin/bash -c 'wpub_key=$(vagrant ssh --no-tty -c \"cat /home/#{k8s['user']}/.ssh/id_rsa.pub\" #{k8s['cluster']['node']}-#{i}) && vagrant ssh --no-tty -c \"echo \${wpub_key} >> /home/#{k8s['user']}/.ssh/authorized_keys\" #{k8s['cluster']['master']}; mpub_key=$(vagrant ssh --no-tty -c \"cat /home/#{k8s['user']}/.ssh/id_rsa.pub\" #{k8s['cluster']['master']}) && vagrant ssh --no-tty -c \"echo \${mpub_key} >> /home/#{k8s['user']}/.ssh/authorized_keys\" #{k8s['cluster']['node']}-#{i}'"}
end

subconfig.trigger.after :up do |trigger_remote|
trigger_remote.run_remote = {inline: <<-SHELL
kube_join=\$(echo "ssh #{k8s['user']}@#{k8s['cluster']['master']} -o StrictHostKeyChecking=no '( cat /home/#{k8s['user']}/.bash_profile | grep KUBEADM_JOIN)'" | su - #{k8s['user']})
kube_join=\$(echo ${kube_join} | awk -F'"' '{print \$2}')
echo "sudo $kube_join" | su - #{k8s['user']}
echo "scp -o StrictHostKeyChecking=no #{k8s['user']}@#{k8s['cluster']['master']}:/etc/kubernetes/admin.conf /home/#{k8s['user']}/" | su - #{k8s['user']}
echo "mkdir -p /home/#{k8s['user']}/.kube" | su - #{k8s['user']}
echo "cp -i /home/#{k8s['user']}/admin.conf /home/#{k8s['user']}/.kube/config" | su - #{k8s['user']}
echo "sudo chown #{k8s['user']}:#{k8s['user']} -R /home/#{k8s['user']}/.kube" | su - #{k8s['user']}
echo "kubectl label nodes #{k8s['cluster']['node']}-#{i} kubernetes.io/role=#{k8s['cluster']['node']}-#{i}" | su - #{k8s['user']}
SHELL
}
end

subconfig.vm.provision "Restart VM", type: "shell" do |reboot|
reboot.privileged = true
reboot.inline = <<-SHELL
echo "----------------------------------|| Reboot to load all config"
SHELL
reboot.reboot = true
end
end
4 changes: 4 additions & 0 deletions kubernetes/kubeadm/centos/lib/trigger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
config.vm.provision "vm-setup", type: "shell" do |vms|
vms.path = "script/bootstrap.sh"
vms.args = ["#{k8s['user']}"]
end
Loading

0 comments on commit 6b83bae

Please sign in to comment.