running pods and containers in Kubernetes

1/28/2020

I am fairly new to Kubernates and what I am able to understand so far,

  1. cluster is collection of node(s)
  2. each node can have a set of running container(s)
  3. set of tightly coupled container(s) itself can be grouped together to form a pod (despite of the node in which the container is running).

First of all, am I correct so far?

secondly, and going through docs about kube-scheduler says,

Control Plane component that watches for newly created pods with no assigned node, and selects a node for them to run on.

and docs also says pods are,

The smallest and simplest Kubernetes object. A Pod represents a set of running containers on your cluster.

My question, rather confusion is since we have already containers running in different nodes, why do we need additional node to run a pod on ?

-- nmxl
kubernetes

2 Answers

1/28/2020

Number 1 & 3 are correct. For number 2 i would say 'Each node can have set pods and each pod can have 1 or more than 1 containers'

and for your last question lets say you create a deployment having 3 pods 2 of them were deployed to node A and it's resources get consumed by 2 of them (No memory or cpu left) but 3rd pod will be in pending state as long as their is no new node to run that pod.

Their is a concept of horizontal pod auto-scaling and cluster auto-scaling https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/ & https://github.com/kubernetes/autoscaler/tree/master/cluster-autoscaler These will further clear your confusion

-- Mohsin Amjad
Source: StackOverflow

1/28/2020
  1. cluster is collection of node(s)

  2. each node can have a set of running container(s)

You are correct.

  1. set of tightly coupled container(s) itself can be grouped together to form a pod (despite of the node in which the container is running).

All containers belonging to a pod run on the same node.

My question, rather confusion is since we have already containers running in different nodes, why do we need additional node to run a pod on ?

It's not the pod that actually runs. The only things that actually run on your nodes are containers. Pod is just a logical grouping of containers and is the basic unit in kubernetes to create a container. (Docker container logo is a whale, a group of whales is called a pod if you want a parallel to remember this). So if the containers that belong to the pod are running, the pod is termed as running.

In the following pod specification, nginx-container and debian-container containers belong to the pod named two-containers. When you create this pod object, kube-scheduler will select a node to run this pod (i.e., to run the two containers) and assigns a node to the pod. The kubelet running on that node then gets notified and starts the two containers on the node. Since the two containers belong to same pod, they are run in same network namespace.

apiVersion: v1
kind: Pod
metadata:
  name: two-containers
spec:

  restartPolicy: Never

  volumes:
  - name: shared-data
    emptyDir: {}

  containers:

  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html

  - name: debian-container
    image: debian
    volumeMounts:
    - name: shared-data
      mountPath: /pod-data
    command: ["/bin/sh"]
    args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]
-- Shashank V
Source: StackOverflow