DEV Community 👩‍💻👨‍💻

Cover image for Overview some concepts in Kubernetes (K8s)
Khoi Doan (Osbkca)
Khoi Doan (Osbkca)

Posted on

Overview some concepts in Kubernetes (K8s)

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

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

apiVersion: v1
kind: Pod
metadata:
 name: nginx-name
spec:
 containers:
 - image: nginx
     name: nginx-name
 ports:
 - containerPort: 80
     protocol: TCP
Enter fullscreen mode Exit fullscreen mode

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:
kubectl apply -f pods.yaml
Enter fullscreen mode Exit fullscreen mode

To verify Pod was created, run:

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

Apply this change by run below command:

kubectl apply -f pods.yaml
Enter fullscreen mode Exit fullscreen mode

To get all pods in Kubernetes with labels

kubectl get pods --show-labels
Enter fullscreen mode Exit fullscreen mode

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:

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
Enter fullscreen mode Exit fullscreen mode

Notes: matchLabels must match with labels in metadata of template section.

Service
Service like a bridge between components in Kubernetes.

Service in Kubernetes

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

Top comments (0)

Super Useful CSS Resources

A collection of 70 hand-picked, web-based tools which are actually useful.
Each will generate pure CSS without the need for JS or any external libraries.