Minikube Ingress DNS

From Chorke Wiki
Revision as of 21:10, 5 October 2024 by Shahed (talk | contribs) (→‎References)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
minikube stop
minikube delete --all
rm -rf ${HOME}/.minikube/*
minikube start
minikube docker-env
minikube addons list
minikube addons enable ingress
minikube addons enable ingress-dns
minikube addons enable metrics-server

kubectl get ns
kubectl get all
kubectl get all  -A
kubectl get pods -A
kubectl get namespaces
minikube kubectl -- get ns
minikube kubectl -- get all
minikube kubectl -- get all  -A
minikube kubectl -- get pods -A
minikube kubectl -- get namespaces
kubectl get  all -n kube-system
kubectl get  configmap -n kube-system
kubectl get  configmap coredns -n kube-system
kubectl edit configmap coredns -n kube-system
kubectl get  configmap coredns -n kube-system -o yaml

Ingress DNS

# minikube nameserver ip
printf -v MINIKUBE_NSLOOKUP '%s' $(minikube ip)

# minikube find coredns last entry
CONFIGMAP_COREDNS_FIND=$(cat <<QRY
        }\n\
        cache 30\n\
        loop\n\
        reload\n\
        loadbalance\n\
    }
QRY
)
# minikube fill coredns last entry
CONFIGMAP_COREDNS_FILL=$(cat <<UPD
${CONFIGMAP_COREDNS_FIND}\n\
    k8s.local:53 {\n\
        errors\n\
        cache 30\n\
        forward . ${MINIKUBE_NSLOOKUP}\n\
    }
UPD
)

K8s » CoreDNS


# minikube update coredns entry for k8s.local
kubectl get configmap coredns -n kube-system -o yaml \
 | sed -z "s|${CONFIGMAP_COREDNS_FIND}|$(echo "${CONFIGMAP_COREDNS_FILL}")|" \
 | kubectl apply -n kube-system -f -

K8s » Dashboard


cat << YML | kubectl apply -n kubernetes-dashboard -f -
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
  labels:
    app.kubernetes.io/version: 1.0.0
    app.kubernetes.io/managed-by: kubectl
    app.kubernetes.io/name: kubernetes-dashboard
    app.kubernetes.io/instance: kubernetes-dashboard
spec:
  ingressClassName: nginx
  rules:
    - host: "www.k8s.local"
      http:
        paths:
          - path: /
            pathType: ImplementationSpecific
            backend:
              service:
                name: kubernetes-dashboard
                port:
                  number: 80
YML
cat <<YML | kubectl delete -n kubernetes-dashboard -f -
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
  labels:
    app.kubernetes.io/version: 1.0.0
    app.kubernetes.io/managed-by: kubectl
    app.kubernetes.io/name: kubernetes-dashboard
    app.kubernetes.io/instance: kubernetes-dashboard
spec:
  ingressClassName: nginx
  rules:
    - host: "www.k8s.local"
      http:
        paths:
          - path: /
            pathType: ImplementationSpecific
            backend:
              service:
                name: kubernetes-dashboard
                port:
                  number: 80
YML

kubectl run -i --tty --rm debug --image=alpine  --restart=Never -- nslookup www.k8s.local
kubectl run -i --tty --rm debug --image=alpine  --restart=Never -- ping www.k8s.local
nslookup www.k8s.local $(minikube ip)

K8s » DNS Config


RESOLVE='/etc/resolv.conf';\
printf -v K8S_DNS '%s' $(minikube ip);\
if  [ -f ${RESOLVE} ]&&[ "$(grep -c ${K8S_DNS} ${RESOLVE})" == 0 ];then\
 cat << EOF | sudo tee -a ${RESOLVE} >/dev/null

nameserver ${K8S_DNS}
EOF
fi;\
cat ${RESOLVE}

nslookup www.k8s.local
ping www.k8s.local

xdg-open http://www.k8s.local &>/dev/null &
gnome-open http://www.k8s.local &>/dev/null &
x-www-browser http://www.k8s.local &>/dev/null &
sensible-browser http://www.k8s.local &>/dev/null &

Dynamic DNS Script

cat << EXE | sudo tee /etc/network/if-{up,post-down}.d/minikube-ifupdown >/dev/null
#!/bin/bash
: '
 @vendor    Chorke Academia, Inc.
 @web       https://cdn.chorke.org/docs/academia
 @version   1.0.00
 @since     1.0.00
'

USER_TARGET="${USER}"

function silent_exit(){
  exit 0
}

function verify_addr(){
  printf -v DNS_HOST '%s' \$(sudo -u \${USER_TARGET} minikube ip)
  if [[ "\${DNS_HOST}" =~ ^(([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))\.){3}([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))$ ]]; then
    if [[ "\${DNS_HOST}" == '127.0.0.1' ]]; then silent_exit; fi
  else silent_exit; fi
}

function verify_kube(){
  printf -v K8S_JSON '%s' \$(sudo -u \${USER_TARGET} minikube status -o 'json')
  printf -v K8S_REST '%s' \$(echo \${K8S_JSON} | jq -r '.APIServer')
  printf -v K8S_KUBE '%s' \$(echo \${K8S_JSON} | jq -r '.Kubelet')
  printf -v K8S_HOST '%s' \$(echo \${K8S_JSON} | jq -r '.Host')

  if [[ "\${K8S_HOST}" == 'Running' ]]&&
     [[ "\${K8S_KUBE}" == 'Running' ]]&&
     [[ "\${K8S_REST}" == 'Running' ]]; then verify_addr
  else silent_exit; fi
}

function verify_tool(){
  if [[ -x "\$(command -v jq)" ]]; then verify_kube; else silent_exit; fi
}

function verify_mini(){
  if [[ -x "\$(command -v minikube)" ]]; then verify_tool; else silent_exit; fi
}

function verify_link(){
  HOST_ETHERS="\$(ip -j link show | jq -r '.[].ifname'| paste -sd' ' -)"
  if [[ "\${HOST_ETHERS}" =~ "\${IFACE}" ]]; then verify_mini; else silent_exit; fi
}

function verify_mode(){
  if [[ "\${MODE}" == 'start' ]]; then verify_link; else verify_mini; fi
}

function verify(){
  verify_mode
}

function amend_nameserver(){
  RESOLV_CONF='/etc/resolv.conf'
  if  [[ -f \${RESOLV_CONF} ]]&&[[ "\$(grep -c \${DNS_HOST} \${RESOLV_CONF})" == 0 ]];then
    cat << CON | sudo tee -a \${RESOLV_CONF} >/dev/null

# minikube dns server
nameserver \${DNS_HOST}
CON
  else silent_exit; fi
}

function handle(){
  amend_nameserver
}

function init(){
  verify
  handle
}

init
EXE
sudo chmod +x /etc/network/if-{up,post-down}.d/minikube-ifupdown

Swiss Knife

kubectl -n kube-system run -i --tty --rm nslookup-cli --image=alpine --restart=Never -- sh
apk --update add inetutils-telnet

nslookup kubernetes-dashboard.kubernetes-dashboard.svc.cluster.local
nslookup postgresql-hl.postgresql.svc.cluster.local
nslookup minikube-host.internal.svc.cluster.local
nslookup postgresql.postgresql.svc.cluster.local
nslookup redis-headless.redis.svc.cluster.local
nslookup www.k8s.local

Playground

minikube status -f '{{.Kubeconfig}}'
minikube status -f '{{.APIServer}}'
minikube status -f '{{.Kubelet}}'
minikube status -f '{{.Worker}}'
minikube status -f '{{.Host}}'
minikube status -o 'json' | jq -r '.Kubeconfig'
minikube status -o 'json' | jq -r '.APIServer'
minikube status -o 'json' | jq -r '.Kubelet'
minikube status -o 'json' | jq -r '.Worker'
minikube status -o 'json' | jq -r '.Host'
ip -j link show | jq -r '.[].ifname'| paste -sd' ' -
ip -j link show type bridge | jq -r '.[].ifname'
ip -o link show | awk -F': ' '{print $2}'
ip -j link show | jq -r '.[].ifname'
ls /sys/class/net

minikube status -f '{{- if .TimeToStop }} timeToStop: {{.TimeToStop}} {{- end }}'
minikube status -f '{{- if .DockerEnv }} docker-env: {{.DockerEnv}} {{- end }}'
minikube status -o 'json' | jq
ip -j link show | jq -r '.[]|select(.link_type=="loopback")'
ip -j link show | jq -r '.[]|select(.link_type=="ether")'
ip -j link show | jq -r '.[]|select(.link_type==null)'

export SYSTEM_RAM_QUOTA=$((3*1024))
export SYSTEM_RAM_LIMIT=$(awk '/^MemTotal:/ {print int($2/1024-3*1024);}' /proc/meminfo)
export SYSTEM_RAM_LIMIT=$(awk -v QUOTA="${SYSTEM_RAM_QUOTA}" '/^MemTotal:/ {print int($2/1024-QUOTA);}' /proc/meminfo)

bash <(curl -s 'https://cdn.chorke.org/exec/cli/bash/install/minikube/network/ifupdown/1.0.0-ubuntu-22.04.sh.txt')
sudo su -c 'export IFACE=lo;export MODE=stop; /etc/network/if-post-down.d/minikube-ifupdown'
sudo su -c 'export IFACE=lo;export MODE=start;/etc/network/if-up.d/minikube-ifupdown'

echo 'export IFACE=lo;export MODE=stop; /etc/network/if-post-down.d/minikube-ifupdown'|sudo su 
echo 'export IFACE=lo;export MODE=start;/etc/network/if-up.d/minikube-ifupdown'|sudo su 
   ls -lah /etc/network/if-{up,post-down}.d/minikube-ifupdown
sudo rm -f /etc/network/if-{up,post-down}.d/minikube-ifupdown

bash <(curl -s 'https://cdn.chorke.org/exec/cli/bash/install/minikube/network/ifupdown/1.0.0-ubuntu-24.04.sh.txt')
sudo /etc/NetworkManager/dispatcher.d/minikube-ifupdown lo down
sudo /etc/NetworkManager/dispatcher.d/minikube-ifupdown lo up

CONFIGMAP_COREDNS_FIND=$(cat <<QRY
        }\n\
        cache 30\n\
        loop\n\
        reload\n\
        loadbalance\n\
    }
QRY
)
CONFIGMAP_COREDNS_FILL=$(cat <<UPD
${CONFIGMAP_COREDNS_FIND}\n\
    k8s.local:53 {\n\
        errors\n\
        cache 30\n\
        forward . 192.168.49.2\n\
    }
UPD
)

kubectl get configmap coredns -n kube-system -o yaml\
 | sed -z "s|${CONFIGMAP_COREDNS_FIND}|$(echo "${CONFIGMAP_COREDNS_FILL}")|"

References