Tuesday, August 29, 2023

Kubernetes with Minikube

  

1. Download Minicube

https://storage.googleapis.com/minikube/releases/latest/minikube-installer.exe
run the minikube-installer.exe and install

2. Add the binary in to your PATH.

 Make sure to run PowerShell as Administrator.
 
 $oldpath=[Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine)
 if($oldpath -notlike "*;C:\minikube*"){`
   [Environment]::SetEnvironmentVariable("Path", $oldpath+";C:\minikube", [EnvironmentVariableTarget]::Machine)`
 }

3. Start your cluster

From a terminal with administrator access, run: (I have run from Powershell with Adminsitrator access)

minikube start

PS C:\windows\system32> minikube start
* minikube v1.22.0 on Microsoft Windows 10 Pro 10.0.19041 Build 19041
* Automatically selected the docker driver. Other choices: hyperv, virtualbox, ssh
* Starting control plane node minikube in cluster minikube
* Pulling base image ...
! The image 'docker.io/kubernetesui/metrics-scraper:v1.0.4' was not found; unable to add it to cache.
! The image 'docker.io/kubernetesui/dashboard:v2.1.0' was not found; unable to add it to cache.
! The image 'k8s.gcr.io/pause:3.4.1' was not found; unable to add it to cache.
! The image 'k8s.gcr.io/kube-controller-manager:v1.21.2' was not found; unable to add it to cache.
! The image 'k8s.gcr.io/kube-scheduler:v1.21.2' was not found; unable to add it to cache.
! The image 'k8s.gcr.io/kube-proxy:v1.21.2' was not found; unable to add it to cache.
! The image 'gcr.io/k8s-minikube/storage-provisioner:v5' was not found; unable to add it to cache.
! The image 'k8s.gcr.io/etcd:3.4.13-0' was not found; unable to add it to cache.
! The image 'k8s.gcr.io/kube-apiserver:v1.21.2' was not found; unable to add it to cache.
! The image 'k8s.gcr.io/coredns/coredns:v1.8.0' was not found; unable to add it to cache.
    > gcr.io/k8s-minikube/kicbase...: 361.09 MiB / 361.09 MiB  100.00% 1.03 MiB
! minikube was unable to download gcr.io/k8s-minikube/kicbase:v0.0.25, but successfully downloaded gcr.io/k8s-minikube/kicbase:v0.0.25 as a fallback image
* Creating docker container (CPUs=2, Memory=4000MB) ...
* Preparing Kubernetes v1.21.2 on Docker 20.10.7 ...
X Unable to load cached images: loading cached images: CreateFile C:\Users\YNAGARAJ\.minikube\cache\images\k8s.gcr.io\kube-proxy_v1.21.2: The system cannot find the file specified.
    > kubelet.sha256: 64 B / 64 B [--------------------------] 100.00% ? p/s 0s
    > kubeadm.sha256: 64 B / 64 B [--------------------------] 100.00% ? p/s 0s
    > kubectl.sha256: 64 B / 64 B [--------------------------] 100.00% ? p/s 0s
    > kubectl: 44.27 MiB / 44.27 MiB [-----------] 100.00% 452.43 KiB p/s 1m40s
    > kubeadm: 42.57 MiB / 42.57 MiB [-----------] 100.00% 274.19 KiB p/s 2m39s
    > kubelet: 112.68 MiB / 112.68 MiB [---------] 100.00% 607.52 KiB p/s 3m10s
  - Generating certificates and keys ...
  - Booting up control plane ...
  - Configuring RBAC rules ...
* Verifying Kubernetes components...
  - Using image gcr.io/k8s-minikube/storage-provisioner:v5
* Enabled addons: storage-provisioner, default-storageclass
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

4. Interact with your cluster

If you already have kubectl installed, you can now use it to access new cluster

kubectl get po -A

PS C:\windows\system32> kubectl get po -A
NAMESPACE     NAME                               READY   STATUS    RESTARTS   AGE
kube-system   coredns-558bd4d5db-mr9nz           1/1     Running   0          36s
kube-system   etcd-minikube                      1/1     Running   0          52s
kube-system   kube-apiserver-minikube            1/1     Running   0          49s
kube-system   kube-controller-manager-minikube   1/1     Running   0          49s
kube-system   kube-proxy-td9gz                   1/1     Running   0          36s
kube-system   kube-scheduler-minikube            1/1     Running   0          49s
kube-system   storage-provisioner                1/1     Running   0          48s

If Kubectl is not installed, you can alternatively use the below command to download it

minikube kubectl -- get po -A

PS C:\windows\system32> minikube kubectl -- get po -A
    > kubectl.exe.sha256: 64 B / 64 B [----------------------] 100.00% ? p/s 0s
    > kubectl.exe: 45.63 MiB / 45.63 MiB [-----------] 100.00% 1.30 MiB p/s 35s
NAMESPACE     NAME                               READY   STATUS    RESTARTS   AGE
kube-system   coredns-558bd4d5db-mr9nz           1/1     Running   0          106s
kube-system   etcd-minikube                      1/1     Running   0          2m2s
kube-system   kube-apiserver-minikube            1/1     Running   0          119s
kube-system   kube-controller-manager-minikube   1/1     Running   0          119s
kube-system   kube-proxy-td9gz                   1/1     Running   0          106s
kube-system   kube-scheduler-minikube            1/1     Running   0          119s
kube-system   storage-provisioner                1/1     Running   0          118s

5. To start all services run below command:

minikube dashboard

PS C:\windows\system32> minikube dashboard
* Enabling dashboard ...
  - Using image kubernetesui/dashboard:v2.1.0
  - Using image kubernetesui/metrics-scraper:v1.0.4
* Verifying dashboard health ...
* Launching proxy ...
* Verifying proxy health ...

PS C:\windows\system32> kubectl get po -A
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE
kube-system            coredns-558bd4d5db-mr9nz                     1/1     Running   0          7m11s
kube-system            etcd-minikube                                1/1     Running   0          7m27s
kube-system            kube-apiserver-minikube                      1/1     Running   0          7m24s
kube-system            kube-controller-manager-minikube             1/1     Running   0          7m24s
kube-system            kube-proxy-td9gz                             1/1     Running   0          7m11s
kube-system            kube-scheduler-minikube                      1/1     Running   0          7m24s
kube-system            storage-provisioner                          1/1     Running   0          7m23s
kubernetes-dashboard   dashboard-metrics-scraper-7976b667d4-g72kw   1/1     Running   0          4m33s
kubernetes-dashboard   kubernetes-dashboard-6fcdf4f6d-l6hnz         1/1     Running   0          4m33s


6. Deploy hello-minikube deployment using Nodeport 8080 and then expose it onto port 7080

   1. Create a name space and deploy the deployment in the same namespace
   
   PS C:\windows\system32> kubectl create ns yash
   namespace/yash created
   
   2. Deploy the hello-minikube deployment
   
   PS C:\windows\system32> kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.4 -n yash
   deployment.apps/hello-minikube created
   
   PS C:\windows\system32> kubectl expose deployment hello-minikube --type=NodePort --port=8080 -n yash
   service/hello-minikube exposed
   
   3. Check the services
   
   PS C:\windows\system32> kubectl get services hello-minikube -n yash
   NAME             TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
   hello-minikube   NodePort   10.104.194.80   <none>        8080:32629/TCP   9s
   
   4. The easiest way to access this service is to let minikube launch a web browser for you:
   
   PS C:\windows\system32> minikube service hello-minikube -n yash
   |-----------|----------------|-------------|---------------------------|
   | NAMESPACE |      NAME      | TARGET PORT |            URL            |
   |-----------|----------------|-------------|---------------------------|
   | yash      | hello-minikube |        8080 | http://192.168.49.2:32629 |
   |-----------|----------------|-------------|---------------------------|
   * Starting tunnel for service hello-minikube.
   |-----------|----------------|-------------|------------------------|
   | NAMESPACE |      NAME      | TARGET PORT |          URL           |
   |-----------|----------------|-------------|------------------------|
   | yash      | hello-minikube |             | http://127.0.0.1:52505 |
   |-----------|----------------|-------------|------------------------|
   * Opening service yash/hello-minikube in default browser...
   ! Because you are using a Docker driver on windows, the terminal needs to be open to run it.
   * Stopping tunnel for service hello-minikube.
   
   or
   
   Alternatively, use kubectl to forward the port:
   
   PS C:\windows\system32> kubectl port-forward service/hello-minikube 7080:8080 -n yash
   Forwarding from 127.0.0.1:7080 -> 8080
   Forwarding from [::1]:7080 -> 8080
   Handling connection for 7080
   Handling connection for 7080
   
   Your application is now available at http://127.0.0.1:7080/
   
   
7. Deploy LoadBalancer   

   To access a LoadBalancer deployment, use the “minikube tunnel” command.
   
   PS C:\windows\system32> kubectl create deployment balanced --image=k8s.gcr.io/echoserver:1.4 -n yash
   deployment.apps/balanced created
   
   PS C:\windows\system32> kubectl expose deployment balanced --type=LoadBalancer --port=8080 -n yash
   service/balanced exposed

   In another window, start the tunnel to create a routable IP for the ‘balanced’ deployment:

   PS C:\windows\system32> minikube tunnel
   * Starting tunnel for service balanced.

   To find the routable IP, run this command and examine the EXTERNAL-IP column:
   PS C:\windows\system32> kubectl get services balanced -n yash
   NAME       TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
   balanced   LoadBalancer   10.98.29.159   127.0.0.1     8080:30947/TCP   3m44s

   The deployment is available at http://127.0.0.1:8080/
   
8. To get the deployments
   
   run -> kubectl get deployments -n yash

   PS C:\windows\system32> kubectl get deployments -n yash
   NAME             READY   UP-TO-DATE   AVAILABLE   AGE
   balanced         1/1     1            1           8m45s
   hello-minikube   1/1     1            1           18m
   
9. To get the Pods

   run -> kubectl get pods -n yash

   PS C:\windows\system32> kubectl get pods -n yash
   NAME                              READY   STATUS    RESTARTS   AGE
   balanced-5744b548b4-vbcnh         1/1     Running   0          9m47s
   hello-minikube-6ddfcc9757-vvhnf   1/1     Running   0          19m
   
10. View Cluster events

   run -> kubectl get events
   
   PS C:\windows\system32> kubectl get events
   LAST SEEN   TYPE     REASON                    OBJECT          MESSAGE
   29m         Normal   Starting                  node/minikube   Starting kubelet.
   29m         Normal   NodeHasSufficientMemory   node/minikube   Node minikube status is now: NodeHasSufficientMemory
   29m         Normal   NodeHasNoDiskPressure     node/minikube   Node minikube status is now: NodeHasNoDiskPressure
   29m         Normal   NodeHasSufficientPID      node/minikube   Node minikube status is now: NodeHasSufficientPID
   29m         Normal   NodeAllocatableEnforced   node/minikube   Updated Node Allocatable limit across pods
   29m         Normal   NodeReady                 node/minikube   Node minikube status is now: NodeReady
   29m         Normal   RegisteredNode            node/minikube   Node minikube event: Registered Node minikube in Controller
   29m         Normal   Starting                  node/minikube   Starting kube-proxy.
   
11. View kubectl configuration

   PS C:\windows\system32> kubectl config view
   apiVersion: v1
   clusters:
   - cluster:
       certificate-authority: C:\Users\YNAGARAJ\.minikube\ca.crt
       extensions:
       - extension:
           last-update: Wed, 01 Sep 2021 20:19:03 IST
           provider: minikube.sigs.k8s.io
           version: v1.22.0
         name: cluster_info
       server: https://127.0.0.1:60254
     name: minikube
   contexts:
   - context:
       cluster: minikube
       extensions:
       - extension:
           last-update: Wed, 01 Sep 2021 20:19:03 IST
           provider: minikube.sigs.k8s.io
           version: v1.22.0
         name: context_info
       namespace: default
       user: minikube
     name: minikube
   current-context: minikube
   kind: Config
   preferences: {}
   users:
   - name: minikube
     user:
       client-certificate: C:\Users\YNAGARAJ\.minikube\profiles\minikube\client.crt
    client-key: C:\Users\YNAGARAJ\.minikube\profiles\minikube\client.key
    
12. Check services

    run -> kubectl get svc -n yash
    
    PS C:\windows\system32> kubectl get svc -n yash
    NAME                     TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
    service/balanced         LoadBalancer   10.98.29.159    127.0.0.1     8080:30947/TCP   12m
    service/hello-minikube   NodePort       10.104.194.80   <none>        8080:32629/TCP   23m
    
13. Delete a service

    run -> kubectl delete service <service_name>
    
    PS C:\windows\system32> kubectl delete service balanced -n yash
    service "balanced" deleted

14. Delete a deployment

    run -> kubectl delete deployment <deployment_name>
    
    PS C:\windows\system32> kubectl delete deployment balanced -n yash
    deployment.apps "balanced" deleted
    
15. Manage your cluster

    minikube pause
    
    PS C:\windows\system32> minikube pause
    * Pausing node minikube ...
    * Paused 18 containers in: kube-system, kubernetes-dashboard, storage-gluster, istio-operator
    
    minikube unpause
    
    PS C:\windows\system32> minikube unpause
    * Unpausing node minikube ...
    * Unpaused 18 containers in: kube-system, kubernetes-dashboard, storage-gluster, istio-operator
    
    minikube stop
    
    PS C:\windows\system32> minikube stop
    * Stopping node "minikube"  ...
    * Powering off "minikube" via SSH ...
    * 1 nodes stopped.
    
    minikube start
    
    delete minikube clusters
    
    minikube stop
    minikube delete
    minikube delete --all
    
16. Get the Kubernetes Dashboard url

    run -> minikube dashboard --url
 
    PS C:\windows\system32> minikube dashboard --url
    * Verifying dashboard health ...
    * Launching proxy ...
    * Verifying proxy health ...
    
    http://127.0.0.1:64376/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
    
    Change the namespace in the url launched
    
    from -> http://127.0.0.1:57253/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/#/overview?namespace=default
    to -> http://127.0.0.1:57253/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/#/overview?namespace=yash
    
    you can see the Deployments, Replica Sets and Pods.
    
    Deployments ->  hello-minikube
    Replica Sets ->  hello-minikube-6ddfcc9757
    Pods ->  hello-minikube-6ddfcc9757-vvhnf
    
    On the dashboard, you can manage all K8s components
    1. Workloads
    2. Service
    3. Config and Storage
    4. Cluster