Kubectl Commands
- To get kubernetes cluster info.
$ kubectl cluster-info
Kubernetes control plane is running at https://192.168.49.2:8443
CoreDNS is running at https://192.168.49.2:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
- To get node information created by minikube
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane 15h v1.28.3
- To get pods informations
$ kubectl get pods
No resources found in default namespace.
Note: This command returns pods informations that are present in default namespace.
- Namespaces are used to group different resources and configurations. To get namespace information:
$ kubectl get namespaces
NAME STATUS AGE
default Active 15h
kube-node-lease Active 15h
kube-public Active 15h
kube-system Active 15h
- To get pods running in a specific namespace.
$ kubectl get pods --namespace=kube-system
NAME READY STATUS RESTARTS AGE
coredns-5dd5756b68-wzz75 1/1 Running 1 (15h ago) 16h
etcd-minikube 1/1 Running 1 (15h ago) 16h
kube-apiserver-minikube 1/1 Running 1 (15h ago) 16h
kube-controller-manager-minikube 1/1 Running 1 (15h ago) 16h
kube-proxy-db2cf 1/1 Running 1 (15h ago) 16h
kube-scheduler-minikube 1/1 Running 1 (15h ago) 16h
storage-provisioner 1/1 Running 3 (18m ago) 16h
- To run nginx docker image inside of a pod.
$ kubectl run nginx --image=nginx
pod/nginx created
The nginx is the name of the pod and --image=nginx specifies the name of docker image from dockerhub.
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 0/1 ContainerCreating 0 15s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 4m20s
- To view detailed information about the pods.
$ kubectl describe pods
Name: nginx
Namespace: default
Priority: 0
Service Account: default
Node: minikube/192.168.49.2
Start Time: Tue, 23 Jan 2024 10:51:47 +0530
Labels: run=nginx
Annotations: <none>
Status: Running
IP: 10.244.0.4
IPs:
IP: 10.244.0.4
Containers:
nginx:
Container ID: docker://eaf445e826d40b29c18a8925648cf7d0d4249c381a3d6fc4deaef69f2a92c1ea
Image: nginx
Image ID: docker-pullable://nginx@sha256:4c0fdaa8b6341bfdeca5f18f7837462c80cff90527ee35ef185571e1c327beac
Port: <none>
Host Port: <none>
State: Running
Started: Tue, 23 Jan 2024 10:55:48 +0530
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-fv2bm (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-fv2bm:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 5m29s default-scheduler Successfully assigned default/nginx to minikube
Normal Pulling 5m28s kubelet Pulling image "nginx"
Normal Pulled 88s kubelet Successfully pulled image "nginx" in 4m0.152s (4m0.152s including waiting)
Normal Created 88s kubelet Created container nginx
Normal Started 88s kubelet Started container nginx
- Get container informations by logging in to node.
$ minikube ssh
docker@minikube:~$ docker ps | grep nginx
eaf445e826d4 nginx "/docker-entrypoint.…" 15 minutes ago Up 15 minutes k8s_nginx_nginx_default_6d27b85e-d4c7-44c8-9852-c9731b376fb4_0
aee51b35b689 registry.k8s.io/pause:3.9 "/pause" 19 minutes ago Up 19 minutes k8s_POD_nginx_default_6d27b85e-d4c7-44c8-9852-c9731b376fb4_0
-
The 'pause' container is a container which holds the network namespace for the pod. Kubernetes creates pause containers to acquire the respective pod’s IP address and set up the network namespace for all other containers that join that pod.
-
Log in the container and ping the nginx server.
$ docker exec -it eaf445e826d4 sh
# hostname
nginx
# hostname -i
10.244.0.4
# curl 10.244.0.4
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
# exit
- Get IP Address of all containers running in the pods using kubectl.
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 37m 10.244.0.4 minikube <none> <none>
- To delete pods.
$ kubectl delete pod nginx
pod "nginx" deleted
- To scale the deployments
$ kubectl scale deployment nginx-deployment --replicas=5
deployment.apps/nginx-deployment scaled