Skip to content

Instantly share code, notes, and snippets.

@johananl
Last active January 13, 2019 16:12
Show Gist options
  • Save johananl/7c98d7c71eeba54603c7189e2f7fb0a6 to your computer and use it in GitHub Desktop.
Save johananl/7c98d7c71eeba54603c7189e2f7fb0a6 to your computer and use it in GitHub Desktop.
Running a local k8s cluster using Vagrant and kubeadm

Requirements

These instructions assume running Vagrant with a libvirt provider.

Install the vagrant-libvirt plugin using the following:

vagrant plugin install vagrant-libvirt

Create Vagrantfile

cat <<EOF > Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.define "master" do |master|
    master.vm.box = "fedora/29-cloud-base"
    master.vm.provider "libvirt" do |p|
      p.cpus = 2
      p.memory = 2048
    end
  end
  config.vm.define "worker1" do |worker1|
    worker1.vm.box = "fedora/29-cloud-base"
    worker1.vm.provider "libvirt" do |p|
      p.cpus = 2
      p.memory = 4096
    end
  end
end
EOF

Prepare master node

vagrant ssh master
# The rest should be done inside the master node

# Configure repository
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kube*
EOF

# Disable SELinux
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

# Install kubelet, kubeadm and kubectl
sudo dnf install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

# Start kubelet
sudo systemctl enable kubelet && sudo systemctl start kubelet

# Install Docker
sudo dnf -y install dnf-plugins-core
sudo dnf config-manager \
    --add-repo \
    https://download.docker.com/linux/fedora/docker-ce.repo
sudo dnf install -y docker-ce

# Start Docker
sudo systemctl enable docker && sudo systemctl start docker

# Add $USER to docker group
sudo usermod -aG docker $USER
logout

NOTE: The kubelet service will not start at this stage. This is expected as the kubelet config file hasn't been generated yet by kubeadm.

Deploy k8s on master node

vagrant ssh master
# The rest should be done inside the master node

# '--pod-network-cidr' is for Calico
sudo kubeadm init --pod-network-cidr=192.168.0.0/16

# Verify k8s is running (master will not be ready until cluster networking is set up)
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get nodes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment