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.
- Install golang and ensure that GOPATH environment is set.
- Install Vagrant and VirtualBox.
- Execute the following commands to add support for file sharing between the host and guest VM.
$ vagrant plugin install vagrant-vbguest
- Clone the kubernetes repository.
$ cd ${GOPATH}/src/k8s.io
$ git clone git@github.com:kubernetes/kubernetes.git
- 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
- Execute the following command to start the VM.
$ vagrant up kubernetes
- 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
- 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
- You can open the kubernetes source code in Visual Studio Code, install required required go tools. Make code changes and repeat step 5.