diff --git a/.env b/.env index 858a8a7..a3878f5 100644 --- a/.env +++ b/.env @@ -1,17 +1,8 @@ -BOX_IMAGE="coolsvap/k8s-node" +BOX_IMAGE="generic/ubuntu1804" PROVIDER="virtualbox" -KUBE_VERSION="1.21.1" -NW_PLUGIN="calico" - -SETUP_MASTER=true -MASTER_IP="172.16.26.10" -POD_NW_CIDR="192.168.0.0/16" -MASTER_HOSTNAME="k8s-master" -MASTER_MEMORY="4096" -MASTER_CPU="2" - NODE_MEMORY="2048" NODE_CPU="1" +HOSTNAME="k8s-node" diff --git a/Vagrantfile b/Vagrantfile index fc3e44e..fd18e53 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -8,19 +8,14 @@ Vagrant.configure("2") do |config| l.memory = ENV["NODE_MEMORY"] end + config.vm.synced_folder ".", "/vagrant" config.vm.provision :shell, :path => "install-node.sh" config.hostmanager.enabled = true config.hostmanager.manage_guest = true - # config.vm.network "public_network" + config.vm.provision :file, source: './master.sh', destination: "/opt" - if ENV["SETUP_MASTER"] - config.vm.define ENV["MASTER_HOSTNAME"] do |subconfig| - subconfig.vm.hostname = ENV["MASTER_HOSTNAME"] - subconfig.vm.network :private_network, ip: ENV["MASTER_IP"] - subconfig.vm.provider :virtualbox do |vb| - vb.customize ["modifyvm", :id, "--cpus", ENV["MASTER_CPU"]] - vb.customize ["modifyvm", :id, "--memory", ENV["MASTER_MEMORY"]] - end - end + config.vm.define ENV["HOSTNAME"] do |subconfig| + subconfig.vm.hostname = ENV["HOSTNAME"] end end + diff --git a/install-node.sh b/install-node.sh index e0eaf13..7198188 100644 --- a/install-node.sh +++ b/install-node.sh @@ -64,5 +64,9 @@ systemctl enable kubelet && systemctl start kubelet #Pull images kubeadm config images pull -apt-get clean && apt-get autoremove -yes +apt-get clean && apt-get autoremove -y + +mkdir -p /etc/k8s-scripts +cp /vagrant/master.sh -p /etc/k8s-scripts/ +cp /vagrant/worker.sh -p /etc/k8s-scripts/ cat /dev/null > ~/bash_history && history -c \ No newline at end of file diff --git a/master.sh b/master.sh new file mode 100644 index 0000000..0f542cf --- /dev/null +++ b/master.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# Use this script to initialize master + +kubeadm reset -f +rm -rf ~/.kube /etc/cni/net.d /etc/kubernetes /var/lib/etcd /var/lib/kubelet /var/run/kubernetes /var/lib/cni /opt/cni +iptables -F + +HOST_IP=`/sbin/ifconfig eth1 | egrep -o 'inet [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | cut -d' ' -f2` +ip route add 10.96.0.0/16 dev eth1 src ${HOST_IP} + +### init k8s +kubeadm init --apiserver-advertise-address=${HOST_IP} --kubernetes-version=${KUBE_VERSION} --pod-network-cidr=${POD_NW_CIDR} --skip-token-print + +mkdir -p $HOME/.kube +sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config +sudo chown $(id -u):$(id -g) $HOME/.kube/config + +cp -R $HOME/.kube /vagrant/ + +kubectl taint nodes --all node-role.kubernetes.io/master- +case $NW_PLUGIN in + "weave" ) kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')" ;; + "calico" ) kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml ;; + *) echo "Running default with calico" && kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml ;; +esac +kubeadm token create --print-join-command --ttl 0 > /etc/.vagrantdata/kubeadm-join \ No newline at end of file diff --git a/worker.sh b/worker.sh new file mode 100644 index 0000000..2344a48 --- /dev/null +++ b/worker.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +kubeadm reset -f +rm -rf ~/.kube /etc/cni/net.d /etc/kubernetes /var/lib/etcd /var/lib/kubelet /var/run/kubernetes /var/lib/cni +iptables -F + +HOST_IP=`/sbin/ifconfig eth1 | egrep -o 'inet [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | cut -d' ' -f2` +ip route add 10.96.0.0/16 dev eth1 src ${HOST_IP} + +$(cat /etc/.vagrantdata/kubeadm-join)