Howto: Minikube on Vagrant with Docker
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|
.vm.provider :libvirt do |libvirt|
config# make sure to have enough resources
.memory = 8192
libvirt.cpus = 10
libvirt.qemu_use_session = false
libvirtend
.vm.define "alma9" do |node|
config.vm.box = "almalinux/9"
config.vm.hostname = "alma9"
config.vm.network "private_network", ip: "192.168.124.200"
config.vm.synced_folder ".", "/vagrant", type: "rsync", rsync__exclude: ".git/"
configend
# 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
=ENV['HTTP_PROXY']
http_proxy=ENV['HTTPS_PROXY']
https_proxy
# Content all shells to use an HTTP proxy
= %Q{
etc_environment 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
= %Q{
docker_http_proxy [Service]
Environment="HTTP_PROXY=#{http_proxy}"
Environment="HTTPS_PROXY=#{https_proxy}"
}
.vm.provision "shell", privileged: true , inline: <<-SHELL
config 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 docker
2 and minikube
3 packages:
.vm.provision "shell", privileged: true , inline: <<-SHELL
config 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 oftype=LoadBalancer
for a pod - 2
-
minikube tunnel
— Simulate a load-balancer with public IP. Creates a route to services deployed withtype=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/
Footnotes
Vagrant Libvirt Documentation
https://vagrant-libvirt.github.io/vagrant-libvirt↩︎Docker Documentation
https://docs.docker.com↩︎Minikube, Kubernetes Project
https://minikube.sigs.k8s.io/docs/↩︎kubectl
, Kubernetes Documentation
https://kubernetes.io/docs/tasks/tools/↩︎Pushing Images, Minikube Documentation
https://minikube.sigs.k8s.io/docs/handbook/pushing/
https://minikube.sigs.k8s.io/docs/commands/image/↩︎Accessing Apps, MiniKube Documentation
https://minikube.sigs.k8s.io/docs/handbook/accessing↩︎MetalLB Documentation
https://metallb.io↩︎Persistant Volumes, MiniKube Documentation
https://minikube.sigs.k8s.io/docs/handbook/persistent_volumes↩︎