Overview some concepts in Kubernetes (K8s)
Tags: devops, kubernetes, osbkca
Published: 2022-12-01 10:19:05

Hi everyone, In prev post, we already known overview about Kubernetes. In this tutorial, we will learn some core concepts in Kubernetes

**Pods - a group of one or more containers**
Pods are the smallest deployable units in Kubernetes system. Pods locates in worker nodes where contains one or more containers. Each Pod like a logic machine ( have IP, hostname, process separate)

![Pod overview](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rlzjiuajb6h5c9h1yd9.png)

**Why do you need Pods?**
You are wondering why not run containers in worker nodes? Actually, Pod is a high level of a container. With a Pod, you can run one or more containers which will have a IP and share a volume.

**Basic Pod configuration.**
To create a Pod in Kubernetes, we will config with a yaml file or json file. In this tutorial, I will use a yaml config file.

Create a file with name `pods.yml`

```yaml
apiVersion: v1
kind: Pod
metadata:
 name: nginx-name
spec:
 containers:
 - image: nginx
	 name: nginx-name
 ports:
 - containerPort: 80
	 protocol: TCP
```
Notes: Indentation is important in yaml file.

- `apiVersion`: version of Kubernetes API
- `kind`: The component will be created (here is Pod)
- `metadata`: Give a name for Pod
- `spec`: Describe about image, list containers, .. of the Pod
Run below command to create a Pod:

```shell
kubectl apply -f pods.yaml
```
To verify Pod was created, run:

```shell
kubectl get pods
```

**Using labels in Pods**
With big system, we have many pods in Kubernetes cluster. To manage effectively, we can use Labels to collect Pods to small groups.

With Label have two attributes

- app: Example ui, order, …
- rel: Example stable, beta and canary

![Pods with labels](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8xrvll2j8czpnrd27ht8.png)

```yaml
apiVersion: v1
kind: Pod
metadata:
 name: nginx-name
 labels:
   app: ui
   rel: stable
spec:
 containers:
 - image: nginx
	 name: nginx-name
 ports:
 - containerPort: 80
	 protocol: TCP
```
Apply this change by run below command:

```shell
kubectl apply -f pods.yaml
```
To get all pods in Kubernetes with labels

```shell
kubectl get pods --show-labels
```

**ReplicaSet**
When a Pod run on Kubernetes, it may be shutdown or crash by many suddenly problems. ReplicaSet ensure that when Pod crashed, it will be created immediately again. ReplicaSet will maintain number of Pod which is declared in config file before. Example:

```yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
  labels:
    tier: frontend
spec:
  # modify replicas according to your case
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: gcr.io/google_samples/gb-frontend:v3
```
Notes: `matchLabels` must match with labels in metadata of template section.

**Service**
Service like a bridge between components in Kubernetes.

![Service in Kubernetes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xew6e5bdmgcgel864p9f.png)

As you know, the Pods have their own IP. Client can connect to the Pods by their IP or hostname. However, when you deploy a new version of Pod or ReplicaSet recreate new versions then IP of Pods will be changed. Furthermore, in Kubernetes has many Pods running at the same time, the clients should not all IPs of Pods. It only should know name of service.

Read more at [here](https://osbkca.me/)