Last active
November 5, 2019 19:21
-
-
Save svanellewee/bebefbadc1307001c6090ca71cae103e to your computer and use it in GitHub Desktop.
Vagrant files for the kubernetes course
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
function insert-master-on-hosts() { | |
local masterip="${1}" | |
grep -q k8smaster /etc/hosts | |
if [[ $? -ne 0 ]] | |
then | |
echo "Updating hosts" | |
( echo "${masterip} k8smaster"; cat /etc/hosts ) > /tmp/newhosts | |
cp /tmp/newhosts /etc/hosts | |
fi | |
} | |
function make-config() { | |
KUBE_VERSION=1.15.1 | |
#KUBE_VERSION=1.16.0 | |
cat <<- EOF | |
apiVersion: kubeadm.k8s.io/v1beta2 | |
kind: ClusterConfiguration | |
kubernetesVersion: ${KUBE_VERSION} #<-- Use the word stable for newest version | |
controlPlaneEndpoint: "k8smaster:6443" #<-- Use the node alias not the IP | |
networking: | |
podSubnet: 192.168.0.0/16 | |
apiServer: | |
extraArgs: | |
advertise-address: 192.168.2.20 | |
EOF | |
} | |
function import-kubeconfig() { | |
for cur_home in /root /home/vagrant | |
do | |
if [[ ! -d "${cur_home}"/.kube ]] | |
then | |
mkdir -p "${cur_home}"/.kube | |
sudo cp -i /etc/kubernetes/admin.conf "${cur_home}"/.kube/config | |
permissions="$(stat -c "%U:%G" ./ )" | |
sudo chown -R "${permissions}" "${cur_home}"/.kube/ | |
fi | |
done | |
} | |
function install-network() { | |
if [[ ! -f "rbac-kdd.yaml" ]] | |
then | |
curl https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml -O | |
kubectl apply -f rbac-kdd.yaml | |
fi | |
sleep 15 | |
if [[ ! -f "calico.yaml" ]] | |
then | |
#curl -k -vvv https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml| sed --expression 's/192.168.0/192.168.2/g' | sed 's|apps/v1beta1|apps/v1|' | sed 's|extensions/v1beta1|apps/v1|' > calico.yaml | |
curl -k -vvv https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml| sed --expression 's/192.168.0/192.168.2/g' > calico.yaml | |
kubectl apply -f calico.yaml | |
fi | |
} | |
masterip="192.168.2.20" | |
if [[ "${HOSTNAME}" == "alpha" ]] | |
then | |
echo "Found ALPHA!" | |
insert-master-on-hosts "${masterip}" | |
if [[ ! -f "kubeadm-config.yaml" ]] | |
then | |
echo "$(make-config)" > kubeadm-config.yaml | |
kubeadm init --skip-phases preflight --config=kubeadm-config.yaml --upload-certs | tee kubeadm-init.out | |
fi | |
import-kubeconfig | |
install-network | |
kubeadm token create > /var/vm-shared/master-token | |
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //' > /var/vm-shared/discovery-token-ca-cert-hash | |
else | |
insert-master-on-hosts "${masterip}" | |
if [[ ! -d "${HOME}"/.kube ]] | |
then | |
kubeadm join --skip-phases preflight \ | |
--token "$(cat /var/vm-shared/master-token)" \ | |
k8smaster:6443 \ | |
--discovery-token-ca-cert-hash "sha256:$(cat /var/vm-shared/discovery-token-ca-cert-hash)" | |
#--apiserver-advertise-address 192.168.2.20 \ | |
fi | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
function insert-master-on-hosts() { | |
local masterip="${1}" | |
grep -q k8smaster /etc/hosts | |
if [[ $? -ne 0 ]] | |
then | |
echo "Updating hosts" | |
( echo "${masterip} k8smaster"; cat /etc/hosts ) > /tmp/newhosts | |
cp /tmp/newhosts /etc/hosts | |
fi | |
} | |
function make-config() { | |
cat <<- "EOF" | |
apiVersion: kubeadm.k8s.io/v1beta2 | |
kind: ClusterConfiguration | |
kubernetesVersion: 1.15.1 #<-- Use the word stable for newest version | |
controlPlaneEndpoint: "k8smaster:6443" #<-- Use the node alias not the IP | |
networking: | |
podSubnet: 192.168.0.0/16 | |
EOF | |
} | |
function import-kubeconfig() { | |
for cur_home in /root /home/vagrant | |
do | |
if [[ ! -d "${cur_home}"/.kube ]] | |
then | |
mkdir -p "${cur_home}"/.kube | |
sudo cp -i /etc/kubernetes/admin.conf "${cur_home}"/.kube/config | |
permissions="$(stat -c "%U:%G" ./ )" | |
sudo chown -R "${permissions}" "${cur_home}"/.kube/ | |
fi | |
done | |
} | |
function install-network() { | |
if [[ ! -f "rbac-kdd.yaml" ]] | |
then | |
curl https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml -O | |
kubectl apply -f rbac-kdd.yaml | |
fi | |
if [[ ! -f "calico.yaml" ]] | |
then | |
curl -k -vvv https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml -O | |
kubectl apply -f calico.yaml | |
fi | |
} | |
apt-get update | |
apt-get install -y apt-transport-https ca-certificates curl gnupg2 software-properties-common | |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - | |
add-apt-repository -y "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | |
apt-get update && apt-get install -y apt-transport-https curl | |
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - | |
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | tee /etc/apt/sources.list.d/kubernetes.list | |
apt-get update | |
apt-get install -y kubelet=1.15.1-00 kubeadm=1.15.1-00 kubectl=1.15.1-00 | |
apt-get install -y docker-ce docker-ce-cli containerd.io | |
apt-get install -y avahi-daemon libnss-mdns | |
cat <<"EOF" > /etc/docker/daemon.json | |
{ | |
"exec-opts": ["native.cgroupdriver=systemd"], | |
"log-driver": "json-file", | |
"log-opts": { | |
"max-size": "100m" | |
}, | |
"storage-driver": "overlay2" | |
} | |
EOF | |
systemctl restart docker.service | |
apt-mark hold kubelet kubeadm kubectl | |
swapoff -a | |
grep -v swap /etc/fstab > /tmp/fstab | |
cp {/tmp,/etc}/fstab | |
echo "$(date +%s) ${HOSTNAME}" >> /var/vm-shared/tests | |
masterip="10.0.2.10" # hardcoded for now | |
kubeadm config images pull |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode: ruby -*- | |
# vi: set ft=ruby : | |
# All Vagrant configuration is done below. The "2" in Vagrant.configure | |
# configures the configuration version (we support older styles for | |
# backwards compatibility). Please don't change it unless you know what | |
# you're doing. | |
Vagrant.configure("2") do |config| | |
config.vm.define :alpha do |alpha| | |
alpha.vm.box = "kube-basic" | |
alpha.vm.network :private_network, ip: "192.168.2.20" | |
alpha.vm.hostname = "alpha" | |
alpha.vm.provider "virtualbox" do |vb| | |
vb.memory = "4096" | |
vb.cpus = 2 | |
end | |
alpha.vm.synced_folder ".", "/var/vm-shared", create: true | |
end | |
config.vm.define :beta do |beta| | |
beta.vm.box = "kube-basic" | |
beta.vm.network :private_network, ip: "192.168.2.30" | |
beta.vm.hostname = "beta" | |
beta.vm.provider "virtualbox" do |vb| | |
vb.memory = "4096" | |
vb.cpus = 2 | |
end | |
beta.vm.synced_folder ".", "/var/vm-shared", create: true | |
end | |
config.vm.provision "shell", path: "provision.sh" | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Now do this: | |
# vagrant package !!!! | |
# vagrant box add kube-base base/package.box #<----- import ./base/package.box as "kube-base" | |
# | |
Vagrant.configure("2") do |config| | |
config.vm.define :alpha do |alpha| | |
alpha.vm.box = "ubuntu/bionic64" | |
alpha.vm.network :private_network, ip: "10.0.2.10", virtualbox__intnet: true | |
alpha.vm.hostname = "alpha" | |
alpha.vm.provider "virtualbox" do |vb| | |
vb.memory = "4096" | |
vb.cpus = 2 | |
end | |
alpha.vm.synced_folder ".", "/var/vm-shared", create: true | |
end | |
# config.vm.network "forwarded_port", guest:6443, host: 7443, host_ip: "127.0.0.1" | |
config.vm.provision "shell", path: "provision.base.sh" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment