Kubernetes Command line

To manage kubernetes cluster, you need to know some basic commands to be able to manage the cluster. Below are basic command line to manage pods, nodes, services, etc.
Pods
Using kubectl get pods
command:
Below command will list all available pod is default namespace
kubectl get pods
Output:
[rahil@k8s-master-node ~]$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-7587c6fdb6-dz7tj 1/1 Running 0 29m
Below command will watch for events and provide you status on console
kubectl get pods -w
Output:
[rahil@k8s-master-node ~]$ kubectl get pods -w
NAME READY STATUS RESTARTS AGE
nginx-7587c6fdb6-dz7tj 0/1 ContainerCreating 0 20s
nginx-7587c6fdb6-dz7tj 1/1 Running 0 58s
If you like to see more details on individual pods, you can use kubectl describe
kubectl describe pods <pod_name>
Output:
[rahil@k8s-master-node ~]$ kubectl describe pods nginx
Name: nginx-7587c6fdb6-dz7tj
Namespace: default
Node: k8s-worker-node-1/192.168.0.226
Start Time: Sun, 25 Feb 2018 17:06:46 -0500
Labels: pod-template-hash=3143729862
run=nginx
Annotations: <none>
Status: Running
IP: 10.244.1.2
Controlled By: ReplicaSet/nginx-7587c6fdb6
Containers:
nginx:
Container ID: docker://22a5e181b9c7b56351ccdc9ed41c1f6dfd776e56d45e464399d9a92479657a18
Image: nginx
Image ID: docker-pullable://docker.io/nginx@sha256:4771d09578c7c6a65299e110b3ee1c0a2592f5ea2618d23e4ffe7a4cab1ce5de
Port: 80/TCP
State: Running
Started: Sun, 25 Feb 2018 17:07:42 -0500
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-jpflm (ro)
Conditions:
Type Status
Initialized True
Ready True
PodScheduled True
Volumes:
default-token-jpflm:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-jpflm
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 6m default-scheduler Successfully assigned nginx-7587c6fdb6-dz7tj to k8s-worker-node-1
Normal SuccessfulMountVolume 6m kubelet, k8s-worker-node-1 MountVolume.SetUp succeeded for volume "default-token-jpflm"
Normal Pulling 6m kubelet, k8s-worker-node-1 pulling image "nginx"
Normal Pulled 6m kubelet, k8s-worker-node-1 Successfully pulled image "nginx"
Normal Created 6m kubelet, k8s-worker-node-1 Created container
Normal Started 5m kubelet, k8s-worker-node-1 Started container
Nodes
Below command will get all the nodes.
kubectl get nodes
Output:
[rahil@k8s-master-node ~]$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master-node Ready master 3h v1.9.3
k8s-worker-node-1 Ready <none> 2h v1.9.3
Below command will provide you all the information about nodes in table view mode.
kubectl get nodes -o wide
Output:
[rahil@k8s-master-node ~]$ kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
k8s-master-node Ready master 3h v1.9.3 <none> CentOS Linux 7 (Core) 3.10.0-693.17.1.el7.x86_64 docker://1.12.6
k8s-worker-node-1 Ready <none> 2h v1.9.3 <none> CentOS Linux 7 (Core) 3.10.0-693.17.1.el7.x86_64 docker://1.12.6
Below command will provide you detail about a node.
kubectl describe nodes <node_name>
Output:
[rahil@k8s-master-node ~]$ kubectl describe nodes k8s-worker-node-1
Name: k8s-worker-node-1
Roles: <none>
Labels: beta.kubernetes.io/arch=amd64
beta.kubernetes.io/os=linux
kubernetes.io/hostname=k8s-worker-node-1
Annotations: flannel.alpha.coreos.com/backend-data={"VtepMAC":"d2:bd:02:7a:cb:65"}
flannel.alpha.coreos.com/backend-type=vxlan
flannel.alpha.coreos.com/kube-subnet-manager=true
flannel.alpha.coreos.com/public-ip=192.168.0.226
node.alpha.kubernetes.io/ttl=0
volumes.kubernetes.io/controller-managed-attach-detach=true
Taints: <none>
CreationTimestamp: Sun, 25 Feb 2018 14:42:46 -0500
Conditions:
Type Status LastHeartbeatTime LastTransitionTime Reason Message
---- ------ ----------------- ------------------ ------ -------
OutOfDisk False Sun, 25 Feb 2018 17:22:32 -0500 Sun, 25 Feb 2018 14:42:46 -0500 KubeletHasSufficientDisk kubelet has sufficient disk space available
MemoryPressure False Sun, 25 Feb 2018 17:22:32 -0500 Sun, 25 Feb 2018 14:42:46 -0500 KubeletHasSufficientMemory kubelet has sufficient memory available
DiskPressure False Sun, 25 Feb 2018 17:22:32 -0500 Sun, 25 Feb 2018 14:42:46 -0500 KubeletHasNoDiskPressure kubelet has no disk pressure
Ready True Sun, 25 Feb 2018 17:22:32 -0500 Sun, 25 Feb 2018 14:44:06 -0500 KubeletReady kubelet is posting ready status
Addresses:
InternalIP: 192.168.0.226
Hostname: k8s-worker-node-1
Capacity:
cpu: 2
memory: 916556Ki
pods: 110
Allocatable:
cpu: 2
memory: 814156Ki
pods: 110
System Info:
Machine ID: 46a73b56be064f2fb8d81ac54f2e0349
System UUID: 8601D8C9-C1D9-2C4B-B6C9-2A3D2A598E4A
Boot ID: 963f0791-c81c-4bb1-aecd-6bd079abea67
Kernel Version: 3.10.0-693.17.1.el7.x86_64
OS Image: CentOS Linux 7 (Core)
Operating System: linux
Architecture: amd64
Container Runtime Version: docker://1.12.6
Kubelet Version: v1.9.3
Kube-Proxy Version: v1.9.3
PodCIDR: 10.244.1.0/24
ExternalID: k8s-worker-node-1
Non-terminated Pods: (3 in total)
Namespace Name CPU Requests CPU Limits Memory Requests Memory Limits
--------- ---- ------------ ---------- --------------- -------------
default nginx-7587c6fdb6-dz7tj 0 (0%) 0 (0%) 0 (0%) 0 (0%)
kube-system kube-flannel-ds-wtdz4 100m (5%) 100m (5%) 50Mi (6%) 50Mi (6%)
kube-system kube-proxy-m2hfm 0 (0%) 0 (0%) 0 (0%) 0 (0%)
Allocated resources:
(Total limits may be over 100 percent, i.e., overcommitted.)
CPU Requests CPU Limits Memory Requests Memory Limits
------------ ---------- --------------- -------------
100m (5%) 100m (5%) 50Mi (6%) 50Mi (6%)
Events: <none>
Services
Below command will give you all the available services in default namespace.
kubectl get services
Output:
[rahil@k8s-master-node ~]$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3h
Below command will give you all the services in all namespaces.
kubectl get services --all-namespaces
Output:
[rahil@k8s-master-node ~]$ kubectl get services --all-namespaces
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3h
kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP 3h