Skip to content

Commit

Permalink
indentation update
Browse files Browse the repository at this point in the history
  • Loading branch information
marthanda93 committed Apr 24, 2021
1 parent 8669d10 commit 359db44
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 125 deletions.
239 changes: 118 additions & 121 deletions kubernetes/centos/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,127 +17,124 @@ 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"
# subconfig.vm.network :forwarded_port, guest: 80, host: 8080, auto_correct: true

# 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 = "2048"
vb.cpus = 2
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
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

(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 = "2048"
vb.cpus = 2
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']}
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
(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']}
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
end

config.vm.provision "vm-setup", type: "shell" do |vms|
vms.path = "script/bootstrap.sh"
vms.args = ["#{k8s['user']}"]
end
end

config.vm.provision "vm-setup", type: "shell" do |vms|
vms.path = "script/bootstrap.sh"
vms.args = ["#{k8s['user']}"]
end
end
7 changes: 3 additions & 4 deletions kubernetes/centos/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
api_version: "2"
image: "centos/8"
ip_part: "192.160.0"
# node_count: 2
user: "vagrant"

cluster:
Expand All @@ -11,12 +10,12 @@ cluster:

resources:
master:
cpus: 1
memory: 1024
cpus: 2
memory: 2048
node:
count: 2
cpus: 2
memory: 2048
count: 2

net:
network_type: private_network

0 comments on commit 359db44

Please sign in to comment.