thanhpk
11/6/2017 - 4:32 AM

Vagranfile to run kubenetes locally

Vagranfile to run kubenetes locally

Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/xenial64"
  config.vm.network "public_network"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "2048"
  end

  config.vm.hostname = "kube"
  
  config.vm.provision "shell", inline: <<-SHELL
    # install docker
    apt-get update && apt-get install -y curl apt-transport-https
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
    echo "deb https://download.docker.com/linux/$(lsb_release -si | tr '[:upper:]' '[:lower:]') $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list
    apt-get update && apt-get install -y docker-ce=$(apt-cache madison docker-ce | grep 17.03 | head -1 | awk '{print $3}')

    service docker stop
    sed -i 's#ExecStart=/usr/bin/dockerd -H fd://#ExecStart=/usr/bin/dockerd -H 0.0.0.0:2376 -H fd://#' /lib/systemd/system/docker.service
    systemctl daemon-reload
    service docker start

    # install kubeadm
    curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
    echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list
    apt-get update && apt-get install -y kubelet kubeadm kubectl htop httpie avahi-daemon

    lanip=`ifconfig | awk -F "[: ]+" '/inet addr:/ { if ($4 != "127.0.0.1") print $4 }' | grep 192.168.5`
    # run kubeadm 
    kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=$lanip --kubernetes-version stable-1.8

    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
		
    kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.9.0/Documentation/kube-flannel.yml

    
    # allow deploy on master node (kube is name of the master node in kubectl get nodes)
    kubectl taint nodes kube node-role.kubernetes.io/master:NoSchedule-
    iptables -P FORWARD ACCEPT
  SHELL
end