Introduction To Kubernetes  Part 2 - Pod

Introduction To Kubernetes Part 2 - Pod

Pod

A pod is a collection of containers sharing a network and mount namespace and is the basic unit of deployment in Kubernetes. All containers in a pod are scheduled on the same node.

Deploy your first pod

apiVersion : v1
kind : Pod 
metadata :
    name : myapp-pod
    labels:
        app: myapp
        type : front-app 
spec: 
      containers: 
        - name : nginx-containers
          image : nginx

Launch or Apply POD

sangam$ kubectl create -f pod.yml 
pod/myapp-pod created

OR

kubectl apply Creates and Updates Resources through local or remote files.

sangam$ kubectl apply -f pod.yml
pod/myapp-pod configured

Check All running Pod And States From Current Namespaces

 sangam$ kubectl get pods
NAME                                 READY   STATUS                       RESTARTS   AGE
myapp-pod                            1/1     Running                      0          6m26s

Check All running Pod And States From All Namespaces

kubectl get pods --all-namespaces

check describetion details of pod

sangam$ kubectl describe pods myapp-pod 
Name:               myapp-pod
Namespace:          sangam14
Priority:           0
PriorityClassName:  <none>
Node:               gke-us-central1-cloud-okteto-com-pro-a09dced8-a7x1/10.128.0.12
Start Time:         Thu, 09 Jul 2020 10:56:50 +0530
Labels:             app=myapp
                    type=front-app
Annotations:        cni.projectcalico.org/podIP: 10.4.134.166/32
                    container.apparmor.security.beta.kubernetes.io/nginx-containers: runtime/default
                    kubectl.kubernetes.io/last-applied-configuration:
                      {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"labels":{"app":"myapp","type":"front-app"},"name":"myapp-pod","namespace":"s...
                    kubernetes.io/egress-bandwidth: 5M
                    kubernetes.io/ingress-bandwidth: 5M
                    kubernetes.io/limit-ranger:
                      LimitRanger plugin set: cpu, memory request for container nginx-containers; cpu, memory limit for container nginx-containers
                    kubernetes.io/psp: cloud-okteto-enterprise-restrictive
                    seccomp.security.alpha.kubernetes.io/pod: runtime/default
Status:             Running
IP:                 10.4.134.166
Containers:
  nginx-containers:
    Container ID:   docker://2380ea5fcd8b2b71dfb8ea807fd0a150b3269a9b92bd00786a354da3e4a0add9
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:0efad4d09a419dc6d574c3c3baacb804a530acd61d5eba72cb1f14e1f5ac0c8f
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Thu, 09 Jul 2020 10:56:53 +0530
    Ready:          True
    Restart Count:  0
    Limits:
      cpu:     500m
      memory:  1Gi
    Requests:
      cpu:     0
      memory:  0
    Environment Variables from:
      okteto-secrets  Secret  Optional: true
    Environment:      <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-j7mjh (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-j7mjh:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-j7mjh
    Optional:    false
QoS Class:       Burstable
Node-Selectors:  okteto-node-pool=pro
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
                 okteto-node-pool=pro:NoSchedule
Events:
  Type    Reason     Age   From                                                         Message
  ----    ------     ----  ----                                                         -------
  Normal  Scheduled  15m   default-scheduler                                            Successfully assigned sangam14/myapp-pod to gke-us-central1-cloud-okteto-com-pro-a09dced8-a7x1
  Normal  Pulling    15m   kubelet, gke-us-central1-cloud-okteto-com-pro-a09dced8-a7x1  Pulling image "nginx"
  Normal  Pulled     15m   kubelet, gke-us-central1-cloud-okteto-com-pro-a09dced8-a7x1  Successfully pulled image "nginx"
  Normal  Created    15m   kubelet, gke-us-central1-cloud-okteto-com-pro-a09dced8-a7x1  Created container nginx-containers
  Normal  Started    15m   kubelet, gke-us-central1-cloud-okteto-com-pro-a09dced8-a7x1  Started container nginx-containers

Get The Documentation For Pod Manifests


sangam:~ sangam$ kubectl explain pods 
KIND:     Pod
VERSION:  v1

DESCRIPTION:
     Pod is a collection of containers that can run on a host. This resource is
     created by clients and scheduled onto hosts.

FIELDS:
   apiVersion   <string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#resources

   kind <string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds

   metadata     <Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata

   spec <Object>
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

   status       <Object>
     Most recently observed status of the pod. This data may not be up to date.
     Populated by the system. Read-only. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

Pod - Args Instruction

Create pod-logging.yml File With Following Contents:

---
    apiVersion: v1
    kind: Pod
    metadata:
      name: counter
    spec:
      containers:
      - name: count
        image: busybox
        args: ['/bin/sh', '-c', 'i=0; while true; do echo "$i: $(date)"; i=$((i+1)); sleep 1;done']

// If you supply a command and args, the default Entrypoint and the default Cmd defined in the Docker image are ignored. Your command is run with your args.

use kubectl create or `kubectl apply'

kubectl create -f pod-logging.yml

List All Running Pod

sangam:~ sangam$ kubectl get po
NAME                                 READY   STATUS                       RESTARTS   AGE
counter                              1/1     Running                      0          89s
myapp-pod                            1/1     Running                      0          25h

Run The Args Specification To Get Logs

Syntax : kubectl logs <pod-name> - c <container-name-specified-in-yml>

it will exec args specific command

kubectl logs counter -c count
0: Fri Jul 10 06:39:17 UTC 2020
1: Fri Jul 10 06:39:18 UTC 2020
2: Fri Jul 10 06:39:19 UTC 2020
3: Fri Jul 10 06:39:20 UTC 2020
4: Fri Jul 10 06:39:21 UTC 2020
5: Fri Jul 10 06:39:22 UTC 2020
6: Fri Jul 10 06:39:23 UTC 2020
7: Fri Jul 10 06:39:24 UTC 2020

Pod : Logging Volume

  • You can use a sidecar container in one of the following ways:

    • The sidecar container streams application logs to its own stdout.
    • The sidecar container runs a logging agent, which is configured to pick up logs from an application container.

A pod runs a single container, and the container writes to two different log files, using two different formats. Here's a configuration file for the Pod:

Create pod-logging-volum.yml File With Following Contents

---
apiVersion: v1
kind: Pod
metadata:
  name: counter-log-vol
spec:
  containers:
  - name: count
    image: busybox
    args:
    - /bin/sh
    - -c
    - >
        i=0;
        while true;
        do
          echo "$i: $(date)" >> /var/log/1.log;
          echo "$(date) INFO $i" >> /var/log/2.log;
          i=$((i+1));
          sleep 1;
        done
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  volumes:
  - name: varlog
    emptyDir: {}

Use kubectl create

or kubectl apply


sangam:~ sangam$ kubectl create -f pod-logging-volum.yml 
pod/counter-log-vol created

List Of All Runnning Pods


sangam:~ sangam$ kubectl get po
NAME                                 READY   STATUS                       RESTARTS   AGE
counter                              1/1     Running                      0          3h39m
counter-log-vol                      1/1     Running                      0          12m
myapp-pod                            1/1     Running                      0          28h

emptyDir

An emptyDir volume is first created when a Pod is assigned to a Node, and exists as long as that Pod is running on that node. As the name says, it is initially empty. Containers in the Pod can all read and write the same files in the emptyDir volume, though that volume can be mounted at the same or different paths in each Container. When a Pod is removed from a node for any reason, the data in the emptyDir is deleted forever.

Check Volume from the inside pod

 kubectl exec -it  counter-log-vol -- bin/sh
 cd /var/log
 ls
 cat 1.log 2.log

Pod Logging Sidecar

Create pod-logging-sidecar.yaml file with following Contents:-

---
apiVersion: v1
kind: Pod
metadata:
  name: counter-log-sidecar
spec:
  containers:
  - name: count
    image: busybox
    args:
    - /bin/sh
    - -c
    - >
        i=0;
        while true;
        do
          echo "$i: $(date)" >> /var/log/1.log;
          echo "$(date) INFO $i" >> /var/log/2.log;
          i=$((i+1));
          sleep 1;
        done
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  - name: counter-log-1
    image: busybox
    args: [/bin/sh, -c, 'tail -n+1 -f /var/log/1.log']
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  - name: counter-log-2
    image: busybox
    args: [/bin/sh, -c, 'tail -n+1 -f /var/log/2.log']
    volumeMounts:
    - name: varlog
      mountPath: /var/log
  volumes:
  - name: varlog
    emptyDir: {}

kubectl Create ' Or kubectl Apply '

kubectl create -f pod-logging-sidecar.yaml 
pod/counter-log-sidecar created

List All Running Pods

sangam:pods sangam$ kubectl get po
NAME                                 READY   STATUS                       RESTARTS   AGE
counter                              1/1     Running                      0          2d2h
counter-log-sidecar                  3/3     Running                      0          4m21s

kubectl logs

sangam:pods sangam$ kubectl exec counter-log-sidecar -c count -it bin/sh
/ # ls
bin   dev   etc   home  proc  root  sys   tmp   usr   var
/ # cd var/log
/var/log # ls
1.log  2.log
/var/log #

Did you find this article valuable?

Support CloudNativeFolks Community by becoming a sponsor. Any amount is appreciated!