Codementor Events

Building and Running Kubernetes from source

Published Mar 11, 2019
Building and Running Kubernetes from source

Running and executing kubernetes locally on non-linux platform would be impossible without using some form of virtualization for e.g VirtualBox, or Vsphere. But setting up a VM on your Mac or Windows is not simple either. Luckily Vagrant makes things a lot simpler. This is particularly helpful if your are looking to make open source contributions to Kubernetes.

In this post we will explore how to build Kubernetes locally and run, using VirtualBox and Vagrant.

  1. Install golang and ensure that GOPATH environment is set.
  2. Install Vagrant and VirtualBox.
  3. Execute the following commands to add support for file sharing between the host and guest VM.
$ vagrant plugin install vagrant-vbguest
  1. Clone the kubernetes repository.
$ cd ${GOPATH}/src/k8s.io
$ git clone git@github.com:kubernetes/kubernetes.git
  1. Create a Vagrantfile with the following content. This file will enable vagrant to create a centos7 VM and install golang, etcd, docker and share the Kubernetes source directory between the host and guest VM.
VM_USER = 'vagrant'
HOST_GOPATH = ENV['GOPATH']
HOST_KUBERNETES_DIR = HOST_GOPATH + '/src/k8s.io/kubernetes'

$install_go = <<-SCRIPT
    curl -fsSLo go.tar.gz "https://dl.google.com/go/go1.11.5.linux-amd64.tar.gz"
    tar -C /usr/local -zxvf go.tar.gz
    echo 'export PATH=${PATH}:/usr/local/go/bin' >> /home/${1}/.bash_profile
    echo "export GOPATH=/home/${1}/go" >> /home/${1}/.bash_profile
SCRIPT

$install_docker = <<-SCRIPT
    yum install -y yum-utils device-mapper-persistent-data lvm2
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    yum install -y docker-ce docker-ce-cli containerd.io
    usermod -a -G docker ${1}
    systemctl start docker.service
SCRIPT

$install_etcd = <<-SCRIPT
    curl -fsSL -o etcd.tar.gz --retry 3 --keepalive-time 2 https://github.com/coreos/etcd/releases/download/v3.3.10/etcd-v3.3.10-linux-amd64.tar.gz
    tar -C /usr/local/bin -zxvf etcd.tar.gz --strip-components=1
SCRIPT

Vagrant.configure("2") do |config|
    config.vm.box = 'centos/7'
    config.vm.hostname = 'kubernetes'
    config.vm.define 'kubernetes'
    config.vm.provider "virtualbox" do |v|  
        v.name = 'kubernetes'
        v.memory = 2048
        v.cpus = 2
    end
    config.vm.synced_folder HOST_KUBERNETES_DIR, '/home/'+VM_USER+'/go/src/k8s.io/kubernetes', create: true, group: VM_USER, owner: VM_USER
    config.vm.provision "shell" do |s|
        s.inline = $install_go
        s.args = [VM_USER]
    end
    config.vm.provision "shell" do |s|
        s.inline = $install_docker
        s.args = [VM_USER]
    end
    config.vm.provision "shell" do |s|
        s.inline = $install_etcd
    end
    config.ssh.username = VM_USER
end

  1. Execute the following command to start the VM.
$ vagrant up kubernetes
  1. To build and start kubernetes inside the VM.
$ vagrant ssh kubernetes
[vagrant@kubernetes ~]$ cd ${GOPATH}/src/k8s.io/kubernetes
[vagrant@kubernetes ~]$ bash hack/local-up-cluster.sh
  1. Open another terminal into the VM to launch a nginx deployment on kubernetes.
$ vagrant ssh kubernetes
[vagrant@kubernetes ~]$ cd ${GOPATH}/src/k8s.io/kubernetes
[vagrant@kubernetes ~]$ export KUBECONFIG=/var/run/kubernetes/admin.kubeconfig
[vagrant@kubernetes ~]$ cluster/kubectl.sh create deployment nginx --image=nginx
  1. You can open the kubernetes source code in Visual Studio Code, install required required go tools. Make code changes and repeat step 5.
Discover and read more posts from Chandan Madhesia
get started
post commentsBe the first to share your opinion
Show more replies