Last active
February 26, 2023 02:20
-
-
Save iul1an/37e5bcafd4bf9ddb85188fd0189414fb to your computer and use it in GitHub Desktop.
Minikube using KVM2 driver on Ubuntu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
cat <<'EOF'> /tmp/minikube-br0.xml | |
<network> | |
<name>minikube</name> | |
<uuid>ec47d11f-87ba-4231-b38d-0efedd392b84</uuid> | |
<forward mode='nat'> | |
<nat> | |
<port start='1024' end='65535'/> | |
</nat> | |
</forward> | |
<bridge name='minikube-br0' stp='on' delay='0'/> | |
<mac address='52:54:00:ca:f9:6a'/> | |
<dns enable='no'/> | |
<ip address='100.99.1.1' netmask='255.255.255.0'> | |
<dhcp> | |
<range start='100.99.1.2' end='100.99.1.99'/> | |
</dhcp> | |
</ip> | |
</network> | |
EOF | |
virsh net-define --file /tmp/minikube-br0.xml | |
virsh net-start --network minikube | |
virsh net-autostart --network minikube | |
minikube start --network minikube --kvm-network minikube | |
rm -f /tmp/minikube-br0.xml | |
if [[ ! -f /etc/libvirt/hooks/qemu ]]; then | |
sudo bash -c 'cat <<"EOF" > /etc/libvirt/hooks/qemu | |
#!/bin/bash | |
# IMPORTANT: Change the "VM NAME" string to match your actual VM Name. | |
# In order to create rules to other VMs, just duplicate the below block and configure | |
# it accordingly. | |
if [ "${1}" = "minikube" ]; then | |
# Update the following variables to fit your setup | |
LAN_CIDR="172.16.31.0/24" | |
VM_NET_CIDR="100.99.1.0/24" | |
INT_IN="eth0" | |
INT_OUT="minikube-br0" | |
if [ "${2}" = "stopped" ] || [ "${2}" = "reconnect" ]; then | |
/sbin/iptables -D LIBVIRT_FWI -i $INT_IN -o $INT_OUT -s $LAN_CIDR -d $VM_NET_CIDR -j ACCEPT -m comment --comment "Allow LAN to access Minikube network" | |
fi | |
if [ "${2}" = "start" ] || [ "${2}" = "reconnect" ]; then | |
/sbin/iptables -I LIBVIRT_FWI -i $INT_IN -o $INT_OUT -s $LAN_CIDR -d $VM_NET_CIDR -j ACCEPT -m comment --comment "Allow LAN to access Minikube network" | |
fi | |
fi | |
EOF' | |
sudo chmod +x /etc/libvirt/hooks/qemu | |
else | |
sudo bash -c 'cat <<"EOF" >> /etc/libvirt/hooks/qemu | |
if [ "${1}" = "minikube" ]; then | |
# Update the following variables to fit your setup | |
LAN_CIDR="172.16.31.0/24" | |
VM_NET_CIDR="100.99.1.0/24" | |
INT_IN="eth0" | |
INT_OUT="minikube-br0" | |
if [ "${2}" = "stopped" ] || [ "${2}" = "reconnect" ]; then | |
/sbin/iptables -D LIBVIRT_FWI -i $INT_IN -o $INT_OUT -s $LAN_CIDR -d $VM_NET_CIDR -j ACCEPT -m comment --comment "Allow LAN to access Minikube network" | |
fi | |
if [ "${2}" = "start" ] || [ "${2}" = "reconnect" ]; then | |
/sbin/iptables -I LIBVIRT_FWI -i $INT_IN -o $INT_OUT -s $LAN_CIDR -d $VM_NET_CIDR -j ACCEPT -m comment --comment "Allow LAN to access Minikube network" | |
fi | |
fi | |
EOF' | |
fi | |
sudo systemctl restart libvirtd.service | |
minikube ip | |
function kubectl () { | |
if which kubectl &>/dev/null; then | |
kubectl "$@" | |
else | |
minikube kubectl -- "$@" | |
fi | |
} | |
# install metallb | |
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.13.7/config/manifests/metallb-native.yaml | |
kubectl apply -f - <<EOF | |
apiVersion: v1 | |
data: | |
secretkey: bWxEVGZJenZMMGJqUlp1REkzL0syZVJ6YnpBRUd3SktPcGhkNHk3VTZHZUJDVWNiaC90aXhWbWI5em5yL3RmRQpEenA3UXliYmlzbnE2NUp1YjdwbVVNcXJrQWVuZ3lEUXFROE5OUElaa3dhN0VxQUNjR0RIYWJoV1hBenRUNGppCm5BTEJ0NmRhNDlPYTZZUkREenB1VUVRaUptR3A2Y25PVmoveGdEdWh1b2c9 | |
kind: Secret | |
metadata: | |
creationTimestamp: null | |
name: memberlist | |
namespace: metallb-system | |
EOF | |
echo "Waiting for MetalLB to get ready..." | |
kubectl -n metallb-system wait pods --for=condition=ready --all --timeout=90s | |
# configure metallb | |
kubectl apply -f - <<EOF | |
--- | |
apiVersion: metallb.io/v1beta1 | |
kind: L2Advertisement | |
metadata: | |
name: default-advertisment | |
namespace: metallb-system | |
--- | |
apiVersion: metallb.io/v1beta1 | |
kind: IPAddressPool | |
metadata: | |
name: default-pool | |
namespace: metallb-system | |
spec: | |
addresses: | |
- 100.99.1.100-100.99.1.254 | |
EOF | |
# install ingress-nginx | |
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.5.1/deploy/static/provider/baremetal/deploy.yaml | |
kubectl \ | |
-n ingress-nginx \ | |
patch svc ingress-nginx-controller \ | |
-p '{"spec": {"type": "LoadBalancer", "loadBalancerIP": "100.99.1.100"}}' | |
unset kubectl | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment