首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

Pod详解

  • 23-11-14 12:22
  • 3868
  • 11864
blog.csdn.net

Pod介绍

Pod结构

在这里插入图片描述

每个Pod中都可以包含一个或者多个容器,这些容器可以分为两类:

  • 用户程序所在的容器,数量可多可少
  • Pause容器,这是每个Pod都会有的一个根容器,它的作用有两个:
  1. 可以以它为依据,评估整个Pod的健康状态

  2. 可以在根容器上设置Ip地址,其它容器都此Ip(Pod IP),以实现Pod内部的网路通信
    这里是Pod内部的通讯,Pod的之间的通讯采用虚拟二层网络技术来实现,我们当前环境用的是Flannel

Pod定义

下面是Pod的资源清单:

apiVersion: v1     # 必选,版本号,例如v1
kind: Pod         # 必选,资源类型,例如 Pod
metadata:         # 必选,元数据
  name: string     # 必选,Pod名称
  namespace: string  # Pod所属的命名空间,默认为"default"
  labels:           # 自定义标签列表
    - name: string                 
spec:  # 必选,Pod中容器的详细定义
  containers:  # 必选,Pod中容器列表
  - name: string   # 必选,容器名称
    image: string  # 必选,容器的镜像名称
    imagePullPolicy: [ Always|Never|IfNotPresent ]  # 获取镜像的策略 
    command: [string]   # 容器的启动命令列表,如不指定,使用打包时使用的启动命令
    args: [string]      # 容器的启动命令参数列表
    workingDir: string  # 容器的工作目录
    volumeMounts:       # 挂载到容器内部的存储卷配置
    - name: string      # 引用pod定义的共享存储卷的名称,需用volumes[]部分定义的的卷名
      mountPath: string # 存储卷在容器内mount的绝对路径,应少于512字符
      readOnly: boolean # 是否为只读模式
    ports: # 需要暴露的端口库号列表
    - name: string        # 端口的名称
      containerPort: int  # 容器需要监听的端口号
      hostPort: int       # 容器所在主机需要监听的端口号,默认与Container相同
      protocol: string    # 端口协议,支持TCP和UDP,默认TCP
    env:   # 容器运行前需设置的环境变量列表
    - name: string  # 环境变量名称
      value: string # 环境变量的值
    resources: # 资源限制和请求的设置
      limits:  # 资源限制的设置
        cpu: string     # Cpu的限制,单位为core数,将用于docker run --cpu-shares参数
        memory: string  # 内存限制,单位可以为Mib/Gib,将用于docker run --memory参数
      requests: # 资源请求的设置
        cpu: string    # Cpu请求,容器启动的初始可用数量
        memory: string # 内存请求,容器启动的初始可用数量
    lifecycle: # 生命周期钩子
		postStart: # 容器启动后立即执行此钩子,如果执行失败,会根据重启策略进行重启
		preStop: # 容器终止前执行此钩子,无论结果如何,容器都会终止
    livenessProbe:  # 对Pod内各容器健康检查的设置,当探测无响应几次后将自动重启该容器
      exec:         # 对Pod容器内检查方式设置为exec方式
        command: [string]  # exec方式需要制定的命令或脚本
      httpGet:       # 对Pod内个容器健康检查方法设置为HttpGet,需要制定Path、port
        path: string
        port: number
        host: string
        scheme: string
        HttpHeaders:
        - name: string
          value: string
      tcpSocket:     # 对Pod内个容器健康检查方式设置为tcpSocket方式
         port: number
       initialDelaySeconds: 0       # 容器启动完成后首次探测的时间,单位为秒
       timeoutSeconds: 0          # 对容器健康检查探测等待响应的超时时间,单位秒,默认1秒
       periodSeconds: 0           # 对容器监控检查的定期探测时间设置,单位秒,默认10秒一次
       successThreshold: 0
       failureThreshold: 0
       securityContext:
         privileged: false
  restartPolicy: [Always | Never | OnFailure]  # Pod的重启策略
  nodeName:  # 设置NodeName表示将该Pod调度到指定到名称的node节点上
  nodeSelector: obeject # 设置NodeSelector表示将该Pod调度到包含这个label的node上
  imagePullSecrets: # Pull镜像时使用的secret名称,以key:secretkey格式指定
  - name: string
  hostNetwork: false   # 是否使用主机网络模式,默认为false,如果设置为true,表示使用宿主机网络
  volumes:   # 在该pod上定义共享存储卷列表
  - name: string    # 共享存储卷名称 (volumes类型有很多种)
    emptyDir: {}       # 类型为emtyDir的存储卷,与Pod同生命周期的一个临时目录。为空值
    hostPath: string   # 类型为hostPath的存储卷,表示挂载Pod所在宿主机的目录
      path: string                # Pod所在宿主机的目录,将被用于同期中mount的目录
    secret:          # 类型为secret的存储卷,挂载集群与定义的secret对象到容器内部
      scretname: string  
      items:     
      - key: string
        path: string
    configMap:         # 类型为configMap的存储卷,挂载预定义的configMap对象到容器内部
      name: string
      items:
      - key: string
        path: string
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

explain查看每种资源的可配置项

# 小提示:
#	在这里,可通过一个命令来查看每种资源的可配置项
#   kubectl explain 资源类型         查看某种资源可以配置的一级属性
[root@k8s-master-01 ~]# kubectl explain pod
KIND:     Pod
VERSION:  v1
FIELDS:
   apiVersion	
   kind	
   metadata	
   spec	
   status	

#	kubectl explain 资源类型.属性     查看属性的子属性
[root@k8s-master-01 ~]# kubectl explain pod.metadata
KIND:     Pod
VERSION:  v1
RESOURCE: metadata 
FIELDS:
   annotations	[string]string>
   clusterName	
   creationTimestamp	
   deletionTimestamp	
   finalizers	<[]string>
   generateName	
   generation	
   labels	[string]string>
   managedFields	<[]Object>
   name	
   namespace	
   ownerReferences	<[]Object>
   resourceVersion	
   selfLink	
   uid	
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

在kubernetes中基本所有资源的一级属性都是一样的,主要包含5部分:

apiVersion       版本,由kubernetes内部定义,版本号必须可以用 kubectl api-versions 查询到

kind                 类型,由kubernetes内部定义,版本号必须可以用 kubectl api-resources 查询到

metadata        元数据,主要是资源标识和说明,常用的有name、namespace、labels等

spec                描述,这是配置中最重要的一部分,里面是对各种资源配置的详细描述                

status              状态信息,里面的内容不需要定义,由kubernetes自动生成
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在上面的属性中,spec是接下来研究的重点,继续看下它的常见子属性:

containers   <[]Object>       容器列表,用于定义容器的详细信息 
 
nodeName 	       根据nodeName的值将pod调度到指定的Node节点上
 
nodeSelector   []>      根据NodeSelector中定义的信息选择将该Pod调度到包含这些label的Node上
 
hostNetwork      	是否使用主机网络模式,默认为false,如果设置为true,表示使用宿主机网络
 
volumes    <[]Object>       存储卷,用于定义Pod上面挂在的存储信息 

restartPolicy	       重启策略,表示Pod在遇到故障的时候的处理策略
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Pod配置

本小节主要来研究 pod.spec.containers 属性,这也是pod配置中最为关键的一项配置。

[root@k8s-master-01 ~]# kubectl explain pod.spec.containers
KIND:     Pod
VERSION:  v1

RESOURCE: containers <[]Object>		# 数组,代表可以有多个容器	
FIELDS:
   name       		 	容器名称
   
   image       			容器需要的镜像地址
   
   imagePullPolicy    		镜像拉取策略 
   
   command  <[]string> 			 容器的启动命令列表,如不指定,使用打包时使用的启动命令
   
   args     <[]string> 			 容器的启动命令需要的参数列表
   
   env      <[]Object> 			 容器环境变量的配置
   
   ports    <[]Object>     		 容器需要暴露的端口号列表
   
   resources       	 	资源限制和资源请求的设置
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

基本配置

创建pod-base.yaml文件,内容如下:

apiVersion: v1
kind: Pod
metadata:
  name: pod-base
  namespace: dev
  labels:
    user: nana
spec:
  containers:
    - name: nginx
      image: nginx1.17.1
    - name: busybox
      image: busybox:1.30
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

上面定义了一个比较简单Pod的配置,里面有两个容器:

  • nginx:用1.17.1版本的nginx镜像创建,(nginx是一个轻量级web容器)

  • busybox:用1.30版本的busybox镜像创建,(busybox是一个小巧的linux命令集合)

# 创建Pod
[root@k8s-master-01 ~]# kubectl apply -f pod-base.yaml 
pod/pod-base created

# 查看Pod状况
# READY 1/2 : 表示当前Pod中有2个容器,其中1个准备就绪,1个未就绪
# RESTARTS  : 重启次数,因为有1个容器故障了,Pod一直在重启试图恢复它
[root@k8s-master-01 ~]# kubectl get pod -n dev
NAME       READY   STATUS             RESTARTS   AGE
pod-base   1/2     CrashLoopBackOff   5          9m35s

# 可以通过describe查看内部的详情
# 此时已经运行起来了一个基本的Pod,虽然它暂时有问题
[root@k8s-master-01 ~]# kubectl describe pod pod-base -n dev
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

镜像拉取

创建 pod-imagepullpolicy.yaml 文件,内容如下:

apiVersion: v1
kind: Pod
metadata:
  name: pod-imagepullpolicy
  namespace: dev
spec:
  containers:
    - name: nginx
      image: nginx
      imagePullPolicy: Always   # 用于设置镜像拉取策略
    - name: busybox
      image: busybox:1.30
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

imagePullPolicy,用于设置镜像拉取策略,kubernetes支持配置三种拉取策略:

  • Always:总是从远程仓库拉取镜像(一直远程下载)

  • IfNotPresent:本地有则使用本地镜像,本地没有则从远程仓库拉取镜像(本地有就本地 本地没远程下载)

  • Never:只使用本地镜像,从不去远程仓库拉取,本地没有就报错 (一直使用本地)

默认值说明:

  • 如果镜像tag为具体版本号, 默认策略是:IfNotPresent

  • 如果镜像tag为:latest(最终版本) ,默认策略是always

# 创建Pod
[root@k8s-master-01 ~]# kubectl create -f pod-imagepullpolicy.yaml
pod/pod-imagepullpolicy created

# 查看Pod详情
# 此时明显可以看到nginx镜像有一步Pulling image "nginx:1.17.1"的过程
[root@k8s-master-01 ~]# kubectl describe -f pod-imagepullpolicy.yaml 
...
Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  105s               default-scheduler  Successfully assigned dev/pod-imagepullpolicy to k8s-node-02
  Normal   Pulling    104s               kubelet            Pulling image "nginx:1.17.2"
  Normal   Pulled     73s                kubelet            Successfully pulled image "nginx:1.17.2" in 31.354177253s
  Normal   Created    72s                kubelet            Created container nginx
  Normal   Started    72s                kubelet            Started container nginx
  Normal   Pulled     31s (x4 over 72s)  kubelet            Container image "busybox:1.30" already present on machine
  Normal   Created    31s (x4 over 72s)  kubelet            Created container busybox
  Normal   Started    31s (x4 over 72s)  kubelet            Started container busybox
  Warning  BackOff    6s (x7 over 70s)   kubelet            Back-off restarting failed container
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

启动命令

在前面的案例中,一直有一个问题没有解决,就是的busybox容器一直没有成功运行,那么到底是什么原因导致这个容器的故障呢?

原来busybox并不是一个程序,而是类似于一个工具类的集合,kubernetes集群启动管理后,它会自动关闭。解决方法就是让其一直在运行,这就用到了command配置。

创建pod-command.yaml文件,内容如下:

apiVersion: v1
kind: Pod
metadata:
  name: pod-command
  namespace: dev
spec:
  containers:
    - name: nginx
      image: nginx:1.17.2
    - name: busybox
      image: busybox:1.30
      #  "/bin/sh","-c",  使用sh执行命令
      command: ["/bin/sh","-c","touch /tmp/hello.txt;while true;do /bin/echo $(date +%T) >> /tmp/hello.txt;sleep 3;done;" ]    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

command,用于在pod中的容器初始化完毕之后运行一个命令。

# 创建Pod
[root@k8s-master-01 ~]# kubectl apply -f pod-command.yaml
pod/pod-command created

# 查看Pod状态
[root@k8s-master-01 ~]# kubectl get -f pod-command.yaml
NAME          READY   STATUS    RESTARTS   AGE
pod-command   2/2     Running   0          7s

# 进入pod中的busybox容器,查看文件内容
# 补充一个命令(进入容器内部): kubectl exec  pod名称 -n 命名空间 -it -c 容器名称 /bin/sh  
# 使用这个命令就可以进入某个容器的内部,然后进行相关操作了
# 比如,可以查看txt文件的内容
[root@k8s-master-01 ~]# kubectl exec pod-command -n dev -it -c busybox /bin/sh 
/ # tail -f /tmp/hello.txt
07:04:07
07:04:10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

特别说明:
通过上面发现command已经可以完成启动命令和传递参数的功能,为什么这里还要提供一个args选项,用于传递参数呢?

command  <[]string> 	# 容器的启动命令列表,如不指定,使用打包时使用的启动命令
  
args  <[]string> 	 # 容器的启动命令需要的参数列表
  • 1
  • 2
  • 3

这其实跟docker有点关系,kubernetes中的command、args两项其实是实现覆盖Dockerfile中ENTRYPOINT的功能。

  1. 如果command和args均没有写,那么用Dockerfile的配置。

  2. 如果command写了,但args没有写,那么Dockerfile默认的配置会被忽略,执行输入的command

  3. 如果command没写,但args写了,那么Dockerfile中配置的ENTRYPOINT的命令会被执行,使用当前args的参数

  4. 如果command和args都写了,那么Dockerfile的配置被忽略,执行command并追加上args参数

环境变量

创建pod-env.yaml文件,内容如下:

apiVersion: v1
kind: Pod
metadata:
  name: pod-env
  namespace: dev
spec:
  containers:
    - name: busybox
      image: busybox:1.30
      command: ["/bin/sh","-c","while true;do /bin/echo $(date +%T);sleep 60; done;"]
      env: # 设置环境变量列表
        - name: "username"
          value: "admin"
        - name: "password"
          value: "123"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

env,环境变量,用于在pod中的容器设置环境变量。

# 创建Pod
[root@k8s-master-01 ~]# kubectl create -f pod-env.yaml 
pod/pod-env created

# 进入容器,输出环境变量
[root@k8s-master-01 ~]# kubectl exec -it pod-env -n dev busybox -it /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/ # echo $username
admin
/ # echo $password
123
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

这种方式不是很推荐,推荐将这些配置单独存储在配置文件中,这种方式将在后面介绍。

端口设置

本小节来介绍容器的端口设置,也就是containers的ports选项pod.spec.containers.ports 。

首先看下ports支持的子选项:
在这里插入图片描述

[root@k8s-master-01 ~]# kubectl explain pod.spec.containers.ports
KIND:     Pod
VERSION:  v1
RESOURCE: ports <[]Object>
   name           	# 端口名称,如果指定,必须保证name在pod中是唯一的		
   containerPort 	# 容器要监听的端口(0 < x < 65536)
   hostPort      	# 容器要在主机上公开的端口,如果设置,主机上只能运行容器的一个副本(一般省略) 
   hostIP         	# 要将外部端口绑定到的主机IP(一般省略)
   protocol       	# 端口协议。必须是UDP、TCP或SCTP。默认为"TCP"。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

创建pod-ports.yaml文件,内容如下:

apiVersion: v1
kind: Pod
metadata:
  name: pod-ports
  namespace: dev
spec:
  containers:
    - name: nginx
      image: nginx:1.17.2
      ports:  # 设置容器暴露的端口列表
        - name: nginx-port
          containerPort: 80
          protocol: TCP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

containerPort,容器要监听的端口( 0 < x < 65536 )

# 创建Pod
[root@k8s-master-01 ~]# kubectl create -f pod-ports.yaml
pod/pod-ports created

# 查看pod
# 在下面可以明显看到配置信息
[root@k8s-master-01 ~]# kubectl get pod pod-ports -n dev -o yaml
...
spec:
  containers:
  - image: nginx:1.17.2
    imagePullPolicy: IfNotPresent
    name: nginx
    ports:
    - containerPort: 80
      name: nginx-port
      protocol: TCP
...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

访问容器中的程序需要使用的是podIp:containerPort

# 查看ip
[root@k8s-master-01 ~]# kubectl get pod pod-ports -n dev -o wide
NAME        READY   STATUS    RESTARTS   AGE   IP           NODE          NOMINATED NODE   READINESS GATES
pod-ports   1/1     Running   0          10m   10.244.1.7   k8s-node-01              

# 在本地访问Nginx
[root@k8s-master-01 ~]# curl 10.244.1.7
<!DOCTYPE html>


Welcome to nginx!<<span class="token operator">/</span>title>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li></ul></pre> 
<h4><a name="t9"></a><a id="_476"></a>资源配额</h4> 
<blockquote> 
 <p>容器中的程序要运行,肯定是要占用一定资源的,比如cpu和内存等,如果不对某个容器的资源做限制,那么它就可能吃掉大量资源,导致其它容器无法运行。</p> 
</blockquote> 
<blockquote> 
 <p>针对这种情况,kubernetes提供了对内存和cpu的资源进行配额的机制,这种机制主要通过<code>resources</code>选项实现,他有两个子选项:</p> 
 <pre data-index="18" class="set-code-show prettyprint"><code class="has-numbering" onclick="mdcp.signin(event)" style="position: unset;">limits: 用于限制运行时容器的最大占用资源,当容器占用资源超过limits时会被终止,并进行重启

requests: 用于设置容器需要的最小资源,如果环境资源不够,容器将无法启动
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre> 
 <p>可以通过上面两个选项设置资源的上下限。</p> 
</blockquote> 
<blockquote> 
 <p><strong>创建pod-resources.yaml文件,内容如下:</strong></p> 
</blockquote> 
<pre data-index="19" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">apiVersion: v1
kind: Pod
metadata:
  name: pod-resources
  namespace: dev
spec:
  containers:
    <span class="token operator">-</span> name: nginx
      image: nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>2
      resources:    <span class="token comment"># 资源配额</span>
        limits:     <span class="token comment"># 限制资源(上限)</span>
          cpu: <span class="token string">"2"</span>    <span class="token comment"># cpu限制,单位是core(核心)数</span>
          memory: <span class="token string">"10Gi"</span>  <span class="token comment"># 内存限制</span>
        requests:    <span class="token comment"># 请求资源(下限)</span>
          cpu: <span class="token string">"1"</span>   <span class="token comment"># cpu限制,单位是core(核心)数</span>
          memory: <span class="token string">"10Mi"</span>    <span class="token comment"># 内存限制</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li></ul></pre> 
<blockquote> 
 <p>在这对cpu和memory的单位做一个说明:</p> 
 <ul><li>cpu:core数,可以为整数或小数</li><li>memory: 内存大小,可以使用Gi、Mi、G、M等形式</li></ul> 
</blockquote> 
<pre data-index="20" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 运行Pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-resources.yaml </span>
pod/pod-resources created

<span class="token comment"># 查看发现pod运行正常</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pod pod-resources -n dev</span>
NAME            READY   STATUS    RESTARTS   AGE
pod-resources   1/1     Running   0          13s

<span class="token comment"># 接下来,停止Pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl delete -f pod-resources.yaml </span>
pod <span class="token string">"pod-resources"</span> deleted

<span class="token comment"># 编辑pod,修改resources.requests.memory的值为10Gi</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># vim pod-resources.yaml </span>

<span class="token comment"># 编辑pod,修改resources.requests.memory的值为10Gi</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-resources.yaml </span>
pod/pod-resources created

<span class="token comment"># 查看Pod状态,发现Pod启动失败</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pod pod-resources -n dev -o wide</span>
NAME            READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
pod-resources   0/1     Pending   0          28s   <none>   <none>   <none>           <none>

<span class="token comment"># 查看pod详情会发现,如下提示</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl describe pod pod-resources -n dev</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
Events:
  <span class="token function">Type</span>     Reason            Age                <span class="token keyword">From</span>               Message
  <span class="token operator">--</span><span class="token operator">--</span>     <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>            <span class="token operator">--</span><span class="token operator">--</span>               <span class="token operator">--</span><span class="token operator">--</span>               <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
  Warning  FailedScheduling  46s <span class="token punctuation">(</span>x2 over 48s<span class="token punctuation">)</span>  default-scheduler  0/3 nodes are available: 1 node<span class="token punctuation">(</span>s<span class="token punctuation">)</span> had taint <span class="token punctuation">{<!-- --></span>node-role<span class="token punctuation">.</span>kubernetes<span class="token punctuation">.</span>io/master: <span class="token punctuation">}</span><span class="token punctuation">,</span> that the pod didn't tolerate<span class="token punctuation">,</span> 2 Insufficient memory<span class="token punctuation">.</span>
<span class="token comment"># 提示内存不足</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li><li style="color: rgb(153, 153, 153);">31</li><li style="color: rgb(153, 153, 153);">32</li><li style="color: rgb(153, 153, 153);">33</li></ul></pre> 
<h3><a name="t10"></a><a id="Pod_550"></a>Pod生命周期</h3> 
<blockquote> 
 <p>我们一般将pod对象从创建至终的这段时间范围称为pod的生命周期,它主要包含下面的过程:</p> 
 <ul><li>pod创建过程</li><li>运行初始化容器(init container)过程</li><li>运行主容器(main container) 
   <ul><li>容器启动后钩子(post start)、容器终止前钩子(pre stop)</li><li>容器的存活性探测(liveness probe)、就绪性探测(readiness probe)</li></ul> </li><li>pod终止过程<br> <img src="https://img1.iyenn.com/img10/c4a7c45db2b2b29e/1957169990099493303.jpeg" alt="在这里插入图片描述"></li></ul> 
 <p>在整个生命周期中,Pod会出现5种状态(相位),分别如下:</p> 
 <ul><li>挂起(Pending):apiserver已经创建了pod资源对象,但它尚未被调度完成或者仍处于下载镜像的过程中</li><li>运行中(Running):pod已经被调度至某节点,并且所有容器都已经被kubelet创建完成</li><li>成功(Succeeded):pod中的所有容器都已经成功终止并且不会被重启</li><li>失败(Failed):所有容器都已经终止,但至少有一个容器终止失败,即容器返回了非0值的退出状态</li><li>未知(Unknown):apiserver无法正常获取到pod对象的状态信息,通常由网络通信失败所导致</li></ul> 
</blockquote> 
<h4><a name="t11"></a><a id="_570"></a>创建和终止</h4> 
<blockquote> 
 <p><strong>pod的创建过程</strong></p> 
 <ol><li> <p>用户通过kubectl或其他api客户端提交需要创建的pod信息给apiServer</p> </li><li> <p>apiServer开始生成pod对象的信息,并将信息存入etcd,然后返回确认信息至客户端</p> </li><li> <p>apiServer开始反映etcd中的pod对象的变化,其它组件使用watch机制来跟踪检查apiServer上的变动</p> </li><li> <p>scheduler发现有新的pod对象要创建,开始为Pod分配主机并将结果信息更新至apiServer</p> </li><li> <p>node节点上的kubelet发现有pod调度过来,尝试调用docker启动容器,并将结果回送至apiServer</p> </li><li> <p>apiServer将接收到的pod状态信息存入etcd中</p> </li></ol> 
</blockquote> 
<blockquote> 
 <p><strong>watch机制: Scheduler、etcd、Controller-manager、kubelete 这些 kubernetes 集群组件默认都监听在Apiserver(应用程序编程接口服务)上</strong><br> <img src="https://img1.iyenn.com/img10/c4a7c45db2b2b29e/695916999009946999163.jpeg" alt="在这里插入图片描述"></p> 
</blockquote> 
<blockquote> 
 <p><strong>pod的终止过程</strong></p> 
 <ol><li> <p>用户向apiServer发送删除pod对象的命令</p> </li><li> <p>apiServcer中的pod对象信息会随着时间的推移而更新,在宽限期内(默认30s),pod被视为dead</p> </li><li> <p>将pod标记为terminating状态</p> </li><li> <p>kubelet在监控到pod对象转为terminating状态的同时启动pod关闭过程</p> </li><li> <p>端点控制器监控到pod对象的关闭行为时将其从所有匹配到此端点的service资源的端点列表中移除</p> </li><li> <p>如果当前pod对象定义了preStop钩子处理器,则在其标记为terminating后即会以同步的方式启动执行</p> </li><li> <p>pod对象中的容器进程收到停止信号</p> </li><li> <p>宽限期结束后,若pod中还存在仍在运行的进程,kubelet 会请求给予该 Pod 的宽限期一次性增加 2 秒钟。如果超过2秒钟,那么pod对象会收到立即终止的信号</p> </li><li> <p>kubelet请求apiServer将此pod资源的宽限期设置为0从而完成删除操作,此时pod对于用户已不可见</p> </li></ol> 
</blockquote> 
<h4><a name="t12"></a><a id="_610"></a>初始化容器</h4> 
<blockquote> 
 <p>初始化容器是在pod的主容器启动之前要运行的容器,主要是做一些主容器的前置工作,它具有两大特征:</p> 
 <ol><li> <p>初始化容器必须运行完成直至结束,若某初始化容器运行失败,那么kubernetes需要重启它直到成功完成</p> </li><li> <p>初始化容器必须按照定义的顺序执行,当且仅当前一个成功之后,后面的一个才能运行</p> </li></ol> 
</blockquote> 
<blockquote> 
 <p>初始化容器有很多的应用场景,下面列出的是最常见的几个:</p> 
 <ul><li>提供主容器镜像中不具备的工具程序或自定义代码</li><li>初始化容器要先于应用容器串行启动并运行完成,因此可用于延后应用容器的启动直至其依赖的条件得到满足</li></ul> 
</blockquote> 
<blockquote> 
 <p>接下来做一个案例,模拟下面这个需求:</p> 
 <pre data-index="21" class="set-code-show prettyprint"><code class="has-numbering" onclick="mdcp.signin(event)" style="position: unset;">假设要以主容器来运行nginx,但是要求在运行nginx之前先要能够连接上mysql和redis所在服务器

为了简化测试,事先规定好mysql(192.168.109.201)和redis(192.168.109.202)服务器的地址
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre> 
</blockquote> 
<blockquote> 
 <p><strong>创建pod-initcontainer.yaml文件,内容如下:</strong></p> 
</blockquote> 
<pre data-index="22" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  <span class="token operator">-</span> name: myapp-container
    image: busybox
    command: <span class="token punctuation">[</span><span class="token string">'sh'</span><span class="token punctuation">,</span> <span class="token string">'-c'</span><span class="token punctuation">,</span> <span class="token string">'echo The app is running! && sleep 3600'</span><span class="token punctuation">]</span>
  initContainers:		<span class="token comment"># 初始化容器</span>
  <span class="token operator">-</span> name: init-myservice
    image: busybox
    command: <span class="token punctuation">[</span><span class="token string">'sh'</span><span class="token punctuation">,</span> <span class="token string">'-c'</span><span class="token punctuation">,</span> <span class="token string">'until nslookup myservice; do echo waiting for myservice; sleep 2; done;'</span><span class="token punctuation">]</span>
  <span class="token operator">-</span> name: init-mydb
    image: busybox
    command: <span class="token punctuation">[</span><span class="token string">'sh'</span><span class="token punctuation">,</span> <span class="token string">'-c'</span><span class="token punctuation">,</span> <span class="token string">'until nslookup mydb; do echo waiting for mydb; sleep 2; done;'</span><span class="token punctuation">]</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li></ul></pre> 
<blockquote> 
 <p><strong>创建service.yaml文件,内容如下:</strong></p> 
</blockquote> 
<pre data-index="23" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">kind: Service
apiVersion: v1
metadata:
  name: myservice
spec:
  ports:
  <span class="token operator">-</span> protocol: TCP
    port: 80
    targetPort: 9376
<span class="token operator">--</span><span class="token operator">-</span>
kind: Service
apiVersion: v1
metadata:
  name: mydb
spec:
  ports:
  <span class="token operator">-</span> protocol: TCP
    port: 80
    targetPort: 9377
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li></ul></pre> 
<pre data-index="24" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 创建pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment">#  kubectl create -f pod-initcontainer.yaml</span>
pod/pod-initcontainer created

<span class="token comment"># 查看pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get -f pod-initcontainer.yaml</span>
NAME        READY   STATUS     RESTARTS   AGE
myapp-pod   0/1     Init:0/2   0          6m7s

<span class="token comment"># 查看pod状态</span>
<span class="token comment"># 发现pod卡在启动第一个初始化容器过程中,后面的容器不会运行</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl describe -f pod-initcontainer.yaml</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
Events:
  <span class="token function">Type</span>    Reason     Age   <span class="token keyword">From</span>               Message
  <span class="token operator">--</span><span class="token operator">--</span>    <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>     <span class="token operator">--</span><span class="token operator">--</span>  <span class="token operator">--</span><span class="token operator">--</span>               <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
  Normal  Scheduled  36s   default-scheduler  Successfully assigned default/myapp-pod to k8s-node-02
  Normal  Pulling    36s   kubelet            Pulling image <span class="token string">"busybox"</span>
  Normal  Pulled     2s    kubelet            Successfully pulled image <span class="token string">"busybox"</span> in 33<span class="token punctuation">.</span>757606183s
  Normal  Created    2s    kubelet            Created container init-myservice
  Normal  Started    2s    kubelet            Started container init-myservice

<span class="token comment"># 创建myservice与mydb服务,再确认pod状态</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f service.yaml</span>
service/myservice created
service/mydb created

<span class="token comment">#  pod状态显示为running,表明两个初始化容器已经成功结束。</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get -f pod-initcontainer.yaml -w</span>
NAME        READY   STATUS     RESTARTS   AGE
myapp-pod   0/1     Init:0/2   0          9m33s
myapp-pod   0/1     Init:1/2   0          10m
myapp-pod   0/1     Init:1/2   0          10m
myapp-pod   0/1     PodInitializing   0          10m
myapp-pod   1/1     Running           0          10m
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li><li style="color: rgb(153, 153, 153);">31</li><li style="color: rgb(153, 153, 153);">32</li><li style="color: rgb(153, 153, 153);">33</li><li style="color: rgb(153, 153, 153);">34</li><li style="color: rgb(153, 153, 153);">35</li></ul></pre> 
<h4><a name="t13"></a><a id="_713"></a>钩子函数</h4> 
<blockquote> 
 <p>钩子函数能够感知自身生命周期中的事件,并在相应的时刻到来时运行用户指定的程序代码。</p> 
 <p>kubernetes在主容器的启动之后和停止之前提供了两个钩子函数:</p> 
 <ul><li> <p>post start:容器创建之后执行,如果失败了会重启容器</p> </li><li> <p>pre stop :容器终止之前执行,执行完成之后容器将成功终止,在其完成之前会阻塞删除容器的操作</p> </li></ul> 
</blockquote> 
<blockquote> 
 <p><strong>钩子处理器支持使用下面三种方式定义动作:</strong></p> 
</blockquote> 
<blockquote> 
 <p><strong>Exec命令:在容器内执行一次命令</strong></p> 
</blockquote> 
<pre data-index="25" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
  lifecycle:
    postStart: 
      exec:
        command:
        <span class="token operator">-</span> <span class="token function">cat</span>
        <span class="token operator">-</span> <span class="token operator">/</span>tmp/healthy
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li></ul></pre> 
<blockquote> 
 <p><strong>TCPSocket:在当前容器尝试访问指定的socket</strong></p> 
</blockquote> 
<pre data-index="26" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>    
  lifecycle:
    postStart:
      tcpSocket:
        port: 8080
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li></ul></pre> 
<blockquote> 
 <p><strong>HTTPGet:在当前容器中向某url发起http请求</strong></p> 
</blockquote> 
<pre data-index="27" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
  lifecycle:
    postStart:
      httpGet:
        path: <span class="token operator">/</span> 	<span class="token comment"># URI地址</span>
        port: 80 	<span class="token comment"># 端口号</span>
        host: 192<span class="token punctuation">.</span>168<span class="token punctuation">.</span>109<span class="token punctuation">.</span>100 	<span class="token comment"># 主机地址</span>
        scheme: HTTP 	<span class="token comment"># 支持的协议,http或者https</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li></ul></pre> 
<blockquote> 
 <h5><a id="exec_763"></a>接下来,以exec方式为例,演示下钩子函数的使用</h5> 
</blockquote> 
<blockquote> 
 <p><strong>创建 pod-hook-exec.yaml文件</strong></p> 
</blockquote> 
<pre data-index="28" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">apiVersion: v1
kind: Pod
metadata:
  name: pod-hook-exec
  namespace: dev
spec:
  containers:
    <span class="token operator">-</span> name: main-container
      image: nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>2
      ports:
        <span class="token operator">-</span> name: nginx-port
          containerPort: 80
      lifecycle:
        postStart:
          exec:   <span class="token comment"># 在容器启动的时候执行一个命令,修改掉nginx的默认首页内容</span>
            command: <span class="token punctuation">[</span> <span class="token string">"/bin/sh"</span><span class="token punctuation">,</span> <span class="token string">"-c"</span><span class="token punctuation">,</span> <span class="token string">"echo postStart... > /usr/share/nginx/html/index.html"</span> <span class="token punctuation">]</span>
        preStop:
          exec:   <span class="token comment"># 在容器停止之前停止nginx服务</span>
            command: <span class="token punctuation">[</span> <span class="token string">"/usr/sbin/nginx"</span><span class="token punctuation">,</span><span class="token string">"-s"</span><span class="token punctuation">,</span><span class="token string">"quit"</span> <span class="token punctuation">]</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li></ul></pre> 
<blockquote> 
 <p><strong>创建pod,观察效果</strong></p> 
</blockquote> 
<pre data-index="29" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 创建pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-hook-exec.yaml</span>
pod/pod-hook-exec created

<span class="token comment"># 查看pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment">#  kubectl get pods  pod-hook-exec -n dev -o wide</span>
NAME            READY   STATUS    RESTARTS   AGE   IP           NODE          NOMINATED NODE   READINESS GATES
pod-hook-exec   1/1     Running   0          32s   10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>2   k8s-node-01   <none>           <none>

<span class="token comment"># 访问pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># curl 10.244.1.2</span>
postStart<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li></ul></pre> 
<h4><a name="t14"></a><a id="_806"></a>容器探测</h4> 
<blockquote> 
 <p>容器探测用于检测容器中的应用实例是否正常工作,是保障业务可用性的一种传统机制。如果经过探测,实例的状态不符合预期,那么kubernetes就会把该问题实例"摘除 ",不承担业务流量。kubernetes提供了两种探针来实现容器探测,分别是:</p> 
 <ul><li><strong>liveness probes</strong>:存活性探针,用于检测应用实例当前是否处于正常运行状态,如果不是,k8s会重启容器</li><li><strong>readiness probes</strong>:就绪性探针,用于检测应用实例当前是否可以接收请求,如果不能,k8s不会转发流量</li></ul> 
</blockquote> 
<blockquote> 
 <p><strong>存活性探针和就绪性探针的差别:</strong></p> 
 <ul><li><strong>livenessProbe 决定是否重启容器,readinessProbe 决定是否将请求转发给容器。</strong></li></ul> 
</blockquote> 
<blockquote> 
 <p><strong>上面两种探针目前均支持三种探测方式:</strong></p> 
</blockquote> 
<blockquote> 
 <p><strong>Exec命令:在容器内执行一次命令,如果命令执行的退出码为0,则认为程序正常,否则不正常</strong></p> 
</blockquote> 
<pre data-index="30" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
  lifecycle:
    postStart: 
      exec:
        command:
        <span class="token operator">-</span> <span class="token function">cat</span>
        <span class="token operator">-</span> <span class="token operator">/</span>tmp/healthy
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li></ul></pre> 
<blockquote> 
 <p><strong>TCPSocket:将会尝试访问一个用户容器的端口,如果能够建立这条连接,则认为程序正常,否则不正常</strong></p> 
</blockquote> 
<pre data-index="31" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>    
  lifecycle:
    postStart:
      tcpSocket:
        port: 8080
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li></ul></pre> 
<blockquote> 
 <p><strong>HTTPGet:调用容器内Web应用的URL,如果返回的状态码在200和399之间,则认为程序正常,否则不正常</strong></p> 
</blockquote> 
<pre data-index="32" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
  lifecycle:
    postStart:
      httpGet:
        path: <span class="token operator">/</span> 	<span class="token comment"># URI地址</span>
        port: 80 	<span class="token comment"># 端口号</span>
        host: 192<span class="token punctuation">.</span>168<span class="token punctuation">.</span>109<span class="token punctuation">.</span>100 	<span class="token comment"># 主机地址</span>
        scheme: HTTP 	<span class="token comment"># 支持的协议,http或者https</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li></ul></pre> 
<blockquote> 
 <h5><a id="livenessProbes_857"></a>下面以livenessProbes为例,做几个演示:</h5> 
</blockquote> 
<p><strong>方式一:Exec</strong></p> 
<blockquote> 
 <p><strong>创建 pod-liveness-exec.yaml</strong></p> 
</blockquote> 
<pre data-index="33" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">apiVersion: v1
kind: Pod
metadata:
  name: pod-liveness-exec
  namespace: dev
spec:
  containers:
    <span class="token operator">-</span> name: nginx
      image: nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>2
      ports:
        <span class="token operator">-</span> name: nginx-port
          containerPort: 80
      livenessProbe:
        exec:
          command: <span class="token punctuation">[</span><span class="token string">"/bin/cat"</span><span class="token punctuation">,</span><span class="token string">"/tmp/hello.txt"</span><span class="token punctuation">]</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li></ul></pre> 
<blockquote> 
 <p><strong>创建pod,观察效果</strong></p> 
</blockquote> 
<pre data-index="34" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 创建Pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-liveness-exec.yaml</span>
pod/pod-liveness-exec created

<span class="token comment"># 查看Pod详情</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl describe -f pod-liveness-exec.yaml</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
Events:
  <span class="token function">Type</span>     Reason     Age                  <span class="token keyword">From</span>               Message
  <span class="token operator">--</span><span class="token operator">--</span>     <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>     <span class="token operator">--</span><span class="token operator">--</span>                 <span class="token operator">--</span><span class="token operator">--</span>               <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
  Normal   Scheduled  2m47s                default-scheduler  Successfully assigned dev/pod-liveness-exec to k8s-node-02
  Normal   Pulling    70s <span class="token punctuation">(</span>x2 over 2m47s<span class="token punctuation">)</span>  kubelet            Pulling image <span class="token string">"nginx:1.17.2"</span>
  Normal   Pulled     23s                  kubelet            Successfully pulled image <span class="token string">"nginx:1.17.2"</span> in 47<span class="token punctuation">.</span>341599402s
  Normal   Created    23s                  kubelet            Created container nginx
  Normal   Started    23s                  kubelet            Started container nginx
  Warning  Unhealthy  8s <span class="token punctuation">(</span>x2 over 18s<span class="token punctuation">)</span>     kubelet            Liveness probe failed: <span class="token operator">/</span>bin/<span class="token function">cat</span>: <span class="token operator">/</span>tmp/hello<span class="token punctuation">.</span>txt: No such file or directory

<span class="token comment"># 观察上面的信息就会发现nginx容器启动之后就进行了健康检查</span>
<span class="token comment"># 检查失败之后,容器被kill掉,然后尝试进行重启(这是重启策略的作用,后面讲解)</span>
<span class="token comment"># 稍等一会之后,再观察pod信息,就可以看到RESTARTS不再是0,而是一直增长</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pods pod-liveness-exec -n dev</span>
NAME                READY   STATUS    RESTARTS   AGE
pod-liveness-exec   1/1     Running   2          3m33s

<span class="token comment"># 当然接下来,可以修改成一个存在的文件,比如/etc/hosts,再试,结果就正常了......</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li></ul></pre> 
<p><strong>方式二:TCPSocket</strong></p> 
<blockquote> 
 <p><strong>创建 pod-liveness-tcpsocket.yaml</strong></p> 
</blockquote> 
<pre data-index="35" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">apiVersion: v1
kind: Pod
metadata:
  name: pod-liveness-tcpsocket
  namespace: dev
spec:
  containers:
  <span class="token operator">-</span> name: nginx
    image: nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>1
    ports:
    <span class="token operator">-</span> name: nginx-port
      containerPort: 80
    livenessProbe:
      tcpSocket:
        port: 8080 <span class="token comment"># 尝试访问8080端口</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li></ul></pre> 
<blockquote> 
 <p><strong>创建pod,观察效果</strong></p> 
</blockquote> 
<pre data-index="36" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 创建Pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl delete -f pod-liveness-tcpsocket.yaml</span>
pod <span class="token string">"pod-liveness-tcpsocket"</span> deleted

<span class="token comment"># 查看Pod详情</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl describe pods pod-liveness-tcpsocket -n dev</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
Events:
  <span class="token function">Type</span>     Reason     Age               <span class="token keyword">From</span>               Message
  <span class="token operator">--</span><span class="token operator">--</span>     <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>     <span class="token operator">--</span><span class="token operator">--</span>              <span class="token operator">--</span><span class="token operator">--</span>               <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
  Normal   Scheduled  61s               default-scheduler  Successfully assigned dev/pod-liveness-tcpsocket to k8s-node-02
  Normal   Pulled     2s <span class="token punctuation">(</span>x3 over 61s<span class="token punctuation">)</span>  kubelet            Container image <span class="token string">"nginx:1.17.2"</span> already present on machine
  Normal   Created    2s <span class="token punctuation">(</span>x3 over 61s<span class="token punctuation">)</span>  kubelet            Created container nginx
  Normal   Started    2s <span class="token punctuation">(</span>x3 over 61s<span class="token punctuation">)</span>  kubelet            Started container nginx
  Warning  Unhealthy  2s <span class="token punctuation">(</span>x6 over 52s<span class="token punctuation">)</span>  kubelet            Liveness probe failed: dial tcp 10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>5:8080: connect: connection refused

<span class="token comment"># 观察上面的信息,发现尝试访问8080端口,但是失败了</span>
<span class="token comment"># 稍等一会之后,再观察pod信息,就可以看到RESTARTS不再是0,而是一直增长</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pods pod-liveness-tcpsocket  -n dev</span>
NAME                     READY   STATUS             RESTARTS   AGE
pod-liveness-tcpsocket   0/1     CrashLoopBackOff   4          2m34s

<span class="token comment"># 当然接下来,可以修改成一个可以访问的端口,比如80,再试,结果就正常了......</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li></ul></pre> 
<p><strong>方式三:HTTPGet</strong></p> 
<blockquote> 
 <p><strong>创建 pod-liveness-httpget.yaml</strong></p> 
</blockquote> 
<pre data-index="37" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">apiVersion: v1
kind: Pod
metadata:
  name: pod-liveness-httpget
  namespace: dev
spec:
  containers:
  <span class="token operator">-</span> name: nginx
    image: nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>2
    ports:
    <span class="token operator">-</span> name: nginx-port
      containerPort: 80
    livenessProbe:
      httpGet:  <span class="token comment"># 其实就是访问http://127.0.0.1:80/hello  </span>
        scheme: HTTP <span class="token comment">#支持的协议,http或者https</span>
        port: 80 <span class="token comment">#端口号</span>
        path: <span class="token operator">/</span>hello <span class="token comment">#URI地址</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li></ul></pre> 
<blockquote> 
 <p><strong>创建pod,观察效果</strong></p> 
</blockquote> 
<pre data-index="38" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 创建Pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment">#  kubectl create -f pod-liveness-httpget.yaml</span>
pod/pod-liveness-httpget created

<span class="token comment"># 查看Pod详情</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl describe pod pod-liveness-httpget -n dev</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
Events:
  <span class="token function">Type</span>     Reason     Age               <span class="token keyword">From</span>               Message
  <span class="token operator">--</span><span class="token operator">--</span>     <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>     <span class="token operator">--</span><span class="token operator">--</span>              <span class="token operator">--</span><span class="token operator">--</span>               <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
  Normal   Scheduled  31s               default-scheduler  Successfully assigned dev/pod-liveness-httpget to k8s-node-01
  Normal   Pulled     2s <span class="token punctuation">(</span>x2 over 31s<span class="token punctuation">)</span>  kubelet            Container image <span class="token string">"nginx:1.17.2"</span> already present on machine
  Normal   Created    2s <span class="token punctuation">(</span>x2 over 30s<span class="token punctuation">)</span>  kubelet            Created container nginx
  Normal   Started    2s <span class="token punctuation">(</span>x2 over 30s<span class="token punctuation">)</span>  kubelet            Started container nginx
  Warning  Unhealthy  2s <span class="token punctuation">(</span>x3 over 22s<span class="token punctuation">)</span>  kubelet            Liveness probe failed: HTTP probe failed with statuscode: 404
  Normal   Killing    2s                kubelet            Container nginx failed liveness probe<span class="token punctuation">,</span> will be restarted

<span class="token comment"># 观察上面信息,尝试访问路径,但是未找到,出现404错误</span>
<span class="token comment"># 稍等一会之后,再观察pod信息,就可以看到RESTARTS不再是0,而是一直增长</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pod pod-liveness-httpget -n dev</span>
NAME                   READY   STATUS    RESTARTS   AGE
pod-liveness-httpget   1/1     Running   5          3m30s

<span class="token comment"># 当然接下来,可以修改成一个可以访问的路径path,比如/,再试,结果就正常了......</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li></ul></pre> 
<p><strong>livenessProbe其他配置</strong></p> 
<blockquote> 
 <p>至此,已经使用livenessProbe演示了三种探测方式,但是查看livenessProbe的子属性,会发现除了这三种方式,还有一些其他的配置,在这里一并解释下:</p> 
</blockquote> 
<pre data-index="39" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl explain pod.spec.containers.livenessProbe</span>
FIELDS:
   exec <Object>  
   tcpSocket    <Object>
   httpGet      <Object>
   initialDelaySeconds  <integer> 	 	<span class="token comment"># 容器启动后等待多少秒执行第一次探测</span>
   timeoutSeconds       <integer>  		<span class="token comment"># 探测超时时间。默认1秒,最小1秒</span>
   periodSeconds        <integer> 	    <span class="token comment"># 执行探测的频率。默认是10秒,最小1秒</span>
   failureThreshold     <integer>  		<span class="token comment"># 连续探测失败多少次才被认定为失败。默认是3。最小值是1</span>
   successThreshold     <integer>  		<span class="token comment"># 连续探测成功多少次才被认定为成功。默认是1</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li></ul></pre> 
<blockquote> 
 <p>下面稍微配置两个,演示下效果即可:</p> 
</blockquote> 
<pre data-index="40" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">apiVersion: v1
kind: Pod
metadata:
  name: pod-liveness-httpget
  namespace: dev
spec:
  containers:
  <span class="token operator">-</span> name: nginx
    image: nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>1
    ports:
    <span class="token operator">-</span> name: nginx-port
      containerPort: 80
    livenessProbe:
      httpGet:
        scheme: HTTP
        port: 80 
        path: <span class="token operator">/</span>
      initialDelaySeconds: 30 		<span class="token comment"># 容器启动后30s开始探测</span>
      timeoutSeconds: 5 		<span class="token comment"># 探测超时时间为5s</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li></ul></pre> 
<pre data-index="41" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 创建Pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-liveness-httpget.yaml</span>
pod/pod-liveness-tcpsocket created

<span class="token comment"># 查看Pod详情</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment">#  kubectl describe -f pod-liveness-httpget.yaml</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
Containers:
  nginx:
    Container ID:   docker:<span class="token operator">/</span><span class="token operator">/</span>3ac50b5ad46c7b2c89e394cd48f54482017d9f111dc3a49da2168e5ec622beae
    Image:          nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>2
    Image ID:       docker-pullable:<span class="token operator">/</span><span class="token operator">/</span>nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Sun<span class="token punctuation">,</span> 08 Aug 2021 01:34:06 <span class="token operator">+</span>0800
    Ready:          True
    Restart Count:  0
    Liveness:       tcp-socket :80 delay=30s timeout=5s period=10s <span class="token comment">#success=1 #failure=3</span>
    Environment:    <none>
    Mounts:
      <span class="token operator">/</span><span class="token keyword">var</span><span class="token operator">/</span>run/secrets/kubernetes<span class="token punctuation">.</span>io/serviceaccount <span class="token keyword">from</span> kube-api-access-k9zh5 <span class="token punctuation">(</span>ro<span class="token punctuation">)</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li></ul></pre> 
<h4><a name="t15"></a><a id="_1080"></a>重启策略</h4> 
<blockquote> 
 <p>在上一节中,一旦容器探测出现了问题,kubernetes就会对容器所在的Pod进行重启,其实这是由pod的重启策略决定的,pod的重启策略有 3 种,分别如下:</p> 
 <ul><li>Always :容器失效时,自动重启该容器,这也是默认值。</li><li>OnFailure : 容器终止运行且退出码不为0时重启</li><li>Never : 不论状态为何,都不重启该容器</li></ul> 
</blockquote> 
<blockquote> 
 <p>重启策略适用于pod对象中的所有容器,首次需要重启的容器,将在其需要时立即进行重启,随后再次需要重启的操作将由kubelet延迟一段时间后进行,且反复的重启操作的延迟时长以此为10s、20s、40s、80s、160s和300s,300s是最大延迟时长。</p> 
</blockquote> 
<blockquote> 
 <p><strong>创建pod-restartpolicy.yaml:</strong></p> 
</blockquote> 
<pre data-index="42" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">apiVersion: v1
kind: Pod
metadata:
  name: pod-restartpolicy
  namespace: dev
spec:
  containers:
  <span class="token operator">-</span> name: nginx
    image: nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>2
    ports:
    <span class="token operator">-</span> name: nginx-port
      containerPort: 80
    livenessProbe:
      httpGet:
        scheme: HTTP
        port: 80
        path: <span class="token operator">/</span>hello
  restartPolicy: Never		 <span class="token comment"># 设置重启策略为Never</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li></ul></pre> 
<blockquote> 
 <p><strong>运行Pod测试</strong></p> 
</blockquote> 
<pre data-index="43" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 创建Pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-restartpolicy.yaml </span>
pod/pod-restartpolicy created

<span class="token comment"># 查看Pod详情,发现nginx容器失败</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl  describe pods pod-restartpolicy  -n dev</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
Events:
  <span class="token function">Type</span>     Reason     Age                <span class="token keyword">From</span>               Message
  <span class="token operator">--</span><span class="token operator">--</span>     <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>     <span class="token operator">--</span><span class="token operator">--</span>               <span class="token operator">--</span><span class="token operator">--</span>               <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
  Normal   Scheduled  63s                default-scheduler  Successfully assigned dev/pod-restartpolicy to k8s-node-01
  Normal   Pulled     63s                kubelet            Container image <span class="token string">"nginx:1.17.2"</span> already present on machine
  Normal   Created    63s                kubelet            Created container nginx
  Normal   Started    63s                kubelet            Started container nginx
  Warning  Unhealthy  34s <span class="token punctuation">(</span>x3 over 54s<span class="token punctuation">)</span>  kubelet            Liveness probe failed: HTTP probe failed with statuscode: 404
  Normal   Killing    34s                kubelet            Stopping container nginx

<span class="token comment"># 多等一会,再观察pod的重启次数,发现一直是0,并未重启  </span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl  get pods pod-restartpolicy -n dev</span>
NAME                READY   STATUS      RESTARTS   AGE
pod-restartpolicy   0/1     Completed   0          4m14s
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li></ul></pre> 
<h3><a name="t16"></a><a id="Pod_1139"></a>Pod调度</h3> 
<blockquote> 
 <p>在默认情况下,<strong>一个Pod在哪个Node节点上运行,默认是由Scheduler组件采用相应的算法计算出来的</strong>,这个过程是不受人工控制的。但是在实际使用中,这并不满足的需求,因为很多情况下,我们想控制某些Pod到达某些节点上,那么应该怎么做呢?这就要求了解kubernetes对Pod的调度规则,kubernetes提供了四大类调度方式:</p> 
 <ul><li> <p>自动调度:运行在哪个节点上完全由Scheduler经过一系列的算法计算得出</p> </li><li> <p>定向调度:NodeName、NodeSelector</p> </li><li> <p>亲和性调度:NodeAffinity、PodAffinity、PodAntiAffinity</p> </li><li> <p>污点(容忍)调度:Taints、Toleration</p> </li></ul> 
</blockquote> 
<h4><a name="t17"></a><a id="_1150"></a>定向调度</h4> 
<blockquote> 
 <p><strong>定向调度,指的是利用在pod上声明<code>nodeName</code>或者<code>nodeSelector</code>,以此将Pod调度到期望的node节点上。</strong><br> 注意,这里的调度是强制的,这就意味着即使要调度的目标Node不存在,也会向上面进行调度,只不过pod运行失败而已。</p> 
</blockquote> 
<blockquote> 
 <h5><a id="NodeName_1155"></a>NodeName</h5> 
 <ul><li>NodeName用于强制约束将Pod调度到指定的Name的Node节点上。这种方式,其实是直接跳过Scheduler的调度逻辑,直接将Pod调度到指定名称的节点。</li></ul> 
</blockquote> 
<blockquote> 
 <p><strong>创建一个pod-nodename.yaml文件</strong></p> 
</blockquote> 
<pre data-index="44" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">apiVersion: v1
kind: Pod
metadata:
  name: pod-nodename
  namespace: dev
spec:
  containers:
  <span class="token operator">-</span> name: nginx
    image: nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>2
  nodeName: k8s-node-01  	 <span class="token comment"># 指定调度到k8s-node-01节点上</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li></ul></pre> 
<blockquote> 
 <p><strong>创建pod,观察效果</strong></p> 
</blockquote> 
<pre data-index="45" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 创建Pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-nodename.yaml</span>
pod/pod-nodename created

<span class="token comment"># 查看Pod调度到NODE属性,确实是调度到了k8s-node-01节点上</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pods pod-nodename -n dev -o wide</span>
NAME           READY   STATUS    RESTARTS   AGE   IP           NODE          NOMINATED NODE   READINESS GATES
pod-nodename   1/1     Running   0          5s    10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>2   k8s-node-01   <none>           <none>

<span class="token comment"># 接下来,删除pod,修改nodeName的值为k8s-node-03(并没有k8s-node-01节点)</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl delete -f pod-nodename.yaml</span>
pod <span class="token string">"pod-nodename"</span> deleted
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># vim pod-nodename.yaml</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-nodename.yaml</span>
pod/pod-nodename created

<span class="token comment"># 再次查看,发现已经向Node3节点调度,但是由于不存在k8s-node-03节点,所以pod无法正常运行</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pods pod-nodename -n dev -o wide</span>
NAME           READY   STATUS    RESTARTS   AGE   IP       NODE          NOMINATED NODE   READINESS GATES
pod-nodename   0/1     Pending   0          8s    <none>   k8s-node-03   <none>           <none>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li></ul></pre> 
<blockquote> 
 <h5><a id="NodeSelector_1198"></a>NodeSelector</h5> 
 <ul><li>NodeSelector用于将pod调度到添加了指定标签的node节点上。它是通过kubernetes的label-selector机制实现的,也就是说,在pod创建之前,会由scheduler使用MatchNodeSelector调度策略进行label匹配,找出目标node,然后将pod调度到目标节点,该匹配规则是强制约束。</li></ul> 
</blockquote> 
<blockquote> 
 <p><strong>首先分别为node节点添加标签</strong></p> 
</blockquote> 
<pre data-index="46" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl label nodes k8s-node-01 nodeenv=pro</span>
node/k8s-node-01 labeled

<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl label nodes k8s-node-02 nodeenv=test</span>
node/k8s-node-02 labeled
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li></ul></pre> 
<blockquote> 
 <p><strong>创建一个pod-nodeselector.yaml文件</strong></p> 
</blockquote> 
<pre data-index="47" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">apiVersion: v1
kind: Pod
metadata:
  name: pod-nodeselector
  namespace: dev
spec:
  containers:
  <span class="token operator">-</span> name: nginx
    image: nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>2
  nodeSelector:
    nodeenv: pro <span class="token comment"># 指定调度到具有nodeenv=pro标签的节点上</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li></ul></pre> 
<blockquote> 
 <p><strong>创建pod,观察效果</strong></p> 
</blockquote> 
<pre data-index="48" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 创建Pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-nodeselector.yaml</span>
pod/pod-nodeselector created

<span class="token comment"># 查看Pod调度到NODE属性,确实是调度到了node1节点上</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment">#  kubectl get pods pod-nodeselector -n dev -o wide</span>
NAME               READY   STATUS    RESTARTS   AGE   IP           NODE          NOMINATED NODE   READINESS GATES
pod-nodeselector   1/1     Running   0          6s    10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>2   k8s-node-01   <none>           <none>

<span class="token comment"># 接下来,删除pod,修改nodeSelector的值为nodeenv: xxxx(不存在打有此标签的节点)</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment">#  kubectl delete -f pod-nodeselector.yaml</span>
pod <span class="token string">"pod-nodeselector"</span> deleted

<span class="token comment"># 接下来,删除pod,修改nodeSelector的值为nodeenv: xxx(不存在打有此标签的节点)</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment">#  kubectl delete -f pod-nodeselector.yaml</span>
pod <span class="token string">"pod-nodeselector"</span> deleted
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment">#  vim pod-nodeselector.yaml</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment">#  kubectl create -f pod-nodeselector.yaml</span>
pod/pod-nodeselector created

<span class="token comment">#再次查看,发现pod无法正常运行,Node的值为none</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pods -n dev -o wide</span>
NAME                        READY   STATUS    RESTARTS   AGE     IP       NODE     NOMINATED NODE   READINESS GATES
pod-nodeselector            0/1     Pending   0          5s      <none>   <none>   <none>           <none>

<span class="token comment"># 查看详情,发现node selector匹配失败的提示</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment">#  kubectl describe pods pod-nodeselector -n dev</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
Events:
  <span class="token function">Type</span>     Reason            Age                <span class="token keyword">From</span>               Message
  <span class="token operator">--</span><span class="token operator">--</span>     <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>            <span class="token operator">--</span><span class="token operator">--</span>               <span class="token operator">--</span><span class="token operator">--</span>               <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
  Warning  FailedScheduling  21s <span class="token punctuation">(</span>x2 over 22s<span class="token punctuation">)</span>  default-scheduler  0/3 nodes are available: 1 node<span class="token punctuation">(</span>s<span class="token punctuation">)</span> had taint <span class="token punctuation">{<!-- --></span>node-role<span class="token punctuation">.</span>kubernetes<span class="token punctuation">.</span>io/master: <span class="token punctuation">}</span><span class="token punctuation">,</span> that the pod didn<span class="token string">'t tolerate, 2 node(s) didn'</span>t match Pod's node affinity/selector<span class="token punctuation">.</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li><li style="color: rgb(153, 153, 153);">31</li><li style="color: rgb(153, 153, 153);">32</li></ul></pre> 
<h4><a name="t18"></a><a id="_1264"></a>亲和性调度</h4> 
<blockquote> 
 <p>上一节,介绍了两种定向调度的方式,使用起来非常方便,但是也有一定的问题,那就是如果没有满足条件的Node,那么Pod将不会被运行,即使在集群中还有可用Node列表也不行,这就限制了它的使用场景。</p> 
</blockquote> 
<blockquote> 
 <p>基于上面的问题,kubernetes还提供了一种亲和性调度(Affinity)。它在NodeSelector的基础之上的进行了扩展,可以通过配置的形式,实现优先选择满足条件的Node进行调度,如果没有,也可以调度到不满足条件的节点上,使调度更加灵活。</p> 
 <p><strong>Affinity</strong>主要分为三类:</p> 
 <ul><li> <p><strong>nodeAffinity(node亲和性)</strong>: 以node为目标,解决pod可以调度到哪些node的问题</p> </li><li> <p><strong>podAffinity(pod亲和性)</strong> : 以pod为目标,解决pod可以和哪些已存在的pod部署在同一个拓扑域中的问题</p> </li><li> <p><strong>podAntiAffinity(pod反亲和性)</strong> : 以pod为目标,解决pod不能和哪些已存在pod部署在同一个拓扑域中的问题</p> </li></ul> 
</blockquote> 
<blockquote> 
 <p>关于亲和性(反亲和性)使用场景的说明:</p> 
 <ul><li> <p><strong>亲和性</strong>:如果两个应用频繁交互,那就有必要利用亲和性让两个应用的尽可能的靠近,这样可以减少因网络通信而带来的性能损耗。</p> </li><li> <p><strong>反亲和性</strong>:当应用的采用多副本部署时,有必要采用反亲和性让各个应用实例打散分布在各个node上,这样可以提高服务的高可用性。</p> </li></ul> 
</blockquote> 
<blockquote> 
 <h5><a id="NodeAffinity_1284"></a>NodeAffinity</h5> 
 <p>首先来看一下 <code>NodeAffinity</code> 的可配置项:</p> 
</blockquote> 
<pre data-index="49" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">pod<span class="token punctuation">.</span>spec<span class="token punctuation">.</span>affinity<span class="token punctuation">.</span>nodeAffinity

  requiredDuringSchedulingIgnoredDuringExecution  	Node节点必须满足指定的所有规则才可以,相当于硬限制
    nodeSelectorTerms  	节点选择列表
      matchFields   	按节点字段列出的节点选择器要求列表
      matchExpressions   	按节点标签列出的节点选择器要求列表<span class="token punctuation">(</span>推荐<span class="token punctuation">)</span>
        key   键
        values 	 值
        operator 	关系符 支持Exists<span class="token punctuation">,</span> DoesNotExist<span class="token punctuation">,</span> In<span class="token punctuation">,</span> NotIn<span class="token punctuation">,</span> Gt<span class="token punctuation">,</span> Lt
        
  preferredDuringSchedulingIgnoredDuringExecution 	优先调度到满足指定的规则的Node,相当于软限制 <span class="token punctuation">(</span>倾向<span class="token punctuation">)</span>
    preference   	一个节点选择器项,与相应的权重相关联
      matchFields   	按节点字段列出的节点选择器要求列表
      matchExpressions   	按节点标签列出的节点选择器要求列表<span class="token punctuation">(</span>推荐<span class="token punctuation">)</span>
        key    	键
        values	 值
        operator 	关系符 支持In<span class="token punctuation">,</span> NotIn<span class="token punctuation">,</span> Exists<span class="token punctuation">,</span> DoesNotExist<span class="token punctuation">,</span> Gt<span class="token punctuation">,</span> Lt
	weight 		倾向权重,在范围1-100。
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li></ul></pre> 
<pre data-index="50" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">关系符的使用说明:

<span class="token operator">-</span> matchExpressions:
  <span class="token operator">-</span> key: nodeenv              <span class="token comment"># 匹配存在标签的key为nodeenv的节点</span>
    operator: Exists

  <span class="token operator">-</span> key: nodeenv              <span class="token comment"># 匹配标签的key为nodeenv,且value是"xxx"或"yyy"的节点</span>
    operator: In
    values: <span class="token punctuation">[</span><span class="token string">"xxx"</span><span class="token punctuation">,</span><span class="token string">"yyy"</span><span class="token punctuation">]</span>

  <span class="token operator">-</span> key: nodeenv              <span class="token comment"># 匹配标签的key为nodeenv,且value大于"xxx"的节点</span>
    operator: Gt
    values: <span class="token string">"xxx"</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li></ul></pre> 
<blockquote> 
 <p>接下来首先演示一下 <code>requiredDuringSchedulingIgnoredDuringExecution</code></p> 
</blockquote> 
<blockquote> 
 <p><strong>创建pod-nodeaffinity-required.yaml文件</strong></p> 
</blockquote> 
<pre data-index="51" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">apiVersion: v1
kind: Pod
metadata:
  name: pod-nodeaffinity-required
  namespace: dev
spec:
  containers:
  <span class="token operator">-</span> name: nginx
    image: nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>2
  affinity:  <span class="token comment"># 亲和性设置</span>
    nodeAffinity: <span class="token comment"># 设置node亲和性</span>
      requiredDuringSchedulingIgnoredDuringExecution: <span class="token comment"># 硬限制</span>
        nodeSelectorTerms:
        <span class="token operator">-</span> matchExpressions: <span class="token comment"># 匹配env的值在["xxx","yyy"]中的标签</span>
          <span class="token operator">-</span> key: nodeenv
            operator: In
            values: <span class="token punctuation">[</span><span class="token string">"xxx"</span><span class="token punctuation">,</span><span class="token string">"yyy"</span><span class="token punctuation">]</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li></ul></pre> 
<blockquote> 
 <p><strong>创建pod,观察效果</strong></p> 
</blockquote> 
<pre data-index="52" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 创建pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-nodeaffinity-required.yaml</span>
pod/pod-nodeaffinity-required created

<span class="token comment"># 查看pod状态 (运行失败)</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pods pod-nodeaffinity-required -n dev -o wide</span>
NAME                        READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
pod-nodeaffinity-required   0/1     Pending   0          5s    <none>   <none>   <none>           <none>

<span class="token comment"># 查看Pod的详情</span>
<span class="token comment"># 发现调度失败,提示node选择失败</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl describe pod pod-nodeaffinity-required -n dev</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
Events:
  <span class="token function">Type</span>     Reason            Age                <span class="token keyword">From</span>               Message
  <span class="token operator">--</span><span class="token operator">--</span>     <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>            <span class="token operator">--</span><span class="token operator">--</span>               <span class="token operator">--</span><span class="token operator">--</span>               <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
  Warning  FailedScheduling  27s <span class="token punctuation">(</span>x2 over 28s<span class="token punctuation">)</span>  default-scheduler  0/3 nodes are available: 1 node<span class="token punctuation">(</span>s<span class="token punctuation">)</span> had taint <span class="token punctuation">{<!-- --></span>node-role<span class="token punctuation">.</span>kubernetes<span class="token punctuation">.</span>io/master: <span class="token punctuation">}</span><span class="token punctuation">,</span> that the pod didn<span class="token string">'t tolerate, 2 node(s) didn'</span>t match Pod's node affinity/selector<span class="token punctuation">.</span>

<span class="token comment">#接下来,停止pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl delete -f pod-nodeaffinity-required.yaml</span>
pod <span class="token string">"pod-nodeaffinity-required"</span> deleted

<span class="token comment"># 修改文件,将values: ["xxx","yyy"]------> ["pro","yyy"]</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># vim pod-nodeaffinity-required.yaml</span>

<span class="token comment"># 再次启动</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-nodeaffinity-required.yaml</span>
pod/pod-nodeaffinity-required created

<span class="token comment"># 此时查看,发现调度成功,已经将pod调度到了node1上</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment">#  kubectl get pods pod-nodeaffinity-required -n dev -o wide</span>
NAME                        READY   STATUS    RESTARTS   AGE   IP           NODE          NOMINATED NODE   READINESS GATES
pod-nodeaffinity-required   1/1     Running   0          6s    10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>3   k8s-node-01   <none>           <none>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li><li style="color: rgb(153, 153, 153);">31</li><li style="color: rgb(153, 153, 153);">32</li><li style="color: rgb(153, 153, 153);">33</li></ul></pre> 
<blockquote> 
 <p>接下来再演示一下 <code>requiredDuringSchedulingIgnoredDuringExecution</code></p> 
</blockquote> 
<blockquote> 
 <p><strong>创建pod-nodeaffinity-preferred.yaml</strong></p> 
</blockquote> 
<pre data-index="53" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">apiVersion: v1
kind: Pod
metadata:
  name: pod-nodeaffinity-preferred
  namespace: dev
spec:
  containers:
  <span class="token operator">-</span> name: nginx
    image: nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>2
  affinity:  <span class="token comment"># 亲和性设置</span>
    nodeAffinity: <span class="token comment"># 设置node亲和性</span>
      preferredDuringSchedulingIgnoredDuringExecution: <span class="token comment"># 软限制</span>
      <span class="token operator">-</span> weight: 1
        preference:
          matchExpressions: <span class="token comment"># 匹配env的值在["xxx","yyy"]中的标签(当前环境没有)</span>
          <span class="token operator">-</span> key: nodeenv
            operator: In
            values: <span class="token punctuation">[</span><span class="token string">"xxx"</span><span class="token punctuation">,</span><span class="token string">"yyy"</span><span class="token punctuation">]</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li></ul></pre> 
<blockquote> 
 <p><strong>创建pod,观察效果</strong></p> 
</blockquote> 
<pre data-index="54" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 创建pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-nodeaffinity-preferred.yaml</span>
pod/pod-nodeaffinity-preferred created

<span class="token comment"># 查看pod状态 (运行成功)</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pod pod-nodeaffinity-preferred -n dev</span>
NAME                         READY   STATUS    RESTARTS   AGE
pod-nodeaffinity-preferred   1/1     Running   0          5s
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li></ul></pre> 
<blockquote> 
 <p><strong>NodeAffinity规则设置的注意事项:</strong></p> 
 <ol><li> <p>如果同时定义了nodeSelector和nodeAffinity,那么必须两个条件都得到满足,Pod才能运行在指定的Node上</p> </li><li> <p>如果nodeAffinity指定了多个nodeSelectorTerms,那么只需要其中一个能够匹配成功即可</p> </li><li> <p>如果一个nodeSelectorTerms中有多个matchExpressions ,则一个节点必须满足所有的才能匹配成功</p> </li><li> <p>如果一个pod所在的Node在Pod运行期间其标签发生了改变,不再符合该Pod的节点亲和性需求,则系统将忽略此变化</p> </li></ol> 
</blockquote> 
<blockquote> 
 <h5><a id="PodAffinity_1433"></a>PodAffinity</h5> 
 <p>PodAffinity主要实现以运行的Pod为参照,实现让新创建的Pod跟参照pod在一个区域的功能。</p> 
 <p>首先来看一下<code>PodAffinity</code>的可配置项:</p> 
</blockquote> 
<pre data-index="55" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">pod<span class="token punctuation">.</span>spec<span class="token punctuation">.</span>affinity<span class="token punctuation">.</span>podAffinity

  requiredDuringSchedulingIgnoredDuringExecution  	硬限制
    namespaces       指定参照pod的namespace
    topologyKey      	指定调度作用域
    labelSelector    	标签选择器
      matchExpressions  	按节点标签列出的节点选择器要求列表<span class="token punctuation">(</span>推荐<span class="token punctuation">)</span>
        key   	 键
        values 	值
        operator 	关系符 支持In<span class="token punctuation">,</span> NotIn<span class="token punctuation">,</span> Exists<span class="token punctuation">,</span> DoesNotExist<span class="token punctuation">.</span>
      matchLabels   	 指多个matchExpressions映射的内容
      
  preferredDuringSchedulingIgnoredDuringExecution 	软限制
    podAffinityTerm  	选项
      namespaces      
      topologyKey
      labelSelector
        matchExpressions  
          key     键
          values 值
          operator
        matchLabels 
    weight 倾向权重,在范围1-100
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li></ul></pre> 
<pre data-index="56" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">topologyKey用于指定调度时作用域<span class="token punctuation">,</span>例如:
    如果指定为kubernetes<span class="token punctuation">.</span>io/hostname,那就是以Node节点为区分范围
	如果指定为beta<span class="token punctuation">.</span>kubernetes<span class="token punctuation">.</span>io/os<span class="token punctuation">,</span>则以Node节点的操作系统类型来区分
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li></ul></pre> 
<blockquote> 
 <p>接下来,演示下<code>requiredDuringSchedulingIgnoredDuringExecution</code></p> 
</blockquote> 
<blockquote> 
 <p><strong>首先创建一个参照Pod,pod-podaffinity-target.yaml:</strong></p> 
</blockquote> 
<pre data-index="57" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">apiVersion: v1
kind: Pod
metadata:
  name: pod-podaffinity-target
  namespace: dev
  labels:
    podenv: pro <span class="token comment">#设置标签</span>
spec:
  containers:
  <span class="token operator">-</span> name: nginx
    image: nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>2
  nodeName: k8s-node-01 <span class="token comment"># 将目标pod名确指定到node1上</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li></ul></pre> 
<pre data-index="58" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 启动目标pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-podaffinity-target.yaml</span>
pod/pod-podaffinity-target created

<span class="token comment"># # 查看pod状况</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment">#  kubectl get pods  pod-podaffinity-target -n dev</span>
NAME                     READY   STATUS    RESTARTS   AGE
pod-podaffinity-target   1/1     Running   0          11s
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li></ul></pre> 
<blockquote> 
 <p><strong>创建pod-podaffinity-required.yaml,内容如下:</strong></p> 
</blockquote> 
<pre data-index="59" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">apiVersion: v1
kind: Pod
metadata:
  name: pod-podaffinity-required
  namespace: dev
spec:
  containers:
  <span class="token operator">-</span> name: nginx
    image: nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>2
  affinity:  <span class="token comment"># 亲和性设置</span>
    podAffinity: <span class="token comment"># 设置pod亲和性</span>
      requiredDuringSchedulingIgnoredDuringExecution: <span class="token comment"># 硬限制</span>
      <span class="token operator">-</span> labelSelector:
          matchExpressions: <span class="token comment"># 匹配env的值在["xxx","yyy"]中的标签</span>
          <span class="token operator">-</span> key: podenv
            operator: In
            values: <span class="token punctuation">[</span><span class="token string">"xxx"</span><span class="token punctuation">,</span><span class="token string">"yyy"</span><span class="token punctuation">]</span>
        topologyKey: kubernetes<span class="token punctuation">.</span>io/hostname
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li></ul></pre> 
<blockquote> 
 <p>上面配置表达的意思是:新Pod必须要与拥有标签nodeenv=xxx或者nodeenv=yyy的pod在同一Node上,显然现在没有这样pod,接下来,运行测试一下。</p> 
</blockquote> 
<pre data-index="60" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 启动pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-podaffinity-required.yaml</span>
pod/pod-podaffinity-required created

<span class="token comment"># 查看pod状态,发现未运行</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pods pod-podaffinity-required -n dev</span>
NAME                       READY   STATUS    RESTARTS   AGE
pod-podaffinity-required   0/1     Pending   0          7s

<span class="token comment"># 查看详细信息</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl describe pods pod-podaffinity-required  -n dev</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
Events:
  <span class="token function">Type</span>     Reason            Age                 <span class="token keyword">From</span>               Message
  <span class="token operator">--</span><span class="token operator">--</span>     <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span>            <span class="token operator">--</span><span class="token operator">--</span>                <span class="token operator">--</span><span class="token operator">--</span>               <span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span>
  Warning  FailedScheduling  37s <span class="token punctuation">(</span>x3 over 118s<span class="token punctuation">)</span>  default-scheduler  0/3 nodes are available: 1 node<span class="token punctuation">(</span>s<span class="token punctuation">)</span> had taint <span class="token punctuation">{<!-- --></span>node-role<span class="token punctuation">.</span>kubernetes<span class="token punctuation">.</span>io/master: <span class="token punctuation">}</span><span class="token punctuation">,</span> that the pod didn<span class="token string">'t tolerate, 2 node(s) didn'</span>t match pod affinity rules<span class="token punctuation">,</span> 2 node<span class="token punctuation">(</span>s<span class="token punctuation">)</span> didn't match pod affinity/anti-affinity rules<span class="token punctuation">.</span>

<span class="token comment"># 接下来修改  values: ["xxx","yyy"]----->values:["pro","yyy"]</span>
<span class="token comment"># 意思是:新Pod必须要与拥有标签nodeenv=pro或者nodeenv=yyy的pod在同一Node上</span>
<span class="token namespace">[root@master ~]</span><span class="token comment"># vim pod-podaffinity-required.yaml</span>

<span class="token comment"># 然后重新创建pod,查看效果</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl delete -f  pod-podaffinity-required.yaml</span>
pod <span class="token string">"pod-podaffinity-required"</span> deleted
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-podaffinity-required.yaml</span>
pod/pod-podaffinity-required created

<span class="token comment"># 发现此时Pod运行正常</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment">#  kubectl get pods pod-podaffinity-required -n dev</span>
NAME                       READY   STATUS    RESTARTS   AGE
pod-podaffinity-required   1/1     Running   0          6s
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li><li style="color: rgb(153, 153, 153);">31</li></ul></pre> 
<blockquote> 
 <p>关于PodAffinity的 preferredDuringSchedulingIgnoredDuringExecution,这里不再演示。</p> 
</blockquote> 
<blockquote> 
 <h5><a id="PodAntiAffinity_1562"></a>PodAntiAffinity</h5> 
 <p>PodAntiAffinity主要实现以运行的Pod为参照,让新创建的Pod跟参照pod不在一个区域中的功能。</p> 
 <p>它的配置方式和选项跟PodAffinty是一样的,这里不再做详细解释,直接做一个测试案例。</p> 
</blockquote> 
<blockquote> 
 <p><strong>继续使用上个案例中目标pod</strong></p> 
</blockquote> 
<pre data-index="61" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pods -n dev -o wide --show-labels</span>
NAME                       READY   STATUS    RESTARTS   AGE     IP           NODE          NOMINATED NODE   READINESS GATES   LABELS
pod-podaffinity-required   1/1     Running   0          4m36s   10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>7   k8s-node-01   <none>           <none>            <none>
pod-podaffinity-target     1/1     Running   0          27m     10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>6   k8s-node-01   <none>           <none>            podenv=pro
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li></ul></pre> 
<blockquote> 
 <p><strong>创建pod-podantiaffinity-required.yaml,内容如下:</strong></p> 
</blockquote> 
<pre data-index="62" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">apiVersion: v1
kind: Pod
metadata:
  name: pod-podantiaffinity-required
  namespace: dev
spec:
  containers:
  <span class="token operator">-</span> name: nginx
    image: nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>2
  affinity: 	 <span class="token comment"># 亲和性设置</span>
    podAntiAffinity: 	<span class="token comment"># 设置pod亲和性</span>
      requiredDuringSchedulingIgnoredDuringExecution: 	<span class="token comment"># 硬限制</span>
      <span class="token operator">-</span> labelSelector:
          matchExpressions: 	<span class="token comment"># 匹配podenv的值在["pro"]中的标签</span>
          <span class="token operator">-</span> key: podenv
            operator: In
            values: <span class="token punctuation">[</span><span class="token string">"pro"</span><span class="token punctuation">]</span>			<span class="token comment"># pro标签指向k8s-node-01主机</span>
        topologyKey: kubernetes<span class="token punctuation">.</span>io/hostname
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li></ul></pre> 
<blockquote> 
 <p>上面配置表达的意思是:新Pod必须要与拥有标签nodeenv=pro的pod不在同一Node上,运行测试一下。</p> 
</blockquote> 
<pre data-index="63" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 创建pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-podantiaffinity-required.yaml</span>
pod/pod-podantiaffinity-required created

<span class="token comment"># 查看pod</span>
<span class="token comment"># 发现调度到了k8s-node-02上</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pods pod-podantiaffinity-required -n dev -o wide</span>
NAME                           READY   STATUS    RESTARTS   AGE     IP           NODE          NOMINATED NODE   READINESS GATES
pod-podantiaffinity-required   1/1     Running   0          5m28s   10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>2   k8s-node-02   <none>           <none>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li></ul></pre> 
<h4><a name="t19"></a><a id="_1614"></a>污点和容忍</h4> 
<blockquote> 
 <h5><a id="Taints_1616"></a>污点(Taints)</h5> 
 <p>前面的调度方式都是站在Pod的角度上,通过在Pod上添加属性,来确定Pod是否要调度到指定的Node上,其实我们也可以站在Node的角度上,通过在Node上添加污点属性,来决定是否允许Pod调度过来。</p> 
 <p><strong>Node被设置上污点之后就和Pod之间存在了一种相斥的关系,进而拒绝Pod调度进来,甚至可以将已经存在的Pod驱逐出去。</strong></p> 
</blockquote> 
<blockquote> 
 <p>污点的格式为:<code>key=value:effect</code>,key和value是污点的标签,effect描述污点的作用,支持如下三个选项:</p> 
 <ul><li> <p><strong>PreferNoSchedule</strong>:kubernetes将尽量避免把Pod调度到具有该污点的Node上,除非没有其他节点可调度</p> </li><li> <p><strong>NoSchedule</strong>:kubernetes将不会把Pod调度到具有该污点的Node上,但不会影响当前Node上已存在的Pod</p> </li><li> <p><strong>NoExecute</strong>:kubernetes将不会把Pod调度到具有该污点的Node上,同时也会将Node上已存在的Pod驱离<br> <img src="https://img1.iyenn.com/img10/c4a7c45db2b2b29e/994116999009956994812.jpeg" alt="在这里插入图片描述"></p> </li></ul> 
</blockquote> 
<blockquote> 
 <p>使用kubectl设置和去除污点的命令示例如下:</p> 
 <pre data-index="64" class="set-code-show prettyprint"><code class="has-numbering" onclick="mdcp.signin(event)" style="position: unset;"># 设置污点
kubectl taint nodes node1 key=value:effect

# 去除污点 
kubectl taint nodes node1 key:effect-

# 去除所有污点 
kubectl taint nodes node1 key-
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li></ul></pre> 
</blockquote> 
<blockquote> 
 <p>接下来,演示下污点的效果:</p> 
 <ol><li> <p>准备节点 k8s-node-01(<strong>为了演示效果更加明显,暂时停止k8s-node-02节点</strong>)</p> </li><li> <p>为 k8s-node-01 节点设置一个污点: tag=nana:PreferNoSchedule;然后创建pod1( pod1 可以 )</p> </li><li> <p>修改为 k8s-node-01 节点设置一个污点: tag=nana:NoSchedule;然后创建pod2( pod1 正常 pod2 失败 )</p> </li><li> <p>修改为 k8s-node-01 节点设置一个污点: tag=nana:NoExecute;然后创建pod3 ( 3个pod都失败 )</p> </li></ol> 
</blockquote> 
<pre data-index="65" class="set-code-hide prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 为node1设置污点(PreferNoSchedule:尽量不要来,除非没办法)</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl taint nodes k8s-node-01 tag=nana:PreferNoSchedule</span>
node/k8s-node-01 tainted

<span class="token comment"># 创建pod1, 发现pod1创建在k8s-node-01主机上</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl run taint1 --image=nginx:1.17.2 -n dev</span>
pod/taint1 created
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pods -n dev -o wide</span>
NAME     READY   STATUS    RESTARTS   AGE   IP           NODE          NOMINATED NODE   READINESS GATES
taint1   1/1     Running   0          6s    10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>2   k8s-node-01   <none>           <none>


<span class="token comment"># 为node1设置污点(取消PreferNoSchedule; 设置NoSchedule:新的不要来,在这里的就不要动了)</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl taint nodes k8s-node-01 tag:PreferNoSchedule-</span>
node/k8s-node-01 untainted
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl taint nodes k8s-node-01 tag=nana:NoSchedule</span>
node/k8s-node-01 tainted

<span class="token comment"># 创建pod2, 发现pod2创建无法建立在k8s-node-01上,状态为none</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl run taint2 --image=nginx:1.17.2 -n dev</span>
pod/taint2 created
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pods -n dev -o wide</span>
NAME     READY   STATUS    RESTARTS   AGE   IP           NODE          NOMINATED NODE   READINESS GATES
taint1   1/1     Running   0          41s   10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>2   k8s-node-01   <none>           <none>
taint2   0/1     Pending   0          9s    <none>       <none>        <none>           <none>

<span class="token comment"># 为node1设置污点(取消NoSchedule; 设置NoExecute:新的不要来,在这里的赶紧走)</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl taint nodes k8s-node-01 tag:NoSchedule-</span>
node/k8s-node-01 untainted
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl taint nodes k8s-node-01 tag=nana:NoExecute</span>
node/k8s-node-01 tainted

<span class="token comment"># 创建pod3,发现k8s-node-01上所有的pod都被清除了,taint3是none状态</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl run taint3 --image=nginx:1.17.2 -n dev</span>
pod/taint3 created
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pods -n dev -o wide</span>
NAME     READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
taint3   0/1     Pending   0          7s    <none>   <none>   <none>           <none>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><div class="hide-preCode-box"><span class="hide-preCode-bt" data-report-view="{"spm":"1001.2101.3001.7365"}"><img class="look-more-preCode contentImg-no-view" src="https://img1.iyenn.com/img10/c4a7c45da7i3a7f8/65811691243225911151.jpeg" alt="" title=""></span></div><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li><li style="color: rgb(153, 153, 153);">31</li><li style="color: rgb(153, 153, 153);">32</li><li style="color: rgb(153, 153, 153);">33</li><li style="color: rgb(153, 153, 153);">34</li><li style="color: rgb(153, 153, 153);">35</li><li style="color: rgb(153, 153, 153);">36</li><li style="color: rgb(153, 153, 153);">37</li><li style="color: rgb(153, 153, 153);">38</li></ul></pre> 
<blockquote> 
 <p><strong>注意: 使用kubeadm搭建的集群,默认就会给master节点添加一个污点标记,所以pod就不会调度到master节点上。</strong></p> 
</blockquote> 
<blockquote> 
 <h5><a id="Toleration_1694"></a>容忍(Toleration)</h5> 
 <p>上面介绍了污点的作用,我们可以在node上添加污点用于拒绝pod调度上来,但是如果就是想<strong>将一个pod调度到一个有污点的node上去</strong>,这时候应该怎么做呢?这就要使用到容忍。<br> <img src="https://img1.iyenn.com/img10/c4a7c45db2b2b29e/124516999009955782.jpeg" alt="在这里插入图片描述"><br> <strong>污点就是拒绝,容忍就是忽略,Node通过污点拒绝pod调度上去,Pod通过容忍忽略拒绝</strong></p> 
</blockquote> 
<blockquote> 
 <p>下面先通过一个案例看下效果:</p> 
 <ol><li>上一小节,已经在node1节点上打上了NoExecute的污点,此时pod是调度不上去的</li><li>本小节,可以通过给pod添加容忍,然后将其调度上去</li></ol> 
</blockquote> 
<blockquote> 
 <p><strong>创建pod-toleration.yaml,内容如下</strong></p> 
</blockquote> 
<pre data-index="66" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;">apiVersion: v1
kind: Pod
metadata:
  name: pod-toleration
  namespace: dev
spec:
  containers:
  <span class="token operator">-</span> name: nginx
    image: nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>2
  tolerations:      <span class="token comment"># 添加容忍</span>
  <span class="token operator">-</span> key: <span class="token string">"tag"</span>        <span class="token comment"># 要容忍的污点的key</span>
    operator: <span class="token string">"Equal"</span> 	<span class="token comment"># 操作符</span>
    value: <span class="token string">"nana"</span>    <span class="token comment"># 容忍的污点的value</span>
    effect: <span class="token string">"NoExecute"</span>   <span class="token comment"># 添加容忍的规则,这里必须和标记的污点规则相同</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li></ul></pre> 
<blockquote> 
 <p><strong>创建pod,观察效果</strong></p> 
</blockquote> 
<pre data-index="67" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token comment"># 创建pod</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl create -f pod-toleration.yaml</span>
pod/pod-toleration created

<span class="token comment"># 添加容忍之后的pod, pod-toleration运行在k8s-node-01上</span>
<span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl get pods -n dev -o wide </span>
NAME             READY   STATUS    RESTARTS   AGE     IP           NODE          NOMINATED NODE   READINESS GATES
pod-toleration   1/1     Running   0          11s     10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>4   k8s-node-01   <none>           <none>
taint3           0/1     Pending   0          4m43s   <none>       <none>        <none>           <none>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li></ul></pre> 
<blockquote> 
 <p><strong>下面看一下容忍的详细配置:</strong></p> 
</blockquote> 
<pre data-index="68" class="set-code-show prettyprint"><code class="prism language-powershell has-numbering" onclick="mdcp.signin(event)" style="position: unset;"><span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl explain pod.spec.tolerations</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
FIELDS:
   key       <span class="token comment"># 对应着要容忍的污点的键,空意味着匹配所有的键</span>
   value     <span class="token comment"># 对应着要容忍的污点的值</span>
   operator  <span class="token comment"># key-value的运算符,支持Equal和Exists(默认)</span>
   effect    <span class="token comment"># 对应污点的effect,空意味着匹配所有影响</span>
   tolerationSeconds   <span class="token comment"># 容忍时间, 当effect为NoExecute时生效,表示pod在Node上的停留时间</span>
<div class="hljs-button signin" data-title="登录后复制" data-report-click="{"spm":"1001.2101.3001.4334"}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li></ul></pre>
                </div><div data-report-view="{"mod":"1585297308_001","spm":"1001.2101.3001.6548","dest":"https://blog.csdn.net/Yosigo_/article/details/119479355","extend1":"pc","ab":"new"}"><div></div></div>
                <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/markdown_views-98b95bb57c.css" rel="stylesheet">
                <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/style-c216769e99.css" rel="stylesheet">
        </div>
        
    </article><article class="baidu_pl">
         id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-704d5b9767.css">
             
                 id="content_views" class="markdown_views prism-atom-one-dark">
                    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                        <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                    <p><img src="https://img1.iyenn.com/thumb02/c4a7c4f8a7c4b21g/3359173964644795588.jpeg" alt="在这里插入图片描述"></p> 
<p>这一部分中,我们将深入探讨<a href="https://so.csdn.net/so/search?q=Azure&spm=1001.2101.3001.7020" target="_blank" class="hl hl-1" data-report-click="{"spm":"1001.2101.3001.7020","dest":"https://so.csdn.net/so/search?q=Azure&spm=1001.2101.3001.7020","extra":"{\"searchword\":\"Azure\"}"}" data-tit="Azure" data-pretit="azure">Azure</a> AI-900认证考试的核心内容、重要模块及学习路径。通过系统化的学习,您将能够掌握关键概念,强化实战技能,从而为考试的成功打下坚实基础。无论您是云计算的初学者,还是想进一步拓展AI知识的IT专业人员,Azure AI-900都是迈向云智能时代的重要一步。</p> 
<h4><a name="t0"></a><a id="1_AI900_5"></a>1. <strong>AI-900考试概述与重要更新</strong></h4> 
<p>在开始深入学习之前,首先要对Azure AI-900<a href="https://so.csdn.net/so/search?q=%E8%AE%A4%E8%AF%81%E8%80%83%E8%AF%95&spm=1001.2101.3001.7020" target="_blank" class="hl hl-1" data-report-click="{"spm":"1001.2101.3001.7020","dest":"https://so.csdn.net/so/search?q=%E8%AE%A4%E8%AF%81%E8%80%83%E8%AF%95&spm=1001.2101.3001.7020","extra":"{\"searchword\":\"认证考试\"}"}" data-tit="认证考试" data-pretit="认证考试">认证考试</a>有一个全面的了解。与其他微软认证考试一样,Azure AI-900的内容会定期更新,通常为小幅度调整,确保其与最新的技术发展和市场需求保持同步。需要注意的是,只有当考试代号发生变化时(例如从AI-900更名为AI-901),才意味着考试内容进行了重大改版。因此,在备考期间要及时关注官方发布的最新资讯和更新信息。</p> 
<h5><a id="11__11"></a>1.1 <strong>考试目标</strong></h5> 
<p><a href="https://learn.microsoft.com/en-us/credentials/certifications/resources/study-guides/ai-900" rel="nofollow">ai-900参考文档</a></p> 
<p>Azure AI-900考试的主要目标是验证考生对Azure人工智能服务的理解,特别是基础知识和实际应用。通过该认证,考生能够展示其在以下几个领域的能力:</p> 
<ul><li>人工智能工作负载及其解决方案</li><li>机器学习基础原理及相关应用</li><li>Azure AI服务(如计算机视觉、自然语言处理等)的应用</li><li>负责任的AI实践,确保AI的公平性、安全性与透明度</li></ul> 
<p><img src="https://img1.iyenn.com/thumb02/c4a7c4f8a7c4b21g/266417396464477397296.jpeg" alt="在这里插入图片描述"></p> 
<h5><a id="12__22"></a>1.2 <strong>考试内容结构</strong></h5> 
<p><img src="https://img1.iyenn.com/thumb02/c4a7c4f8a7c4b21g/416317396464476984.jpeg" alt="在这里插入图片描述"></p> 
<p>Azure AI-900认证考试主要分为以下几大模块:</p> 
<ul><li>人工智能工作负载概述(30%)</li><li>机器学习基础原理(25%)</li><li>Azure AI服务应用(35%)</li><li>负责任的AI原则(10%)</li></ul> 
<p>了解各模块的重点内容,可以帮助您有针对性地进行复习和备考。</p> 
<hr> 
<p><img src="https://img1.iyenn.com/thumb02/c4a7c4f8a7c4b21g/175117396464478670.jpeg" alt="在这里插入图片描述"></p> 
<h4><a name="t1"></a><a id="2__37"></a>2. <strong>人工智能工作负载详解</strong></h4> 
<h5><a id="21__39"></a>2.1 <strong>预测与预报</strong></h5> 
<p>AI-900考试的第一部分集中在预测和预报相关的工作负载。这里的核心概念包括:</p> 
<ul><li><strong>分类预测</strong>:将数据分为不同的类别,例如识别图像中的物体或判断邮件是否为垃圾邮件。</li><li><strong>回归分析</strong>:预测一个连续的数值结果,例如股票价格预测或房价评估。</li><li><strong>时间序列预测</strong>:预测一段时间内的数据变化趋势,常用于销售、气象等领域。</li><li><strong>异常检测</strong>:通过AI识别数据中的异常模式,适用于金融欺诈检测、网络安全等领域。</li></ul> 
<h5><a id="22__46"></a>2.2 <strong>计算机视觉应用</strong></h5> 
<p>计算机视觉是人工智能中一个非常重要的领域,AI-900考试也要求考生了解相关应用:</p> 
<ul><li><strong>图像分类</strong>:通过训练模型识别和分类图像中的对象。</li><li><strong>物体检测</strong>:不仅识别物体的种类,还能确定物体在图像中的位置。</li><li><strong>OCR文字识别</strong>:将图像中的文本转换为可编辑文本,例如扫描文档的自动数字化。</li><li><strong>人脸检测</strong>:利用计算机视觉技术识别和验证图像中的人脸,常见应用包括人脸解锁和安防监控。</li></ul> 
<h5><a id="23_NLP_53"></a>2.3 <strong>自然语言处理技术(NLP)</strong></h5> 
<p>自然语言处理(NLP)让计算机能够理解和生成人类语言,是现代AI的重要分支。AI-900认证涵盖了多个NLP相关的核心服务:</p> 
<ul><li><strong>文本分析服务</strong>:包括关键短语提取、实体识别、情感分析和语言建模等应用,广泛用于文本数据的自动处理。</li><li><strong>语音识别与合成</strong>:Azure的语音服务可以将语音转化为文本,或将文本转化为语音,实现语音助手和实时翻译等功能。</li></ul> 
<hr> 
<h4><a name="t2"></a><a id="3__60"></a>3. <strong>机器学习核心概念</strong></h4> 
<h5><a id="31__62"></a>3.1 <strong>数据处理流程</strong></h5> 
<p>AI-900认证强调机器学习的基础流程,考生需要理解整个模型开发过程:</p> 
<ol><li><strong>数据获取</strong>:从不同的来源收集数据,包括结构化和非结构化数据。</li><li><strong>数据准备</strong>:包括数据清洗、填补缺失值、去除噪声等处理。</li><li><strong>特征工程</strong>:从原始数据中提取有意义的特征,优化模型性能。</li><li><strong>模型训练</strong>:使用训练数据训练机器学习模型,使其能够进行预测。</li><li><strong>模型评估</strong>:通过验证集和测试集评估模型的效果,确保其泛化能力。</li><li><strong>模型部署</strong>:将训练好的模型部署到生产环境中,提供实时预测服务。</li></ol> 
<h5><a id="32_AutoML_71"></a>3.2 <strong>AutoML与设计器</strong></h5> 
<p>Azure AI-900考试强调了AutoML(自动化机器学习)和可视化设计器在机器学习中的应用:</p> 
<ul><li><strong>自动化机器学习(AutoML)</strong>:通过自动化流程帮助数据科学家和开发者快速构建和部署模型。</li><li><strong>可视化pipeline构建</strong>:Azure机器学习设计器提供了拖拽式界面,使用户能够在不编写代码的情况下构建机器学习pipeline,适合初学者和没有编程经验的用户。</li></ul> 
<hr> 
<h4><a name="t3"></a><a id="4_AI_78"></a>4. <strong>对话式AI解决方案</strong></h4> 
<h5><a id="41__80"></a>4.1 <strong>聊天机器人开发</strong></h5> 
<p>对话式AI在现代企业中得到广泛应用,Azure提供了多个工具和服务来开发智能聊天机器人:</p> 
<ul><li><strong>Web聊天集成</strong>:将聊天机器人集成到网站和应用中,提供自助服务。</li><li><strong>QnA Maker应用</strong>:通过QnA Maker,用户可以轻松构建基于知识库的问答系统。</li><li><strong>Azure Bot Service</strong>:为开发者提供了丰富的开发、测试和部署工具,支持创建复杂的聊天机器人。</li></ul> 
<h5><a id="42_AI_86"></a>4.2 <strong>负责任的AI实践</strong></h5> 
<p>微软在AI领域提出了六大原则,确保AI技术的公平性、透明度和可靠性,这些原则对AI-900考试至关重要:</p> 
<ul><li><strong>公平性</strong>:确保AI模型不会在不同群体中产生偏见。</li><li><strong>可靠性与安全性</strong>:确保AI系统的稳定性和安全性。</li><li><strong>隐私与安全</strong>:保护用户数据和隐私。</li><li><strong>包容性</strong>:确保所有人都能平等使用AI技术。</li><li><strong>透明度</strong>:让AI系统的行为对用户可解释。</li><li><strong>问责制</strong>:确保AI的决策能够追溯,并对结果负责。</li></ul> 
<hr> 
<h4><a name="t4"></a><a id="5__97"></a>5. <strong>备考策略与建议</strong></h4> 
<p>在备考过程中,保持系统化的学习路径非常重要。以下是一些关键的备考建议:</p> 
<ul><li><strong>学习官方教材和文档</strong>:官方文档通常包含最准确和最新的信息,是最可靠的学习资源。</li><li><strong>动手实践</strong>:在Azure平台上进行实际操作,体验AI服务的具体应用,可以帮助加深理解。</li><li><strong>参加模拟考试</strong>:通过模拟考试可以帮助您熟悉考试的题型和格式,同时发现自己在知识掌握上的盲点。</li><li><strong>制定学习计划</strong>:保持每天学习的时间,不要超负荷。学习的内容要逐步推进,确保每个概念都理解透彻。</li></ul> 
<hr> 
<h4><a name="t5"></a><a id="6__107"></a>6. <strong>总结</strong></h4> 
<p>Azure AI-900认证考试不仅仅是对人工智能基本概念的测试,更是一个让您了解和应用Azure AI服务的机会。通过系统学习和实践,您将掌握从机器学习到对话式AI解决方案的核心技能,并能够在真实世界中使用这些知识。在学习过程中,要注重理论与实践的结合,理解AI服务的应用场景,并确保掌握负责任的AI原则。</p> 
<p>通过备考,您将为自己的人工智能职业生涯奠定坚实基础,迈出进入<a href="https://so.csdn.net/so/search?q=%E4%BA%91%E8%AE%A1%E7%AE%97&spm=1001.2101.3001.7020" target="_blank" class="hl hl-1" data-report-view="{"spm":"1001.2101.3001.7020","dest":"https://so.csdn.net/so/search?q=%E4%BA%91%E8%AE%A1%E7%AE%97&spm=1001.2101.3001.7020","extra":"{\"searchword\":\"云计算\"}"}" data-report-click="{"spm":"1001.2101.3001.7020","dest":"https://so.csdn.net/so/search?q=%E4%BA%91%E8%AE%A1%E7%AE%97&spm=1001.2101.3001.7020","extra":"{\"searchword\":\"云计算\"}"}" data-tit="云计算" data-pretit="云计算">云计算</a>和AI领域的第一步。</p> 
<h4><a name="t6"></a><a id="FAQ_113"></a>常见问题解答(FAQ)</h4> 
<h5><a id="Q1_AI900_115"></a>Q1: AI-900考试的难度如何?</h5> 
<p>A1: 对于有一定基础的学习者来说,难度适中。考试重点是AI的基本概念和Azure服务的应用。</p> 
<h5><a id="Q2__118"></a>Q2: 考试内容多久更新一次?</h5> 
<p>A2: 微软会定期进行小幅更新,通常每年会有更新。重大更新较为少见。</p> 
<h5><a id="Q3__121"></a>Q3: 如何最有效地准备考试?</h5> 
<p>A3: 建议结合官方教材、实践操作和模拟测试,重点掌握机器学习、计算机视觉和自然语言处理等领域的应用。</p> 
<h5><a id="Q4_AutoML_124"></a>Q4: AutoML与传统机器学习有什么区别?</h5> 
<p>A4: AutoML使机器学习过程自动化,适合快速构建模型,而传统机器学习方法提供更高的灵活性和控制力。</p> 
<h5><a id="Q5__127"></a>Q5: 考试中最重要的模块是什么?</h5> 
<p>A5: 机器学习基础、计算机视觉和自然语言处理是考试的重点模块,务必重点复习这些内容。</p> 
<h2><a name="t7"></a><a id="AzureAI900_131"></a><strong>Azure基础认证(AI-900)完全指南</strong></h2> 
<ol><li> <p><strong>认证概述</strong>:<a href="http://iyenn.com/rec/1649809.html" rel="nofollow">认证概述</a></p> </li><li> <p><strong>考试的核心内容</strong>:<a href="http://iyenn.com/rec/1649810.html" rel="nofollow">考试核心内容</a></p> </li><li> <p><strong>AI层级</strong>:<a href="http://iyenn.com/rec/1649811.html" rel="nofollow">AI层级</a></p> </li><li> <p><strong>AI基础概念</strong>:<a href="http://iyenn.com/rec/1649812.html" rel="nofollow">AI基础概念</a></p> </li><li> <p><strong>数据集</strong>:<a href="http://iyenn.com/rec/1649813.html" rel="nofollow">数据集</a></p> </li><li> <p><strong>数据标注</strong>:<a href="http://iyenn.com/rec/1649814.html" rel="nofollow">数据标注</a></p> </li><li> <p><strong>监督学习与无监督强化学习</strong>:<a href="http://iyenn.com/rec/1649815.html" rel="nofollow">监督学习与无监督强化学习</a></p> </li><li> <p><strong>神经网络与深度学习</strong>:<a href="http://iyenn.com/rec/1649816.html" rel="nofollow">神经网络与深度学习</a></p> </li><li> <p><strong>GPU</strong>:<a href="http://iyenn.com/rec/1649817.html" rel="nofollow">GPU</a></p> </li><li> <p><strong>CUDA</strong>:<a href="http://iyenn.com/rec/1649818.html" rel="nofollow">CUDA</a></p> </li><li> <p><strong>ML Pipeline</strong>:<a href="http://iyenn.com/rec/1649819.html" rel="nofollow">ML Pipeline</a></p> </li><li> <p><strong>预测和预报</strong>:<a href="http://iyenn.com/rec/1649820.html" rel="nofollow">预测和预报</a></p> </li><li> <p><strong>评估指标</strong>:<a href="http://iyenn.com/rec/1649821.html" rel="nofollow">评估指标</a></p> </li><li> <p><strong>Jupyter Notebooks</strong>:<a href="http://iyenn.com/rec/1649822.html" rel="nofollow">Jupyter Notebooks</a></p> </li><li> <p><strong>回归分析</strong>:<a href="http://iyenn.com/rec/1649823.html" rel="nofollow">回归分析</a></p> </li><li> <p><strong>分类</strong>:<a href="http://iyenn.com/rec/1649824.html" rel="nofollow">分类</a></p> </li><li> <p><strong>聚类</strong>:<a href="http://iyenn.com/rec/1649825.html" rel="nofollow">聚类</a></p> </li><li> <p><strong>混淆矩阵</strong>:<a href="http://iyenn.com/rec/1649826.html" rel="nofollow">混淆矩阵</a></p> </li></ol> 
<p><strong>本文为原创内容,未经许可不得转载。</strong></p>
                </div> data-report-view="{"mod":"1585297308_001","spm":"1001.2101.3001.6548","dest":"http://iyenn.com/rec/1649810.html","extend1":"pc","ab":"new"}">></div></div>
                <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/markdown_views-f23dff6052.css" rel="stylesheet">
                <link href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/style-e504d6a974.css" rel="stylesheet">
        </div>
         id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"> class="blog_extension blog_extension_type1" id="blog_extension">
           class="blog_extension_card" data-report-click="{"spm":"1001.2101.3001.6470"}" data-report-view="{"spm":"1001.2101.3001.6470"}">
             class="blog_extension_card_left">
            <img src="https://img1.iyenn.com/thumb02/c4a7c4f8a7b2b2f8/1156173690285661885.jpeg" alt="">
          </div>
             class="blog_extension_card_cont">
               class="blog_extension_card_cont_l">
                <span class="text">海棠AI实验室</span>
                 class="blog_extension_card_cont_r">
                  <img class="weixin" src="https://img1.iyenn.com/thumb02/c4a7c45da7i3a7i3/624416914901196911299.jpeg" alt="">
                  <span>微信公众号</span>
                  <img class="go" src="https://img1.iyenn.com/thumb02/c4a7c45da7i3a7i3/452916914901198231.jpeg" alt="">
                </div>
              </div>
              <span class="style">北二外海棠AI实验室导师,主要研究AI、LLM</span>
            </div>
          </div></div></div>
    </article>
                </div>
                <div style="margin-top: 5px;color: #828a91;font-size: 12px">
                    <span>注:本文转载自blog.csdn.net的我的紫霞辣辣的文章<a href="https://blog.csdn.net/Yosigo_/article/details/119479355" target="_blank">"https://blog.csdn.net/Yosigo_/article/details/119479355"</a>。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。</span>
                </div>
                <div class="content-footer post-content-footer">
                    <div class="inner-wrapper-sticky">
                        <div class="post-content-footer-in">
                            <div class="content-footer-poster">
                                <button data-title="去评论" class="mobile-hidden b2tooltipbox comment-span"><a
                                        href="#demoAnchor"><i class="b2font b2-chat-2-fill "></i></a></button>
                                
                                
                                <a href="/member/login/">
                                    <button data-title="喜欢" class="mobile-hidden b2tooltipbox comment-span"
                                            data-id="1649810"><span class=""><i
                                            class="b2font b2-heart-fill "></i><b>3868</b></span></button>
                                </a>
                                
                                
                                <button data-title="分享到脸书" class="mobile-hidden b2tooltipbox comment-span"
                                        style="margin-top: 22px"><a
                                        href="javascript:fbShare();">
                                    <svg t="1690264724364" class="fenxiang_icon" viewBox="0 0 1024 1024"
                                         version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2299"
                                         width="24"
                                         height="24">
                                        <path d="M933.8 195.213c0-58.296-47.259-105.554-105.554-105.554h-633.12c-58.296 0-105.554 47.258-105.554 105.554v633.12c0 58.295 47.258 105.554 105.554 105.554h633.12c58.295 0 105.554-47.259 105.554-105.554v-633.12zM776.21 300.46H669.787v105.4H776.21v105.401H669.787v317.225h-105.4V511.261H458.985v-105.4h105.4V267.9c0-35.433 39.029-72.84 78.785-72.84H776.21v105.4z"
                                              fill="#6C6E72" p-id="2300"></path>
                                    </svg>
                                </a>
                                </button>
                                <button data-title="分享到推特" class="mobile-hidden b2tooltipbox comment-span"><a
                                        href="javascript:twitterShare();">
                                    <svg t="1690265072794" class="fenxiang_icon" viewBox="0 0 1024 1024"
                                         version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2903"
                                         width="24"
                                         height="24">
                                        <path d="M1024 512c0-282.763636-229.236364-512-512-512C229.236364 0 0 229.236364 0 512s229.236364 512 512 512C794.763636 1024 1024 794.763636 1024 512zM236.311273 693.946182c8.843636 1.117091 17.873455 1.722182 26.996364 1.722182 53.015273 0.093091 101.794909-19.037091 140.474182-51.153455-49.524364-1.163636-91.275636-36.165818-105.658182-84.200727 6.888727 1.442909 14.010182 2.280727 21.271273 2.327273 10.333091 0.046545 20.293818-1.349818 29.789091-4.049455-51.758545-11.450182-90.763636-60.555636-90.763636-119.063273 0-0.512 0-1.024 0-1.536 15.266909 9.216 32.674909 14.801455 51.246545 15.639273C279.365818 431.709091 259.397818 394.472727 259.397818 352.581818c0-22.155636 5.585455-42.821818 15.313455-60.509091 55.808 73.774545 139.170909 122.740364 233.146182 128.837818-1.954909-8.797091-2.932364-17.966545-2.932364-27.322182 0-66.141091 50.641455-118.923636 113.105455-117.899636 32.535273 0.558545 61.952 15.499636 82.571636 38.958545 25.786182-4.933818 49.989818-14.429091 71.819636-27.694545-8.424727 27.601455-26.391273 50.594909-49.757091 64.977455 22.900364-2.56 44.683636-8.610909 64.977455-17.780364-15.173818 23.598545-34.350545 44.218182-56.459636 60.695273 0.232727 5.12 0.325818 10.24 0.325818 15.36 0 157.044364-113.803636 338.152727-321.861818 338.059636C345.832727 748.311273 286.347636 728.296727 236.311273 693.946182z"
                                              p-id="2904" fill="#6C6E72"></path>
                                    </svg>
                                </a>
                                </button>
                                <button data-title="分享到Whatsapp" class="mobile-hidden b2tooltipbox comment-span"><a
                                        href="javascript:whatsApp();">
                                    <svg t="1690265463406" class="fenxiang_icon" viewBox="0 0 1024 1024"
                                         version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4099"
                                         id="mx_n_1690265463407" width="24" height="24">
                                        <path d="M512 85.333333a426.666667 426.666667 0 0 0-345.6 676.693334L128 878.933333a20.906667 20.906667 0 0 0 5.12 21.76 21.76 21.76 0 0 0 21.76 5.546667l121.6-39.253333A426.666667 426.666667 0 1 0 512 85.333333z m248.32 602.453334a119.893333 119.893333 0 0 1-85.333333 60.16c-22.186667 4.693333-51.626667 8.533333-149.333334-32a530.346667 530.346667 0 0 1-213.333333-187.733334 242.773333 242.773333 0 0 1-50.773333-128 136.96 136.96 0 0 1 42.666666-104.106666 63.573333 63.573333 0 0 1 42.666667-15.36h14.08c12.373333 0 18.773333 0 27.306667 20.906666s34.986667 85.333333 37.973333 92.16a24.32 24.32 0 0 1 2.133333 23.04 78.933333 78.933333 0 0 1-14.08 19.626667c-5.973333 7.253333-12.373333 12.8-18.346666 20.48a20.053333 20.053333 0 0 0-5.12 26.88 381.44 381.44 0 0 0 69.12 85.333333 313.173333 313.173333 0 0 0 100.266666 61.866667 26.453333 26.453333 0 0 0 29.866667-4.693333 426.666667 426.666667 0 0 0 33.28-42.666667 23.893333 23.893333 0 0 1 30.72-8.96c11.52 3.84 72.533333 34.133333 85.333333 40.106667s20.906667 9.386667 23.893334 14.506666a106.666667 106.666667 0 0 1-2.986667 58.453334z"
                                              p-id="4100" fill="#6C6E72"></path>
                                    </svg>
                                </a>
                                </button>
                                <!--                                <a class="tooltip be-btn-beshare be-btn-link be-btn-link-l use-beshare-link-btn bk dah"-->
                                <!--                                   rel="external nofollow" onclick="myFunction()" onmouseout="outFunc()">-->
                                <!--                                    <span class="sharetip bz copytipl">复制链接</span>-->
                                <!--                                </a>-->
                                <div>
                                    <span class="sharetip bz copytipl">复制链接</span>
                                    <button class="btn btn-secondary mobile-hidden b2tooltipbox comment-span copy_button">
                                        <a>
                                            <svg t="1690338204788" class="fenxiang_icon" viewBox="0 0 1024 1024"
                                                 version="1.1"
                                                 xmlns="http://www.w3.org/2000/svg" p-id="10669" width="24" height="24">
                                                <path d="M512 1024C229.228 1024 0 794.772 0 512S229.228 0 512 0s512 229.228 512 512-229.228 512-512 512z m54.545-565.02l-1.798-1.764a102.207 102.207 0 0 0-11.002-9.523l-35.453 35.442c4.176 2.446 8.078 5.393 11.605 8.92l1.866 1.763a58.277 58.277 0 0 1 0 82.341l-96.904 96.882a58.334 58.334 0 0 1-82.341 0l-1.832-1.798a58.243 58.243 0 0 1 0-82.306l43.816-43.828a149.675 149.675 0 0 1-10.866-58.732l-67.812 67.72c-41.836 41.825-41.836 110.251 0 152.053l1.787 1.798c41.836 41.79 110.228 41.79 152.052 0l96.882-96.916c41.757-41.825 41.757-110.25 0-152.052z m141.38-141.37l-1.82-1.797c-41.802-41.825-110.228-41.825-152.053 0l-96.882 96.916c-41.824 41.79-41.824 110.216 0 152.053l1.798 1.763c3.505 3.425 7.225 6.576 11.002 9.523l35.454-35.476a57.845 57.845 0 0 1-11.583-8.92l-1.798-1.763a58.311 58.311 0 0 1 0-82.375l96.905-96.882a58.197 58.197 0 0 1 82.284 0l1.798 1.797a58.277 58.277 0 0 1 0 82.341l-43.76 43.828c7.612 18.796 11.196 38.81 10.844 58.766l67.789-67.755c41.824-41.79 41.824-110.216 0.022-152.007z"
                                                      p-id="10670" fill="#6C6E72"></path>
                                            </svg>
                                        </a>
                                    </button>
                                </div>
                                <div class="btn-group  ">
                                    <button type="button" class="btn btn-secondary dropdown-toggle"
                                            data-toggle="dropdown" aria-expanded="false">
                                        <svg t="1690338289501" class="fenxiang_icon" viewBox="0 0 1024 1024"
                                             version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="11788" width="24"
                                             height="24">
                                            <path d="M960 128H64c-35.3 0-64 28.6-64 64v640c0 35.3 28.7 64 64 64h160c8.8 0 16-7.2 16-16v-12c0-2.2 1.8-4 4-4h151c2.2 0 4 1.8 4 4v12c0 8.8 7.2 16 16 16h193c8.8 0 16-7.2 16-16v-12c0-2.2 1.8-4 4-4h152c2.2 0 4 1.8 4 4v12c0 8.8 7.2 16 16 16h160c35.3 0 64-28.7 64-64V192c0-35.4-28.7-64-64-64zM576 680v-48c0-4.4 3.6-8 8-8h304c4.4 0 8 3.6 8 8v48c0 4.4-3.6 8-8 8H584c-4.4 0-8-3.6-8-8z m0-128v-48c0-4.4 3.6-8 8-8h304c4.4 0 8 3.6 8 8v48c0 4.4-3.6 8-8 8H584c-4.4 0-8-3.6-8-8z m320-128c0 4.4-3.6 8-8 8H584c-4.4 0-8-3.6-8-8v-48c0-4.4 3.6-8 8-8h304c4.4 0 8 3.6 8 8v48zM447.2 736h-288c-8.8 0-16.8-3.6-22.6-9.4-5.8-5.8-9.4-13.8-9.4-22.6 0-57.1 27.2-107.8 69.3-140 4-3.1 4.3-9 0.5-12.3-33-29.3-53.8-72.1-53.8-119.7 0-87.7 70.5-158.9 157.9-160 87.8-1.1 160.7 69.6 162 157.4 0.8 48.6-20.1 92.4-53.7 122.2-3.8 3.4-3.6 9.3 0.5 12.4 21.1 16.1 38.4 36.8 50.4 60.6 12.1 23.8 18.9 50.8 18.9 79.3 0 17.8-14.3 32.1-32 32.1z"
                                                  p-id="11789" fill="#6C6E72"></path>
                                        </svg>
                                    </button>
                                    <div class="dropdown-menu" style="padding: 5px;box-shadow: 0px 0px 6px 0px #999;">
                                        <div class="menu_img">
                                            <div id="qrcode" style="width:100%; height:100%;"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div style="display: flex;justify-content: space-between;align-items: end;">
                    <div class="post-tags-meat_news">
<!--                        <a class="b2-radius" href="/list_101/">-->
<!--                            <span class="tag-img"><i class="b2font b2-price-tag-3-line "></i></span>-->
<!--                            <span class="tag-text"></span>-->
<!--                        </a>-->
                        
                    </div>
                </div>
                <div class="mobile-show">
                <div style="display: flex;justify-content: end;flex-wrap: wrap" class="share_mobile">
                    <a href="javascript:whatsApp();">
                        <svg t="1690265463406" class="fenxiang_icon" viewBox="0 0 1024 1024"
                             version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4099"
                             id="mx_n_1690265463407" width="24" height="24">
                            <path d="M512 85.333333a426.666667 426.666667 0 0 0-345.6 676.693334L128 878.933333a20.906667 20.906667 0 0 0 5.12 21.76 21.76 21.76 0 0 0 21.76 5.546667l121.6-39.253333A426.666667 426.666667 0 1 0 512 85.333333z m248.32 602.453334a119.893333 119.893333 0 0 1-85.333333 60.16c-22.186667 4.693333-51.626667 8.533333-149.333334-32a530.346667 530.346667 0 0 1-213.333333-187.733334 242.773333 242.773333 0 0 1-50.773333-128 136.96 136.96 0 0 1 42.666666-104.106666 63.573333 63.573333 0 0 1 42.666667-15.36h14.08c12.373333 0 18.773333 0 27.306667 20.906666s34.986667 85.333333 37.973333 92.16a24.32 24.32 0 0 1 2.133333 23.04 78.933333 78.933333 0 0 1-14.08 19.626667c-5.973333 7.253333-12.373333 12.8-18.346666 20.48a20.053333 20.053333 0 0 0-5.12 26.88 381.44 381.44 0 0 0 69.12 85.333333 313.173333 313.173333 0 0 0 100.266666 61.866667 26.453333 26.453333 0 0 0 29.866667-4.693333 426.666667 426.666667 0 0 0 33.28-42.666667 23.893333 23.893333 0 0 1 30.72-8.96c11.52 3.84 72.533333 34.133333 85.333333 40.106667s20.906667 9.386667 23.893334 14.506666a106.666667 106.666667 0 0 1-2.986667 58.453334z"
                                  p-id="4100" fill="#6C6E72"></path>
                        </svg>
                    </a>
                    <a href="javascript:fbShare();">
                        <svg t="1690264724364" class="fenxiang_icon" viewBox="0 0 1024 1024"
                             version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2299"
                             width="24"
                             height="24">
                            <path d="M933.8 195.213c0-58.296-47.259-105.554-105.554-105.554h-633.12c-58.296 0-105.554 47.258-105.554 105.554v633.12c0 58.295 47.258 105.554 105.554 105.554h633.12c58.295 0 105.554-47.259 105.554-105.554v-633.12zM776.21 300.46H669.787v105.4H776.21v105.401H669.787v317.225h-105.4V511.261H458.985v-105.4h105.4V267.9c0-35.433 39.029-72.84 78.785-72.84H776.21v105.4z"
                                  fill="#6C6E72" p-id="2300"></path>
                        </svg>
                    </a>
                    <a href="javascript:twitterShare();">
                        <svg t="1690265072794" class="fenxiang_icon" viewBox="0 0 1024 1024"
                             version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2903"
                             width="24"
                             height="24">
                            <path d="M1024 512c0-282.763636-229.236364-512-512-512C229.236364 0 0 229.236364 0 512s229.236364 512 512 512C794.763636 1024 1024 794.763636 1024 512zM236.311273 693.946182c8.843636 1.117091 17.873455 1.722182 26.996364 1.722182 53.015273 0.093091 101.794909-19.037091 140.474182-51.153455-49.524364-1.163636-91.275636-36.165818-105.658182-84.200727 6.888727 1.442909 14.010182 2.280727 21.271273 2.327273 10.333091 0.046545 20.293818-1.349818 29.789091-4.049455-51.758545-11.450182-90.763636-60.555636-90.763636-119.063273 0-0.512 0-1.024 0-1.536 15.266909 9.216 32.674909 14.801455 51.246545 15.639273C279.365818 431.709091 259.397818 394.472727 259.397818 352.581818c0-22.155636 5.585455-42.821818 15.313455-60.509091 55.808 73.774545 139.170909 122.740364 233.146182 128.837818-1.954909-8.797091-2.932364-17.966545-2.932364-27.322182 0-66.141091 50.641455-118.923636 113.105455-117.899636 32.535273 0.558545 61.952 15.499636 82.571636 38.958545 25.786182-4.933818 49.989818-14.429091 71.819636-27.694545-8.424727 27.601455-26.391273 50.594909-49.757091 64.977455 22.900364-2.56 44.683636-8.610909 64.977455-17.780364-15.173818 23.598545-34.350545 44.218182-56.459636 60.695273 0.232727 5.12 0.325818 10.24 0.325818 15.36 0 157.044364-113.803636 338.152727-321.861818 338.059636C345.832727 748.311273 286.347636 728.296727 236.311273 693.946182z"
                                  p-id="2904" fill="#6C6E72"></path>
                        </svg>
                    </a>

                    <a>
                        <span class="sharetip bz copytipl2">复制链接</span>
                        <button class="copy_button2">
                            <svg t="1690338204788" class="fenxiang_icon" viewBox="0 0 1024 1024" version="1.1"
                                 xmlns="http://www.w3.org/2000/svg" p-id="10669" width="24" height="24">
                                <path d="M512 1024C229.228 1024 0 794.772 0 512S229.228 0 512 0s512 229.228 512 512-229.228 512-512 512z m54.545-565.02l-1.798-1.764a102.207 102.207 0 0 0-11.002-9.523l-35.453 35.442c4.176 2.446 8.078 5.393 11.605 8.92l1.866 1.763a58.277 58.277 0 0 1 0 82.341l-96.904 96.882a58.334 58.334 0 0 1-82.341 0l-1.832-1.798a58.243 58.243 0 0 1 0-82.306l43.816-43.828a149.675 149.675 0 0 1-10.866-58.732l-67.812 67.72c-41.836 41.825-41.836 110.251 0 152.053l1.787 1.798c41.836 41.79 110.228 41.79 152.052 0l96.882-96.916c41.757-41.825 41.757-110.25 0-152.052z m141.38-141.37l-1.82-1.797c-41.802-41.825-110.228-41.825-152.053 0l-96.882 96.916c-41.824 41.79-41.824 110.216 0 152.053l1.798 1.763c3.505 3.425 7.225 6.576 11.002 9.523l35.454-35.476a57.845 57.845 0 0 1-11.583-8.92l-1.798-1.763a58.311 58.311 0 0 1 0-82.375l96.905-96.882a58.197 58.197 0 0 1 82.284 0l1.798 1.797a58.277 58.277 0 0 1 0 82.341l-43.76 43.828c7.612 18.796 11.196 38.81 10.844 58.766l67.789-67.755c41.824-41.79 41.824-110.216 0.022-152.007z"
                                      p-id="10670" fill="#6C6E72"></path>
                            </svg>
                        </button>
                    </a>
                    <a class="dropdown mobile_dropup">
                        <button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown"
                                aria-expanded="false">
                            <svg t="1690338289501" class="fenxiang_icon" viewBox="0 0 1024 1024" version="1.1"
                                 xmlns="http://www.w3.org/2000/svg" p-id="11788" width="24" height="24">
                                <path d="M960 128H64c-35.3 0-64 28.6-64 64v640c0 35.3 28.7 64 64 64h160c8.8 0 16-7.2 16-16v-12c0-2.2 1.8-4 4-4h151c2.2 0 4 1.8 4 4v12c0 8.8 7.2 16 16 16h193c8.8 0 16-7.2 16-16v-12c0-2.2 1.8-4 4-4h152c2.2 0 4 1.8 4 4v12c0 8.8 7.2 16 16 16h160c35.3 0 64-28.7 64-64V192c0-35.4-28.7-64-64-64zM576 680v-48c0-4.4 3.6-8 8-8h304c4.4 0 8 3.6 8 8v48c0 4.4-3.6 8-8 8H584c-4.4 0-8-3.6-8-8z m0-128v-48c0-4.4 3.6-8 8-8h304c4.4 0 8 3.6 8 8v48c0 4.4-3.6 8-8 8H584c-4.4 0-8-3.6-8-8z m320-128c0 4.4-3.6 8-8 8H584c-4.4 0-8-3.6-8-8v-48c0-4.4 3.6-8 8-8h304c4.4 0 8 3.6 8 8v48zM447.2 736h-288c-8.8 0-16.8-3.6-22.6-9.4-5.8-5.8-9.4-13.8-9.4-22.6 0-57.1 27.2-107.8 69.3-140 4-3.1 4.3-9 0.5-12.3-33-29.3-53.8-72.1-53.8-119.7 0-87.7 70.5-158.9 157.9-160 87.8-1.1 160.7 69.6 162 157.4 0.8 48.6-20.1 92.4-53.7 122.2-3.8 3.4-3.6 9.3 0.5 12.4 21.1 16.1 38.4 36.8 50.4 60.6 12.1 23.8 18.9 50.8 18.9 79.3 0 17.8-14.3 32.1-32 32.1z"
                                      p-id="11789" fill="#6C6E72"></path>
                            </svg>
                        </button>
                        <div class="dropdown-menu mobile_menu"
                             style="padding: 5px;box-shadow: 0px 0px 6px 0px #999;">
                            <div class="menu_img">
                                <div id="qrcode_two" style="width:100%; height:100%;"></div>
                            </div>
                        </div>
                    </a>
                </div>
            </div>


            </article>
            <div class="related-posts mg-t mg-b box b2-radius">
                <div class="related-posts-title">相关推荐</div>
                <div class="hidden-line">
                    <div class="related-posts-in">
                    </div>
                </div>
            </div>
            <div class="comments-box" id="demoAnchor">
                <div id="comments" class="comments-area box b2-radius">
                    <div class="comments-title">
                        <div class="comment-info">
							<span ref="commentCount" class="comment-count">
								发表评论</span>
                        </div>
                        <div id="comment-form" class="comment-form">
                            <div class="toolbox-left">
                                <div class="profile-box" style="display: flex">
                                    <a class="profile-href comment-overlay-login">
                                        <!--                                                            <span class="profile-name">发表评论</span>-->
                                        
                                        
                                        <a class="login-info">登录后才能发表评论和回复</a>
                                        <a class="text-secondary" href="/member/register/">注册</p>
                                            
                                            
                                            
                                            
                                            <a class="text-secondary" href="/member/login/">/ 登录</a>
                                            
                                            
                                        </a>
                                </div>
                            </div>
                        </div>
                    </div><!-- .comments-title -->
                    <div class="container" style="padding-bottom: 20px;">
                        <form class="my-4" onsubmit="return submitcomment(this);" data-action="/comment/add/?contentid=1649810">
                            <div class="form-group">
								<textarea name="comment" id="comment"
                                          onblur="if(value.length>200){value=value.substr(0,200)}" class="form-control"
                                          placeholder="请输入评论内容(限200字数)" onkeyup="value=value.substr(0,200)"></textarea>
                            </div>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-6 col-md-3">
                                        <input type="text" name="checkcode" required id="checkcode"
                                               onfocus='updateimg()' class="form-control" placeholder="请输入验证码">
                                    </div>
                                    <div class="col-6  col-md-3" id="text">
                                        <img title="点击刷新" style="height:33px;" id="codeimg" src="/core/code.php"
                                             onclick="this.src='/core/code.php?'+Math.round(Math.random()*10);"/>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <button type="submit" class="btn btn-info mb-2">提交评论</button>
                            </div>
                        </form>
                        <h4>评论记录:</h4>
                        <a name="comment"></a>
                        
                        <!-- 分页 -->
                        <!--						<div class="text-center my-5 text-secondary">未查询到任何数据!</div>-->
                        
                        <div class="tac text-secondary">未查询到任何数据!</div>
                        


                        <!-- 评论回复弹框 -->
                        <div class="modal" tabindex="-1" role="dialog" id="reply">
                            <div class="modal-dialog">
                                <div class="modal-content">

                                    <div class="modal-header">
                                        <h5 class="modal-title">回复评论:</h5>
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">×</span>
                                        </button>
                                    </div>

                                    <form onsubmit="return submitcomment(this);" data-action="" id="replyform">
                                        <div class="modal-body">
                                            <div class="form-group">
												<textarea name="comment" id="comment"
                                                          onblur="if(value.length>200) value=value.substr(0,200)"
                                                          class="form-control" placeholder="请输入评论内容(限200字数)"
                                                          onkeyup="value=value.substr(0,200)"></textarea>
                                            </div>

                                            <div class="form-group">
                                                <div class="row">
                                                    <div class="col-6">
                                                        <input type="text" name="checkcode" required id="checkcode"
                                                               class="form-control" placeholder="请输入验证码">
                                                    </div>
                                                    <div class="col-6" id="text">
                                                        <img title="点击刷新" style="height:33px;" id="codeimg"
                                                             src="/core/code.php"
                                                             onclick="this.src='/core/code.php?'+Math.round(Math.random()*10);"/>
                                                    </div>
                                                </div>
                                            </div>

                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-dismiss="modal">
                                                关闭
                                            </button>
                                            <button type="submit" class="btn btn-info" id="text">提交评论
                                            </button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>

                    </div>


                </div><!-- #comments -->

            </div>
        </div>
        <aside id="secondary" class="widget-area" style="margin-right: 0;margin-left: 16px;margin-bottom: 10px;">
            <div class="sidebars">
                <div class="inner-wrapper-sticky">
                    <div class="sidebar-innter widget-ffixed">
                        <section id="tag_cloud-10" class="widget widget_tag_cloud mg-b box b2-radius mobile-hidden">
                            <h2 class="widget-title_fenlei">分类栏目</h2>
                            <div class="tagcloud">
                                
                                <a href="/list_103/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >后端<span class="tag-link-count">
										(14832)</span></a>
                                
                                <a href="/list_104/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >前端<span class="tag-link-count">
										(14280)</span></a>
                                
                                <a href="/list_105/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >移动开发<span class="tag-link-count">
										(3760)</span></a>
                                
                                <a href="/list_106/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >编程语言<span class="tag-link-count">
										(3851)</span></a>
                                
                                <a href="/list_107/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >Java<span class="tag-link-count">
										(3904)</span></a>
                                
                                <a href="/list_108/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >Python<span class="tag-link-count">
										(3298)</span></a>
                                
                                <a href="/list_109/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >人工智能<span class="tag-link-count">
										(10119)</span></a>
                                
                                <a href="/list_110/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >AIGC<span class="tag-link-count">
										(2810)</span></a>
                                
                                <a href="/list_111/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >大数据<span class="tag-link-count">
										(3499)</span></a>
                                
                                <a href="/list_112/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >数据库<span class="tag-link-count">
										(3945)</span></a>
                                
                                <a href="/list_113/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >数据结构与算法<span class="tag-link-count">
										(3757)</span></a>
                                
                                <a href="/list_114/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >音视频<span class="tag-link-count">
										(2669)</span></a>
                                
                                <a href="/list_115/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >云原生<span class="tag-link-count">
										(3145)</span></a>
                                
                                <a href="/list_116/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >云平台<span class="tag-link-count">
										(2965)</span></a>
                                
                                <a href="/list_117/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >前沿技术<span class="tag-link-count">
										(2993)</span></a>
                                
                                <a href="/list_118/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >开源<span class="tag-link-count">
										(2160)</span></a>
                                
                                <a href="/list_119/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >小程序<span class="tag-link-count">
										(2860)</span></a>
                                
                                <a href="/list_120/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >运维<span class="tag-link-count">
										(2533)</span></a>
                                
                                <a href="/list_121/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >服务器<span class="tag-link-count">
										(2698)</span></a>
                                
                                <a href="/list_122/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >操作系统<span class="tag-link-count">
										(2325)</span></a>
                                
                                <a href="/list_123/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >硬件开发<span class="tag-link-count">
										(2491)</span></a>
                                
                                <a href="/list_124/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >嵌入式<span class="tag-link-count">
										(2955)</span></a>
                                
                                <a href="/list_125/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >微软技术<span class="tag-link-count">
										(2769)</span></a>
                                
                                <a href="/list_126/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >软件工程<span class="tag-link-count">
										(2056)</span></a>
                                
                                <a href="/list_127/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >测试<span class="tag-link-count">
										(2865)</span></a>
                                
                                <a href="/list_128/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >网络空间安全<span class="tag-link-count">
										(2948)</span></a>
                                
                                <a href="/list_129/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >网络与通信<span class="tag-link-count">
										(2797)</span></a>
                                
                                <a href="/list_130/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >用户体验设计<span class="tag-link-count">
										(2592)</span></a>
                                
                                <a href="/list_131/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >学习和成长<span class="tag-link-count">
										(2593)</span></a>
                                
                                <a href="/list_132/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >搜索<span class="tag-link-count">
										(2744)</span></a>
                                
                                <a href="/list_133/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >开发工具<span class="tag-link-count">
										(7108)</span></a>
                                
                                <a href="/list_134/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >游戏<span class="tag-link-count">
										(2829)</span></a>
                                
                                <a href="/list_135/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >HarmonyOS<span class="tag-link-count">
										(2935)</span></a>
                                
                                <a href="/list_136/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >区块链<span class="tag-link-count">
										(2782)</span></a>
                                
                                <a href="/list_137/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >数学<span class="tag-link-count">
										(3112)</span></a>
                                
                                <a href="/list_138/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >3C硬件<span class="tag-link-count">
										(2759)</span></a>
                                
                                <a href="/list_139/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >资讯<span class="tag-link-count">
										(2909)</span></a>
                                
                                <a href="/list_140/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >Android<span class="tag-link-count">
										(4709)</span></a>
                                
                                <a href="/list_141/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >iOS<span class="tag-link-count">
										(1850)</span></a>
                                
                                <a href="/list_142/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >代码人生<span class="tag-link-count">
										(3043)</span></a>
                                
                                <a href="/list_143/" class="tag-cloud-link tag-link-279 tag-link-position-1"
                                   style="font-size: 8pt;"
                                >阅读<span class="tag-link-count">
										(2841)</span></a>
                                
                            </div>
                        </section>
                        <section id="b2-widget-hot-4" class="widget b2-widget-hot box b2-radius mobile-hidden">
                            <div class="b2-widget-title">
                                <h2 class="widget-title_hot">热门文章</h2>
                            </div>
                            <div class="b2-widget-box">
                                <ul class="b2-widget-list-ul">
                                </ul>
                            </div>
                        </section>
                        <div style="display:none;" class="scode">101</div>
                        <div style="display:none;" class="name">推荐</div>
                    </div>
                </div>
            </div>
        </aside>
    </div>
    <footer id="colophon" class="footer">
    <div class="site-footer-nav">
        <div class="wrapper">
            <div class="Box-v1-cmoUUM tBcFR">
                
                <span>
									<a href="/About-us/">关于我们</a>
								</span>
                

                
                <span>
									<a href="/Privacy-policy/">隐私政策</a>
								</span>
                

                
                <span>
									<a href="/Disclaimer/">免责声明</a>
								</span>
                
                
                <span>
									<a href="/Contact-us/">联系我们</a>
								</span>
                

            </div>
            <div class="footer-bottom">
                <div class="footer-bottom-left">
                    <div class="copyright">Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.</div>
                </div>
            </div>

        </div>
    </div>
    <div class="dmtop" style="bottom: 25px;">Scroll to Top</div>
</footer>
</div>
<script>
    $(document).ready(function () {
        jQuery(window).scroll(function () {
            if (jQuery(this).scrollTop() > 1) {
                jQuery('.dmtop').css({bottom: "25px"});
            } else {
                jQuery('.dmtop').css({bottom: "-100px"});
            }
        });
        jQuery('.dmtop').click(function () {
            jQuery('html, body').animate({scrollTop: '0px'}, 800);
            return false;
        });
    });
</script>
<style>
    .site-footer {
        background-color: #0a0a0a
    }

    .site-footer-nav {
        background-color: #fff;
    }

    #bigTriangleColor {
        background-color: #0a0a0a
    }
</style>
</body>
</html>
    <script src="https://cdn.staticfile.org/clipboard.js/2.0.4/clipboard.min.js"></script>
    <script>
        $(".copy_button").click(function () {
            var html = window.location.href;
            new ClipboardJS('.copy_button', {
                text: function (trigger, e) {
                    return html;
                }
            }).on('success', function (e) {
                $('.copytipl').replaceWith('<span class="sharetip bz copytipl">复制成功</span>');
                $('.copytipl').show()
                e.clearSelection();
            }).on('error', function (e) {
                $('.copytipl').replaceWith('<span class="sharetip bz copytipl">复制失败</span>');
            });
        })
        $(".copy_button").hover(function () {
            $('.copytipl').replaceWith('<span class="sharetip bz copytipl">复制链接</span>');
            $('.copytipl').show()
        }, function () {
            $('.copytipl').hide()
        })
        $(".copy_button2").hover(function () {
            $('.copytipl2').replaceWith('<span class="sharetip bz copytipl2">复制链接</span>');
            $('.copytipl2').show()
        }, function () {
            $('.copytipl2').hide()
        })
        $(".copy_button2").click(function () {
            var html = window.location.href;
            new ClipboardJS('.copy_button2', {
                text: function (trigger, e) {
                    return html;
                }
            }).on('success', function (e) {
                $('.copytipl2').replaceWith('<span class="sharetip bz copytipl2">复制成功</span>');
                $('.copytipl2').show()
                setTimeout(function () {
                    $('.copytipl2').hide()
                }, 1500)
                e.clearSelection();
            }).on('error', function (e) {
                $('.copytipl2').replaceWith('<span class="sharetip bz copytipl2">复制失败</span>');
            });
        })
    </script>
    <script type="text/javascript">
        var qrcode = new QRCode(document.getElementById("qrcode"), {
            width: 100,
            height: 100,
            text: window.location.href
        });
        var qrcode_two = new QRCode(document.getElementById("qrcode_two"), {
            width: 100,
            height: 100,
            text: window.location.href
        });
    </script>
    <script>
        //share facebook

        function fbShare(contentId) {

            u = document.getElementsByClassName("share_url")[0].content;

            t = document.getElementsByClassName("share_title")[0].content;

            d = document.getElementsByClassName("share_desc")[0].content;

            i = document.getElementsByClassName("share_img")[0].content;

            window.open("https://www.facebook.com/sharer/sharer.php?u=" + encodeURIComponent(u) + "&t=" + encodeURIComponent("\n\n" + t) + "&d=" + encodeURIComponent("\n\n" + d) + "&i=" + encodeURIComponent(i), "sharer", "toolbar=0,status=0,width=626,height=436");

            "sharer", "toolbar=0,status=0,width=626,height=436"

        }

        //share twitter

        function twitterShare(contentId) {

            u = document.getElementsByClassName("share_url")[0].content;

            t = document.getElementsByClassName("share_title")[0].content;

            d = document.getElementsByClassName("share_desc")[0].content;

            i = document.getElementsByClassName("share_img")[0].content;

            window.open("https://twitter.com/intent/tweet?url=" + encodeURIComponent(u) + "&t=" + encodeURIComponent("\n\n" + t) + "&d=" + encodeURIComponent("\n\n" + d) + "&i=" + encodeURIComponent(i), "sharer", "toolbar=0,status=0,width=626,height=436");

        }

        //share whatsapp

        function whatsApp(contentId) {

            u = document.getElementsByClassName("share_url")[0].content;

            t = document.getElementsByClassName("share_title")[0].content;

            d = document.getElementsByClassName("share_desc")[0].content;

            i = document.getElementsByClassName("share_img")[0].content;

            location = "https://web.whatsapp.com/send?text=" + encodeURIComponent(t) + "&u" + encodeURIComponent("\n\n" + u) + "&d=" + encodeURIComponent("\n\n" + d) + "&i=" + encodeURIComponent(i) + "&via=lopscoop";

        }
    </script>
    <script>
        $('.entry-content a').click(function () {
            // var hreF = $(this).attr('href');
            // window.open(hreF);//在新窗口打开
            $(this).attr("target", "_blank");
        })
        $(".code-block-extension-copyCodeBtn").click(function () {
            var html = $(this).parent().parent().siblings(".code-block-extension-codeShowNum").text();
            // console.log(html,'1')
            new ClipboardJS('.code-block-extension-copyCodeBtn', {
                text: function (trigger, e) {
                    return html;
                }
            }).on('success', function (e) {
                alert("复制成功!");
                e.clearSelection();
            }).on('error', function (e) {
                alert('Error!');
            });
        })
        var commentDanger = {
            alert: function (msg, type) {
                if (typeof (type) == "undefined") {
                    type = "danger";
                }
                var divElement = $("<div></div>").addClass('alert').addClass('alert-' + type).addClass('alert-dismissible').addClass('col-md-4').addClass('col-md-offset-4');
                divElement.css({
                    "position": "fixed",
                    "top": "80px",
                    "width": "100%",
                    "margin": "0 auto",
                    "left": "0",
                    "right": "0",
                    "z-index": "99999999999"
                });
                divElement.text(msg);
                var closeBtn = $('<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>');
                $(divElement).append(closeBtn);
                $('.content-area').before(divElement);
                return divElement;
            },

            message: function (msg, type) {
                var divElement = commentDanger.alert(msg, type);
                var isIn = false;

                divElement.on({
                    mouseover: function () {
                        isIn = true;
                    },
                    mouseout: function () {
                        isIn = false;
                    }
                });

                // After a short delay, it rises and disappears
                setTimeout(function () {
                    var IntervalMS = 20;
                    var floatSpace = 60;
                    var nowTop = divElement.offset().top;
                    var stopTop = nowTop - floatSpace;
                    divElement.fadeOut(IntervalMS * floatSpace);

                    var upFloat = setInterval(function () {
                        if (nowTop >= stopTop) {
                            divElement.css({"top": nowTop--});
                        } else {
                            clearInterval(upFloat);
                            divElement.remove();
                        }
                    }, IntervalMS);

                    if (isIn) {
                        clearInterval(upFloat);
                        divElement.stop();
                    }

                    divElement.hover(function () {
                        clearInterval(upFloat);
                        divElement.stop();
                    }, function () {
                        divElement.fadeOut(IntervalMS * (nowTop - stopTop));
                        upFloat = setInterval(function () {
                            if (nowTop >= stopTop) {
                                divElement.css({"top": nowTop--});
                            } else {
                                clearInterval(upFloat);
                                divElement.remove();
                            }
                        }, IntervalMS);
                    });
                }, 2500);
            }
        }

        var commentSuccess = {
            alert: function (msg, type) {
                if (typeof (type) == "undefined") {
                    type = "success";
                }
                var divElement = $("<div></div>").addClass('alert').addClass('alert-' + type).addClass('alert-dismissible').addClass('col-md-4').addClass('col-md-offset-4');
                divElement.css({
                    "position": "fixed",
                    "top": "80px",
                    "width": "100%",
                    "margin": "0 auto",
                    "left": "0",
                    "right": "0",
                    "z-index": "99999999999"
                });
                divElement.text(msg);
                var closeBtn = $('<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>');
                $(divElement).append(closeBtn);
                $('.content-area').before(divElement);
                return divElement;
            },

            message: function (msg, type) {
                var divElement = commentSuccess.alert(msg, type);
                var isIn = false;

                divElement.on({
                    mouseover: function () {
                        isIn = true;
                    },
                    mouseout: function () {
                        isIn = false;
                    }
                });

                // After a short delay, it rises and disappears
                setTimeout(function () {
                    var IntervalMS = 20;
                    var floatSpace = 60;
                    var nowTop = divElement.offset().top;
                    var stopTop = nowTop - floatSpace;
                    divElement.fadeOut(IntervalMS * floatSpace);

                    var upFloat = setInterval(function () {
                        if (nowTop >= stopTop) {
                            divElement.css({"top": nowTop--});
                        } else {
                            clearInterval(upFloat);
                            divElement.remove();
                        }
                    }, IntervalMS);

                    if (isIn) {
                        clearInterval(upFloat);
                        divElement.stop();
                    }

                    divElement.hover(function () {
                        clearInterval(upFloat);
                        divElement.stop();
                    }, function () {
                        divElement.fadeOut(IntervalMS * (nowTop - stopTop));
                        upFloat = setInterval(function () {
                            if (nowTop >= stopTop) {
                                divElement.css({"top": nowTop--});
                            } else {
                                clearInterval(upFloat);
                                divElement.remove();
                            }
                        }, IntervalMS);
                    });
                }, 2500);
            }
        }
        var id = $('.likebox').data('id');
        if ($.cookie('likes_' + id)) {
            $('.likebox').find(".b2font").replaceWith('<i class="b2font b2-heart-fill red"></i>');
        }
        $('.likebox').click(function () {
            var id = $(this).data('id');
            if ($.cookie('likes_' + id)) {
                commentDanger.message("您已点过赞了,请勿重复赞!");
            } else {
                $(this).find('#support_number').text(parseInt($(this).find('#support_number').text()) + 1);
                // $(this).find(".b2font").addClass('red');
                $(this).find(".b2font").replaceWith('<i class="b2font b2-heart-fill red"></i>');
                var likeslink = $(this).data('likeslink');
                var likes = parseInt($(this).find('.likes').text())
                $.ajax({
                    url: likeslink,
                    data: {
                        'likes': likes
                    },
                    success: function (response, status) {
                        commentSuccess.message("点赞!");
                        window.location.reload();
                        $(this).find(".b2font").replaceAll('<i class="b2font b2-heart-fill red"></i>');
                    },
                    error: function (xhr, status, error) {
                        alert('返回数据异常!');
                    }
                });
            }
            ;
        })


        function updateimg() {

            $('#codeimg').attr('src', '/core/code.php?' + Math.round(Math.random() * 10));
        }


        //评论回复弹框
        $('.replybtn').on("click", function () {
            var url = $(this).data("action");
            $("#reply").modal("show");
            $("#replyform").data("action", url);
        });

        //提交评论
        function submitcomment(obj) {
            var url = $(obj).data("action");
            //console.log(url);
            var comment = $(obj).find("#comment").val();
            //alert(comment.length);
            var checkcode = $(obj).find("#checkcode").val();
            //alert(checkcode);
            //console.log('2');
            if (comment.length > 0 || comment.length > 3) {
                $.ajax({
                    type: 'POST',
                    url: url,
                    date: new Date(),
                    cache: true,
                    async: true,
                    dataType: 'json',
                    data: {
                        comment: comment,
                        checkcode: checkcode
                    },
                    success: function (response, status) {
                        if (response.code) {
                            // alert(response.data);
                            commentSuccess.message(response.data);
                            $(obj)[0].reset();
                            $(".modal").modal("hide");
                        } else {
                            commentDanger.message(response.data);
                            if (response.tourl != "") {
                                if (confirm(response.data + '是否立即跳转登录?')) {
                                    location.href = response.tourl;
                                }
                            }
                            $('.codeimg').click(); //更新验证码
                        }
                    },
                    error: function (xhr, status, error) {
                        alert('返回数据异常!');
                    }
                });
            } else {
                alert('請輸入數據!');
            }
            return false;
        }
    </script>

    <script>
        var Page = 1;
        var load = false;
        var scode = jQuery(".scode").text();
        var sortname = jQuery(".name").text();
        var Dom = jQuery('.related-posts-in');
        var List = jQuery('.b2-widget-list-ul');
        Page = parseInt(Page);
        fs();
        hot_list();

        function fs() {
            var Num = 4;
            var url = '/api.php/list/' + scode + '/page/' + Page + '/num/' + Num;
            load = true;
            jQuery.ajax({
                type: 'POST',
                url: url,
                cache: true,
                async: true,
                dataType: 'json',
                data: {
                    appid: 'admin',
                    timestamp: '1760989291',
                    signature: '50c06d72137e7aab4617a95af790d035',
                },
                success: function (response, status) {
                    var Data = response.data;
                    if (response.code) {
                        jQuery.each(Data, function (index, value,) {
                            if (value.ico == null || value.ico == "") {
                                var Html = '<div class="related-posts-item">\n' +
                                    '                            <div>\n' +
                                    '                                <div class="related-post-thumb">\n' +
                                    '                                    <a href="' + value.contentlink + '" class="link-block lazy_back"></a>\n' +
                                    '                                    <span class="cat"><a href="' + value.sortlink + '">' + value.sortname + '</a></span><span class="img_title"><a\n' +
                                    '                                            href="' + value.contentlink + '" title="' + value.title + '">' + value.title + '</a></span>\n' +
                                    '                                </div>\n' +
                                    '                                <a href="' + value.contentlink + '"><h2 title="' + value.title + '">' + value.title + '</h2></a>\n' +
                                    '                                <div class="post-excerpt" style="margin:0 0 5px 0">\n' +
                                    '                                    <a href="' + value.contentlink + '" style="color: #797C80 !important;" title="' + value.description + '">' + value.description + '</a>\n' +
                                    '                                </div>\n' +
                                    '                                <div class="realte-post-meta">\n' +
                                    '                                    <span>' + value.date.slice(0, 11) + '</span>\n' +
                                    '                                    <span><i class="b2font b2-heart-fill "></i>' + value.likes + '</span>\n' +
                                    '                                    <span><i class="b2font b2-eye-fill "></i>' + value.visits + '</span>\n' +
                                    '                                </div>\n' +
                                    '                            </div>\n' +
                                    '                        </div>';
                            } else {
                                var Html = '<div class="related-posts-item">\n' +
                                    '                            <div>\n' +
                                    '                                <div class="related-post-thumb">\n' +
                                    '                                    <a href="' + value.contentlink + '" class="link-block lazy_back"><img src="' + value.ico + '"></a>\n' +
                                    '                                    <span class="cat"><a href="' + value.sortlink + '">' + value.sortname + '</a></span>\n' +
                                    '                                </div>\n' +
                                    '                                <a href="' + value.contentlink + '"><h2 title="' + value.title + '">' + value.title + '</h2></a>\n' +
                                    '                                <div class="post-excerpt" style="margin:0 0 5px 0">\n' +
                                    '                                    <a href="' + value.contentlink + '" style="color: #797C80 !important;" title="' + value.description + '">' + value.description + '</a>\n' +
                                    '                                </div>\n' +
                                    '                                <div class="realte-post-meta">\n' +
                                    '                                    <span>' + value.date.slice(0, 11) + '</span>\n' +
                                    '                                    <span><i class="b2font b2-heart-fill "></i>' + value.likes + '</span>\n' +
                                    '                                    <span><i class="b2font b2-eye-fill "></i>' + value.visits + '</span>\n' +
                                    '                                </div>\n' +
                                    '                            </div>\n' +
                                    '                        </div>';
                            }

                            Dom.append(Html);
                        });
                        load = false;
                        return Page += 1;
                    } else {
                        $('.related-posts').hide();
                    }
                },
                error: function (xhr, status, error) {
                    console.log(error);
                }
            })
        }

        function hot_list() {
            var listNum = [];
            var number = 0;
            var nums = 1;
            var Num = 10;
            var url = '/api.php/list/' + scode + '/page/' + Page + '/num/' + Num;
            load = true;
            jQuery.ajax({
                type: 'POST',
                url: url,
                cache: true,
                async: true,
                dataType: 'json',
                data: {
                    appid: 'admin',
                    timestamp: '1760989291',
                    signature: '50c06d72137e7aab4617a95af790d035',
                },
                success: function (response, status) {
                    var Data = response.data;
                    for (let i in Data) {
                        listNum.push(Data[i])
                    }
                    var scrollS = [];
                    var su = number + 10;
                    for (var jj = number; jj < su; jj++) {
                        scrollS.push(listNum[jj]);
                        number = number + 1;
                    }
                    if (response.code) {
                        scrollS.forEach(function (item, index) {
                            var Html = '<li class="b2-widget-box widget-post widget-post-none">\n' +
                                '                                        <div class="b2-widget-post-order widget-order-'+nums+'"><span\n' +
                                '                                                class="b2-radius">'+nums+'</span></div>\n' +
                                '                                        <div class="b2-widget-post-title">\n' +
                                '                                            <a href="' + item['contentlink'] +'"><h2 title="' + item['title'] +'">' + item['title'] +'</h2></a>\n' +
                                '                                        </div>\n' +
                                '                                    </li>';
                            List.append(Html);
                            nums++;
                        })
                        // });
                        load = false;
                        return Page += 1;
                    } else {
                        $('#b2-widget-hot-4').hide();
                    }
                },
                error: function (xhr, status, error) {
                    console.log(error);
                }
            })
        }
    </script>