Kubernetes Imperative Commands Every Engineer Should Learn

Aug 5, 2020 · 3 min read

In this post, you will learn about the basic imperative commands that kubernetes offers, which will allow you to create and deploy objects more efficiently and stand out as a DevOps Engineer.

When working with kubernetes, you will mostly be creating objects in a declarative way using YAML definition files.

However, imperative commands can help in getting one time tasks done quickly, as well as generating definition file templates easily, saving a considerable amount of time.

Before we begin, familiarize yourself with the following command parameters:

--dry-run : By default, as soon as this is executed, the resource will be created.

--dry-run=client : This will NOT create the resource. Instead, kubernetes will tell you if it is possible to summon the object.

-o yaml : This will output the resource definition file in a YAML format on the screen.

Use the last two in combination to conveniently generate a resource definition file. Later, you can modify it as required, instead of writing the file from scratch.

For example, the following generates a POD manifest file:

kubectl run nginx --image=nginx --dry-run=client -o yaml

Let’s look at more imperative commands for generating PODs.

Pod Commands

Create an NGINX Pod:

kubectl run nginx --image=nginx

Generate a POD manifest YAML file:

kubectl run nginx --image=nginx --dry-run=client -o yaml

As a reminder, the -o yaml parameter tells kubectl to output a yaml file, while — dry-run instructs kubernetes to NOT create the POD.

Let’s now learn about deployment related imperative commands.

Deployment Commands

To create a deployment from the command line:

kubectl create deployment --image=nginx nginx

Generate a deployment YAML file template:

kubectl create deployment --image=nginx nginx --dry-run=client -o yaml

kubectl create deployment does not have a — replicas option. Having said that, if you wish to scale the deployment, you would have to first create it and then scale it using the kubectl scale command.

The following command will create a deployment YAML definition file with the name “nginx-deployment.yaml”:

kubectl create deployment --image=nginx nginx --dry-run=client -o yaml > nginx-deployment.yaml

Again, the -o parameter tells kubectl to output a yaml file, while the > nginx-deployment.yaml argument provides kubectl with the desired output file name.

You can then modify the YAML file to your needs.

Services Commands

The following command creates a service named “redis-service” of type ClusterIP, with the purpose of exposing the redis pod on port 6379.

kubectl expose pod redis --port=6379 --name redis-service --dry-run=client -o yaml

Note: This will automatically use the POD’s labels as selectors


kubectl create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml

The above command will not use the POD’s labels as selectors, instead it will assume selectors as app=redis. You cannot pass in selectors as an option. Alternatively, generate the file and modify the selectors before creating the service.

The following command creates a service named nginx of type NodePort to expose the nginx pod’s port 80 on port 30080 on the nodes:

kubectl expose pod nginx --port=80 --name nginx-service --type=NodePort --dry-run=client -o yaml

Note: This will automatically use the pod’s labels as selectors, but you cannot specify the node port. You have to generate a definition file and then add the node port in manually before creating the service with the pod.


kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml

This will not use the pod’s labels as selectors.

The last couple commands have their own challenges: One cannot accept a selector, the other cannot accept a node port. In my opinion, I recommend going with the kubectl expose command.

If you need to specify a node port, generate a definition file using the same command and manually input the nodeport before creating the service.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store