Kubernetes was born out of the need to make our complex applications highly available, scalable, portable and deployable in small microservices independently. It also extends its capabilities to make adoption of DevOps processes and helps you set up modern Incident Response strategies to enhance the reliability of your applications.
Despite its complex internal architecture and API flows, Kubernetes makes it easy for users to do things with objects like deployments, services, ConfigMaps and secrets, since Kubernetes is designed to abstract away many of the complexities of managing these objects. As a result, users can focus on running their applications without having to worry about the internal details of Kubernetes.
Let's explore how users can interact with Kubernetes.
Kubectl is like a Swiss Army knife of container orchestration, deployments and management. It is a powerful command line tool that can be used to manage Kubernetes deployments and resources. With it, you can inspect Kubernetes objects, view logs and perform other operational tasks. Kubectl is so powerful that every action in Kubernetes can be controlled via kubectl commands.
There are two main ways to manage Kubernetes objects: imperative (with kubectl commands) and declarative (by writing manifests and then using kubectl apply). Each has its own advantages and disadvantages, so it's best to choose one method based on your use case.
Imperative commands are great for learning and interactive experiments, but they don't give you full access to the Kubernetes API. They're more commonly used to create YAML manifests and objects on the go. Declarative commands are useful for reproducible deployments and production. They're commonly used in CI/CD pipelines and Helm charts, where YAML manifests need to be present beforehand.
Each type of command has pros and cons, so it's important to decide which one will work best for your needs. Here's a brief overview of the pros and cons of each method.
In this section, we will see how to use kubectl imperative commands to interact with Kubernetes clusters and do various operations on Kubernetes objects. This can be useful for managing Kubernetes clusters and developing applications.
The kubectl CLI is commonly used to create objects. In order to create an object, kubectl’s imperative commands require the user to explicitly specify the type of object they wish to create, along with the properties they wish to associate with the object being created.
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/dd6286a94c58243e0a01ae3c704db4b6.js</p>
Since some Kubernetes objects have several subtypes, a user is required to specify the subtype-of-object parameter as well. For example, a Kubernetes service has several subtypes like ClusterIP, NodePort or LoadBalancer. A user is required to specify the service type as a subtype-of-object parameter when creating a service.
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/0b835b603874692bda31c2fe92fea58d.js</p>
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/332f894e36bced342debe289f1e5dc73.js</p>
Another example of where you would be required to specify a subtype-of-object parameter is when you're creating a secret. A secret inside Kubernetes can be of the generic, TLS or Docker-registry type.
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/67f0f5f915a2c01b20e6e65ac5ae8d75.js</p>
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/5396a49d889c07b1fb2ba1c5e0e6d41e.js</p>
It can be helpful to become more familiar with the imperative commands related to the creation, so let's take a look at some additional syntaxes along with examples.
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/ed88b49581a812b0ce12c1a61ab68fc7.js</p>
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/497f8e9a2c842a0f84f582f7ffa54a27.js</p>
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/846e9e23a397a1e4858bde6c7a414d45.js</p>
In addition to 'create', there are other functions that can help you work with Kubernetes, like 'expose' and 'run'. Let's take a look at them, too.
A service object is how a user interacts with an application in Kubernetes, and how different microservices communicate with each other. In other words, for any application to be usable on Kubernetes, it requires a service object.
The kubectl CLI is commonly used to expose applications by creating services. In order to expose an application — for example, deployments in most cases — kubectl's imperative commands require the user to explicitly specify the name of the deployment that they wish to expose, along with port numbers that they wish to expose on the deployment object.
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/3eb3198cdbb1af7320ea614d312e9cc9.js</p>
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/15bdc9c7c750eb821469efa2e18f734e.js</p>
A pod is the smallest unit of a containerized application that can be managed by Kubernetes. You usually create pods using deployment objects, but there are many use cases where you have to create a pod independently. In fact, creating a Kubernetes pod independently is a great way to get started with managing your own containerized applications. By understanding how to create and manage pods on your own, you'll be well on your way to becoming a Kubernetes expert.
Kubectl's CLI is commonly used to create pods. To create a pod, kubectl's imperative commands require the user to explicitly specify the image that would be running in the pod.
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/502d15c1f0807b68d08150aceffecd23.js</p>
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/eb5069aa9253ad5ea64876ad0859d8f0.js</p>
Apart from the commands discussed above, there are some important parameters that can help you work faster with Imperative commands and Kubernetes in general. Let’s take a look at them.
Kubernetes has a lot of commands, which makes it impossible for the user to memorize everything. Though everything is available on the documentation, going over the documentation, again and again, is not efficient and could seriously slow you down. Kubectl gives an elegant solution to this in the form of ‘explain’. You can think of ‘explain’ as mini documentation that is accessible through the CLI.
‘Explain’ provides a list of parameters that a command supports, along with the use case of the commands with examples. Most importantly, it can provide you with a full-fledged YAML containing all the parameters that the object supports. Let’s take a close look.
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/7312e67d0d533e247a354f2e4190d173.js</p>
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/1b40c66ce1535a0893b23ebc95e8cfa8.js</p>
If you're working with declarative commands, you'll need a YAML manifest. However, if you use imperative commands, you can generate these files without actually creating the objects. For example, if you use the `—dry-run=client -o yaml" flag with an imperative command like kubectl, you'll get a YAML version of the operation (like creating objects) that you're trying to do without actually applying the changes on the cluster. This can save you time and effort that would be spent writing YAML files from scratch. Plus, by storing the generated manifests in code repositories, you can use them declaratively as needed.
<p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/c42679753ef5ea5034fafef2fbe88241.js</p>
Learning any new technology takes time and effort, but with Kubernetes, there are many ways you can make the learning process easier. First, get familiar with kubectl and its documentation. Second, learn some of the imperative commands, which will help you remember key commands and perform operations more quickly.
In this article, we've covered everything you need to know about imperative commands — what they are, when to use them and how to use them correctly. We've provided multiple examples of advanced imperative commands in action, so you can start using them for your own experiments and projects. Thanks for reading!