Howto: Minikube on Vagrant with Docker

Kubernetes
Vagrant
Howto
Published

November 30, 2024

Modified

February 5, 2025

Vagrant

Note that following example has been tested on Linux using Vagrant Libvirt 1 as provider. Some statements in the VagrantFile are Libvirt specific, and need adjustment in case another provider is used.

Vagrant.configure("2") do |config|

  config.vm.provider  do |libvirt|
    # make sure to have enough resources
    libvirt.memory = 8192
    libvirt.cpus = 10
    libvirt.qemu_use_session = false
  end

  config.vm.define "alma9" do |node|
    config.vm.box = "almalinux/9"
    config.vm.hostname = "alma9"
    config.vm.network "private_network", "192.168.124.200"
    config.vm.synced_folder ".", "/vagrant", "rsync", ".git/"
  end

  # TODO: ...add configurations to install Docker & Minikube

end

In case an HTTP proxy is required reach the Internet, add the following configurations. Note that this reads the HTTP_PROXY environment variables to configure the services in the Vagrant instance:

# Read the HTTP proxy configuration from the environment
http_proxy=ENV['HTTP_PROXY']
https_proxy=ENV['HTTPS_PROXY']

# Content all shells to use an HTTP proxy
etc_environment = %Q{
HTTP_PROXY="#{http_proxy}"
HTTPS_PROXY="#{https_proxy}"
no_proxy="localhost,127.0.0.1,10.96.0.0/12,192.168.59.0/24,192.168.49.0/24,192.168.39.0/24"
}

# Configure Docker to use an HTTP proxy
docker_http_proxy = %Q{
[Service]
Environment="HTTP_PROXY=#{http_proxy}"
Environment="HTTPS_PROXY=#{https_proxy}"
}

config.vm.provision "shell", true , <<-SHELL
  echo "proxy=#{http_proxy}" >> /etc/dnf/dnf.conf
  echo "#{etc_environment}" >> /etc/environment
  mkdir -p /etc/systemd/system/docker.service.d
  echo "#{docker_http_proxy}" > /etc/systemd/system/docker.service.d/http-proxy.conf
SHELL

Install docker2 and minikube3 packages:

config.vm.provision "shell", true , <<-SHELL
  dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  dnf install -y git docker-ce docker-ce-cli containerd.io docker-compose-plugin
  systemctl enable --now docker
  usermod -aG docker vagrant
  dnf install -y https://storage.googleapis.com/minikube/releases/latest/minikube-latest.x86_64.rpm
SHELL

MiniKube

Start the cluster (not logged in as root):

# start the cluster
minikube start

# check cluster state
minikube kubectl -- get po -A

# create in shell alias
sudo tee -a /etc/profile.d/minikube.sh <<EOF
alias kubectl="minikube kubectl --"
EOF
source /etc/profile.d/minikube.sh

Note that will download the appropriate version of kubectl 4

Containers

Load your custom container from local Docker into Minikube5:

# talk directly to the container runtime in the cluster
minikube image load $container_name

# verify that the container images is available to MiniKube
minikube image ls | grep $container_name

# delete a container image
minikube image rm $container

Run a pod from a local image

kubectl run node-echo --image=node-echo --image-pull-policy=Never

Access

kubectl port-forward — Forward one or more local ports to a pod

kubectl port-forward pods/node-echo 8080:80

# send a GET request
curl http://localhost:8080

MiniKube provide several alternatives to access services6

Service

1kubectl expose pod node-echo --type=NodePort --port=8080
# list all services
kubectl get services

# print service IP-address and port
2minikube service node-echo --url

# clean up
kubectl delete service node-ech0
1
kubectl expose — Forward traffic directly to a node port
2
minikube service

Tunnel

1kubectl expose pod node-echo --type=LoadBalancer --port=8080

# in a second console (ctrl-c to stop)
2minikube tunnel

# identify the external IP address 
kubectl get service node-echo
curl $external_ip:8080
1
kubectl expose — Create a service of type=LoadBalancer for a pod
2
minikube tunnel — Simulate a load-balancer with public IP. Creates a route to services deployed with type=LoadBalancer. Routes from the host to the CIDR of the cluster using the cluster’s IP address as a gateway.

MetalLB

# enable the required component
minikube addons enable metallb

# configure the IP address range for the load balancer
minikube addons configure metallb
-- Enter Load Balancer Start IP: 192.168.49.10
-- Enter Load Balancer End IP: 192.168.49.20

kubectl expose pod node-echo --type=LoadBalancer --port=8080
# identify the external IP address 
kubectl get service node-echo
curl $external_ip:8080

MetalLB7 load balancer

  • Requires an address range that matches the network range of MiniKube
  • Check the MiniKube address with minikube ip

Storage

Minikube supports PersistentVolumes8 of type hostPath

apiVersion: v1
kind: PersistentVolume
metadata:
  name: example
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 5Gi
  hostPath:
    path: /data/example/