Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save germainlefebvre4/93c61cb8a9379972f9c9c88ef75a2599 to your computer and use it in GitHub Desktop.
Save germainlefebvre4/93c61cb8a9379972f9c9c88ef75a2599 to your computer and use it in GitHub Desktop.
Minikube & WSL2 - Access to your kubernetes (namespaced) services with a browser from your WSL2 machine (here Ubuntu).
#!/bin/bash
# IMPORTANT #1: Run WSL machine as administrator.
# IMPORTANT #2: Disable read-only on Windows hosts file (c:\Windows\System32\drivers\etc`hosts)
# and allow Write right to current user.
# IMPORTANT #3: Install apache2 package and activate proxy module
# apt update
# apt install -y apache2
# service apache2 start
# a2enmod proxy_http
# Running:
# sudo ./wsl2-sync-minikube-services-windows-host.sh
# Output:
# Available DNS:
# url > service (namespace)
# http://k8s-default-web-1.local > web-1 (default)
# http://k8s-default-web-2.local > web-2 (default)
# http://k8s-default-web-3.local > web-3 (default)
# http://k8s-test-web-1.local > web-1 (test)
# http://k8s-test-web-2.local > web-2 (test)
# Gather system informations
IP_ADDR=$(ip addr show eth0 | grep -E 'inet[^6]' | awk '{print $2}' | cut -d'/' -f1)
IP_ADDR_ESC=$(ip addr show eth0 | grep -E 'inet[^6]' | awk '{print $2}' | cut -d'/' -f1 | sed 's/\./\\./g')
minikube service list | grep $(minikube ip) > .minikube_refresh
# Reset Windows hosts for Kubernetes
sed -i "/^${IP_ADDR_ESC}/d" /mnt/c/Windows/System32/drivers/etc/hosts
echo "${IP_ADDR} " >> /mnt/c/Windows/System32/drivers/etc/hosts
rm -f /etc/apache2/sites-enabled/k8s-*.conf
echo "Available DNS:"
echo " url > service (namespace)"
cat .minikube_refresh | while read -r line ; do
DNS=$(echo $line | awk '{print "k8s" "-" $2 "-" $4 ".local"}')
ENDPOINT=$(echo $line | awk '{print $8}')
K8S_NS=$(echo $line | awk '{print $2}')
K8S_SVC=$(echo $line | awk '{print $4}')
# Windows hosts
sed -i "s/^$IP_ADDR/& ${DNS}/" /mnt/c/Windows/System32/drivers/etc/hosts
# Apache virtualhost
cat << EOF > /etc/apache2/sites-enabled/${DNS}.conf
<VirtualHost *:80>
ServerName ${DNS}
ProxyPass / ${ENDPOINT}
ProxyPassReverse / ${ENDPOINT}
</VirtualHost>
EOF
# Sumup
echo " http://$DNS > $K8S_SVC ($K8S_NS)"
done
rm .minikube_refresh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment