A Savior — Imperative in K8s
There are two basic ways to deploy to Kubernetes: Imperative acts as a command which is active and immediate, whereas declarative is passive, by writing manifest file and using kubectl apply.
Why Imperative?
The imperative command is the first mode of managing objects, to use CLI for CUD (Create, Update, Delete) objects on Kubernetes cluster without specifying on manifest file ahead of time. They are a blessing for Kubernetes application developers and administrators because they are very easy to remember and handy. According to K8s, it’s like a ‘Swiss Army Knife” of container orchestration and management.
Imperative commands can help in getting tasks done quickly, as well as generating definition file templates easily. It saves a considerable amount of time and prevents human errors.
Object Creation
Imperative commands and objects are created, managed/modified using the CLI. As per kubernetes Documentation, the kubectl
tool supports creation commands driven by object type. These commands support more object types and are more explicit about their intent, but require users to know the type of objects they intend to create.
Creation of pods
kubectl run <pod-name> --image=<image-name> e.g. kubectl run nginx --image=nginx
Creation of config maps (Literals)
kubectl create cm <configmap-name> --from-literal=<key>=<value> e.g. kubectl create cm config.properties --from-literal=name=opstree
Creation of Secrets (Literals)
kubectl create secret generic <secret-name> --from-literal=<key>=<value> e.g. kubectl create secret generic usersecrets --from-literal=name=opstree
Creation of Deployments
kubectl create deployment <deployment-name> --image=<image-name> kubectl create deployment <deployment-name> --image=<image-name> --replicas=<replica-count>kubectl create deployment ubuntu --image=ubuntu --replicas=3
Creation of service
kubectl expose deployment <deployment-name> --type=<svc-type> --port=<port-number> --target-port=<target-port-number> --name=<svc-name>e.g. kubectl expose deployment nginx --type=ClusterIP --port=8080 --target-port=80 --name=nginx-clusterip-svc kubectl expose deployment ubuntu --type=NodePort --port=8080 --target-port=80 --name=nginx-nodeport-svc
Modifying properties of an existing deployments
Commands for common update operations include :
scale
: Horizontally scales a controller to add or remove Pods by updating the replica count of the controller.annotate
: Add or remove an annotation from an object.label
: Add or remove a label from an object.set
<field>
: Set the aspect of an object.
kubectl scale deployment <deployment-name> --replicas=<new-replica-count> kubectl set image deployment <deployment-name> <name-of-container>=<new-image-name>e.g. kubectl scale deployment nginx --replicas=4
kubectl set image deployment ubuntu ubuntu=ubuntu:18.04
Performing rollout on existing Deployment
kubectl rollout status deployment <deployment-name> kubectl rollout history deployment <deployment-name> kubectl rollout undo deployment <deployment-name> --to-revision=<revision-number>e.g. kubectl rollout status deployment nginx kubectl rollout history deployment nginx kubectl rollout undo deployment nginx --to-revision=2
Create role
kubectl create role <role-name> --verb=<list-of-verbs> --resource=<list-of-resource> kubectl create clusterrole <clusterrole-name> --verb=<list-of-verbs> --resource=<list-of-resource>e.g. kubectl create role dev --verb=get,list,watch --resource=pods,pods/status
kubectl create clusterrole qa --verb=get,list --resource=pods,services
Create role binding
kubectl create rolebinding <rolebinding-name> --role=<role-name> --serviceaccount=<namespace>:<serviceaccount-name> kubectl create clusterrolebinding <clusterrolebinding-name> --clusterrole=<role-name> --serviceaccount=<namespace>:<serviceaccount-name>e.g. kubectl create rolebinding devrole --role=dev --serviceaccount=default:opstree
kubectl create clusterrolebinding qarb --clusterrole=qa --serviceaccount=default:opstree
Summary:
Hope you got the list of important imperative kubectl
commands to communicate in HTTP REST API which is the dereliction stoner interface of the Kubernetes platform. The stylish way to learn all these kubectl
commands is to try using them in the Kubernetes platform and find out the results driven by it. And if you guys have come across any more such imperative commands then do share with us in the comment box.
Originally published at http://blog.opstree.com on April 19, 2022.