Wednesday, December 30, 2020

Kubernetes for Developers #6: Kubernetes Objects

Kubernetes objects are persistent entities and used to represent the state of the cluster.

It will be created by using imperative command on live objects or declaratively from a file.

kubectl command line interface used to send Kubernetes objects to API server and make necessary actions in the cluster.

In general, we specify Kubernetes objects in the .yml file and send to kubectl CLI. Kubectl converts the information to JSON while interacting with API server

Syntax:

apiVersionv1    # v1, apps/v1, and extensions/v1beta1
kindPod   # Pod, Deployment, Service etc.
metadata:
  namenginx-pod
spec:

  • apiVersion - version of the Kubernetes API you are using to create an object
  • kind - kind of object you want to create
  • metadata - It helps to uniquely identify the object, including a name, labels and optional namespace
  • spec – It is used to define desired state for the object

1. Pod

A pod is the most basic unit of the Kubernetes cluster. It usually contains one or more running containers. Pods are designed to be ephemeral in nature which means that they can be destroyed at any time. Containers in a pod share the same network and storage

apiVersionv1
kindPod
metadata:
  namefirst-pod
  labels:
    namefirst-pod
spec:
  containers:
    - namefirst-pod
      imagehello-world
      ports:
        - containerPort8080

containers: It contains,

  • name:The name of the container that you’ll run in your pod.
  • image:The image of the application you want to run in your pods.
  • containerPort:  It is the port of your application container is listening to.


2. ReplicationController

It is used to create multiple instances of same pod in the cluster node. It ensures that at any given time, the desired number of pods specified are in the running state. If a pod stops or dies, the ReplicationController creates another one to replace it.

apiVersionv1
kindReplicationController
metadata:
  namemyapp
spec:
  replicas2
  selector:
    appmyapp
  template:
    metadata:
      namefirst-pod
      labels:
        namefirst-pod
    spec:
      containers:
        - namefirst-pod
          imagehello-world
          ports:
            - containerPort8080

Definition says replicas:2 it means that any given time two pods must be running in the cluster. If any pod fails, replication controller creates new pod immediately.

The template section provides characteristics of the pod. It is like Pod definition.

3. Replicaset

It is the next generation of ReplicationController

Replicaset

ReplicationController

It uses matchLabels specified under selection and works as set-based selector

Ex: env=prod/qa

It selects all the objects where key=env irrespective of the value

It uses labels selected under selector section and works as equality-based selector

Ex:  env=prod

It selects all objects where key=env and value=prod

Selector attribute is mandatory

selector attribute is not required

It uses rollout technique and will be used internally by Deployment objects

It uses rolling update technique. It means, each pod template changes one at a time until all the pods are updated.

It is not meant to be created on their own, it creates automatically when deployment objects are created

It is used to create on their own

It belongs to apiVersion: apps/v1

It belongs to apiVersion: v1


apiVersionapps/v1
kindReplicaSet
metadata:
  namefrontend
  labels:
    appguestbook
    tierfrontend
spec:
  replicas3
  selector:
    matchLabels:
      tierfrontend
  template:
    metadata:
      labels:
        tierfrontend
    spec:
      containers:
        - namephp-redis
          imagegcr.io/google_samples/gb-frontend:v3

4. Deployment

It encapsulates both Replicaset and Pod to provide declarative method for defining a resource.

It is used for managing pods and internally uses Replicaset for creating number of pods.

It can be used to scale your application by increasing the number of running pods, or update the running application

apiVersionapps/v1
kindDeployment
metadata:
  namenginx-deployment
  labels:
    appnginx
spec:
  replicas3
  selector:
    matchLabels:
      appnginx
  template:
    metadata:
      labels:
        appnginx
    spec:
      containers:
        - namenginx
          imagenginx:1.7.9
          ports:
            - containerPort80

5. Namespace

It is used for grouping Kubernetes objects in the cluster and actions will be performed against the namespace.

Example: create namespace for each environment or Team

apiVersionv1
kindNamespace
metadata:
    nameproduction
_ _ _

apiVersionv1
kindNamespace
metadata:
    namestaging

namespace will be referred in the metadata section of Kubernetes object.

apiVersionv1
kindPod
metadata:
  namefirst-pod
  namespacestaging
  labels:
    namefirst-pod
    apphello-world-app
spec:

6. ConfigMap

It is used for creating configurations for the container and will be changed dynamically while containers are running

There are 3 ways to create configMap

a) from a directory:

  • Create a directory and name it my_config
  •  Inside the directory, create two files: name1.txt and name2.txt
  •  In the name1.txt file, add the following lines:
nameRama
gendermale
cityabcd

  •  In the name2.txt file add the following lines:

nameKrishna
gendermale
cityxyz

execute the following command to create the directory

> kubectl create configmap my-config --from-file=my_config/

b) from files:

The approach is the same as the one for creating ConfigMap from a directory. But in this case, we specify the file path instead of a folder

> kubectl create configmap my-config --from-file=my_config/name1.txt

c) from literals:

> kubectl create configmap my-config --from-literal=name=rama --from-literal=gender=male

We can consume ConfigMaps in the pod by specifying “ConfigMapRef”

apiVersionv1
kindPod
metadata:
  nameapi-test-pod
spec:
  containers:
    - nametest-container
      imagek8s.gcr.io/busybox
      command: ["/bin/sh""-c""env"]
      envFrom:
        - configMapRef:
            namemy-config

use “configMapKeyRef” for specifying selected key

apiVersionv1
kindPod
metadata:
  nameapi-test-pod
spec:
  containers:
    - nametest-container
      imagek8s.gcr.io/busybox
      command: ["/bin/sh""-c""env"]
      env:
        - nametest
          valuetest123
        - nameRAMA_CITY
          valueFrom:
            configMapKeyRef:
              namemy-config
              keycity


Kubernetes for Developers Journey.

Happy Coding :)

No comments:

Post a Comment