Skip to content

Commit

Permalink
Merge pull request openebs#1238 from yudaykiran/Fix-for-Issue#1043
Browse files Browse the repository at this point in the history
Fix-for-Issue#1043: CentOS-based test bed for kubernetes cluster
  • Loading branch information
kmova authored Feb 27, 2018
2 parents d8152ac + 502db1a commit 77de55e
Show file tree
Hide file tree
Showing 14 changed files with 738 additions and 271 deletions.
60 changes: 60 additions & 0 deletions k8s/lib/scripts/configure_k8s_cni.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash
kubeversion=`kubectl version --short | grep 'Server Version' | awk {'print $3'}`
kuberegex='^v1[.][0-8][.][0-9][0-9]?$'

function patch_kube_proxy(){
kubectl -n kube-system get ds -l 'k8s-app=kube-proxy' -o json \
| jq '.items[0].spec.template.spec.containers[0].command |= .+ ["--proxy-mode=userspace"]' \
| kubectl apply -f - \
&& kubectl -n kube-system delete pods -l 'k8s-app=kube-proxy'
}

function setup_k8s_weave() {
kubectl apply -f $HOME/setup/cni/weave/weave-daemonset-k8s-1.6.yaml

if [[ $? -ne 0 ]]; then

kubectl delete -f $HOME/setup/cni/weave/weave-daemonset-k8s-1.6.yaml
export kubever=$(kubectl version | base64 | tr -d '\n')
kubectl apply -f https://cloud.weave.works/k8s/net?k8s-version=$kubever

if [[ $? -ne 0 ]]; then
"Unable to apply the Pod Network. SSH into the master and apply a Pod Network for your Cluster."
fi

fi
}

function setup_k8s_flannel() {

cat $HOME/setup/cni/flannel/kube-flannel.yml

kubectl apply -f $HOME/setup/cni/flannel/kube-flannel.yml

if [[ $? -ne 0 ]]; then

kubectl delete -f $HOME/setup/cni/flannel/kube-flannel.yml
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml

if [[ $? -ne 0 ]]; then
echo "Unable to apply the Pod Network. SSH into the master and apply a Pod Network for your Cluster."
fi

fi

}

#Patching kube-proxy to run with --proxy-mode=userspace
echo Patching the kube-proxy for CNI Networks...
patch_kube_proxy

[[ $kubeversion =~ $kuberegex ]]

if [[ $? -eq 1 ]]; then
echo Configure Pod Network with flannel
setup_k8s_flannel
else
echo Configure Pod Network with Weave
setup_k8s_weave
fi

70 changes: 50 additions & 20 deletions k8s/lib/scripts/configure_k8s_host.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
# Variables:
machineip=
masterip=
clusterip=
tokensha=
token=
hostname=`hostname`
kubeversion=`sudo kubeadm version -o short`
kuberegex='^v1[.][0-7][.][0-9][0-9]?$'

function get_machine_ip(){
ifconfig \
| grep -oP "inet addr:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" \
ip addr show \
| grep -oP "inet \\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" \
| grep -oP "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | sort \
| tail -n 1 | head -n 1
}
Expand All @@ -20,22 +22,46 @@ function update_hosts(){
}

function setup_k8s_minion(){
sudo kubeadm join --token=$token ${masterip}:6443

[[ $kubeversion =~ $kuberegex ]]
# For versions 1.8 and above, discovery token SHA is necessary to join the master
if [[ $? -eq 1 ]]; then
echo Setting up the Minion using Discovery Token SHA: $tokensha

sudo iptables -F && sudo iptables -t nat -F && sudo iptables -t mangle -F && sudo iptables -X

sudo kubeadm join --token=$token ${masterip}:6443 --discovery-token-ca-cert-hash sha256:${tokensha}
else

sudo iptables -F && sudo iptables -t nat -F && sudo iptables -t mangle -F && sudo iptables -X

sudo kubeadm join --token=$token ${masterip}:6443
fi

}

function join_cni_network(){
sudo route add $clusterip gw $masterip
function disable_swap()
{
sudo swapoff -a

sudo sed -i.bak '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

cat <<EOF | sudo tee -a /etc/systemd/system/kubelet.service.d/90-local-extras.conf > /dev/null
Environment="KUBELET_EXTRA_ARGS=--fail-swap-on=false"
EOF
sudo systemctl daemon-reload
sudo systemctl restart kubelet
}

function show_help() {
cat << EOF
Usage : $(basename "$0") --masterip=<KubeMaster IP> --token=<Token> --clusterip=<Cluster IP>
Usage : $(basename "$0") --masterip=<KubeMaster IP> --token=<Token> --token-sha=<Discovery-Token SHA>
Create a Kubernetes Minion Node and Join the cluster.
-h|--help Display this help and exit.
-i|--masterip Kubemaster IP IP of kubemaster to join the cluster.
-t|--token Token Token generated by kubeadm init.
-c|--clusterip Cluster IP ClusterIP of kubernetes to join CNI network.
-s|--token-sha SHA Discovery Token SHA for the Cluster.
EOF
}

Expand Down Expand Up @@ -102,10 +128,10 @@ while :; do
exit 1
;;

-c|--clusterip) # Takes an option argument,
-s|--token-sha) # Takes an option argument,
# ensuring it has been specified.
if [ -n "$2" ]; then
clusterip=$2
tokensha=$2
shift
else
printf 'ERROR: "--clusterip" requires \
Expand All @@ -114,13 +140,13 @@ while :; do
fi
;;

--clusterip=?*) # Delete everything up to "="
--token-sha=?*) # Delete everything up to "="
# and assign the remainder.
clusterip=${1#*=}
tokensha=${1#*=}
;;

--clusterip=) # Handle the case of an empty --clusterip=
printf 'ERROR: "--clusterip" requires \
--token-sha=) # Handle the case of an empty --token-sha=
printf 'ERROR: "--token-sha" requires \
a non-empty option argument.\n' >&2
exit 1
;;
Expand Down Expand Up @@ -153,8 +179,8 @@ if [ -z "$token" ]; then
exit
fi

if [ -z "$clusterip" ]; then
echo "ClusterIP is mandatory."
if [ -z "$tokensha" ]; then
echo "Discovery Token SHA is mandatory."
show_help
exit
fi
Expand All @@ -166,11 +192,15 @@ machineip=`get_machine_ip`
echo Updating the host files...
update_hosts

[[ $kubeversion =~ $kuberegex ]]
# For versions 1.8 and above, swap needs to be disabled
if [[ $? -eq 1 ]]; then
#Disable swap for Kubernetes 1.8 and above
echo Disable swap
disable_swap
fi

#Join the cluster
echo Setting up the Minion using IPAddress: $machineip
echo Setting up the Minion using Token: $token
setup_k8s_minion

#Add route to the minion ip to the cluster ip
echo Joining the CNI Network...
join_cni_network
48 changes: 44 additions & 4 deletions k8s/lib/scripts/configure_k8s_master.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,55 @@
machineip=
hostname=`hostname`
kubeversion="v1.7.5"
kuberegex='^v1.[0-7].[0-9][0-9]?$'
kubecniregex='^v1[.][0-8][.][0-9][0-9]?$'

function get_machine_ip(){
ifconfig | \
grep -oP "inet addr:\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" \
ip addr show | \
grep -oP "inet \\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" \
| grep -oP "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | sort |\
tail -n 1 | head -n 1
}

function setup_k8s_master() {
sudo kubeadm init --apiserver-advertise-address=$machineip \
--kubernetes-version=$kubeversion

# HEPTIO Pro Tip
# Flush iptables for any residue left behind by kubeadm reset
sudo iptables -F && sudo iptables -t nat -F && sudo iptables -t mangle -F && sudo iptables -X

# Releases the port 10251, which causes the pre-flight checks to fail.
# Kubeadm init, will start the kubelet if it is not running.
sudo systemctl stop kubelet

[[ $kubeversion =~ $kubecniregex ]]

if [[ $? -eq 1 ]]; then
# Use Flannel Pod Network for now for version 1.9.0 and above
sudo kubeadm init --apiserver-advertise-address=$machineip \
--kubernetes-version=$kubeversion --pod-network-cidr=10.244.0.0/16
else
sudo kubeadm init --apiserver-advertise-address=$machineip \
--kubernetes-version=$kubeversion
fi
}

function update_hosts(){
sudo sed -i "/$hostname/ s/.*/$machineip\t$hostname/g" /etc/hosts
}

function disable_swap()
{
sudo swapoff -a

sudo sed -i.bak '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

cat <<EOF | sudo tee -a /etc/systemd/system/kubelet.service.d/90-local-extras.conf > /dev/null
Environment="KUBELET_EXTRA_ARGS=--fail-swap-on=false"
EOF
sudo systemctl daemon-reload
sudo systemctl restart kubelet
}

#Code
#Get the ip of the machine
machineip=`get_machine_ip`
Expand All @@ -29,6 +61,14 @@ machineip=`get_machine_ip`
echo Updating the host file...
update_hosts

[[ $kubeversion =~ $kuberegex ]]
# For versions 1.8 and above, swap needs to be disabled
if [[ $? -eq 1 ]]; then
#Disable swap for Kubernetes 1.8 and above
echo Disable swap
disable_swap
fi

#Create the Cluster
echo Setting up the Master using IPAddress: $machineip
setup_k8s_master
21 changes: 0 additions & 21 deletions k8s/lib/scripts/configure_k8s_weave.sh

This file was deleted.

45 changes: 35 additions & 10 deletions k8s/lib/vagrant/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,30 @@ box_Mode=ENV['OPENEBS_BUILD_BOX'] || 2

kube_version=ENV['KUBE_VERSION'] || "1.7.5"

distro=ENV['DISTRIBUTION'] || "ubuntu"

docker=ENV['DOCKER'] || "docker-cs"

required_plugins = %w(vagrant-vbguest)

required_plugins.each do |plugin|
need_restart = false
unless Vagrant.has_plugin? plugin
system "vagrant plugin install #{plugin}"
need_restart = true
end
exec "vagrant #{ARGV.join(' ')}" if need_restart
end

Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.

if Vagrant.has_plugin?("vagrant-vbguest") then
config.vbguest.auto_update = true
end

if ((box_Mode.to_i < BOX_MODE_OPENEBS.to_i) || \
(box_Mode.to_i > BOX_MODE_KUBERNETES.to_i))
puts "Invalid value set for OPENEBS_BUILD_BOX."
Expand All @@ -42,13 +61,15 @@ Vagrant.configure("2") do |config|

# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "ubuntu/xenial64"

if(distro == "ubuntu")
config.vm.box = "ubuntu/xenial64"
else
config.vm.box = "centos/7"
end
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
config.vm.box_check_update = false

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
Expand Down Expand Up @@ -102,11 +123,12 @@ Vagrant.configure("2") do |config|

config.vm.provision :shell,
path: "boxes/k8s/prepare_k8s.sh",
:args => "#{distro}",
privileged: true

config.vm.provision :shell,
path: "boxes/k8s/fetch_kubeadm.sh",
:args => "#{kube_version}",
:args => "#{kube_version} #{distro} #{docker}",
privileged: true

config.vm.provision :shell,
Expand All @@ -116,19 +138,22 @@ Vagrant.configure("2") do |config|

config.vm.provision :shell,
path: "boxes/k8s-dashboard/fetch_dashboard.sh",
privileged: true

:args => "#{distro}",
privileged: true

config.vm.provision :shell,
path: "boxes/k8s-weave/fetch_weave.sh",
:args => "#{distro}",
privileged: true

config.vm.provision :shell,
path: "boxes/ubuntu-xenial/prepare_network.sh",
path: "boxes/k8s-flannel/fetch_flannel.sh",
:args => "#{distro}",
privileged: true

config.vm.provision :shell,
path: "boxes/k8s/cleanup_k8s.sh",
:args => "#{kube_version}",
:args => "#{kube_version} #{distro}",
privileged: true

elsif box_Mode.to_i == BOX_MODE_OPENEBS.to_i
Expand Down
10 changes: 8 additions & 2 deletions k8s/lib/vagrant/boxes/k8s-dashboard/fetch_dashboard.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#!/bin/bash
distribution=${1:-"ubuntu"}

sudo docker pull gcr.io/google_containers/kubernetes-dashboard-amd64:v1.6.3

mkdir -p /home/ubuntu/setup/dashboard
cp /vagrant/boxes/k8s-dashboard/external/kubernetes-dashboard-1.6.3.yaml /home/ubuntu/setup/dashboard/
if [ "$distribution" = "ubuntu" ]; then
mkdir -p /home/ubuntu/setup/dashboard
cp /vagrant/boxes/k8s-dashboard/external/kubernetes-dashboard-1.6.3.yaml /home/ubuntu/setup/dashboard/
else
mkdir -p /home/vagrant/setup/dashboard
cp /vagrant/boxes/k8s-dashboard/external/kubernetes-dashboard-1.6.3.yaml /home/vagrant/setup/dashboard/
fi
Loading

0 comments on commit 77de55e

Please sign in to comment.