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:

apiVersion: v1    # v1, apps/v1, and extensions/v1beta1
kind: Pod   # Pod, Deployment, Service etc.
metadata:
  name: nginx-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

apiVersion: v1
kind: Pod
metadata:
  name: first-pod
  labels:
    name: first-pod
spec:
  containers:
    - name: first-pod
      image: hello-world
      ports:
        - containerPort: 8080

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.

apiVersion: v1
kind: ReplicationController
metadata:
  name: myapp
spec:
  replicas: 2
  selector:
    app: myapp
  template:
    metadata:
      name: first-pod
      labels:
        name: first-pod
    spec:
      containers:
        - name: first-pod
          image: hello-world
          ports:
            - containerPort: 8080

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


apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
        - name: php-redis
          image: gcr.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

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.7.9
          ports:
            - containerPort: 80

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

apiVersion: v1
kind: Namespace
metadata:
    name: production
_ _ _

apiVersion: v1
kind: Namespace
metadata:
    name: staging

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

apiVersion: v1
kind: Pod
metadata:
  name: first-pod
  namespace: staging
  labels:
    name: first-pod
    app: hello-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:
name: Rama
gender: male
city: abcd

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

name: Krishna
gender: male
city: xyz

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”

apiVersion: v1
kind: Pod
metadata:
  name: api-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: ["/bin/sh", "-c", "env"]
      envFrom:
        - configMapRef:
            name: my-config

use “configMapKeyRef” for specifying selected key

apiVersion: v1
kind: Pod
metadata:
  name: api-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: ["/bin/sh", "-c", "env"]
      env:
        - name: test
          value: test123
        - name: RAMA_CITY
          valueFrom:
            configMapKeyRef:
              name: my-config
              key: city


Kubernetes for Developers Journey.

Happy Coding :)

Comments