Kubernetes commands for beginners
Tags: beginners, tutorial, kubernetes, learning
Published: 2023-08-26 00:34:24

- 1. Create [Minikube cluster]
(https://minikube.sigs.k8s.io/docs/drivers/virtualbox/) on VirtualBox VM 
```
minikube start --driver=docker
minikube start --driver=virtualbox

minikube start --driver=virtualbox --no-vtx-check
minikube start --driver=virtualbox --no-vtx-check --memory=4098

minikube start --memory=4098 --driver=hyperkit
minikube start --memory=4098 --driver=hyperv
```
- 1.a View Context 
```
kubectl config view
```
- 1.b Switch context to Mini-Kube

```
kubectl config use-context minikube
kubectl config use-context arn:aws:eks:us-east-1:917165987323:cluster/demo-cluster

```
- 1.c Set Default 
- To make docker/Virtual the default driver:
```
minikube config set driver docker
minikube config set driver virtualbox
```
- 2. View cluster info
 
```
PS C:\WINDOWS\system32> kubectl cluster-info
Kubernetes control plane is running at https://192.168.XX.XXX:8443
CoreDNS is running at https://192.168.XX.XXX:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'
```
- 3. Get nodes 
```
PS C:\WINDOWS\system32> kubectl get nodes
NAME       STATUS   ROLES           AGE   VERSION
minikube   Ready    control-plane   13m   v1.27.4 
```
- 4. Get namespaces 
```
PS C:\WINDOWS\system32> kubectl get namespaces
NAME              STATUS   AGE
default           Active   14m
kube-node-lease   Active   14m
kube-public       Active   14m
kube-system       Active   14m
```
- 5. Get All pods 
```
PS C:\WINDOWS\system32> kubectl get pods -A
NAMESPACE     NAME                               READY   STATUS    RESTARTS      AGE
kube-system   coredns-5d78c9869d-df2qj           1/1     Running   0             14m
kube-system   etcd-minikube                      1/1     Running   0             14m
kube-system   kube-apiserver-minikube            1/1     Running   0             14m
kube-system   kube-controller-manager-minikube   1/1     Running   0             14m
kube-system   kube-proxy-8zw65                   1/1     Running   0             14m
kube-system   kube-scheduler-minikube            1/1     Running   0             14m
kube-system   storage-provisioner                1/1     Running   1 (13m ago)   14m
```
- 6. get all services 
```
PS C:\WINDOWS\system32> kubectl get services -A
NAMESPACE     NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
default       kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP                  15m
kube-system   kube-dns     ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   15m
```

- 7. get namespaces
```
PS C:\WINDOWS\system32> kubectl get namespaces
NAME              STATUS   AGE
default           Active   31m
kube-node-lease   Active   31m
kube-public       Active   31m
kube-system       Active   31m
```
- 8. delete namespaces through a yaml file
```
PS C:\WINDOWS\system32> kubectl delete -f namespace.yaml
```
- 9. get pods in "development" namespace
```
PS C:\WINDOWS\system32> kubectl get pods -n development
NAME                                   READY   STATUS    RESTARTS   AGE
pod-info-deployment-869667679b-b9k5s   1/1     Running   0          72s
pod-info-deployment-869667679b-kgkk4   1/1     Running   0          72s
pod-info-deployment-869667679b-snhnz   1/1     Running   0          72s
```
- 10. get pods in "default" namespace
```
PS C:\WINDOWS\system32> kubectl get pods
```
- 11. #delete pod with its NAME
Before: 
```
PS C:\WINDOWS\system32> kubectl get pods -n development
NAME                                   READY   STATUS    RESTARTS   AGE
pod-info-deployment-869667679b-b9k5s   1/1     Running   0          9m16s
pod-info-deployment-869667679b-kgkk4   1/1     Running   0          9m16s
pod-info-deployment-869667679b-snhnz   1/1     Running   0          9m16s
```
After: 
```
PS C:\WINDOWS\system32> kubectl delete pod pod-info-deployment-869667679b-snhnz -n development
pod "pod-info-deployment-869667679b-snhnz" deleted
```

- 12. get namespaces
```
PS C:\WINDOWS\system32> kubectl get ns
NAME              STATUS   AGE
default           Active   68m
development       Active   24m
kube-node-lease   Active   68m
kube-public       Active   68m
kube-system       Active   68m
production        Active   21m

```
- 13. get deployment list by  namespace
```
PS C:\WINDOWS\system32> kubectl get deployments -n development
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
pod-info-deployment   3/3     3            3           15m
```

- 14. get pods by namespace
```
PS C:\WINDOWS\system32> kubectl get pods -n development
NAME                                   READY   STATUS    RESTARTS   AGE
pod-info-deployment-869667679b-b9k5s   1/1     Running   0          15m
pod-info-deployment-869667679b-f5dcv   1/1     Running   0          3m11s
pod-info-deployment-869667679b-rd72p   1/1     Running   0          4m35s
```
- 15. describe pod 
```
PS C:\WINDOWS\system32> kubectl describe pod pod-info-deployment-869667679b-b9k5s -n development
Name:             pod-info-deployment-869667679b-b9k5s
Namespace:        development
Priority:         0
Service Account:  default
Node:             minikube/192.168.59.100
Start Time:       Fri, 25 Aug 2023 12:33:35 -0500
```
- 16. ssh into a container via busybox - through an interactive terminal  --> `kubectl exec -it busybox-55fdb79844-94tf8 -- /bin/sh`
```
PS C:\WINDOWS\system32> kubectl exec -it busybox-55fdb79844-94tf8 -- /bin/sh
/ # ls
bin    dev    etc    home   lib    lib64  proc   root   sys    tmp    usr    var
/ # wget 10.244.0.4:3000
Connecting to 10.244.0.4:3000 (10.244.0.4:3000)
saving to 'index.html'
index.html           100% |**************************************************************************************************************|   103  0:00:00 ETA
'index.html' saved
/ # cat index.html
{"pod_name":"pod-info-deployment-869667679b-b9k5s","pod_namespace":"development","pod_ip":"10.244.0.4"}/ # exit

```

- 17. get pods IP address 
 

```
kubectl get pods -n development -o wide
NAME                                   READY   STATUS    RESTARTS   AGE   IP            NODE       NOMINATED NODE   READINESS GATES
pod-info-deployment-869667679b-b9k5s   1/1     Running   0          90m   10.244.0.4    minikube   <none>           <none>
pod-info-deployment-869667679b-crx5q   1/1     Running   0          73m   10.244.0.9    minikube   <none>           <none>
pod-info-deployment-869667679b-rd72p   1/1     Running   0          78m   10.244.0.7    minikube   <none>           <none>
quote-service-5d654676cc-n7c4j         1/1     Running   0          74s   10.244.0.12   minikube   <none>           <none>
quote-service-5d654676cc-qzxx2         1/1     Running   0          74s   10.244.0.11   minikube   <none>           <none>

```
- 18. get pods in default namespace - you will find `busybox-55fdb79844-94tf8`

```
PS C:\WINDOWS\system32> kubectl get pods
NAME                       READY   STATUS    RESTARTS   AGE
busybox-55fdb79844-94tf8   1/1     Running   0          99m
```
- 19. Errors/ Troubleshooting: 
- If you get below error, run `minikube start --driver=virtualbox --no-vtx-check` [click](https://stackoverflow.com/questions/70813386/how-to-resolve-minikube-start-error-this-computer-doesnt-have-vt-x-amd-v-enab) here for more details
```
minikube v1.25.1 on Microsoft Windows 10 Home Single Language 10.0.19043 Build 19043
MINIKUBE_HOME=C:\os\minikube\Minikube
Automatically selected the virtualbox driver
Starting control plane node minikube in cluster minikube
Creating virtualbox VM (CPUs=2, Memory=4000MB, Disk=20000MB) ... ! StartHost failed, but will try again: creating host: create: precreate: This computer doesn't have VT-X/AMD-v enabled. Enabling it in the BIOS is mandatory
Creating virtualbox VM (CPUs=2, Memory=4000MB, Disk=20000MB) ...
Failed to start virtualbox VM. Running "minikube delete" may fix it: creating host: create: precreate: This computer doesn't have VT-X/AMD-v enabled. Enabling it in the BIOS is mandatory
```
- 20. Minikube commands
- Config: 
- `minikube config view`

```
minikube config view
- driver: virtualbox
```
- Show Docker context: `docker context show`
- Show docker context: `docker context ls`
- 

- 21. Delete Minikube Cluster
 - `minikube delete` 
 - `minikube delete --all` 
 - `minikube delete -o` 
 - `minikube delete -purge` 

```
--all=false:
        Set flag to delete all profiles

    -o, --output='text':
        Format to print stdout in. Options include: [text,json]

    --purge=false:
        Set this flag to delete the '.minikube' folder from your user directory.
```