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. Please refer Kubectl commands Part 01 of this series for more information.
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
Services
[ved@arch ~]$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 19d
[ved@arch ~]$ kubectl describe svc
Name: kubernetes
Namespace: default
Labels: component=apiserver
provider=kubernetes
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.96.0.1
IPs: 10.96.0.1
Port: https 443/TCP
TargetPort: 6443/TCP
Endpoints: 192.168.122.113:6443,192.168.122.114:6443,192.168.122.115:6443
Session Affinity: None
Events: <none>
[ved@arch ~]$ kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 19d <none>
[ved@arch ~]$ kubectl get svc -o yaml
apiVersion: v1
items:
- apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2022-03-04T06:52:52Z"
labels:
component: apiserver
provider: kubernetes
name: kubernetes
namespace: default
resourceVersion: "203"
//Output omitted
[ved@arch ~]$ kubectl get svc -–show-labels
DaemonSets
[ved@arch ~]$ kubectl get ds
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
fluentd 4 4 4 4 4 <none> 9m17s
[ved@arch ~]$ kubectl get ds --all-namespaces
NAMESPACE NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
default fluentd 4 4 4 4 4 <none> 10m
kube-system kube-proxy 7 7 7 7 7 kubernetes.io/os=linux 19d
kube-system weave-net 7 7 7 7 7 <none> 15d
[ved@arch ~]$ kubectl describe ds [daemonset-name] -n [namespace-name]
[ved@arch ~]$ kubectl get ds [ds-name] -n [ns-name] -o yaml
Events
[ved@arch ~]$ kubectl get events
LAST SEEN TYPE REASON OBJECT MESSAGE
36m Normal SandboxChanged pod/apache-6d96f8c8d-hp422 Pod sandbox changed, it will be killed and re-created.
36m Normal Pulling pod/apache-6d96f8c8d-hp422 Pulling image "httpd"
35m Normal Pulled pod/apache-6d96f8c8d-hp422 Successfully pulled image "httpd" in 7.53548445s
35m Normal Created pod/apache-6d96f8c8d-hp422 Created container httpd
//Output omitted
[ved@arch ~]$ kubectl get events -n kube-system
LAST SEEN TYPE REASON OBJECT MESSAGE
37m Normal SandboxChanged pod/coredns-64897985d-67p2p Pod sandbox changed, it will be killed and re-created.
37m Normal Pulled pod/coredns-64897985d-67p2p Container image "k8s.gcr.io/coredns/coredns:v1.8.6" already present on machine
37m Normal Created pod/coredns-64897985d-67p2p Created container coredns
37m Normal Started pod/coredns-64897985d-67p2p Started container coredns
//Output omitted
[ved@arch ~]$ kubectl get events -w
Logs
[ved@arch ~]$ kubectl logs [pod-name]
[ved@arch ~]$ kubectl logs nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
[ved@arch ~]$ kubectl logs –-since=1h [pod-name]
[ved@arch ~]$ kubectl logs --since=1h nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh:
//Output omitted
[ved@arch ~]$ kubectl logs –-tail 20 [pod-name]
[ved@arch ~]$ kubectl logs --tail 20 nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh
//Output omitted
[ved@arch ~]$ kubectl logs -f -c [container-name] [pod-name]
[ved@arch ~]$ kubectl logs -f -c nginx nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/03/24 07:25:13 [notice] 1#1: using the "epoll" event method
2022/03/24 07:25:13 [notice] 1#1: nginx/1.21.6
2022/03/24 07:25:13 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
//Output omitted
[ved@arch ~]$ kubectl logs pod1 > pod.log
Conclusion
We have explored Kubernetes kubectl commands which are used in day to day life of sysadmins and developers.
Please refer given link for more kubectl commands.