Sunday, 19 July 2020

Kubernetes: describe: Get current Pod configuration

‘kubectl describe pod {podName}’ command is used to get the current Pod configuration. This command queries ‘etcd’ (etcd is a Kubernetes key-value database) and returns all the information about the given resource.

 

Let’s create a Pod from yml definition file and see the configuration using ‘describe’ command.

 

employeeServicePod.yml

apiVersion: v1
kind: Pod
metadata:
  name: employee-service
  labels:
    app: employee-service
    author: krishna
    serviceType: webservice
spec:
  containers:
    - name: employee-service-container
      image: jboss/wildfly

Now create a pod by executing below command.

kubectl create -f employeeServicePod.yml

 

$kubectl create -f employeeServicePod.yml
pod/employee-service created

 

Let’s describe the pod ‘employee-service’ by executing the below command.

 

kubectl describe pod employee-service

$kubectl describe pod employee-service 
Name:         employee-service
Namespace:    default
Priority:     0
Node:         minikube/192.168.99.100
Start Time:   Sun, 07 Jun 2020 00:07:52 +0530
Labels:       app=employee-service
              author=krishna
              serviceType=webservice
Annotations:  <none>
Status:       Running
IP:           172.17.0.6
IPs:
  IP:  172.17.0.6
Containers:
  employee-service-container:
    Container ID:   docker://60f9841ce5a9d9827cda31ad277f168085368a9f874f325dbffdfbb64094b0ac
    Image:          jboss/wildfly
    Image ID:       docker-pullable://jboss/wildfly@sha256:67a4f90b213bc2600d08d90e82df58be83813d118d163fbcc8765b6eeaade7e6
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Sun, 07 Jun 2020 00:07:56 +0530
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-w7rp7 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-w7rp7:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-w7rp7
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age        From               Message
  ----    ------     ----       ----               -------
  Normal  Scheduled  <unknown>  default-scheduler  Successfully assigned default/employee-service to minikube
  Normal  Pulling    39s        kubelet, minikube  Pulling image "jboss/wildfly"
  Normal  Pulled     35s        kubelet, minikube  Successfully pulled image "jboss/wildfly"
  Normal  Created    35s        kubelet, minikube  Created container employee-service-container
  Normal  Started    35s        kubelet, minikube  Started container employee-service-container



Previous                                                    Next                                                    Home

Kubernetes: Namespaces

Namespaces are used for resource isolation. For example, two different namespaces can have a resource(like Pod) with the same name.

 

Using namespaces, we can split resources into non-overlapping groups.

 

Are there any resources that are not tied to a single namespace?

Yes, Kubernetes has some resource types that are not tied to a single namespace. For example, a node resource does not tie to any single namespace.

 

You can execute the command ‘kubectl get all --all-namespaces’ to see a list of namespaces available in your Kubernetes environment.

$kubectl get all --all-namespaces
NAMESPACE              NAME                                             READY   STATUS    RESTARTS   AGE
kube-system            pod/coredns-66bff467f8-mdqf8                     1/1     Running   4          31d
kube-system            pod/coredns-66bff467f8-qrv7b                     1/1     Running   4          31d
kube-system            pod/etcd-minikube                                1/1     Running   4          31d
kube-system            pod/kube-apiserver-minikube                      1/1     Running   4          31d
kube-system            pod/kube-controller-manager-minikube             1/1     Running   8          31d
kube-system            pod/kube-proxy-9zlxd                             1/1     Running   4          31d
kube-system            pod/kube-scheduler-minikube                      1/1     Running   8          31d
kube-system            pod/storage-provisioner                          1/1     Running   8          31d
kubernetes-dashboard   pod/dashboard-metrics-scraper-84bfdf55ff-f299w   1/1     Running   3          30d
kubernetes-dashboard   pod/kubernetes-dashboard-bc446cc64-wj865         1/1     Running   4          30d

NAMESPACE              NAME                                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                  AGE
default                service/kubernetes                  ClusterIP   10.96.0.1        <none>        443/TCP                  30d
kube-system            service/kube-dns                    ClusterIP   10.96.0.10       <none>        53/UDP,53/TCP,9153/TCP   31d
kubernetes-dashboard   service/dashboard-metrics-scraper   ClusterIP   10.110.152.155   <none>        8000/TCP                 30d
kubernetes-dashboard   service/kubernetes-dashboard        ClusterIP   10.100.34.170    <none>        80/TCP                   30d

NAMESPACE     NAME                        DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGE
kube-system   daemonset.apps/kube-proxy   1         1         1       1            1           kubernetes.io/os=linux   31d

NAMESPACE              NAME                                        READY   UP-TO-DATE   AVAILABLE   AGE
kube-system            deployment.apps/coredns                     2/2     2            2           31d
kubernetes-dashboard   deployment.apps/dashboard-metrics-scraper   1/1     1            1           30d
kubernetes-dashboard   deployment.apps/kubernetes-dashboard        1/1     1            1           30d

NAMESPACE              NAME                                                   DESIRED   CURRENT   READY   AGE
kube-system            replicaset.apps/coredns-66bff467f8                     2         2         2       31d
kubernetes-dashboard   replicaset.apps/dashboard-metrics-scraper-84bfdf55ff   1         1         1       30d
kubernetes-dashboard   replicaset.apps/kubernetes-dashboard-bc446cc64         1         1         1       30d

How to create a namespace?

Syntax

kubectl create ns {nameSpaceName}

 

Example

kubectl create ns data-platform

$kubectl create ns data-platform
namespace/data-platform created

Create a namespace from the definition file

 

namespaceDef.yml

apiVersion: v1
kind: Namespace  
metadata:
  name: aero
$kubectl create -f namespaceDef.yml 
namespace/aero created

You can query all the namespaces to confirm that the namespaces are created.

$kubectl get ns
NAME                   STATUS   AGE
aero                   Active   4s
data-platform          Active   34s
default                Active   36d
kube-node-lease        Active   35d
kube-public            Active   36d
kube-system            Active   36d
kubernetes-dashboard   Active   34d

How to create a pod in the namespace?

You can specify the namespace to be used using ‘metadata.namespace’ property. If you do not specify any namespace, then the resource (can be Pod, Replicaset, Deployment) will get created in the default namespace.

 

nameSpaceDemoPod.yml

apiVersion: v1
kind: Pod
metadata:
  name: employee-service
  labels:
    app: employee-service
    author: krishna
    serviceType: webservice
  namespace: data-platform
spec:
  containers:
    - name: employee-service-container
      image: jboss/wildfly

Let’s create the Pod by executing the below command.

 

kubectl create -f nameSpaceDemoPod.yml

$kubectl create -f nameSpaceDemoPod.yml 
pod/employee-service created

Let’s get the pods in all namespaces by executing below command.

$kubectl get pods --all-namespaces
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE
data-platform          employee-service                             1/1     Running   0          45s
kube-system            coredns-66bff467f8-mdqf8                     1/1     Running   4          31d
kube-system            coredns-66bff467f8-qrv7b                     1/1     Running   4          31d
kube-system            etcd-minikube                                1/1     Running   4          31d
kube-system            kube-apiserver-minikube                      1/1     Running   4          31d
kube-system            kube-controller-manager-minikube             1/1     Running   8          31d
kube-system            kube-proxy-9zlxd                             1/1     Running   4          31d
kube-system            kube-scheduler-minikube                      1/1     Running   8          31d
kube-system            storage-provisioner                          1/1     Running   8          31d
kubernetes-dashboard   dashboard-metrics-scraper-84bfdf55ff-f299w   1/1     Running   3          30d
kubernetes-dashboard   kubernetes-dashboard-bc446cc64-wj865         1/1     Running   4          30d

As you see the output, Pod ‘employee-service’ is created in the namespace ‘data-platform’.

 

You can delete the namespace by executing the below command.

 

kubectl delete ns data-platform

$kubectl delete ns data-platform
namespace "data-platform" deleted

Once you delete a namespace, all the resources in the namespace will get deleted.

 


Previous                                                    Next                                                    Home

Kuberenetes: exec: Get bash shell of running container

Using ‘kubectl exec’ command, you can log in to a container.

 

Step 1: Create a definition file to create a Pod.

 

employeeServicePod.yml
apiVersion: v1
kind: Pod
metadata:
  name: employee-service
  labels:
    app: employee-service
    author: krishna
    serviceType: webservice
spec:
  containers:
    - name: employee-service-container
      image: jboss/wildfly

Step 2: Create pod from the definition file.

$kubectl create -f employeeServicePod.yml 
pod/employee-service created

Wait until Pod is in Running state.

$kubectl get pods
NAME               READY   STATUS    RESTARTS   AGE
employee-service   1/1     Running   0          12s

Execute the below command to launch the bash shell of the container.

kubectl exec -it {podName} -c {containerName} -- /bin/bash

 

Execute below command to open bash shell of employee-service-container.

kubectl exec -it employee-service -c employee-service-container -- /bin/bash

$kubectl exec -it employee-service -c employee-service-container -- /bin/bash
[jboss@employee-service ~]$ 
[jboss@employee-service ~]$ pwd
/opt/jboss
[jboss@employee-service ~]$ 
[jboss@employee-service ~]$ ls
wildfly
[jboss@employee-service ~]$ 

Previous                                                    Next                                                    Home

Thursday, 16 July 2020

Kubernetes: Specify multiple containers in the same pod definition

A Pod can encapsulate more than one container. For example, following Pod definition encapsulate two containers.

a.   Container ‘user-service-terminal’ runs with the image busybox.

b.   Container ‘user-service’ runs with the image jboss/wildfly.

 

podWithTwoContainers.yml

apiVersion: v1
kind: Pod
metadata:
  name: container-with-two-pods
  labels:
    app: hello-world
    author: krishna
    serviceType: user-service
spec:
  containers:
    - name: user-service-terminal
      image: busybox
      command:
        - sleep 
        - "30"
    - name: user-service
      image: jboss/wildfly

Let’s create Pod using the above definition file.

$kubectl create -f podWithTwoContainers.yml 
pod/container-with-two-pods created

Let’s get more information about the pod using ‘kubectl describe’ command.

$kubectl describe pod container-with-two-pods
Name:         container-with-two-pods
Namespace:    default
Priority:     0
Node:         minikube/192.168.99.100
Start Time:   Sat, 06 Jun 2020 16:23:44 +0530
Labels:       app=hello-world
              author=krishna
              serviceType=user-service
Annotations:  <none>
Status:       Running
IP:           172.17.0.6
IPs:
  IP:  172.17.0.6
Containers:
  user-service-terminal:
    Container ID:  docker://6a116bacf83bd5d443ba13a3a838379c026ed5a43e2933cf6fdb244bb703eccd
    Image:         busybox
    Image ID:      docker-pullable://busybox@sha256:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209
    Port:          <none>
    Host Port:     <none>
    Command:
      sleep
      30
    State:          Running
      Started:      Sat, 06 Jun 2020 16:23:50 +0530
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-w7rp7 (ro)
  user-service:
    Container ID:   docker://09b65ce085383ee69df6afe574341d0f7d22ee04f133431252e62c5d5a4176f2
    Image:          jboss/wildfly
    Image ID:       docker-pullable://jboss/wildfly@sha256:67a4f90b213bc2600d08d90e82df58be83813d118d163fbcc8765b6eeaade7e6
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Sat, 06 Jun 2020 16:23:54 +0530
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-w7rp7 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-w7rp7:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-w7rp7
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age        From               Message
  ----    ------     ----       ----               -------
  Normal  Scheduled  <unknown>  default-scheduler  Successfully assigned default/container-with-two-pods to minikube
  Normal  Pulling    22s        kubelet, minikube  Pulling image "busybox"
  Normal  Pulled     16s        kubelet, minikube  Successfully pulled image "busybox"
  Normal  Created    16s        kubelet, minikube  Created container user-service-terminal
  Normal  Started    16s        kubelet, minikube  Started container user-service-terminal
  Normal  Pulling    16s        kubelet, minikube  Pulling image "jboss/wildfly"
  Normal  Pulled     12s        kubelet, minikube  Successfully pulled image "jboss/wildfly"
  Normal  Created    12s        kubelet, minikube  Created container user-service
  Normal  Started    12s        kubelet, minikube  Started container user-service

You can confirm that this Pod is running with two containers from Containers:, Events: section.

 

You can delete the pod by executing the below command.

$kubectl delete pod container-with-two-pods
pod "container-with-two-pods" deleted

When multiple containers running in the same Pod, they can share the data using shared storage.


Previous                                                    Next                                                    Home

Kubernetes: Specify environment variables in Pod definition file

You can specify environment variables using ‘env’ property.

 

envVariables.yml
apiVersion: v1
kind: Pod
metadata:
  name: read-env-variables-pod
  labels:
    app: hello-world
    author: krishna
    serviceType: terminal-app
spec:
  containers:
    - name: execute-command-container
      image: busybox
      command: ["/bin/sh"]
      args: ["-c", "while true; do echo \"Hi, $AUTHOR , about you : $ABOUT_ME\"; sleep 5; done"]
      env:
      - name: AUTHOR
        value: krishna
      - name: ABOUT_ME
        value: I am a blogger

Create Pod using the above definition file.

$kubectl create -f envtVariables.yml 
pod/read-env-variables-pod created

Make sure that the Pod is running.

$kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
read-env-variables-pod   1/1     Running   0          32s

Now, you can see what is happening in the container using ‘kubectl logs' command.

$kubectl logs read-env-variables-pod
Hi, krishna , about you : I am a blogger
Hi, krishna , about you : I am a blogger
Hi, krishna , about you : I am a blogger

For every 5 seconds, environment variables get printed to console.

 

You can delete the pod by executing the below command.

kubectl delete pod read-env-variables-pod

$kubectl delete pod read-env-variables-pod
pod "read-env-variables-pod" deleted

Previous                                                    Next                                                    Home

Sunday, 12 July 2020

Kubernetes: Specify the command to run in pod definition file

You can specify the command to run in the Pod definition file itself.

 

helloWorldTerminalApp.yml
apiVersion: v1
kind: Pod
metadata:
  name: hello-world
  labels:
    app: hello-world
    author: krishna
    serviceType: terminal-app
spec:
  containers:
    - name: hello-world
      image: busybox
      command:
        - sleep 
        - "30"

Above command creates pod with the image busybox. I specify the command to run in ‘command:’ property.

 

Let’s create another Pod definition file, that prints something to console. So we can see what is happening via ‘kubectl logs’ command.

 

executeCommandPod.yml
apiVersion: v1
kind: Pod
metadata:
  name: execute-command-pod
  labels:
    app: hello-world
    author: krishna
    serviceType: terminal-app
spec:
  containers:
    - name: execute-command-container
      image: busybox
      command: ["/bin/sh"]
      args: ["-c", "while true; do echo 'Hello World'; sleep 5; done"]

As you see the definition file, I am just printing the message 'Hello World' to console for every 5 seconds.

 

Let’s create a Pod using the above definition file.
$kubectl create -f executeCommandPod.yml 
pod/execute-command-pod created

Wait until Pod is in Running state.

$kubectl get pods
NAME                  READY   STATUS    RESTARTS   AGE
execute-command-pod   1/1     Running   0          9s

Use ‘kubectl logs’ command to see the output of the command that we gave in the definition file.

$kubectl logs execute-command-pod
Hello World
Hello World

Wait for 5 seconds and re-execute the logs command, you will see another ‘Hello World’ message.

$kubectl logs execute-command-pod
Hello World
Hello World
Hello World

You can delete the pod by executing the below command.

kubectl delete pods execute-command-pod

$kubectl delete pods execute-command-pod
pod "execute-command-pod" deleted

Previous                                                    Next                                                    Home

Kubernetes: Define a pod in yaml

In this post, I am going to explain how to define a pod in YAML file and create a pod using this YAML definition file. You can also create a manifest file using json, but YAML is the preferred approach because of readability.

 

Step 1: Create employeeServicePod.yml file

 

employeeServicePod.yml
apiVersion: v1
kind: Pod
metadata:
  name: employee-service
  labels:
    app: employee-service
    author: krishna
    serviceType: webservice
spec:
  containers:
    - name: employee-service-container
      image: jboss/wildfly

Step 2: Execute below command to create a pod from employeeServicePod.yml file.

$kubectl create -f employeeServicePod.yml
pod/employee-service created

Explanation

apiVersion:

It specifies the version of the Kubernetes API that you are using to create the object.

 

Following table summarizes possible values for apiVersion.

 

Kind

apiVersion

Pod

v1

Service

v1

ReplicationController

v1

ReplicaSet

apps/v1

Deployment

apps/v1

 

kind:

It specifies the type of object that we are trying to create. Possible values are Pod, Service, ReplicaSet, and Deployment

 

metadata:

Specifies information about the object like labels, name etc., You can add any number of custom key:value pairs under labels section. Labels are used to identify the object when hundreds of pods managed by Kubernetes.

 

spec:

This section is different for different types of objects. As you see the yaml file, 'containers:' is a list, we can specify multiple containers in a pod object.

 

In the containers: section you add the following properties.

 

Property

Description

name

Name Of the Container

image

Image that should be used

command

Command to run in the container

args

Arguments that are passed to the command

env

Environment variables that are available to the container.

 

Step 3: You can execute the command ‘kubectl get pods’ to get the information about pods in your system.

$kubectl get pods
NAME               READY   STATUS    RESTARTS   AGE
employee-service   1/1     Running   0          3m1s

You can delete a pod by executing the below command.

kubectl delete pod {podName}

$kubectl delete pod employee-service
pod "employee-service" deleted
$
$kubectl get pods
No resources found in default namespace.

Previous                                                    Next                                                    Home