Minikube

From Chorke Wiki
Jump to navigation Jump to search
sudo mkdir -p /opt/cli/minikube-linux-amd64/bin
sudo wget -cq https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64\
 -O /opt/cli/minikube-linux-amd64/bin/minikube

sudo chmod +x /opt/cli/minikube-linux-amd64/bin/minikube
sudo update-alternatives --install /usr/local/bin/minikube minikube\
 /opt/cli/minikube-linux-amd64/bin/minikube 1980
Order Port Type Internal IP Internal Port External IP External Port
1 TCP 0.0.0.0 32443 127.0.0.1 49153
2 TCP 0.0.0.0 8443 127.0.0.1 49154
3 TCP 0.0.0.0 5000 127.0.0.1 49155
4 TCP 0.0.0.0 2376 127.0.0.1 49156
5 TCP 0.0.0.0 22 127.0.0.1 49157

Kubectl

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key\
 | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

cat << EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list >/dev/null
deb [arch=$(dpkg --print-architecture)\
 signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg]\
 https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /
EOF

sudo apt update
sudo apt install kubectl

Cluster

minikube start
:'
😄  minikube v1.29.0 on Ubuntu 22.04
✨  Automatically selected the docker driver. Other choices: kvm2, qemu2, none, ssh
📌  Using Docker driver with root privileges
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
💾  Downloading Kubernetes v1.26.1 preload ...
    > preloaded-images-k8s-v18-v1...:  397.05 MiB / 397.05 MiB  100.00% 33.42 M
    > gcr.io/k8s-minikube/kicbase...:  407.19 MiB / 407.19 MiB  100.00% 7.46 Mi
🔥  Creating docker container (CPUs=2, Memory=5900MB) ...
🐳  Preparing Kubernetes v1.26.1 on Docker 20.10.23 ...
    ▪ Generating certificates and keys ...
    ▪ Booting up control plane ...
    ▪ Configuring RBAC rules ...
🔗  Configuring bridge CNI (Container Networking Interface) ...
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🌟  Enabled addons: storage-provisioner, default-storageclass
🔎  Verifying Kubernetes components...
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
'
kubectl get po -A
minikube kubectl -- get po -A
:'
NAMESPACE     NAME                               READY   STATUS    RESTARTS        AGE
kube-system   coredns-64897985d-thxhw            1/1     Running   0               8m14s
kube-system   etcd-minikube                      1/1     Running   0               8m26s
kube-system   kube-apiserver-minikube            1/1     Running   0               8m26s
kube-system   kube-controller-manager-minikube   1/1     Running   0               8m26s
kube-system   kube-proxy-xqfl2                   1/1     Running   0               8m13s
kube-system   kube-scheduler-minikube            1/1     Running   0               8m26s
kube-system   storage-provisioner                1/1     Running   1 (7m43s ago)   8m25s
'

Dashboard

minikube addons enable metrics-server
minikube dashboard
:'
🔌  Enabling  ...
    ▪ Using image kubernetesui/metrics-scraper:v1.0.7
    ▪ Using image kubernetesui/dashboard:v2.3.1
🤔  Verifying dashboard health ...
🚀  Launching proxy ...
🤔  Verifying proxy health ...
🎉  Opening http://127.0.0.1:36403/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...
Gtk-Message: 17:34:36.652: Failed to load module "canberra-gtk-module"
Gtk-Message: 17:34:36.654: Failed to load module "canberra-gtk-module"
'

Deploy

Deployment/NodePort:
kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.4
kubectl expose deployment hello-minikube --type=NodePort --port=8080

kubectl port-forward service/hello-minikube 7080:8080
http://localhost:7080/

kubectl get services      hello-minikube
kubectl delete services   hello-minikube
kubectl delete deployment hello-minikube
Deployment/LoadBalancer:
kubectl create deployment balanced --image=k8s.gcr.io/echoserver:1.4  
kubectl expose deployment balanced --type=LoadBalancer --port=8080

kubectl get services      balanced
kubectl delete services   balanced
kubectl delete deployment balanced

Example

docker build -t chorke/crawler:1.0.00 -f ./Dockerfile .
docker tag      chorke/crawler:1.0.00 reg.chorke.org/chorke/crawler:1.0.00
docker push     reg.chorke.org/chorke/crawler:1.0.00
docker pull     hub.chorke.org/chorke/crawler:1.0.00
docker tag      hub.chorke.org/chorke/crawler:1.0.00 chorke/crawler:1.0.00

minikube image load chorke/crawler:1.0.00
minikube image ls --format table
kubectl run crawler --image=chorke/crawler:1.0.00 --image-pull-policy=Never --restart=Never
kubectl logs crawler
kubectl get pod

kubectl delete pod crawler -n default
kubectl delete pod crawler
Deployment/NodePort:
kubectl create deployment crawler --image=chorke/crawler:1.0.00
kubectl expose deployment crawler --type=NodePort --port=1983

kubectl port-forward service/crawler 1983:1983
http://localhost:1983/crawler

kubectl get services      crawler
kubectl delete services   crawler
kubectl delete deployment crawler

Ingress DNS

minikube start
cat /etc/resolv.conf | grep nameserver
minikube addons enable ingress
minikube addons enable ingress-dns
nslookup academia.local
nslookup academia.local $(minikube ip)

kubectl get configmap coredns -n kube-system -o yaml
kubectl get configmap coredns -n kube-system -o "jsonpath={.data.Corefile}"

Volatile Fixes

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}

Robust Fixes

RESOLVE='/etc/systemd/resolved.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

# k8s coredns
DNS=${K8S_DNS}
EOF
fi;\
cat ${RESOLVE}
sudo systemctl restart systemd-resolved
sudo systemctl status  systemd-resolved

sudo systemctl restart NetworkManager.service
systemctl status NetworkManager.service
systemctl status systemd-resolved
systemctl status dnsmasq
cat /etc/resolv.conf | grep nameserver
nmcli dev show | grep DNS
journalctl -u dnsmasq
tail /var/log/syslog

kubectl edit configmap coredns -n kube-system
apiVersion: v1
data:
  Corefile: |
    .:53 {
        errors

        reload
        loadbalance
    }
    k8s.local:53 {
        errors
        cache 30
        forward . 192.168.49.2
    }
    academia.local:53 {
        errors
        cache 30
        forward . 192.168.49.2
    }
kind: ConfigMap
metadata:
  creationTimestamp: "2023-07-04T00:08:54Z"
  name: coredns
  namespace: kube-system
  resourceVersion: "300"
  uid: d8eec45d-1452-467f-8861-8811658c773a

kubectl run -i --tty --rm debug --image=busybox --restart=Never -- nslookup academia.local
kubectl run -i --tty --rm debug --image=busybox --restart=Never -- ping academia.local

kubectl run -i --tty --rm debug --image=alpine  --restart=Never -- nslookup academia.local
kubectl run -i --tty --rm debug --image=alpine  --restart=Never -- ping academia.local

Ingress DNS » 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

Mounts » 9P

9P mounts are flexible and work across all hypervisors, but suffers from performance and reliability issues when used with large folders (>600 files).

MINIKUBE_BRIDGE="br-$(docker network ls -fname=minikube --format=json|jq -r '.ID')"
sudo ufw allow in on ${MINIKUBE_BRIDGE}
sudo ufw status numbered
sudo ufw reload
cat << EXE | sudo -i -u minikube bash
      mkdir -p ${HOME}/volumes
minikube mount ${HOME}/volumes:/var/minikube --port=1980
EXE

Playground

minikube image rm docker.io/jenkins/jenkins:2.452.1-jdk17
minikube image ls
minikube config view host-only-cidr
minikube config get  host-only-cidr
minikube config list
minikube status

cat ~/.minikube/machines/minikube/config.json|jq -r '.Driver.NodeConfig.Memory'
cat ~/.minikube/machines/minikube/config.json|jq -r '.Driver.NodeConfig.CPU'
minikube config set memory 16384
minikube config get memory
minikube config view  memory
minikube config unset memory

SYSTEM_RAM_LIMIT=$(awk '/^MemTotal:/ { print int($2/1024); }' /proc/meminfo)
SYSTEM_CPU_LIMIT=$(getconf _NPROCESSORS_ONLN)
minikube config view
minikube config
minikube config set memory $((${SYSTEM_RAM_LIMIT}-1024*4))
minikube config set cpus   $((${SYSTEM_CPU_LIMIT}-4))
minikube config set host-only-cidr 10.20.23.1/24
minikube delete && minikube start

minikube start && minikube config view;\
for p in ingress ingress-dns dashboard metrics-server;\
do minikube addons enable ${p};done
minikube stop && minikube start
minikube config view
minikube addons list
kubectl cluster-info dump
kubectl cluster-info
kubectl plugin list

kubectl config set-context  minikube --cluster=minikue
kubectl config set-context  minikube
kubectl config get-contexts
kubectl config get-clusters
kubectl config view
minikube start -p v1.27 --kubernetes-version v1.27.0
minikube start -p v1.30 --kubernetes-version v1.30.0
minikube profile v1.30
minikube profile list
minikube profile

sudo -i -u minikube minikube status
sudo -i -u minikube minikube ip
minikube stop   -p v1.27
minikube delete -p v1.27
minikube tunnel

Knowledge

minikube stop
minikube start
minikube pause

minikube delete
minikube unpause
minikube addons list
minikube delete --all
minikube docker-env
minikube image ls --format table
minikube config set memory 16384
 
minikube start --cpus 8 --memory 16384
eval $(minikube -p minikube docker-env)
minikube start -p aged --kubernetes-version=v1.16.1
minikube start -p aged --kubernetes-version=v1.24.0

docker ps -a
docker images
docker image ls
docker network ls

docker rm $(docker ps -a -q)
docker stop $(docker ps -a -q)

docker build\
 -t chorke/crawler:1.0.00 -f ./Dockerfile .

docker logs crawler
docker stop crawler && docker rm crawler
kubectl get pod
kubectl cluster-info
kubectl get namespaces
kubectl -n kube-system get svc -o wide

minikube image ls --format table
minikube image load chorke/crawler:1.0.00

kubectl run crawler --image=chorke/crawler:1.0.00\
 --image-pull-policy=Never --restart=Never

kubectl logs crawler
kubectl delete pod crawler

docker container inspect minikube \
--format="{{ (index (index .NetworkSettings.Ports \"8443/tcp\") 0).HostPort }}"

docker container ls --filter=name=minikube --format="{{.Ports}}" | tr , \\n

kubectl get po -A
kubectl explain pods
kubectl get services
kubectl get services crawler
kubectl describe pods crawler
kubectl get pods --all-namespaces

kubectl get services
kubectl get services -n default
kubectl get services --all-namespaces

kubectl delete pod crawler
kubectl delete -n default pod crawler

kubectl delete services crawler
kubectl delete deployment crawler

kubectl get nodes
kubectl get pods -o wide
kubectl get po,jobs,cronjob
minikube kubectl -- get po -A
minikube kubectl -- explain pods
minikube kubectl -- get services
minikube kubectl -- get service crawler
minikube kubectl -- describe pods crawler
minikube kubectl -- get pods --all-namespaces 

minikube service --all
minikube service list -n default
minikube service list

minikube kubectl -- delete pod crawler
minikube kubectl -- delete -n default pod crawler

minikube kubectl -- delete services crawler
minikube kubectl -- delete deployment crawler

minikube kubectl -- get nodes
minikube kubectl -- get pods -o wide
minikube kubectl -- get po,jobs,cronjob

minikube addons enable metallb
minikube addons enable olm
minikube addons list
minikube addons enable default-storageclass
minikube addons enable metrics-server
minikube addons configure metallb

minikube addons enable storage-provisioner
minikube addons enable dashboard

minikube ssh
minikube addons enable ingress-dns
minikube addons enable ingress

ping host.minikube.internal

kubectl get configmap coredns -n kube-system -o yaml
nslookup academia.local $(minikube ip)

getconf _NPROCESSORS_CONF
getconf _NPROCESSORS_ONLN
nproc --all
grep ^cpu\\scores /proc/cpuinfo | uniq |  awk '{print $4}'
python -c 'import os; print(os.cpu_count())'
grep -c ^processor /proc/cpuinfo

awk '/^MemAvailable:/ { print $2/1024; }' /proc/meminfo
awk '/^MemTotal:/ { print $2/1024; }' /proc/meminfo
awk '/^MemFree:/ { print $2/1024; }' /proc/meminfo

References