How to launch a local Kubernetes With Vagrant

3/5/2020

As a developer, I want to launch a local Kubernetes development environment automatically using kubeadm. I expect to create my environment running "vagrant up", and after that I must be able to login into the Vagrant VM and run kubectl commands.

I want to include a simple Python script that list all the running pods in all the namespaces. Call this script at the end of "vagrant up or provision" process to validate Kubernetes installation.

Finally I want to upload the scripts to GitHub.

-- Carlos Javier A. Helguero
kubeadm
kubernetes
namespaces
python
vagrant

1 Answer

3/6/2020

What i have is a bootstrap script called from my Vagrantfile, for example:

config.vm.provision :shell, path:./bootstrap.sh, privileged: false, :args => arguments_for_bootstrap

In such bootstrap script i install the software necessary to run my kubernetes stuff. As this is a vagrant machine, i would suggest standalone minikube "cluster". If using docker technology for containers, i would install, for example docker, kubectl and minikube, and probably, some more things like helm orchestrator, etc.

For example:

### DOCKER ###
sudo apt-get update
# Install packages to allow apt to use a repository over HTTPS
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
# Add Dockers official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Set up the stable repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# Update
sudo apt-get update
# Install docker-ce
sudo apt-get install -y docker-ce=18.06.3~ce~3-0~ubuntu
# Add user to group 'docker'
sudo usermod -aG docker $USER

### KUBECTL ###
curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v1.14.8/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/

### MINIKUBE ###
curl -Lo minikube https://storage.googleapis.com/minikube/releases/v1.4.0/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
# Start
sudo minikube start --vm-driver=none
# Configure permissions
[ -d /root/.kube ] && sudo mv /root/.kube $HOME/.kube # this will write over any previous configuration
sudo chown -R $USER $HOME/.kube
sudo chgrp -R $USER $HOME/.kube
[ -d /root/.minikube ] && sudo mv /root/.minikube $HOME/.minikube # this will write over any previous configuration
sudo chown -R $USER $HOME/.minikube
sudo chgrp -R $USER $HOME/.minikube

Finally, i would add provision scripts to do whatever checking you need. If you prefer python script, you could use kubectl api for python (i.e. https://github.com/kubernetes-client/python), but i suggest directly bash scripting:

kubectl get pod --all-namespaces 

The best way to afford tasks on provision, is using scriptlets which you can enable/disable just by giving +x permission:

    @files = Dir.glob(provision.d + "/*").sort
    @files.each do |file|
      next if file == '.' or file == '..'
      next if not File.stat(file).executable?
      config.vm.provision :shell, path:file, privileged: false
    end

Then, you would have locally a bunch of scriptlets to do things in specific order:

provision.d/K01_helm_install_my_app
...
provision.d/K30_check_k8s_health

Regarding your third question, i assume that having a github account, all the scripts necessary to recreate your development environment would be your Vagrantfile, bootstrap.sh script, and provision.d directory with scriptlets inside to do all the steps you consider to do on vagrant up.

-- eramos
Source: StackOverflow