Introduction to Kubernetes Imperative Commands

December 16, 2022
Share this post:
Introduction to Kubernetes Imperative Commands
Table of Contents:

    Introduction

    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.

    Running applications in Kubernetes

    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

    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.

    Imperative and Declarative methods

    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.

    Pros and Cons

    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.

    Command Pros Cons
    Declarative The chance for human error is low when the YAML manifest is already present. If you don't have YAML manifests, you cannot use declarative commands.
    Imperative Since you can create objects on the fly with these commands, productivity is increased. This is especially useful when you need to create an object quickly and don't have time to go through the process of creating it from scratch. There's an increased chance of human error when every configuration has to be specified in one-liners.

    Deep Dive Into Imperative Commands

    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.

    Create

    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.

    • The syntax for kubectl's imperative command to create objects can be summarized as follows:

    <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.

    • The syntax for creating a 'nodeport' service is pretty simple and can be summarized as follows:

    <p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/0b835b603874692bda31c2fe92fea58d.js</p>

    • An example of the above syntax:

    <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.

    • The syntax for creating a generic secret is pretty straightforward:

    <p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/67f0f5f915a2c01b20e6e65ac5ae8d75.js</p>

    • An example of the above syntax:

    <p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/5396a49d889c07b1fb2ba1c5e0e6d41e.js</p>

    Examples

    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.

    • Example: Create a config map with a ‘name=squadcast’ key-value pair.

    <p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/ed88b49581a812b0ce12c1a61ab68fc7.js</p>

    • Example: Create a deployment with the name Squadcast running three replicas of Ubuntu.

    <p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/497f8e9a2c842a0f84f582f7ffa54a27.js</p>

    • Example: Create a job with the name Squadcast executing the sleep command on Ubuntu.

    <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.

    Expose

    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.

    • To expose port 80 for your deployment, use the following syntax:

    <p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/3eb3198cdbb1af7320ea614d312e9cc9.js</p>

    • An example of the above syntax:

    <p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/15bdc9c7c750eb821469efa2e18f734e.js</p>

    Run

    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.

    • The syntax for running pod running ‘ubuntu’ is as follows:

    <p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/502d15c1f0807b68d08150aceffecd23.js</p>

    • An example of the above syntax:

    <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.

    Explain

    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.

    • Examples to see parameters and description of a command:

    <p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/7312e67d0d533e247a354f2e4190d173.js</p>

    • Examples to see a full-fledged YAML of a command:

    <p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/1b40c66ce1535a0893b23ebc95e8cfa8.js</p>

    Imperative to Declarative

    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.

    • Example: 

    <p>CODE: https://gist.github.com/ShubhanjanMedhi-dev/c42679753ef5ea5034fafef2fbe88241.js</p>

    Conclusion

    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!

    Squadcast is an incident management tool that’s purpose-built for SRE. Get rid of unwanted alerts, receive relevant notifications and integrate with popular ChatOps tools. Work in collaboration using virtual incident war rooms and use automation to eliminate toil.

    squadcast
    Written By:
    December 16, 2022
    December 16, 2022
    Share this post:
    Subscribe to our latest updates
    Enter your Email Id
    Thank you! Your submission has been received!
    Oops! Something went wrong while submitting the form.
    More from
    Squadcast Community
    Looking back at our journey through 2022
    Looking back at our journey through 2022
    December 30, 2022
    Introducing Squadcast Premium
    Introducing Squadcast Premium
    October 3, 2022
    Kubernetes as a Service using Amazon EKS
    Kubernetes as a Service using Amazon EKS
    June 20, 2022
    Learn how organizations are using Squadcast
    to maintain and improve upon their Reliability metrics
    Learn how organizations are using Squadcast to maintain and improve upon their Reliability metrics
    mapgears
    "Mapgears simplified their complex On-call Alerting process with Squadcast.
    Squadcast has helped us aggregate alerts coming in from hundreds...
    bibam
    "Bibam found their best PagerDuty alternative in Squadcast.
    By moving to Squadcast from Pagerduty, we have seen a serious reduction in alert fatigue, allowing us to focus...
    tanner
    "Squadcast helped Tanner gain system insights and boost team productivity.
    Squadcast has integrated seamlessly into our DevOps and on-call team's workflows. Thanks to their reliability...
    Alexandre Lessard
    System Analyst
    Martin do Santos
    Platform and Architecture Tech Lead
    Sandro Franchi
    CTO
    Squadcast is a leader in Incident Management on G2 Squadcast is a leader in Incident Management on G2 Users love Squadcast on G2 Best IT Management Products 2022 Squadcast is a leader in IT Service Management (ITSM) Tools on G2 Squadcast is a leader in IT Service Management (ITSM) Tools on G2 Squadcast is a leader in IT Service Management (ITSM) Tools on G2
    Squadcast awarded as "Best Software" in the IT Management category by G2 🎉 Read full report here.
    What our
    customers
    have to say
    mapgears
    "Mapgears simplified their complex On-call Alerting process with Squadcast.
    Squadcast has helped us aggregate alerts coming in from hundreds of services into one single platform. We no longer have hundreds of...
    Alexandre Lessard
    System Analyst
    bibam
    "Bibam found their best PagerDuty alternative in Squadcast.
    By moving to Squadcast from Pagerduty, we have seen a serious reduction in alert fatigue, allowing us to focus...
    Martin do Santos
    Platform and Architecture Tech Lead
    tanner
    "Squadcast helped Tanner gain system insights and boost team productivity.
    Squadcast has integrated seamlessly into our DevOps and on-call team's workflows. Thanks to their reliability metrics we have...
    Sandro Franchi
    CTO
    Revamp your Incident Response.
    Peak Reliability
    Easier, Faster, More Automated with SRE.
    Incident Response Mobility
    Manage incidents on the go with Squadcast mobile app for Android and iOS devices
    google playapple store
    Squadcast - On-call shouldn't suck. Incident response for SRE/DevOps, IT | Product Hunt Embed
    Squadcast is a leader in Incident Management on G2 Squadcast is a leader in Incident Management on G2 Users love Squadcast on G2 Best IT Management Products 2022 Squadcast is a leader in IT Service Management (ITSM) Tools on G2 Squadcast is a leader in IT Service Management (ITSM) Tools on G2 Squadcast is a leader in IT Service Management (ITSM) Tools on G2
    Squadcast - On-call shouldn't suck. Incident response for SRE/DevOps, IT | Product Hunt Embed
    Squadcast is a leader in IT Service Management (ITSM) Tools on G2 Squadcast is a leader in Incident Management on G2 Users love Squadcast on G2
    Best IT Management Products 2022 Squadcast is a leader in IT Service Management (ITSM) Tools on G2 Squadcast is a leader in IT Service Management (ITSM) Tools on G2
    Squadcast is a leader in IT Service Management (ITSM) Tools on G2
    Copyright © Squadcast Inc. 2017-2023