Kubernetes commands

1. Cluster Management commands

A Kubernetes cluster is a collection of nodes that execute containerized applications. 

It lets containers run across several machines and environments: cloud-based, virtual, on-premises, and physical.

Listed below are the kubectl commands that can be utilized to manage a cluster.

1. Display endpoint information regarding the services and master in the cluster

command:  kubectl cluster-info

2. Show the Kubernetes version functioning on the client and server

command: kubectl version

3. Get the configuration of the cluster

kubectl config view

4. Make a list of the available API resources

kubectl api-resources

5. Make a list of the available API versions

kubectl api-versions

6. List everything

kubectl get all –all-namespaces

2. Namespaces commands

Shortcode = ns

1.Create namespace <name>

kubectl create namespace <namespace_name>

2. List one or more namespaces

kubectl get namespace <namespace_name>

3. Show the detailed condition of one or more namespace

kubectl describe namespace <namespace_name>

4. Delete a namespace

kubectl delete namespace <namespace_name>

5. Edit and modify the namespace’s definition

kubectl edit namespace <namespace_name>

6. Display Resource (CPU/Memory/Storage) usage for a namespace

kubectl top namespace <namespace_name>

3. Node operations commands

A Node is a worker machine in Kubernetes and can either be a virtual or a physical machine, which depends on the cluster. Every Node is handled by the control plane. A Node can contain several pods, and the Kubernetes control plane handles scheduling the pods automatically across the Nodes in the cluster. The following commands can be utilized for Node Operations.

1. Revise the taints on one or more nodes

kubectl taint node <node_name>

2. List one or more nodes

kubectl get node

3. Delete a node or multiple nodes

kubectl delete node <node_name>

4. Display Resource usage (CPU/Memory/Storage) for nodes

kubectl top node

5. Resource allocation per node

kubectl describe nodes | grep Allocated -A 5

6. Pods running on a node

kubectl get pods -o wide | grep <node_name>

7. Annotate a node

kubectl annotate node <node_name>

8. Mark a node as unschedulable

kubectl cordon node <node_name>

9. Mark node as schedulable

kubectl uncordon node <node_name>

10. Drain a node in preparation for maintenance

kubectl drain node <node_name>

11. Add the labels of one or more nodes

kubectl label node

4. Listing Resources commands

Kubernetes resources are also regarded as Kubernetes objects related to a certain namespace, you can either utilize individual kubectl get command to jot down every resource one by one, or you can jot down all the resources in a Kubernetes namespace by executing a single command. Mentioned below is the list of commands to get the resource information.

1. Create a plain-text list of all namespaces

kubectl get namespaces

2. Create a plain-text list of all pods

kubectl get pods

3. Create a comprehensive plain-text list of all pods

kubectl get pods -o wide

4. Create a list of all pods functioning on a certain node server

kubectl gets pods–field-selector=spec. nodeName=[server-name]

5. In plain text, make a list of a specific replication controller

kubectl get replicationcontroller [replication-controller-name]

6. Generate a plain-text list of all replication services and controllers

kubectl get replicationcontroller, services

5. Daemonsets commands:

A Daemonset assures that some or all  Nodes run a copy of a Pod. As nodes are incorporated into the cluster, Pods are implemented to them. As nodes are erased from the cluster, those Pods are garbage collected. Erasing a DaemonSet will clean up the Pods it created.

1. List one or more daemonsets 

kubectl get daemonset

2. Edit and modify the definition of one or more daemonset

kubectl edit daemonset <daemonset_name>

3. Delete a daemonset

kubectl delete daemonset <daemonset_name>

4. Create a new daemonset

kubectl create daemonset <daemonset_name>

5. Manage the rollout of a daemonset

kubectl rollout daemonset

6. Show the comprehensive state of daemonsets within a namespace

kubectl describe ds <daemonset_name> -n <namespace_name>

6. Events commands:

Shortcode = ev

Kubernetes events are objects that display what is happening within a cluster, like what decisions were implemented by the scheduler or why some pods were erased from the node. Events are the first thing to look at for application, along with infrastructure operations when something is not functioning as anticipated. Mentioned below are the kubectl commands to get the events.

1. List current events for all resources in the system

kubectl get events

2. List Warnings only

kubectl get events –field-selector type=Warning

3. List events but exclude Pod events

kubectl get events –field-selector involvedObject.kind!=Pod

4. Pull events for a single node with a distinct name

kubectl get events –field-selector involvedObject.kind=Node, involvedObject.name=<node_name>

5. From a list of events, filter out normal events

kubectl get events –field-selector type!=Normal

7. Logs commands

You can use Kubernetes logs commands to monitor, log and debug the pods

1. Print the logs for a pod 

kubectl logs <pod_name>

2. Print the logs for a pod for the last hour

kubectl logs –since=1h <pod_name>

3. Get the current 20 lines of logs

kubectl logs –tail=20 <pod_name>

4. Get logs from a service and choose which container optionally

kubectl logs -f <service_name> [-c <$container>]

5. Adhere to new logs and print the logs for a pod

kubectl logs -f <pod_name>

6. For a container in a pod, Print the logs

kubectl logs -c <container_name> <pod_name>

7. Output the logs for a pod into a ‘pod.log’ file

kubectl logs <pod_name> pod.log

8. View the logs for the last failed pod

kubectl logs –previous <pod_name>

8. Deployments commands:

Shortcode = deploy.

A Kubernetes Deployment is utilized to inform Kubernetes how to design or change instances of the pods that hold a containerized application. Deployments can enhance the number of replica pods, enable the rollout of revised code in a controlled way, or roll back to an earlier deployment version if required.

1. List one or more deployments

kubectl get deployment

2. Show the in-depth state of one or more deployments

kubectl describe deployment <deployment_name>

3. Edit and revise the definition of one or more deployments on the server

kubectl edit deployment <deployment_name>

4.  Generate a new deployment

kubectl create deployment <deployment_name>

5. Delete deployments

kubectl delete deployment <deployment_name>

6. Check the rollout status of a deployment

kubectl rollout status deployment <deployment_name>

9. Replication Controllers commands:

Shortcode = rc

1. Make a list of  the replication controllers

kubectl get rc

2. Make a list of  the replication controllers by namespace

kubectl get rc –namespace=”<namespace_name>” 

10. ReplicaSets commands:

Shortcode = rs

1. List ReplicaSets

kubectl get replicasets

2. Show the detailed state of one or more ReplicaSets

kubectl describe replicasets <replicaset_name>

3. Scale a ReplicaSet

kubectl scale –replicas=[x]

11. Secrets commands:

A Kubernetes Secret is an object that comprises a minor portion of sensitive data like a token, a key, or a password. Such data might otherwise be inserted in an image or in a Pod specification. Users can build Secrets and the system also generates a few Secrets with the help of the following kubectl commands.

1. Create a secret

kubectl create secret

2. List secrets

kubectl get secrets

3. List details about secrets

kubectl describe secrets

4. Delete a secret

kubectldelete secret <secret_name>

12. Services and Service Accounts commands:

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.

1. Make a list of  one or more services

kubectl get services

2. Show the detailed state of a service

kubectl describe services

3. Reveal a replication controller, service, deployment, or pod as a new Kubernetes service

kubectl expose deployment [deployment_name]

4. Edit and modify the definition of one or more services

kubectl edit services

5. List service accounts

kubectl get serviceaccounts

6. Show the in-depth state of one or more service accounts

kubectl describe serviceaccounts

7. Replace a service account 

kubectl replace serviceaccount

8. Delete a service account

kubectl delete serviceaccount <service_account_name>

9. Kubectl commands adhere to syntax or a common structure, which lets administrators read and verify every kubectl command entered in the terminal window. There are four important parameters to each kubectl call:

Kubectl Syntax

10. The <command> parameter is the operation that should be executed on a resource. Kubectl backs several operations, such as describe, create, get, execute and delete

kubectl <command> <type> <name> <flags>

The <type> parameter specifies the resource type, like pods, bindings, and nodes. Usually, Resource type designations make use of abbreviations to streamline the command line. For instance, the “persistentvolumeclaims” type can be shortened to “pvc.” The <type> parameter is strong since there are several resource types, which also include namespaces, services, jobs, resource quotas, replication controllers, leases, and events. Programmers and Kubernetes administrators should be acquainted with a complete list of resource types.

The <name> parameter defines the name of the resource in the environment. If we omit the name parameter, the details for all resources are returned, similarly to a wildcard argument. Also, administrators can point out multiple resource types and names in the exact command line, as mentioned below.

11. This is effective when the names are all the same resource type, for instance:

kubectl <command> <type> <name1> <name2> … <nameX>

12. Kubectl syntax also backs the combination of several resource types and names on the exact command line in two ways:

kubectl get pod test-pod1 test-pod2

13. Lastly, the <flags> parameter incorporates optional flags to the command line. Flags differ with the command, so not all flags are available for all commands. For instance, the -s, (one dash shorthand notation) or –server (two dashes, longhand notation) flags designate the port and address of the Kubernetes API server.

kubectl <command> <type1/name1> <type2/name2> … <typeX/nameX>

or:

kubectl get pod/test-pod1 replicationcontroller/xyzcorp-rc1

The -o or –output <flag> sends responses to a terminal window in a certain format. For instance, the -o yaml flag will output a YAML-formatted API object, whereas the -o json flag will output a JSON-formatted API object.


Kubernetes Interview Question Set -1

Kubernetes Interview Question Set -2

No comments:

Post a Comment