Kubernetes基础:Pod的详细介绍

本文的演练环境为基于Virtualbox搭建的Kubernetes集群,具体搭建步骤可以参考kubeadm安装kubernetes V1.11.1 集群

1. 基本概念

1.1 Pod是什么

Pod是Kubernetes中能够创建和部署的最小单元,是Kubernetes集群中的一个应用实例,总是部署在同一个节点Node上。Pod中包含了一个或多个容器,还包括了存储、网络等各个容器共享的资源。Pod支持多种容器环境,Docker则是最流行的容器环境。

  • 单容器Pod,最常见的应用方式。
  • 多容器Pod,对于多容器Pod,Kubernetes会保证所有的容器都在同一台物理主机或虚拟主机中运行。多容器Pod是相对高阶的使用方式,除非应用耦合特别严重,一般不推荐使用这种方式。一个Pod内的容器共享IP地址和端口范围,容器之间可以通过 localhost 互相访问。

Pod并不提供保证正常运行的能力,因为可能遭受Node节点的物理故障、网络分区等等的影响,整体的高可用是Kubernetes集群通过在集群内调度Node来实现的。通常情况下我们不要直接创建Pod,一般都是通过Controller来进行管理,但是了解Pod对于我们熟悉控制器非常有好处。

1.2 Pod带来的好处

Pod带来的好处

  • Pod做为一个可以独立运行的服务单元,简化了应用部署的难度,以更高的抽象层次为应用部署管提供了极大的方便。
  • Pod做为最小的应用实例可以独立运行,因此可以方便的进行部署、水平扩展和收缩、方便进行调度管理与资源的分配。
  • Pod中的容器共享相同的数据和网络地址空间,Pod之间也进行了统一的资源管理与分配。

1.3 常用Pod管理命令

Pod的配置信息中有几个重要部分,apiVersion、kind、metadata、spec以及status。其中apiVersionkind是比较固定的,status是运行时的状态,所以最重要的就是metadataspec两个部分。

先来看一个典型的配置文件,命名为 first-pod.yml

apiVersion: v1
kind: Pod
metadata:
  name: first-pod
  labels:
    app: bash
    tir: backend
spec:
  containers:
  - name: bash-container
    image: docker.io/busybox
    command: [‘sh‘, ‘-c‘, ‘echo Hello Kubernetes! && sleep 3600‘]

在编写配置文件时,可以通过API Reference来参考,也可以通过命令查看。

[[email protected] ~]# kubectl explain pod
KIND:     Pod
VERSION:  v1

DESCRIPTION:
     Pod is a collection of containers that can run on a host. This resource is
     created by clients and scheduled onto hosts.

FIELDS:
   apiVersion   <string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#resources

   kind <string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds

   metadata <Object>
     Standard object‘s metadata. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata

   spec <Object>
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

   status   <Object>
     Most recently observed status of the pod. This data may not be up to date.
     Populated by the system. Read-only. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
[[email protected] ~]# kubectl explain pod.spec
KIND:     Pod
VERSION:  v1

RESOURCE: spec <Object>

DESCRIPTION:
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status

     PodSpec is a description of a pod.

FIELDS:
   activeDeadlineSeconds    <integer>
     Optional duration in seconds the pod may be active on the node relative to
     StartTime before the system will actively try to mark it failed and kill
     associated containers. Value must be a positive integer.

   affinity <Object>
     If specified, the pod‘s scheduling constraints

   automountServiceAccountToken <boolean>
     AutomountServiceAccountToken indicates whether a service account token
     should be automatically mounted.

1.3.1 创建

利用kubectl命令行管理工具,我们可以直接在命令行通过配置文件创建。如果安装了Dashboard图形管理界面,还可以通过图形界面创建Pod。因为最终Pod的创建都是落在命令上的,这里只介绍如何使用kubectl管理工具来创建。

使用配置文件的方式创建Pod。

$ kubectl create -f first-pod.yml 

1.3.2 查看配置

如果想了解一个正在运行的Pod的配置,可以通过以下命令获取。

[[email protected] ~]# kubectl get pod first-pod -o yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: 2018-08-08T01:45:16Z
  labels:
    app: bash
  name: first-pod
  namespace: default
  resourceVersion: "184988"
  selfLink: /api/v1/namespaces/default/pods/first-pod
  uid: b2d3d2b7-9aac-11e8-84f4-080027b7c4e9
spec:
  containers:
  - command:
    - sh
    - -c
    - echo Hello Kubernetes! && sleep 3600
    image: docker.io/busybox
    imagePullPolicy: Always
    name: bash-container
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-trvqv
      readOnly: true
  dnsPolicy: ClusterFirst
  nodeName: devops-102
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: default-token-trvqv
    secret:
      defaultMode: 420
      secretName: default-token-trvqv
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: 2018-08-08T01:45:16Z
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: 2018-08-08T01:45:16Z
    message: ‘containers with unready status: [bash-container]‘
    reason: ContainersNotReady
    status: "False"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: null
    message: ‘containers with unready status: [bash-container]‘
    reason: ContainersNotReady
    status: "False"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: 2018-08-08T01:45:16Z
    status: "True"
    type: PodScheduled
  containerStatuses:
  - image: docker.io/busybox
    imageID: ""
    lastState: {}
    name: bash-container
    ready: false
    restartCount: 0
    state:
      waiting:
        reason: ContainerCreating
  hostIP: 192.168.0.102
  phase: Pending
  qosClass: BestEffort
  startTime: 2018-08-08T01:45:16Z

1.3.3 查看日志

可以查看命令行标准输出的日志。

[[email protected] ~]# kubectl logs first-pod
Hello Kubernetes!

如果Pod中有多个容器,查看特定容器的日志需要指定容器名称kubectl logs pod-name -c container-name

1.3.4 标签管理

标签是Kubernetes管理Pod的重要依据,我们可以在Pod yaml文件中 metadata 中指定,也可以通过命令行进行管理。

显示Pod的标签

[[email protected] ~]# kubectl get pods --show-labels
NAME        READY     STATUS    RESTARTS   AGE       LABELS
first-pod   1/1       Running   0          15m       app=bash

使用 second-pod.yml 我们再创建一个包含两个标签的Pod。

[[email protected] ~]# kubectl create -f first-pod.yml
pod/second-pod created
[[email protected] ~]# kubectl get pods --show-labels
NAME         READY     STATUS              RESTARTS   AGE       LABELS
first-pod    1/1       Running             0          17m       app=bash
second-pod   0/1       ContainerCreating   0          20s       app=bash,tir=backend

根据标签来查询Pod。

[[email protected] ~]# kubectl get pods -l tir=backend --show-labels
NAME         READY     STATUS    RESTARTS   AGE       LABELS
second-pod   1/1       Running   0          1m        app=bash,tir=backend

增加标签

[[email protected] ~]# kubectl label pod first-pod tir=frontend
pod/first-pod labeled
[[email protected] ~]# kubectl get pods --show-labels
NAME         READY     STATUS    RESTARTS   AGE       LABELS
first-pod    1/1       Running   0          24m       app=bash,tir=frontend
second-pod   1/1       Running   0          7m        app=bash,tir=backend

修改标签

[[email protected] ~]# kubectl label pod first-pod tir=unkonwn --overwrite
pod/first-pod labeled
[[email protected] ~]# kubectl get pods --show-labels
NAME         READY     STATUS    RESTARTS   AGE       LABELS
first-pod    1/1       Running   0          25m       app=bash,tir=unkonwn
second-pod   1/1       Running   0          8m        app=bash,tir=backend

可以将标签显示为列

[[email protected] ~]# kubectl get pods -L app,tir
NAME         READY     STATUS    RESTARTS   AGE       APP       TIR
first-pod    1/1       Running   0          26m       bash      unkonwn
second-pod   1/1       Running   0          9m        bash      backend

标签是Kubernetes中非常强大的一个功能,Node节点也可以增加标签,再利用Pod的标签选择器,可以将Pod分配到不同类型的Node上。

1.3.5 删除Pod

[[email protected] ~]# kubectl delete pods first-pod
pod "first-pod" deleted

也可以根据标签选择器删除。

[[email protected] ~]# kubectl delete pods -l tir=backend
pod "second-pod" deleted

1.4 Pod的生命周期

像单独的容器应用一样,Pod并不是持久运行的。Pod创建后,Kubernetes为其分配一个UID,并且通过Controller调度到Node中运行,然后Pod一直保持运行状态直到运行正常结束或者被删除。在Node发生故障时,Controller负责将其调度到其他的Node中。Kubernetes为Pod定义了几种状态,分别如下:

  • Pending,Pod已创建,正在等待容器创建。经常是正在下载镜像,因为这一步骤最耗费时间。
  • Running,Pod已经绑定到某个Node并且正在运行。或者可能正在进行意外中断后的重启。
  • Succeeded,表示Pod中的容器已经正常结束并且不需要重启。
  • Failed,表示Pod中的容器遇到了错误而终止。
  • Unknown,因为网络或其他原因,无法获取Pod的状态。

2. 如何对Pod进行健康检查

Kubernetes利用Handler功能,可以对容器的状况进行探测,有以下三种形式。

  • ExecAction:在容器中执行特定的命令。
  • TCPSocketAction:检查容器端口是否可以连接。
  • HTTPGetAction:检查HTTP请求状态是否正常。

这部分内容展开来也比较多,这部分的内容参考Kubernetes中Pod的健康检查

3. Init Containers

Pod中可以包含一到多个Init Container,在其他容器之前开始运行。Init Container 只能是运行到完成状态,即不能够一直存在。Init Container必须依次执行。在App Container运行前,所有的Init Container必须全部正常结束。

在Pod启动过程中,Init Container在网络和存储初始化完成后开始按顺序启动。Pod重启的时候,所有的Init Container都会重新执行。

However, if the Pod restartPolicy is set to Always, the Init Containers use RestartPolicy OnFailure.

3.1 好处

  • 运行一些不希望在 App Container 中运行的命令或工具
  • 包含一些App Image中没有的工具或特定代码
  • 应用镜像构建人员和部署人员可以独立工作而不需要依赖对方
  • 拥有与App Container不同的命名空间
  • 因为在App Container运行前必须运行结束,适合做一些前置条件的检查和配置

3.2 语法

先看一下解释

[[email protected] ~]# kubectl explain pod.spec.initContainers
KIND:     Pod
VERSION:  v1

RESOURCE: initContainers <[]Object>

DESCRIPTION:
     List of initialization containers belonging to the pod. Init containers are
     executed in order prior to containers being started. If any init container
     fails, the pod is considered to have failed and is handled according to its
     restartPolicy. The name for an init container or normal container must be
     unique among all containers. Init containers may not have Lifecycle
     actions, Readiness probes, or Liveness probes. The resourceRequirements of
     an init container are taken into account during scheduling by finding the
     highest request/limit for each resource type, and then using the max of of
     that value or the sum of the normal containers. Limits are applied to init
     containers in a similar fashion. Init containers cannot currently be added
     or removed. Cannot be updated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/init-containers/

     A single application container that you want to run within a pod.

具体语法。

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: docker.io/busybox
    command: [‘sh‘, ‘-c‘, ‘echo The app is running! && sleep 3600‘]
  initContainers:
  - name: init-myservice
    image: docker.io/busybox
    command: [‘sh‘, ‘-c‘, ‘echo init-service && sleep 2‘]
  - name: init-mydb
    image: docker.io/busybox
    command: [‘sh‘, ‘-c‘, ‘echo init-mydb && sleep 2‘]

兼容性问题
1.5之前的语法都写在 annotation 中,1.6 以上的版本使用 .spec.initContainers 字段。建议还是使用 1.6 版本的语法。1.6、1.7的版本还兼容1.5以下的版本,1.8之后就不再兼容老版本了。

4. Pod Preset

利用这个特性,可以在Pod启动过程中向Pod中注入密码 Secrets、存储 Volumes、挂载点 Volume Mounts和环境变量。通过标签选择器来指定Pod。利用这个特性,Pod Template的维护人员就不需要为每个Pod显示的提供相关的属性。

具体的工作步骤

  • 检查所有可用的ProdPresets
  • 检查是否有ProdPreset的标签与即将创建的Pod相匹配
  • 将PodPreset中定义的参数与Pod定义合并
  • 如果参数合并出错,则丢弃ProPreset参数,继续创建Pod
  • 为Pod增加注解,表示层被ProdPreset修改过,形式为 podpreset.admission.kubernetes.io/podpreset-<pod-preset name>: "<resource version>"

对于 EnvEnvFromVolumeMounts Kubernetes修改Container Spec,对于Volume修改Pod Spec。

4.1 对个别Pod停用

在Spec中增加注解:

podpreset.admission.kubernetes.io/exclude: "true"

5. 中断

Pod会因为各种各样的原因发生中断。

5.1 计划内中断

  • 删除部署 Deployment或者其他控制器
  • 更新部署模版导致的Pod重启
  • 直接删除Pod
  • 集群的缩容
  • 手工移除

5.2 计划外中断

  • 硬件故障、物理节点宕机
  • 集群管理员误删VM
  • 云供应商故障导致的主机不可用
  • Kernel panic
  • 集群网络分区导致节点消失
  • 资源耗尽导致的节点剔除

5.3 PDB Disruption Budgets

Kubernetes offers features to help run highly available applications at the same time as frequent voluntary disruptions. We call this set of features Disruption Budgets.

Kubernetes允许我们创建一个PDB对象,来确保一个RS中运行的Pod不会在一个预算(个数)之下。
Eviction API。

PDB是用来解决集群管理和应用管理职责分离的情况,如果你的单位不存在这种情况,就可以不使用PDB。

参考资料

    1. Pods
    2. Kubernetes in action

原文地址:https://www.cnblogs.com/sea520/p/11411661.html

时间: 2024-08-30 08:03:52

Kubernetes基础:Pod的详细介绍的相关文章

kubernetes之pod超详细解读--第二篇(三)

8.资源对象对pod的调度 ??在kubernetes集群中,pod基本上都是容器的载体,通常需要通过相应的资源对象来完成一组pod的调度和自动控制功能,比如:deployment.daemonSet.RC.Job等等.接下来小编将一一介绍这些资源对象如何调度pod. (1)Deployment/RC 自动化调度 ??Deployment/RC的主要功能之一就是自动部署一个容器应用的多个副本,以及持续监控副本数量,在集群内始终维持用户指定的副本数量.举例:(这里以deployment为例) ap

kubernetes之pod超详细解读--第一篇(三)

   小编在这里向各位博友道个歉,上篇文章确实写的有些应付,但怎么说,部署确实因人而异,而且很少有刚刚进公司就让你搭建一个集群,一般公司都有自己的集群,所以小编觉得,侧重点不应该在安装,应该在维护!虽然有些牵强,但小编保证,这一篇绝对有质量!希望看了小编的博客,大家对pod有更深入的认识.   这篇文章,小编打算介绍关于pod的11个重要的知识点,大家要有耐心的看下去哦!虽然内容比较多,有兴趣的朋友可以细细阅读,小编会尽可能的用比较容易理解的话和图,去介绍比较重要并且难以理解的地方. 1. po

linux 常用基础命令 tar 详细介绍

[命令介绍] tar命令可以为linux的文件和目录创建档案.利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件.tar最初被用来在磁带上创建档案,现在,用户可以在任何设备上创建档案.利用tar命令,可以把一大堆的文件和目录全部打包成一个文件,这对于备份文件或将几个文件组合成为一个文件以便于网络传输是非常有用的. 首先要弄清两个概念:打包和压缩. 打包是指将一大堆文件或目录变成一个总的文件: 压缩则是将一个大的文件通过一些压缩算法变成一个小文件.

linux 常用基础命令 cat 详细介绍

cat 输出文件内容: 命令说明:cat(Concatenate的缩写),一条linux内置命令,把一个或者多个文件连接在一起,并标准输出或输入.常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示.它常与重定向符号配合使用. 命令功能: a)  一次显示整个文件:catfilename b)  从键盘创建一个文件:cat> filename 只能创建新文件,不能编辑已有文件 c)  将几个文件合并为一个文件:catfile1 file2 > file 注: cat f

Linux shell脚本基础学习详细介绍(完整版)一

Linux shell脚本基础学习这里我们先来第一讲,介绍shell的语法基础,开头.注释.变量和 环境变量,向大家做一个基础的介绍,虽然不涉及具体东西,但是打好基础是以后学习轻松地前提.1. Linux 脚本编写基础◆1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在这个例子中我们使用/bin/sh来执行程序. 当编辑好脚本时,如果要执行该脚本,还必须使其可执行. 要使脚本可执

Linux shell脚本基础学习详细介绍(完整版)二

详细介绍Linux shell脚本基础学习(五) Linux shell脚本基础前面我们在介绍Linux shell脚本的控制流程时,还有一部分内容没讲就是有关here document的内容这里继续. Linux shell脚本基础已经被分成好几个部分了,这里对控制流程的内容也就马上讲完了,这是最后一部分关于here document,这里举例稍微有点复杂,我们慢慢来分析这个复杂Linux shell脚本. 6. Here documents 当要将几行文字传递给一个命令时,here docu

Saltstack 基础详细介绍

1.saltstack 基础详细介绍  Saltstack使用Python开发,是一个非常简单易用和轻量级的管理工具.由Master和Minion构成,通过ZeroMQ进行通信    Saltstack的master端监听4505与4506端口,4505为salt的消息发布系统,4506为salt客户端与服务端通信的端口:salt客户端程序不监听端口,客户端启动后,会主动连接master端注册,然后一直保持该TCP连接,master通过这条TCP连接对客户端控制,如果连接断开,master对客户

【COCOS2DX-LUA 脚本开发之一】在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途!

[COCOS2DX-LUA 脚本开发之一]在Cocos2dX游戏中使用Lua脚本进行游戏开发(基础篇)并介绍脚本在游戏中详细用途! 分类: [Cocos2dx Lua 脚本开发 ] 2012-04-16 10:08 30803人阅读 评论(18) 收藏 举报 游戏脚本luaanimationpython 本站文章均为李华明Himi原创,转载务必在明显处注明:转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-cocos2dx/681.htm

较详细介绍IBM AIX操作系统上常用的korn shell的基础知识

[导读]较详细介绍IBM AIX操作系统上常用的korn shell的基础知识. Korn Shell执行命令的顺序1.关键字,如if,for,function等2.别名(Aliases)3.内部命令,如cd, whence和函数等4.函数Functions5.脚本Scripts.在PATH中的可执行程序※找到命令的根源用下面的命令:whence -v command或:type commandAlias(别名)alias aliasname=command取消一个别名:unalias alia