Kubernetes 简明教程

Kubernetes - Pod

Pod 是 Kubernetes 集群的节点内的一个容器及其存储的集合。可以创建一个 Pod,其中包含多个容器。例如,将数据库容器和数据容器保存在同一个 Pod 中。

A pod is a collection of containers and its storage inside a node of a Kubernetes cluster. It is possible to create a pod with multiple containers inside it. For example, keeping a database container and data container in the same pod.

Types of Pod

共有两种 Pod -

There are two types of Pods −

  1. Single container pod

  2. Multi container pod

Single Container Pod

它们可以通过 kubctl 运行命令轻松创建,其中我们在 Docker 注册表上有定义好的映像,在创建 Pod 时将提取映像。

They can be simply created with the kubctl run command, where you have a defined image on the Docker registry which we will pull while creating a pod.

$ kubectl run <name of pod> --image=<name of the image from registry>

Example − 我们将使用 Docker 集线器上可用的 tomcat 映像创建 Pod。

Example − We will create a pod with a tomcat image which is available on the Docker hub.

$ kubectl run tomcat --image = tomcat:8.0

还可以通过创建 yaml 文件然后运行 kubectl create 命令来完成此操作。

This can also be done by creating the yaml file and then running the kubectl create command.

apiVersion: v1
kind: Pod
metadata:
   name: Tomcat
spec:
   containers:
   - name: Tomcat
    image: tomcat: 8.0
    ports:
containerPort: 7500
   imagePullPolicy: Always

在创建上述 yaml 文件后,我们将会使用 tomcat.yml 名称保存文件并运行 create 命令来运行文档。

Once the above yaml file is created, we will save the file with the name of tomcat.yml and run the create command to run the document.

$ kubectl create –f tomcat.yml

它将创建名为 tomcat 的 Pod。我们可以将 describe 命令与 kubectl 一起使用来描述 Pod。

It will create a pod with the name of tomcat. We can use the describe command along with kubectl to describe the pod.

Multi Container Pod

多容器 Pod 使用 yaml mail 和容器定义创建。

Multi container pods are created using yaml mail with the definition of the containers.

apiVersion: v1
kind: Pod
metadata:
   name: Tomcat
spec:
   containers:
   - name: Tomcat
    image: tomcat: 8.0
    ports:
containerPort: 7500
   imagePullPolicy: Always
   -name: Database
   Image: mongoDB
   Ports:
containerPort: 7501
   imagePullPolicy: Always

在上述代码中,我们创建了一个 Pod,其中包含两个容器,一个用于 tomcat,另一个用于 MongoDB。

In the above code, we have created one pod with two containers inside it, one for tomcat and the other for MongoDB.