37、Kubernetes Pods and ReplicaSets: An In - Depth Guide

Kubernetes Pods and ReplicaSets: An In - Depth Guide

1. Introduction to Pods

In a Kubernetes cluster, you can’t run containers directly; instead, you run pods. Pods are the atomic units of deployment in Kubernetes. A pod is an abstraction of one or more co - located containers that share the same kernel namespaces, like the network namespace.

For example, Docker stack deploy creates a Deployment for web and db services and a Kubernetes service for the web service so it can be accessed within the cluster. Before proceeding further, you can remove the stack from the cluster using the command:

$ kubectl delete –f '*.yaml'
1.1 Pod Structure and Networking
  • Pod Structure : A pod can have one or more containers. Each pod gets a unique IP address in the whole Kubernetes cluster. For instance, if we have two pods, Pod 1 and Pod 2, they might have IP addresses like 10.0.12.3 and 10.0.12.5 respectively. All containers within a pod share the same Linux kernel namespaces, especially the network namespace.
  • Networking :
    • Docker Container Networking : When a Docker container is created without specifying a network, Docker Engine creates a virtual ethernet (veth) endpoint. Each container has its own network namespace for isolation. The veth endpoints are connected to the docker0 bridge.
    • Kubernetes Pod Networking : Kubernetes first creates a pause container when creating a new pod. The pause container creates and manages the namespaces that other containers in the pod will share. Other containers in the pod can reuse the network namespace of the pause container using the --net container:pause option.
$ docker container create --net container:pause ...

The following table compares Docker container and Kubernetes pod networking:
| Aspect | Docker Container | Kubernetes Pod |
| ---- | ---- | ---- |
| Network Namespace | Each container has its own | Containers in a pod share the same |
| Endpoint Creation | Docker creates a veth endpoint for each container | Only the pause container has a veth endpoint, others reuse it |

1.2 Sharing the Network Namespace

To simulate the creation of a pod:
1. Create a pause container using Nginx:

$ docker container run –detach \
    --name pause nginx:alpine
  1. Add a second container called main and attach it to the same network namespace as the pause container:
$ docker container run --name main \ -d -it \
    --net container:pause \
    alpine:latest ash
  1. Exec into the main container:
$ docker exec -it main /bin/sh
  1. Test the connection to Nginx in the pause container:
/ # wget -qO – localhost
  1. Check the IP address of eth0 in both containers:
/ # ip a show eth0
  1. Inspect the bridge network:
$ docker network inspect bridge
  1. Remove the containers:
$ docker container rm pause main

The mermaid flowchart below shows the process of creating a pod and testing network sharing:

graph LR
    A[Create Pause Container] --> B[Create Main Container in Pause's NS]
    B --> C[Exec into Main Container]
    C --> D[Test Nginx Connection]
    D --> E[Check IP Address]
    E --> F[Inspect Bridge Network]
    F --> G[Remove Containers]
2. Pod Life Cycle

A pod has a life cycle similar to a container but is more complex due to the presence of multiple containers.
- Pending : When a pod is created on a cluster node, it first enters the pending status.
- Running : Once all containers in the pod are up and running successfully, the pod enters the running status.
- Succeeded : If the pod is asked to terminate and all containers terminate with an exit code of zero, the pod enters the succeeded status.
- Failed : There are three scenarios for a pod to enter the failed state:
- During startup, if at least one container fails (non - zero exit code).
- When the pod is running, if one of the containers crashes or exits with a non - zero exit code.
- During termination, if at least one container exits with a non - zero exit code.

The following mermaid flowchart shows the pod life cycle:

graph LR
    A[Pod Created] --> B[Pending]
    B --> C{All Containers Running?}
    C -- Yes --> D[Running]
    C -- No --> E[Failed]
    D --> F{Termination Requested?}
    F -- Yes --> G{All Containers Exit 0?}
    G -- Yes --> H[Succeeded]
    G -- No --> E
3. Pod Specifications

When creating a pod in a Kubernetes cluster, a declarative approach is preferred. Pod specifications can be written in YAML or JSON. Here is a sample YAML specification in a pod.yaml file:

apiVersion: v1
kind: Pod
metadata:
  name: web - pod
spec:
  containers:
  - name: web
    image: nginx:alpine
    ports:
    - containerPort: 80

To apply this specification to the cluster:
1. Open a new terminal window and navigate to the relevant subfolder:

$ cd ~/The - Ultimate - Docker - Contianer - Book/ch16
  1. Make sure you are using the right context for kubectl :
$ kubectl config use - context docker - desktop
  1. Create a new file called pod.yml and add the pod specification to it, then save the file.
  2. Create the pod:
$ kubectl create - f pod.yaml
  1. List all pods in the cluster:
$ kubectl get pods
  1. Get detailed information about the pod:
$ kubectl describe pod/web - pod
4. Pods and Volumes

Pods can use volumes for accessing and storing persistent data.
1. Create a PersistentVolumeClaim:
- Create a file called volume - claim.yaml with the following content:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my - data - claim
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
- Create the claim:
$ kubectl create - f volume - claim.yaml
- List the claim:
$ kubectl get pvc
  1. Use the volume in a pod:
    • Create a file called pod - with - vol.yaml with the following content:
apiVersion: v1
kind: Pod
metadata:
  name: web - pod
spec:
  containers:
  - name: web
    image: nginx:alpine
    ports:
    - containerPort: 80
    volumeMounts:
    - name: my - data
      mountPath: /data
  volumes:
  - name: my - data
    persistentVolumeClaim:
      claimName: my - data - claim
- Create the pod:
$ kubectl create - f pod - with - vol.yaml
- Exec into the container to check the volume mount:
$ kubectl exec - it web - pod -- /bin/sh
/ # cd /data
/data # echo "Hello world!" > sample.txt
/data # exit
- Delete the pod:
$ kubectl delete pod/web - pod
- Recreate the pod:
$ kubectl create - f pod - with - vol.yaml
- Exec into the container and output the data:
$ kubectl exec - it web - pod  -- ash
/ # cat /data/sample.txt
- Exit the container and delete the pod and the persistent volume claim.

Kubernetes Pods and ReplicaSets: An In - Depth Guide

5. Kubernetes ReplicaSets

A single pod is not sufficient in an environment with high - availability requirements. Kubernetes ReplicaSets are used to define and manage a collection of identical pods running on different cluster nodes.

  • Function of ReplicaSets : A ReplicaSet ensures that the desired number of pods is running at all times. If a pod crashes, the ReplicaSet schedules a new pod on a node with free resources. If there are more pods than the desired number, it deletes the superfluous pods. This provides a self - healing and scalable set of pods.

The following table summarizes the key features of ReplicaSets:
| Feature | Description |
| ---- | ---- |
| Desired State | Defines container images, number of pod instances, etc. |
| Self - Healing | Schedules new pods if existing ones crash |
| Scalability | Can adjust the number of pods based on requirements |

The mermaid flowchart below shows how a ReplicaSet manages pods:

graph LR
    A[ReplicaSet] --> B{Actual Pods = Desired Pods?}
    B -- Yes --> C[Maintain Status Quo]
    B -- No --> D{Actual < Desired?}
    D -- Yes --> E[Spawn New Pod]
    D -- No --> F[Delete Extra Pods]
6. ReplicaSet Specification

Kubernetes allows both imperative and declarative approaches to define and create a ReplicaSet. The declarative approach is recommended.

Here is a sample specification for a Kubernetes ReplicaSet in a replicaset.yaml file:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs - web
spec:
  selector:
    matchLabels:
      app: web
  replicas: 3
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        ports:
        - containerPort: 80

The key elements of this specification are:
1. Selector : Determines which pods are part of the ReplicaSet. In this case, it selects pods with the label app: web .
2. Replicas : Defines the number of pod instances to run. Here, it is set to 3.
3. Template : Defines the metadata and specification of the pods in the ReplicaSet.

To create the ReplicaSet using the declarative approach:
1. Create the replicaset.yaml file with the above content.
2. Apply the specification to the cluster:

$ kubectl create -f replicaset.yaml
  1. List all ReplicaSets in the cluster:
$ kubectl get rs

The output will look like this:

NAME        DESIRED  CURRENT   READY    AGE
rs - web         3       3         3      51s

In summary, understanding pods, their life cycle, specifications, and how they interact with volumes is fundamental in Kubernetes. ReplicaSets build on this knowledge to provide high - availability and scalability. By following the steps and examples provided, you can effectively manage pods and ReplicaSets in your Kubernetes cluster.

代码转载自:https://pan.quark.cn/s/9cde95ebe57a 横道图,亦称为甘特图,是一种可视化的项目管理手段,用于呈现项目的进度安排和时间框架。 在信息技术领域,特别是在项目执行与软件开发范畴内,横道图被普遍采用来监控作业、配置资源以及保障项目能按时交付。 此类图表借助水平条带图示来标示各个任务的起止时间点,使项目成员与管理者可以明确掌握项目的整体发展状况。 周期表或可指代计算机科学中的“作业调度周期表”或“资源配置周期表”。 在计算机系统中,作业调度是一项核心功能,它规定了哪个进程或线程能够在中央处理器上执行以及执行的具体时长。 周期表有助于系统管理者洞察作业的执行频率和资源使用状况,进而提升系统的运作效能和响应能力。 不仅如此,周期表也可能意指数据处理或研究中的周期性文档,如在金融分析中按期更新的市场信息文档。 在压缩文件“横道图,周期表.zip”内含的“横道图,周期表.doc”文件,很可能是对某个项目或任务管理的详尽阐述,涵盖利用横道图来制定和展示项目的时间进程,以及可能牵涉的周期性作业调度或资源配置情形。 文件或许包含以下部分:1. **项目简介**:阐述项目的目标、范畴、预期成效及参与项目的团队成员。 2. **横道图详述**:具体列出了项目中的各项任务,每个任务的启动与终止时间,以及它们之间的关联性。 横道图通常涵盖关键节点,这些节点是项目中的重要事件,象征重要阶段的实现。 3. **任务配置**:明确了每个任务的责任归属,使项目成员明晰自己的职责和截止日期。 4. **进展更新**:若文件是动态维护的,可能会记录项目的实际进展与计划进展的对比,有助于识别延误并调整计划。 5. **周期表探讨**:深入说明了周期性作业的调度,如定期的会议、报告递交、...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值