Skip to content

Instantly share code, notes, and snippets.

@jacobtomlinson
Last active October 26, 2022 15:50
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jacobtomlinson/4b835d807ebcea73c6c8f602613803d4 to your computer and use it in GitHub Desktop.
Save jacobtomlinson/4b835d807ebcea73c6c8f602613803d4 to your computer and use it in GitHub Desktop.
A script to update your /etc/hosts file from minikube ingest records
#!/bin/bash
#
# A script to update your /etc/hosts file from minikube ingest records
#
# Installation
# ------------
# curl -L https://gist.github.com/jacobtomlinson/4b835d807ebcea73c6c8f602613803d4/raw/minikube-update-hosts.sh > /usr/local/bin/minikube-update-hosts
# chmod +x /usr/local/bin/minikube-update-hosts
set -e
INGRESSES=$(kubectl --context=minikube --all-namespaces=true get ingress -o jsonpath='{.items[*].spec.rules[*].host}')
MINIKUBE_IP=$(minikube ip)
HOSTS_ENTRY="$MINIKUBE_IP $INGRESSES"
if grep -Fq "$MINIKUBE_IP" /etc/hosts > /dev/null
then
sudo sed -i '' "s/^$MINIKUBE_IP.*/$HOSTS_ENTRY/" /etc/hosts
echo "Updated hosts entry"
else
echo "$HOSTS_ENTRY" | sudo tee -a /etc/hosts
echo "Added hosts entry"
fi
@alwinmarkcf
Copy link

Instead of:

kubectl --context=minikube --all-namespaces=true get ingress | grep -v NAMESPACE | awk '{ print $2 }' | tr '\r\n' ' '
kubectl --context=minikube --all-namespaces=true get ingress -o jsonpath='{.items[*].spec.rules[*].host}'

Also for me it would have been awk '{print $3}'

@orefalo
Copy link

orefalo commented Dec 8, 2019

#!/bin/bash
#
# A script to update your /etc/hosts file from minikube ingest records
#
# Installation
# ------------
# curl -L https://gist.github.com/jacobtomlinson/4b835d807ebcea73c6c8f602613803d4/raw/minikube-update-hosts.sh > /usr/local/bin/minikube-update-hosts
# chmod +x /usr/local/bin/minikube-update-hosts

INGRESSES=$(kubectl --context=minikube --all-namespaces=true get ingress -o jsonpath='{.items[*].spec.rules[*].host}')

MINIKUBE_IP=$(minikube ip)

HOSTS_ENTRY="$MINIKUBE_IP $INGRESSES"

if grep -Fq "$MINIKUBE_IP" /etc/hosts > /dev/null
then
    sudo sed -i '' "s/^$MINIKUBE_IP.*/$HOSTS_ENTRY/" /etc/hosts
    echo "Updated hosts entry"
else
    echo "$HOSTS_ENTRY" | sudo tee -a /etc/hosts
    echo "Added hosts entry"
fi

@jacobtomlinson
Copy link
Author

Thanks @alwinmarkcf and @orefalo I've updated the gist.

@svilenkov
Copy link

I'd advise to use set -e on top of the script, because if something goes wrong with the kubectl or minikube commands, you will end up scrambling up your /etc/hosts file

@jacobtomlinson
Copy link
Author

I'd advise to use set -e on top of the script, because if something goes wrong with the kubectl or minikube commands, you will end up scrambling up your /etc/hosts file

Done thanks @svilenkov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment