Sunday, January 17, 2021

Kubernetes for Developers #7: Imperative vs. Declarative Kubernetes Objects

kubectl CLI supports both imperative and declarative ways to create and manage Kubernetes objects like Pod, Deployment, Service, namespace etc.

Imperative way:

In this approach, we operate directly on live objects in a cluster using kubectl CLI as arguments or flags.

It follows verb-driven commands to create or manage Kubernetes objects.

Syntax:


Kubectl create <objecttype> [<subtype>] <name> : # for creating new Kubernetes object
Kubectl run:  # for creating new Pod to run a container
Kubectl expose# used for creating new service object to load balance traffic
across multiple Pods
Kubetl autoscale# used for creating new Deployment for auto scaling Pods
Kubectl scale# used for add/remove Pods by updating replica count
Kubectl label# used for add/removing a label from an object
Kubectl edit# used for editing configuration in an editor
Kubectl delete# used for deleting an object from a cluster
Kubectl get# used for getting basic information about an object
Kubectl describe# used for getting detailed information about an object
Kubectl logs# used for getting stdout and stderr for a container running in a Pod

Example:


> kubectl create ns testnamespace
> kubectl create deployment nginx --image nginx 
> kubectl run my-helloworld --image helloworld:1.0
> kubectl get deployment my-helloworld
> kubectl describe deployment my-helloworld
> kubectl label deployment my-helloworld foo=bar
> kubectl expose deployment my-helloworld --port 80 --target-port 3000
> kubectl logs deployment/my-helloworld
> kubectl scale deployment my-helloworld –replicas=2
> kubectl delete deployment/nginx 

for more info visit @http://millionvisit.blogspot.com/2020/12/kubernetes-for-developers-3-kubectl-cli.html

Advantages:

  • Commands are simple, easy to learn and remember.
  • Commands require only a single step to make changes to the cluster.

Disadvantages

  • Commands cannot be committed to source control system and difficult to review.
  • Commands do not provide an audit trail associated with changes.
  • Commands do not provide a template for creating new objects.

Declarative way:

In this approach, we create YAML or JSON files for creating, updating, or deleting Kubernetes objects in a cluster using kubectl CLI.

Syntax:


kubectl create -f <filename|url># used for creating an object from a specified file
kubectl replace -f <filename|url># used for updating a live object from a specified file
kubectl delete -f <filename|url># used for deleting an object from a specified file
kubectl get -f <filename|url> -o yaml# used for viewing info about an object from a specified file
kubectl apply -f <filename|url> : # used for creating/Updating an object from a specified file
kubectl apply -f <directory> : # used for creating/Updating all objects specified in the directory

Example:


> kubectl create -f nginx.yaml
> kubectl delete -f nginx.yaml -f redis.yaml
> kubectl replace -f nginx.yaml
> kubectl get -f nginx.yaml -o yaml

for more info visit @http://millionvisit.blogspot.com/2020/12/kubernetes-for-developers-3-kubectl-cli.html

Advantages:

  • Object configurations can be stored in a source control system for reviewing changes before push.
  • Object configuration provides a template for creating new objects.

Disadvantages

  • Object configuration requires basic understanding of the object schema.
  • Object configuration requires the additional step of writing a YAML file.

Convert Imperative to Declarative way:

  Option A:

  1. Export the live object to a local object configuration file:

kubectl get {<kind>/<name>} --export -o yaml > {<kind>_<name>}.yaml

Ex: 

> kubectl get deployments testnginx --export -o yaml > testnginx2.yaml

Option B:  

  1.  Export the live object to a local object configuration file:

       kubectl get {<kind>/<name>} -o yaml > {<kind>_<name>}.yaml

> kubectl get deployments testnginx -o yaml > testnginx2.yaml  

2. Remove the status field from the configuration file

3. use replace command to execute the file

    kubectl replace -f <kind>_<name>.yaml

> kubectl replace -f testnginx2.yaml  

Kubernetes for Developers Journey.

Happy Coding :)

No comments:

Post a Comment