CKAD - Revision - Core Concepts
Tags: kubernetes, devops, linux, docker
Published: 2020-01-26 18:10:32

# Pods

A pod is a group of **one or more containers** (such as Docker containers), with shared storage/network, and a specification for how to run the containers. A Pod’s contents are always **co-located and co-scheduled**, and **run in a shared context**.

Pods share **networking** and **storage**:

* containers within a Pod share an IP address and port space, and can find each other via localhost

* a pod can specify a set of shared storage volumes, allowing containers to share data.

```
Restarting a container in a Pod should not be confused with restarting
the Pod. The Pod itself does not run, but is an environment the containers
run in and persists until it is deleted.
```

Here is an example of a pod template:
```
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox
    command: ['sh', '-c', 'echo Hello Kubernetes! && sleep 3600']
```

# Services

A service is an abstraction which defines a **logical set of Pods and a policy by which to access them** (sometimes this pattern is called a micro-service). The set of Pods targeted by a Service is usually determined by a **selector**.

Services allow for decoupling - if a set of "frontend" pods needs to talk to a set of "backend" pods, this is possible due to the Service abstraction.

Here is an example of a service template that would match the above pods:
```
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:    <--------------------- this is what selects pods
    app: myApp <--------------------- 
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
```
**Kubernetes assigns this Service an IP address (sometimes called the “cluster IP”), which is used by the Service proxies**.

**Kubernetes ServiceTypes allow you to specify what kind of Service you want. The default is ClusterIP**.

Type values and their behaviors are:

* **ClusterIP**: exposes the Service on a cluster-internal IP. Choosing this value makes the Service only reachable from within the cluster. This is the default ServiceType.

* **NodePort**: exposes the Service on each Node’s IP at a static port (the NodePort). A ClusterIP Service, to which the NodePort Service routes, is automatically created. You’ll be able to contact the NodePort Service, from outside the cluster, by requesting <NodeIP>:<NodePort>.

* **LoadBalancer*: exposes the Service externally using a cloud provider’s load balancer. NodePort and ClusterIP Services, to which the external load balancer routes, are automatically created.

* **ExternalName**: allows the return of an alias to an external service. **It is used for services not yet brought into the Kubernetes cluster** (a bit like a placeholder). Once those services are ready we can then change the type and traffic would flow to the internal objects.

# Volumes

On-disk files in a Container are ephemeral, which presents some problems for non-trivial applications when running in Containers. First, **when a Container crashes, kubelet will restart it, but the files will be lost - the Container starts with a clean state**. Second, **when running Containers together in a Pod it is often necessary to share files between those Containers**. 

**The Kubernetes Volume abstraction solves both of these problems**.

```
A volume has the same lifetime as the pod that encloses it
```

# Namespaces

Kubernetes supports multiple virtual clusters backed by the same physical cluster. These virtual clusters are called namespaces.
Namespaces are intended for use in environments with many users spread across multiple teams, or projects.

# Deployment

A Deployment provides declarative updates for Pods and ReplicaSets.

# ReplicaSet

A ReplicaSet’s purpose is to maintain a stable set of replica Pods running at any given time. As such, it is often used to guarantee the availability of a specified number of identical Pods.

You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate.

# DaemonSet

A DaemonSet ensures that all (or some) Nodes run a copy of a Pod such as cluster storage daemon, logs collection daemon or monitoring daemon.

# StatefulSet

StatefulSet is the workload API object used to manage stateful applications.

Manages the deployment and scaling of a set of Pods, and provides guarantees about the ordering and uniqueness of these Pods.