# 1. Main K8s Components

### Pod

* Smallest unit of K8s
* Abstraction over container
* Usually 1 application per Pod
* Each Pod gets its own IP address
* New IP address on re-creation
  * E.g, DB dies and new DB is created which will get new IP address

<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2F9btCWVRk83KzjCa4DDV3%2Fimage.png?alt=media&#x26;token=62441da1-a3dc-4ec1-93c9-a246ed48cc3a" alt=""><figcaption></figcaption></figure>

### [Service](https://notes.nomanaziz.me/devops/orchestration/kubernetes/10.-k8-services) & [Ingress](https://notes.nomanaziz.me/devops/orchestration/kubernetes/6.-k8s-ingress)

* Permanant IP address
  * Can be attached to each pod
* Lifecycle of Pod and Service are not connected
  * If Pod dies then service and its ip remains

#### External Service

* Service which is accessible externally using url
* Format of url is [http://ip:port](https://notes.nomanaziz.me/devops/orchestration/kubernetes/http:/ip:port)

#### Internal Service

* Service which should not be accessible externally
* Have same format of url [http://ip:port](https://notes.nomanaziz.me/devops/orchestration/kubernetes/http:/ip:port)

#### Ingress

* Solves the problem of having static ip, port and protocol assigned to external services
* Assigns a domain name and certificate to requests
* Acts as a reverse proxy&#x20;

<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2Fvvooz2RYQLJQ3b2I5mh1%2Fimage.png?alt=media&#x26;token=199d32f6-9672-44ff-a8e5-c25075bde1ac" alt=""><figcaption></figcaption></figure>

### ConfigMap & Secret

#### ConfigMap

Problem :-

* Suppose DB url was configured inside my-app and its image was built and pushed to dockerhub
* Now, the DB url gets changed and image must be modified, rebuilt and pushed to dockerhub then pulled into pod and restart

Solution :-

* Thats where **ConfigMap** comes in, we can store such external configuration of our app and attach it to the pod.

<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2FT2kEgpf5p5QUYEiDXVAX%2Fimage.png?alt=media&#x26;token=91a9af15-4ff4-448d-9333-ca10b315d624" alt=""><figcaption></figcaption></figure>

#### Secret

* Works to solve similar problem as ConfigMap
* ConfigMap stores configuration in plain text format so it should not be used to store secrets such as username, passwords, etc.
* Secret should be used instead
* Things are stored in base64 encoded format
* Built in security mechanism is not enabled by default

***

### [Volumes](https://notes.nomanaziz.me/devops/orchestration/kubernetes/8.-k8-volumes)

* Used for data storage, same concept as docker since k8s does not manage data persistance
* Storage can be on **local** machine or **remote** storage outside of k8s cluster

***

### Deployment & [StatefulSet](https://notes.nomanaziz.me/devops/orchestration/kubernetes/9.-k8-statefulset)

#### Deployment (Replication)

* Solves the problem of single point of failure
  * In case Pod dies
* We replication everything on another node for fault tolerance or load balancer
* Replica is connected to same service
* We don't create another pod but define **blueprint** for pods
* It is an abstraction layer on top of pods.
* In practice deployment is used instead of pods.
* **DBs can't be replicated via deployment**
  * Because database has a state
  * To avoid data inconsistencies
* **Should be used for StateLess apps**

#### StatefulSet

* Solves the DB replication problem of Deployment
* **Should be used for StateFul apps or Databases**
* Deploying StatefulSet is not easy
* DBs are often hosted outside of K8s cluster

<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2FWGejPUTB2Kg1uTaVhwVy%2Fimage.png?alt=media&#x26;token=6cf80b9c-b8c3-4624-bded-c241440ebb64" alt=""><figcaption></figcaption></figure>

#### Layers of Abstraction

<figure><img src="https://1920086362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfv51K0WXLZdwTryHQZc%2Fuploads%2FohYmqQKkaTp95f78cKuV%2Fimage.png?alt=media&#x26;token=aa05ffd9-05f6-4541-893d-7ded087a1c77" alt=""><figcaption></figcaption></figure>
