Kubectl; Basic Client for Interaction within a Kubernetes Cluster
Table of contents
No headings in the article.
Kubectl is a command line tool used to execute, deploy, get and interact within the Kubernetes cluster. Kubectl worked with the Kubernetes RESTAPI to be able to discharge its utilities and perform the required task.
There are other clients such as the Kubernetes Dashboard which can be used to interact within a cluster but kubectl comes in handy when it comes to the command line.
Other than the HELM, the commands in Kubernetes is run with Kubectl and this blogpost shall look into a few of them.
- Kubectl get
This kubectl command is used to get the object in the Kubernetes cluster, the object could be deployments, pods, nodes, replicasets, services, etc and also with the kubectl get command, you can view any Kubernetes object with information such as name, type, cluster IP address, port and its communication protocol, age, status, etc.
$ kubectl get <object-name>
There is more you can do with the kubectl command such as getting all namespaces, setting all output, finding selectors, etc $ kubectl get --help will show you all the tags that you can parse within the command to get more information.
- Kubectl create
This command is used to create a new object or component, several objects can be created in Kubernetes such as replicas, pods, namespaces, services, deployments, etc.
Even though pods are the smallest unit of a Kubernetes cluster, it is a good practice to be created just like that, instead, the abstraction layer of the pod called “deployment” can be created using this command.
When a deployment is created using this command, it will automatically create a pod at its basic level and a replicaset at its middle using the pod creation blueprint.
To create a deployment, certain parameters are to be specified
$ kubectl create <object type> <choose a name for your deployment> --image=<specify the container image>
What this would do is create a deployment based on the container image specified. The image would be automatically downloaded from the docker repository. A pod and a replicaset were also created off the deployment.
In essence, a deployment manages a replicaset, a replicaset manages the replicas of pods within it while the pod contains an abstraction of a container. Also, after deployment is created, it can be edited and the modification will take effect on the lineups below it, the replicaSet, and the pod.
- Kubectl apply
kubectl apply has the same usage as the create command. The little distinction is that kubectl is used to create more resources or modify an existing object.
The apply function takes the configuration and parameters specified or predefined in the config map, a YAML file for Kubernetes object configuration, and creates an object based on that.
$ kubectl apply -f <filename>
The -f tag above is used to specify that mongo.yaml is a file.
- Kubectl delete
This command is used to delete or remove an object from the Kubernetes cluster.
$ kubectl delete <object-type> <object-name or object id>
- Kubectl exec
This command is used to execute commands in a running container. The exec command can only execute when the resource in the cluster is in its running state. Aside from executing containers in a running state, $ kubectl exec is also a powerful debugging tool.
$ kubectl exec -it <object_name or id> /bin/bash
Where $ kubectl exec is the command used to execute a command in a container, -it is used to set the container in an interactive mode, object-name is the name of the deployment whereas /bin/bash is to execute in a shell.
- Kubectl describe
With the kubectl command, you can get detailed information about any Kubernetes object. Information ranging from the name, IP address, service account, node, container information, condition, volume, event, etc.
$ kubectl describe <object-type> <object-name>
- Kubectl logs
This command shows detailed log information of the objects running within a cluster.
$ kubectl logs <object_name>