Kubernetes is an open source container orchestration engine. It provides basic mechanisms for deployment, maintenance, and scaling of applications. Kubernetes is hosted by the Cloud Native Computing Foundation (CNCF). In this tutorial, we will explore Kubernetes kubectl commands which are used in day to day life of sysadmins or developers.
Lab
We have setup 7 nodes kubernetes cluster which consists of 3 master nodes and 4 worker nodes.
[ved@arch ~]$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
master1.example.com Ready control-plane,master 18d v1.23.4
master2.example.com Ready control-plane,master 18d v1.23.4
master3.example.com Ready control-plane,master 18d v1.23.4
node1.example.com Ready <none> 18d v1.23.4
node2.example.com Ready <none> 18d v1.23.4
node3.example.com Ready <none> 18d v1.23.4
node4.example.com Ready <none> 18d v1.23.4
Kubectl commands for Viewing Resource Information
Nodes
[ved@arch ~]$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
master1.example.com Ready control-plane,master 18d v1.23.4
master2.example.com Ready control-plane,master 18d v1.23.4
master3.example.com Ready control-plane,master 18d v1.23.4
node1.example.com Ready <none> 18d v1.23.4
node2.example.com Ready <none> 18d v1.23.4
node3.example.com Ready <none> 18d v1.23.4
node4.example.com Ready <none> 18d v1.23.4
[ved@arch ~]$ kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
master1.example.com Ready control-plane,master 18d v1.23.4 192.168.122.113 <none> CentOS Linux 7 (Core) 3.10.0-1160.59.1.el7.x86_64 docker://1.13.1
master2.example.com Ready control-plane,master 18d v1.23.4 192.168.122.114 <none> CentOS Linux 7 (Core) 3.10.0-1160.59.1.el7.x86_64 docker://1.13.1
master3.example.com Ready control-plane,master 18d v1.23.4 192.168.122.115 <none> CentOS Linux 7 (Core) 3.10.0-1160.59.1.el7.x86_64 docker://1.13.1
node1.example.com Ready <none> 18d v1.23.4 192.168.122.110 <none> CentOS Linux 7 (Core) 3.10.0-1160.59.1.el7.x86_64 docker://1.13.1
node2.example.com Ready <none> 18d v1.23.4 192.168.122.111 <none> CentOS Linux 7 (Core) 3.10.0-1160.59.1.el7.x86_64 docker://1.13.1
node3.example.com Ready <none> 18d v1.23.4 192.168.122.112 <none> CentOS Linux 7 (Core) 3.10.0-1160.59.1.el7.x86_64 docker://1.13.1
node4.example.com Ready <none> 18d v1.23.4 192.168.122.116 <none> CentOS Linux 7 (Core) 3.10.0-1160.59.1.el7.x86_64 docker://1.13.1
[ved@arch ~]$ kubectl describe nodes
[ved@arch ~]$ kubectl get no -o yaml
[ved@arch ~]$ kubectl get node –select or =[ label -name]
[ved@arch ~]$ kubectl top node [node_name]
[ved@arch ~]$ kubectl top node node1.example.com
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
node1.example.com 34m 1% 345Mi 19%
Pods
[ved@arch ~]$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 89s
[ved@arch ~]$
[ved@arch ~]$ kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 2m13s 10.43.0.1 node4.example.com <none> <none>
[ved@arch ~]$ kubectl describe po
Name: nginx
Namespace: default
Priority: 0
Node: node4.example.com/192.168.122.116
Start Time: Tue, 22 Mar 2022 19:22:00 +0530
Labels: run=nginx
Annotations: <none>
Status: Running
IP: 10.43.0.1
IPs:
IP: 10.43.0.1
Containers:
nginx:
Container ID: docker://37a9d0f36d14ab23ddd675cab75d6811386824cc717c1b7b90902b93c7d7e84a
Image: nginx
Image ID: docker-pullable://docker.io/nginx@sha256:e1211ac17b29b585ed1aee166a17fad63d344bc973bc63849d74c6452d549b3e
Port: <none>
Host Port: <none>
State: Running
Started: Tue, 22 Mar 2022 19:23:15 +0530
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-7hh8t (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-7hh8t:
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 4m default-scheduler Successfully assigned default/nginx to node4.example.com
Normal Pulling 3m59s kubelet Pulling image "nginx"
Normal Pulled 2m44s kubelet Successfully pulled image "nginx" in 1m14.475060065s
Normal Created 2m44s kubelet Created container nginx
Normal Started 2m44s kubelet Started container nginx
[ved@arch ~]$ kubectl get po --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 19m run=nginx
pod1 1/1 Running 0 13m env=prod
[ved@arch ~]$ kubectl get po -l='env=prod'
NAME READY STATUS RESTARTS AGE
pod1 1/1 Running 0 9m51s
[ved@arch ~]$ kubectl get po -o yaml
[ved@arch ~]$ kubectl get pod nginx -o yaml
[ved@arch ~]$ kubectl get pod [pod_name] -o yaml > file.yaml
Namespaces
[ved@arch ~]$ kubectl get ns
NAME STATUS AGE
default Active 18d
kube-node-lease Active 18d
kube-public Active 18d
kube-system Active 18d
metrics Active 13d
[ved@arch ~]$ kubectl get ns -o yaml
[ved@arch ~]$ kubectl describe ns
Name: default
Labels: kubernetes.io/metadata.name=default
Annotations: <none>
Status: Active
No resource quota.
No LimitRange resource.
Name: kube-node-lease
Labels: kubernetes.io/metadata.name=kube-node-lease
Annotations: <none>
Status: Active
No resource quota.
No LimitRange resource.
Name: kube-public
Labels: kubernetes.io/metadata.name=kube-public
Annotations: <none>
Status: Active
No resource quota.
No LimitRange resource.
Name: kube-system
Labels: kubernetes.io/metadata.name=kube-system
Annotations: <none>
Status: Active
No resource quota.
No LimitRange resource.
Name: metrics
Labels: kubernetes.io/metadata.name=metrics
Annotations: <none>
Status: Active
No resource quota.
No LimitRange resource.
Deployments
[ved@arch ~]$ kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
apache 1/1 1 1 86s
[ved@arch ~]$ kubectl describe deploy
Name: apache
Namespace: default
CreationTimestamp: Wed, 23 Mar 2022 10:27:16 +0530
Labels: app=apache
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=apache
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=apache
Containers:
httpd:
Image: httpd
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: apache-6d96f8c8d (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 2m26s deployment-controller Scaled up replica set apache-6d96f8c8d to 1
[ved@arch ~]$ kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
apache 1/1 1 1 3m58s httpd httpd app=apache
[ved@arch ~]$ kubectl get deploy -o yaml
apiVersion: v1
items:
- apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2022-03-23T04:57:16Z"
generation: 1
labels:
app: apache
name: apache
namespace: default
//[ved@arch ~]$ kubectl get deploy -o yaml
apiVersion: v1
items:
- apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
creationTimestamp: "2022-03-23T04:57:16Z"
generation: 1
labels:
app: apache
name: apache
namespace: default
//Output omitted
Conclusion
We have explored Kubernetes kubectl commands which are used in day to day life of sysadmins and developers. Kubernetes kubectl commands are very long. Please refer below article for aliasing kubectl commands using which you can run kubectl commands very quickly. Please refer given link for more kubectl commands.