7 Variants of Kubernetes Imperative commands to know!

Filed Under: Random
Kubernetes Imperative Commands

Hello, readers! In this article, we will be focusing on 7 Variants of Kubernetes Imperative commands to create resources, in detail.

So, let us begin!! 馃檪

There are two ways to generate and create Kubernetes resources —

  1. Declarative way — This method makes use of traditional YAML files to have all the configurations at one place.
  2. Imperative way — Here, we try to create resources through commands instead of YAML files, at ease.

Kubernetes Imperative commands

This article talks about the Imperative way of creating k8 resources. We will be creating various resources such as

  • Services
  • Creation of Pods
  • Deletion of Pods
  • Having a workload deployed
  • Attaching labels to namespaces, nodes, etc

1. Creation & Deletion of pod

We can have a pod running within all the basic configurations using the below command.

kubectl run pod-name –image image-name -n namespace

Here, we pass the image name on the basis of which the pod will run and function.

In the below example, we have created a pod named redis with the redis:alpine image. Further using –labels we have added a label tier=db to the pod.

kubectl run redis --image=redis:alpine --labels=tier=db

We can also delete all the pods of a particular namespace using the below commands. The –all-pods attribute deletes all the pods from the namespace ‘foo’.

kubectl delete --all pods --namespace=foo

2. Deployment of a workload

When it comes to deploying a workload, that can be easily done using the below imperative command.

kubectl create deploy httpd-frontend --image=httpd:2.4-alpine --replicas=3

Through the above command, we can create a deployment with the name httpd-frontend, setting the base image as httpd:2.4-alpine. Also, we can set the replicas for deployment right away during creation.


3. Imperative way of scaling a Replicaset

We can now scale the replicas for the workloads without having to visit and edit the YAML files.

  1. Here, we have scaled a replicaset (rs) to the max count of 5 using the below command.
kubectl scale --replicas=5 rs/new-replica-set

2. Here, we have scaled a deployment set to 6 replicas for the particular namespace demo.

kubectl -n demo scale --replicas=6 deploy demo-deploy

4. Exposing services in an imperative manner

With the below command, we can imperatively expose a pod to a service that is being created at run-time.

kubectl expose pod redis --port=6379 --target-port=6379 --name=redis-service

Now, the above command exposes the pod redis over the dynamically created service redis-service against port and target port as 6379. By this, we attach a backend service to the already running pod without having to create a separate service through YAML files.


5. Generation of secrets

The creation of secrets can also be performed easily through imperative commands as shown below.

kubectl create secret docker-registry gcr-pull-key \
--docker-server=gcr.io \
--docker-username=_json_key \
--docker-password="$(cat gke_key.json)" \
--docker-email=xyz@slb.com

Using the above command, we can create a docker-registry secret gcr-pull-key that stores the username and password (son file) for a particular service connection.

We can also create TLS type secrets using a single command.

Below, we have created a tls secret demo-tls passing the key and cert as a parameter for the particular namespace foo.

kubectl create secret tls demo-tls --key "auth.key" --cert "auth.cer" -n foo

6. Labelling the nodes

Apart from resources, we can easily add labels to nodes and namespaces using the label attribute.

Below, we have applied a label app=blue to node node1.

kubectl label node node1 app=blue

7. Setting image in an imperative way

We can alter the image set for the already deployed workload using the set image command as shown below.

kubectl set image deployment/myapp-deployment nginx=nginx:1.9.1

Here, we have set the image nginx:1.9.1 to the already deployed workload myapp-deployment.


Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you have any questions.

For more such posts related to Kubernetes, Stay tuned with us.

Till then, Happy Learning!! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content