Kubernetes – Kubectl working and Common Commands
Kubectl is the command line configuration tool to interact with Kubernetes clusters using Kubernetes API server. kubectl allows users to create, inspect, update, and delete Kubernetes objects.
Kubectl Charctersitisc
- Kubectl can be pronounced as “cube CTL”, “kube control”, “cube cuttle”
- It is a is a robust CLI that runs commands against the Kubernetes cluster and controls the cluster manager
- kubectl is known as the swiss army knife of container orchestration and management
- kubectl is designed to make this process more comfortable and straightforward
- kubectl allows users to create, inspect, update, and delete Kubernetes objects
- Every Kubernetes command has an API endpoint, and kubectl’s primary purpose is to carry out HTTP requests to the API.
How Kubectl works?
Every time when we run a command with kubectl, it builds an HTTP REST API request underneath and sends this request to the Kubernetes API server, and then retrieves the result and displays it on your terminal. In fact, when we want to execute any Kubernetes operation, we can simply make an HTTP request to its corresponding API endpoint.
Kuberents can be controlled with a tool like curl by manually issuing HTTP requests to the Kubernetes API but Kubectl just makes it easier for us to use the Kubernetes API.
Most Common Kubectl Commands:
- Cluster Management: A Kubernetes cluster is a set of nodes that run containerized applications. It allows containers to run across multiple machines and environments: virtual, physical, cloud-based, and on-premises. Following kubectl commands can be used to manage a cluster
- kubectl cluster-info : Display endpoint information about the master and services in the cluster
- kubectl version : Display the Kubernetes version running on the client and server
- kubectl config view : Get the configuration of the cluster
- kubectl api-resource : List the API resources that are available
- kubectl api-versions : List the API versions that are available
- kubectl get all –all -namespaces : List everything
- Deployments : A Kubernetes Deployment is used to tell Kubernetes how to create or modify instances of the pods that hold a containerized application. Deployments can scale the number of replica pods, enable rollout of updated code in a controlled manner, or roll back to an earlier deployment version if necessary.
- kubectl get deployment : List one or more deployments
- kubectl describe deployment <deployment_name> : Display the detailed state of one or more deployments
- kubectl edit deployment <deployment_name> : Edit and update the definition of one or more deployment on the server
- kubectl create deployment <deployment_name> : Create one a new deployment
- kubectl delete deployment <deployment_name> : Delete deployments
- kubectl rollout status deployment <deployment_name> : See the rollout status of a deployment
- Events: Kubernetes events are objects that show you what is happening inside a cluster, such as what decisions were made by the scheduler or why some pods were evicted from the node. Events are the first thing to look at for application, as well as infrastructure operations when something is not working as expected. Following are the kubectl commands to get the events.
- kubectl get events : List recent events for all resources in the system
- kubectl get events –field-selector type=Warning : List Warnings only
- kubectl get events –field-selector involvedObject.kind!=Pod : List events but exclude Pod events
- kubectl get events –field-selector involvedObject.kind=Node, involvedObject.name=<node_name> : Pull events for a single node with a specific name
- kubectl get events –field-selector type!=Normal : Filter out normal events from a list of events
- Logs : Kubernets logs commands can be used to monitor, logging and debugging the pods.
- kubectl logs <pod_name> : Print the logs for a pod
- kubectl logs –since=1h <pod_name> : Print the logs for the last hour for a pod
- kubectl logs –tail=20 <pod_name> : Get the most recent 20 lines of logs
- kubectl logs -f <service_name> [-c <$container>] : Get logs from a service and optionally select which container
- kubectl logs -f <pod_name> : Print the logs for a pod and follow new logs
- kubectl logs -c <container_name> <pod_name> : Print the logs for a container in a pod
- kubectl logs <pod_name> pod.log : Output the logs for a pod into a file named ‘pod.log’
- kubectl logs –previous <pod_name> : View the logs for a previously failed pod
- Namespaces : Namespaces are Kubernetes objects which partition a single Kubernetes cluster into multiple virtual clusters. Each Kubernetes namespace provides the scope for Kubernetes Names it contains; which means that using the combination of an object name and a Namespace, each object gets an unique identity across the cluster.
- kubectl create namespace <namespace_name> : Create namespace <name>
- kubectl get namespace <namespace_name> : List one or more namespaces
- kubectl describe namespace <namespace_name> : Display the detailed state of one or more namespace
- kubectl delete namespace <namespace_name> : Delete a namespace
- kubectl edit namespace <namespace_name> : Edit and update the definition of a namespace
- kubectl top namespace <namespace_name> : Display Resource (CPU/Memory/Storage) usage for a namespace
- Node Operations: A Node is a worker machine in Kubernetes and may be either a virtual or a physical machine, depending on the cluster. Each Node is managed by the control plane. A Node can have multiple pods, and the Kubernetes control plane automatically handles scheduling the pods across the Nodes in the cluster. Following commands can be used for Node Operations.
- kubectl taint node <node_name> : Update the taints on one or more nodes
- kubectl get node : List one or more nodes
- kubectl delete node <node_name> : Delete a node or multiple nodes
- kubectl top node : Display Resource usage (CPU/Memory/Storage) for nodes
- kubectl describe nodes | grep Allocated -A 5 : Resource allocation per node
- kubectl get pods -o wide | grep <node_name> : Pods running on a node
- kubectl annotate node <node_name> : Annotate a node
- kubectl cordon node <node_name> : Mark a node as unschedulable
- kubectl uncordon node <node_name> : Mark node as schedulable
- kubectl drain node <node_name> : Drain a node in preparation for maintenance
- kubectl label node : Add or update the labels of one or more nodes
- Pods : Pods are the atomic unit on the Kubernetes platform. When we create a Deployment on Kubernetes, it creates Pods with containers inside them. Each Pod is tied to the Node where it is scheduled, and remains there until termination or deletion or restarted. Following kubectl command can be used for Pods Operations.
- kubectl get pod : List one or more pods
- kubectl delete pod <pod_name> : Delete a pod
- kubectl create pod <pod_name> : Create a pod
- kubectl exec <pod_name> -c <container_name> <command> : Execute a command against a container in a pod
- kubectl exec -it <pod_name> /bin/sh : Get interactive shell on a a single-container pod
- kubectl top pod : Display Resource usage (CPU/Memory/Storage) for pods
- kubectl describe pod <pod_name> : Display the detailed state of a pods
- kubectl annotate pod <pod_name> <annotation> : Add or update the annotations of a pod
- kubectl label pod <pod_name> : Add or update the label of a pod
- Replication Controllers and ReplicaSets
- kubectl get rc : List the replication controllers
- kubectl get rc –namespace=”<namespace_name>” : List the replication controllers by namespace
- kubectl get replicasets : List ReplicaSets
- kubectl describe replicasets <replicaset_name> : Display the detailed state of one or more ReplicaSets
- kubectl scale –replicas=[x] : Scale a ReplicaSet
- Secrets: A Kubernets Secret is an object that contains a small amount of sensitive data such as a password, a token, or a key. Such information might otherwise be put in a Pod specification or in an image. Users can create Secrets and the system also creates some Secrets using following kubectl commands.
- kubectl create secret : Create a secret
- kubectl get secrets : List secrets
- kubectl describe secrets : List details about secrets
- kubectl delete secret <secret_name> : Delete a secret
- Services and Service Accounts: A Kubernetes service is a logical abstraction for a deployed group of pods in a cluster (which all perform the same function) and Service accounts are used to provide an identity for pods. Pods that want to interact with the API server will authenticate with a particular service account.
- kubectl get services : List one or more services
- kubectl describe services : Display the detailed state of a service
- kubectl expose deployment [deployment_name] : Expose a replication controller, service, deployment or pod as a new Kubernetes service
- kubectl edit services : Edit and update the definition of one or more services
- kubectl get serviceaccounts : List service accounts
- kubectl describe serviceaccounts : Display the detailed state of one or more service accounts
- kubectl replace serviceaccount : Replace a service account
- kubectl delete serviceaccount <service_account_name> : Delete a service account
Reference:
- https://kubernetes.io/
- https://raw.githubusercontent.com/sangam14/kubernets101/
Related Posts:
- NFV Management and Orchestration [MANO] key and performance indicators [KPIs]
- Network Function Virtualization (NFV)
- Virtual Network Function (VNF) Definition, Architecture and Design
- Virtual Network’s Most Common Definitions
- Open Stack, its components and deployment models
- Open Stack Undercloud and Overcloud