Skip to content

Instantly share code, notes, and snippets.

@nicosingh
Last active August 8, 2018 15:00
Show Gist options
  • Save nicosingh/e30ec58036743eb1deda3fa3fa2738ce to your computer and use it in GitHub Desktop.
Save nicosingh/e30ec58036743eb1deda3fa3fa2738ce to your computer and use it in GitHub Desktop.
K8S cluster using Vagrant
.vagrant/
tmp/
  • TL;DR (mkdir -p tmp, vagrant up, export, kubectl proxy)
  • default configuration (number of nodes, ip addresses)
  • requirements (vagrant, vbox, kubectl)
  • instructions (vagrant up, export, kubectl get nodes, kubectl get pods, kubectl proxy, token, UI)
  • TODO: deploy a service
#!/bin/bash
# install kubernetes
echo "installing kubernetes..."
apt-get update && apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet kubeadm kubectl
# kubelet requires swap off
swapoff -a
# get the IP address that VirtualBox has given this VM
IPADDR=`ifconfig enp0s8 | grep Mask | awk '{print $2}'| cut -f2 -d:`
echo $IPADDR > /tmp/shared/k8s-master-ip-address
# set up Kubernetes
echo "initializing kubernetes..."
NODENAME=$(hostname -s)
kubeadm init --apiserver-cert-extra-sans=$IPADDR --node-name $NODENAME --apiserver-advertise-address=$IPADDR --pod-network-cidr "10.244.0.0/16"
# set up admin creds for the vagrant user
echo "setting up kubernetes configuration files..."
sudo --user=vagrant mkdir -p /home/vagrant/.kube
cp -r /etc/kubernetes/admin.conf /home/vagrant/.kube/config
cp -r /etc/kubernetes/admin.conf /tmp/shared/k8s-master.conf
chown $(id -u vagrant):$(id -g vagrant) /home/vagrant/.kube/config
# set up kubernetes networking
echo "installing network driver..."
su - vagrant -c "sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml"
# create token for minions
echo "creating kubernetes token..."
su - vagrant -c "sudo kubeadm token create > /tmp/shared/k8s-master-token"
su - vagrant -c "openssl x509 -in /etc/kubernetes/pki/ca.crt -noout -pubkey | openssl rsa -pubin -outform DER 2>/dev/null | sha256sum | cut -d' ' -f1 > /tmp/shared/k8s-master-token-cert-hash"
# set up admin UI
echo "setting up admin UI..."
su - vagrant -c "sudo kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml"
# set up admin user
su - vagrant -c "sudo kubectl create -f https://raw.githubusercontent.com/tedsluis/kubernetes-via-kubeadm/99b021fd784316eed91a2e127254e4e382b6d792/admin-user.yaml"
su - vagrant -c "sudo kubectl create -f https://raw.githubusercontent.com/tedsluis/kubernetes-via-kubeadm/99b021fd784316eed91a2e127254e4e382b6d792/admin-user-clusterrolebinding.yaml"
su - vagrant -c "sudo kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}') | grep 'token: ' > /tmp/shared/admin-user-token"
echo "POST-INSTALLATION STEPS:"
echo "-----------------------"
echo "Run this command to connect kubectl with our new server:"
echo " export KUBECONFIG=`pwd`/tmp/k8s-master.conf"
echo "and then test it getting the k8s nodes list:"
echo " kubectl get nodes"
echo "To go to the Admin UI, copy the admin-user token stored at tmp/admin-user-token in your host. And then, run the command to proxy the UI to your host:"
echo " kubectl proxy"
echo "to finally browse: http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/"
echo ""
# set up sample app
su - vagrant -c "sudo kubectl apply -f https://raw.githubusercontent.com/containous/traefik/master/examples/k8s/traefik-rbac.yaml"
su - vagrant -c "sudo kubectl apply -f https://raw.githubusercontent.com/containous/traefik/master/examples/k8s/traefik-ds.yaml"
su - vagrant -c "sudo kubectl apply -f https://raw.githubusercontent.com/containous/traefik/master/examples/k8s/ui.yaml"
su - vagrant -c "sudo kubectl apply -f https://raw.githubusercontent.com/containous/traefik/master/examples/k8s/cheese-deployments.yaml"
su - vagrant -c "sudo kubectl apply -f https://raw.githubusercontent.com/containous/traefik/master/examples/k8s/cheese-services.yaml"
su - vagrant -c "sudo kubectl apply -f https://raw.githubusercontent.com/containous/traefik/master/examples/k8s/cheese-ingress.yaml"
su - vagrant -c "sudo kubectl apply -f https://raw.githubusercontent.com/containous/traefik/master/examples/k8s/cheeses-ingress.yaml"
#!/bin/bash
# install kubernetes
echo "installing kubernetes..."
apt-get update && apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet kubeadm kubectl
# kubelet requires swap off
swapoff -a
# join kubernetes master
echo "joining kubernetes cluster..."
K8S_IPADDR=$(cat /tmp/shared/k8s-master-ip-address)
K8S_TOKEN=$(cat /tmp/shared/k8s-master-token)
K8S_TOKEN_HASH=$(cat /tmp/shared/k8s-master-token-cert-hash)
kubeadm join $K8S_IPADDR:6443 --token $K8S_TOKEN --discovery-token-ca-cert-hash sha256:$K8S_TOKEN_HASH
# number of nodes (minions)
WORKER_NODES=2
Vagrant.configure("2") do |config|
config.vm.define "k8s-master" do |master|
master.vm.box = "bento/ubuntu-16.04"
master.vm.network "private_network", ip: "192.168.50.9"
master.vm.hostname = "k8s-master"
master.vm.provision "docker"
master.vm.provision "shell", path: "provision-master.sh"
master.vm.synced_folder "tmp/", "/tmp/shared"
master.vm.network "forwarded_port", guest: 8001, host: 8001
master.vm.network "forwarded_port", guest: 80, host: 8002
end
(1..WORKER_NODES).each do |i|
config.vm.define "k8s-node-#{i}" do |node|
node.vm.box = "bento/ubuntu-16.04"
node.vm.network "private_network", ip: "192.168.50.1#{i}"
node.vm.hostname = "k8s-node-#{i}"
node.vm.provision "docker"
node.vm.provision "shell", path: "provision-node.sh"
node.vm.synced_folder "tmp/", "/tmp/shared"
node.vm.network "forwarded_port", guest: 80, host: "80#{Random.rand(03..99)}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment