Pod详解

Pod介绍

Pod结构

Pod详解_第1张图片

每个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

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	
 
  

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

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

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

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

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

status              状态信息,里面的内容不需要定义,由kubernetes自动生成
 
  

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

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

restartPolicy	       重启策略,表示Pod在遇到故障的时候的处理策略

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       	 	资源限制和资源请求的设置
 
  

基本配置

创建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

上面定义了一个比较简单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

镜像拉取

创建 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

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

启动命令

在前面的案例中,一直有一个问题没有解决,就是的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;" ]    

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

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

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

这其实跟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"

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

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

端口设置

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

首先看下ports支持的子选项:
Pod详解_第2张图片

[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"。

创建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

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
...

访问容器中的程序需要使用的是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><<span class="token operator">/</span>title>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</code></pre> 
  <h3>资源配额</h3> 
  <blockquote> 
   <p>容器中的程序要运行,肯定是要占用一定资源的,比如cpu和内存等,如果不对某个容器的资源做限制,那么它就可能吃掉大量资源,导致其它容器无法运行。</p> 
  </blockquote> 
  <blockquote> 
   <p>针对这种情况,kubernetes提供了对内存和cpu的资源进行配额的机制,这种机制主要通过<code>resources</code>选项实现,他有两个子选项:</p> 
   <pre><code>limits: 用于限制运行时容器的最大占用资源,当容器占用资源超过limits时会被终止,并进行重启

requests: 用于设置容器需要的最小资源,如果环境资源不够,容器将无法启动
</code></pre> 
   <p>可以通过上面两个选项设置资源的上下限。</p> 
  </blockquote> 
  <blockquote> 
   <p><strong>创建pod-resources.yaml文件,内容如下:</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell">apiVersion: v1
kind: Pod
metadata:
  name: pod<span class="token operator">-</span>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>
</code></pre> 
  <blockquote> 
   <p>在这对cpu和memory的单位做一个说明:</p> 
   <ul> 
    <li>cpu:core数,可以为整数或小数</li> 
    <li>memory: 内存大小,可以使用Gi、Mi、G、M等形式</li> 
   </ul> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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<span class="token operator">/</span>pod<span class="token operator">-</span>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<span class="token operator">-</span>resources   1<span class="token operator">/</span>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<span class="token operator">/</span>pod<span class="token operator">-</span>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<span class="token operator">-</span>resources   0<span class="token operator">/</span>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>
  Warning  FailedScheduling  46s <span class="token punctuation">(</span>x2 over 48s<span class="token punctuation">)</span>  default<span class="token operator">-</span>scheduler  0<span class="token operator">/</span>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<span class="token operator">-</span>role<span class="token punctuation">.</span>kubernetes<span class="token punctuation">.</span>io<span class="token operator">/</span>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>
</code></pre> 
  <h2>Pod生命周期</h2> 
  <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> <a href="http://img.e-com-net.com/image/info8/295bd22f2b634c1f9c9b81088ef40763.jpg" target="_blank"><img src="http://img.e-com-net.com/image/info8/295bd22f2b634c1f9c9b81088ef40763.jpg" alt="Pod详解_第3张图片" width="650" height="382" style="border:1px solid black;"></a></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> 
  <h3>创建和终止</h3> 
  <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> <a href="http://img.e-com-net.com/image/info8/9b3213561b9b4618aa1c183b5d86e1d8.jpg" target="_blank"><img src="http://img.e-com-net.com/image/info8/9b3213561b9b4618aa1c183b5d86e1d8.jpg" alt="Pod详解_第4张图片" width="650" height="355" style="border:1px solid black;"></a></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中还存在仍在运行的进程,那么pod对象会收到立即终止的信号</p> </li> 
    <li> <p>kubelet请求apiServer将此pod资源的宽限期设置为0从而完成删除操作,此时pod对于用户已不可见</p> </li> 
   </ol> 
  </blockquote> 
  <h3>初始化容器</h3> 
  <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><code>假设要以主容器来运行nginx,但是要求在运行nginx之前先要能够连接上mysql和redis所在服务器

为了简化测试,事先规定好mysql(192.168.109.201)和redis(192.168.109.202)服务器的地址
</code></pre> 
  </blockquote> 
  <blockquote> 
   <p><strong>创建pod-initcontainer.yaml文件,内容如下:</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell">apiVersion: v1
kind: Pod
metadata:
  name: myapp<span class="token operator">-</span>pod
  labels:
    app: myapp
spec:
  containers:
  <span class="token operator">-</span> name: myapp<span class="token operator">-</span>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<span class="token operator">-</span>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<span class="token operator">-</span>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>
</code></pre> 
  <blockquote> 
   <p><strong>创建service.yaml文件,内容如下:</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell">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
</code></pre> 
  <pre><code class="prism language-powershell"><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<span class="token operator">/</span>pod<span class="token operator">-</span>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<span class="token operator">-</span>pod   0<span class="token operator">/</span>1     Init:0<span class="token operator">/</span>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>
  Normal  Scheduled  36s   default<span class="token operator">-</span>scheduler  Successfully assigned default<span class="token operator">/</span>myapp<span class="token operator">-</span>pod to k8s<span class="token operator">-</span>node<span class="token operator">-</span>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<span class="token operator">-</span>myservice
  Normal  Started    2s    kubelet            Started container init<span class="token operator">-</span>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<span class="token operator">/</span>myservice created
service<span class="token operator">/</span>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<span class="token operator">-</span>pod   0<span class="token operator">/</span>1     Init:0<span class="token operator">/</span>2   0          9m33s
myapp<span class="token operator">-</span>pod   0<span class="token operator">/</span>1     Init:1<span class="token operator">/</span>2   0          10m
myapp<span class="token operator">-</span>pod   0<span class="token operator">/</span>1     Init:1<span class="token operator">/</span>2   0          10m
myapp<span class="token operator">-</span>pod   0<span class="token operator">/</span>1     PodInitializing   0          10m
myapp<span class="token operator">-</span>pod   1<span class="token operator">/</span>1     Running           0          10m
</code></pre> 
  <h3>钩子函数</h3> 
  <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><code class="prism language-powershell"><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<span class="token operator">/</span>healthy
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</code></pre> 
  <blockquote> 
   <p><strong>TCPSocket:在当前容器尝试访问指定的socket</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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>
</code></pre> 
  <blockquote> 
   <p><strong>HTTPGet:在当前容器中向某url发起http请求</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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>
</code></pre> 
  <blockquote> 
   <h4>接下来,以exec方式为例,演示下钩子函数的使用</h4> 
  </blockquote> 
  <blockquote> 
   <p><strong>创建 pod-hook-exec.yaml文件</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell">apiVersion: v1
kind: Pod
metadata:
  name: pod<span class="token operator">-</span>hook<span class="token operator">-</span>exec
  namespace: dev
spec:
  containers:
    <span class="token operator">-</span> name: main<span class="token operator">-</span>container
      image: nginx:1<span class="token punctuation">.</span>17<span class="token punctuation">.</span>2
      ports:
        <span class="token operator">-</span> name: nginx<span class="token operator">-</span>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>
</code></pre> 
  <blockquote> 
   <p><strong>创建pod,观察效果</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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<span class="token operator">/</span>pod<span class="token operator">-</span>hook<span class="token operator">-</span>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<span class="token operator">-</span>hook<span class="token operator">-</span>exec   1<span class="token operator">/</span>1     Running   0          32s   10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>2   k8s<span class="token operator">-</span>node<span class="token operator">-</span>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>
</code></pre> 
  <h3>容器探测</h3> 
  <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><code class="prism language-powershell"><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<span class="token operator">/</span>healthy
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</code></pre> 
  <blockquote> 
   <p><strong>TCPSocket:将会尝试访问一个用户容器的端口,如果能够建立这条连接,则认为程序正常,否则不正常</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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>
</code></pre> 
  <blockquote> 
   <p><strong>HTTPGet:调用容器内Web应用的URL,如果返回的状态码在200和399之间,则认为程序正常,否则不正常</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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>
</code></pre> 
  <blockquote> 
   <h4>下面以livenessProbes为例,做几个演示:</h4> 
  </blockquote> 
  <p><strong>方式一:Exec</strong></p> 
  <blockquote> 
   <p><strong>创建 pod-liveness-exec.yaml</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell">apiVersion: v1
kind: Pod
metadata:
  name: pod<span class="token operator">-</span>liveness<span class="token operator">-</span>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<span class="token operator">-</span>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>
</code></pre> 
  <blockquote> 
   <p><strong>创建pod,观察效果</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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<span class="token operator">/</span>pod<span class="token operator">-</span>liveness<span class="token operator">-</span>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>
  Normal   Scheduled  2m47s                default<span class="token operator">-</span>scheduler  Successfully assigned dev<span class="token operator">/</span>pod<span class="token operator">-</span>liveness<span class="token operator">-</span>exec to k8s<span class="token operator">-</span>node<span class="token operator">-</span>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 operator">/</span><span class="token function">cat</span>: <span class="token operator">/</span>tmp<span class="token operator">/</span>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<span class="token operator">-</span>liveness<span class="token operator">-</span>exec   1<span class="token operator">/</span>1     Running   2          3m33s

<span class="token comment"># 当然接下来,可以修改成一个存在的文件,比如/etc/hosts,再试,结果就正常了......</span>
</code></pre> 
  <p><strong>方式二:TCPSocket</strong></p> 
  <blockquote> 
   <p><strong>创建 pod-liveness-tcpsocket.yaml</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell">apiVersion: v1
kind: Pod
metadata:
  name: pod<span class="token operator">-</span>liveness<span class="token operator">-</span>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<span class="token operator">-</span>port
      containerPort: 80
    livenessProbe:
      tcpSocket:
        port: 8080 <span class="token comment"># 尝试访问8080端口</span>
</code></pre> 
  <blockquote> 
   <p><strong>创建pod,观察效果</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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>
  Normal   Scheduled  61s               default<span class="token operator">-</span>scheduler  Successfully assigned dev<span class="token operator">/</span>pod<span class="token operator">-</span>liveness<span class="token operator">-</span>tcpsocket to k8s<span class="token operator">-</span>node<span class="token operator">-</span>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<span class="token operator">-</span>liveness<span class="token operator">-</span>tcpsocket   0<span class="token operator">/</span>1     CrashLoopBackOff   4          2m34s

<span class="token comment"># 当然接下来,可以修改成一个可以访问的端口,比如80,再试,结果就正常了......</span>
</code></pre> 
  <p><strong>方式三:HTTPGet</strong></p> 
  <blockquote> 
   <p><strong>创建 pod-liveness-httpget.yaml</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell">apiVersion: v1
kind: Pod
metadata:
  name: pod<span class="token operator">-</span>liveness<span class="token operator">-</span>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<span class="token operator">-</span>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>
</code></pre> 
  <blockquote> 
   <p><strong>创建pod,观察效果</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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<span class="token operator">/</span>pod<span class="token operator">-</span>liveness<span class="token operator">-</span>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>
  Normal   Scheduled  31s               default<span class="token operator">-</span>scheduler  Successfully assigned dev<span class="token operator">/</span>pod<span class="token operator">-</span>liveness<span class="token operator">-</span>httpget to k8s<span class="token operator">-</span>node<span class="token operator">-</span>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<span class="token operator">-</span>liveness<span class="token operator">-</span>httpget   1<span class="token operator">/</span>1     Running   5          3m30s

<span class="token comment"># 当然接下来,可以修改成一个可以访问的路径path,比如/,再试,结果就正常了......</span>
</code></pre> 
  <p><strong>livenessProbe其他配置</strong></p> 
  <blockquote> 
   <p>至此,已经使用livenessProbe演示了三种探测方式,但是查看livenessProbe的子属性,会发现除了这三种方式,还有一些其他的配置,在这里一并解释下:</p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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>
</code></pre> 
  <blockquote> 
   <p>下面稍微配置两个,演示下效果即可:</p> 
  </blockquote> 
  <pre><code class="prism language-powershell">apiVersion: v1
kind: Pod
metadata:
  name: pod<span class="token operator">-</span>liveness<span class="token operator">-</span>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<span class="token operator">-</span>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>
</code></pre> 
  <pre><code class="prism language-powershell"><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<span class="token operator">/</span>pod<span class="token operator">-</span>liveness<span class="token operator">-</span>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<span class="token operator">-</span>pullable:<span class="token operator">/</span><span class="token operator">/</span>nginx@sha256:5411d8897c3da841a1f45f895b43ad4526eb62d3393c3287124a56be49962d41
    Port:           80<span class="token operator">/</span>TCP
    Host Port:      0<span class="token operator">/</span>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<span class="token operator">-</span>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<span class="token operator">/</span>secrets<span class="token operator">/</span>kubernetes<span class="token punctuation">.</span>io<span class="token operator">/</span>serviceaccount <span class="token keyword">from</span> kube<span class="token operator">-</span>api<span class="token operator">-</span>access<span class="token operator">-</span>k9zh5 <span class="token punctuation">(</span>ro<span class="token punctuation">)</span>
</code></pre> 
  <h3>重启策略</h3> 
  <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><code class="prism language-powershell">apiVersion: v1
kind: Pod
metadata:
  name: pod<span class="token operator">-</span>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<span class="token operator">-</span>port
      containerPort: 80
    livenessProbe:
      httpGet:
        scheme: HTTP
        port: 80
        path: <span class="token operator">/</span>hello
  restartPolicy: Never		 <span class="token comment"># 设置重启策略为Never</span>
</code></pre> 
  <blockquote> 
   <p><strong>运行Pod测试</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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<span class="token operator">/</span>pod<span class="token operator">-</span>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>
  Normal   Scheduled  63s                default<span class="token operator">-</span>scheduler  Successfully assigned dev<span class="token operator">/</span>pod<span class="token operator">-</span>restartpolicy to k8s<span class="token operator">-</span>node<span class="token operator">-</span>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<span class="token operator">-</span>restartpolicy   0<span class="token operator">/</span>1     Completed   0          4m14s
</code></pre> 
  <h2>Pod调度</h2> 
  <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> 
  <h3>定向调度</h3> 
  <blockquote> 
   <p><strong>定向调度,指的是利用在pod上声明<code>nodeName</code>或者<code>nodeSelector</code>,以此将Pod调度到期望的node节点上。</strong><br> 注意,这里的调度是强制的,这就意味着即使要调度的目标Node不存在,也会向上面进行调度,只不过pod运行失败而已。</p> 
  </blockquote> 
  <blockquote> 
   <h4>NodeName</h4> 
   <ul> 
    <li>NodeName用于强制约束将Pod调度到指定的Name的Node节点上。这种方式,其实是直接跳过Scheduler的调度逻辑,直接将Pod调度到指定名称的节点。</li> 
   </ul> 
  </blockquote> 
  <blockquote> 
   <p><strong>创建一个pod-nodename.yaml文件</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell">apiVersion: v1
kind: Pod
metadata:
  name: pod<span class="token operator">-</span>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<span class="token operator">-</span>node<span class="token operator">-</span>01  	 <span class="token comment"># 指定调度到k8s-node-01节点上</span>
</code></pre> 
  <blockquote> 
   <p><strong>创建pod,观察效果</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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<span class="token operator">/</span>pod<span class="token operator">-</span>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<span class="token operator">-</span>nodename   1<span class="token operator">/</span>1     Running   0          5s    10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>2   k8s<span class="token operator">-</span>node<span class="token operator">-</span>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<span class="token operator">/</span>pod<span class="token operator">-</span>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<span class="token operator">-</span>nodename   0<span class="token operator">/</span>1     Pending   0          8s    <none>   k8s<span class="token operator">-</span>node<span class="token operator">-</span>03   <none>           <none>
</code></pre> 
  <blockquote> 
   <h4>NodeSelector</h4> 
   <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><code class="prism language-powershell"><span class="token namespace">[root@k8s-master-01 ~]</span><span class="token comment"># kubectl label nodes k8s-node-01 nodeenv=pro</span>
node<span class="token operator">/</span>k8s<span class="token operator">-</span>node<span class="token operator">-</span>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<span class="token operator">/</span>k8s<span class="token operator">-</span>node<span class="token operator">-</span>02 labeled
</code></pre> 
  <blockquote> 
   <p><strong>创建一个pod-nodeselector.yaml文件</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell">apiVersion: v1
kind: Pod
metadata:
  name: pod<span class="token operator">-</span>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>
</code></pre> 
  <blockquote> 
   <p><strong>创建pod,观察效果</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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<span class="token operator">/</span>pod<span class="token operator">-</span>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<span class="token operator">-</span>nodeselector   1<span class="token operator">/</span>1     Running   0          6s    10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>2   k8s<span class="token operator">-</span>node<span class="token operator">-</span>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<span class="token operator">/</span>pod<span class="token operator">-</span>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<span class="token operator">-</span>nodeselector            0<span class="token operator">/</span>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>
  Warning  FailedScheduling  21s <span class="token punctuation">(</span>x2 over 22s<span class="token punctuation">)</span>  default<span class="token operator">-</span>scheduler  0<span class="token operator">/</span>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<span class="token operator">-</span>role<span class="token punctuation">.</span>kubernetes<span class="token punctuation">.</span>io<span class="token operator">/</span>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<span class="token operator">/</span>selector<span class="token punctuation">.</span>
</code></pre> 
  <h3>亲和性调度</h3> 
  <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> 
   <h4>NodeAffinity</h4> 
   <p>首先来看一下 <code>NodeAffinity</code> 的可配置项:</p> 
  </blockquote> 
  <pre><code class="prism language-powershell">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<span class="token operator">-</span>100。
</code></pre> 
  <pre><code class="prism language-powershell">关系符的使用说明:

<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>
</code></pre> 
  <blockquote> 
   <p>接下来首先演示一下 <code>requiredDuringSchedulingIgnoredDuringExecution</code></p> 
  </blockquote> 
  <blockquote> 
   <p><strong>创建pod-nodeaffinity-required.yaml文件</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell">apiVersion: v1
kind: Pod
metadata:
  name: pod<span class="token operator">-</span>nodeaffinity<span class="token operator">-</span>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>
</code></pre> 
  <blockquote> 
   <p><strong>创建pod,观察效果</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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<span class="token operator">/</span>pod<span class="token operator">-</span>nodeaffinity<span class="token operator">-</span>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<span class="token operator">-</span>nodeaffinity<span class="token operator">-</span>required   0<span class="token operator">/</span>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>
  Warning  FailedScheduling  27s <span class="token punctuation">(</span>x2 over 28s<span class="token punctuation">)</span>  default<span class="token operator">-</span>scheduler  0<span class="token operator">/</span>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<span class="token operator">-</span>role<span class="token punctuation">.</span>kubernetes<span class="token punctuation">.</span>io<span class="token operator">/</span>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<span class="token operator">/</span>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<span class="token operator">/</span>pod<span class="token operator">-</span>nodeaffinity<span class="token operator">-</span>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<span class="token operator">-</span>nodeaffinity<span class="token operator">-</span>required   1<span class="token operator">/</span>1     Running   0          6s    10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>3   k8s<span class="token operator">-</span>node<span class="token operator">-</span>01   <none>           <none>
</code></pre> 
  <blockquote> 
   <p>接下来再演示一下 <code>requiredDuringSchedulingIgnoredDuringExecution</code></p> 
  </blockquote> 
  <blockquote> 
   <p><strong>创建pod-nodeaffinity-preferred.yaml</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell">apiVersion: v1
kind: Pod
metadata:
  name: pod<span class="token operator">-</span>nodeaffinity<span class="token operator">-</span>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>
</code></pre> 
  <blockquote> 
   <p><strong>创建pod,观察效果</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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<span class="token operator">/</span>pod<span class="token operator">-</span>nodeaffinity<span class="token operator">-</span>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<span class="token operator">-</span>nodeaffinity<span class="token operator">-</span>preferred   1<span class="token operator">/</span>1     Running   0          5s
</code></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> 
   <h4>PodAffinity</h4> 
   <p>PodAffinity主要实现以运行的Pod为参照,实现让新创建的Pod跟参照pod在一个区域的功能。</p> 
   <p>首先来看一下<code>PodAffinity</code>的可配置项:</p> 
  </blockquote> 
  <pre><code class="prism language-powershell">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<span class="token operator">-</span>100
</code></pre> 
  <pre><code class="prism language-powershell">topologyKey用于指定调度时作用域<span class="token punctuation">,</span>例如:
    如果指定为kubernetes<span class="token punctuation">.</span>io<span class="token operator">/</span>hostname,那就是以Node节点为区分范围
	如果指定为beta<span class="token punctuation">.</span>kubernetes<span class="token punctuation">.</span>io<span class="token operator">/</span>os<span class="token punctuation">,</span>则以Node节点的操作系统类型来区分
</code></pre> 
  <blockquote> 
   <p>接下来,演示下<code>requiredDuringSchedulingIgnoredDuringExecution</code></p> 
  </blockquote> 
  <blockquote> 
   <p><strong>首先创建一个参照Pod,pod-podaffinity-target.yaml:</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell">apiVersion: v1
kind: Pod
metadata:
  name: pod<span class="token operator">-</span>podaffinity<span class="token operator">-</span>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<span class="token operator">-</span>node<span class="token operator">-</span>01 <span class="token comment"># 将目标pod名确指定到node1上</span>
</code></pre> 
  <pre><code class="prism language-powershell"><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<span class="token operator">/</span>pod<span class="token operator">-</span>podaffinity<span class="token operator">-</span>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<span class="token operator">-</span>podaffinity<span class="token operator">-</span>target   1<span class="token operator">/</span>1     Running   0          11s
</code></pre> 
  <blockquote> 
   <p><strong>创建pod-podaffinity-required.yaml,内容如下:</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell">apiVersion: v1
kind: Pod
metadata:
  name: pod<span class="token operator">-</span>podaffinity<span class="token operator">-</span>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<span class="token operator">/</span>hostname
</code></pre> 
  <blockquote> 
   <p>上面配置表达的意思是:新Pod必须要与拥有标签nodeenv=xxx或者nodeenv=yyy的pod在同一Node上,显然现在没有这样pod,接下来,运行测试一下。</p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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<span class="token operator">/</span>pod<span class="token operator">-</span>podaffinity<span class="token operator">-</span>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<span class="token operator">-</span>podaffinity<span class="token operator">-</span>required   0<span class="token operator">/</span>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>
  Warning  FailedScheduling  37s <span class="token punctuation">(</span>x3 over 118s<span class="token punctuation">)</span>  default<span class="token operator">-</span>scheduler  0<span class="token operator">/</span>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<span class="token operator">-</span>role<span class="token punctuation">.</span>kubernetes<span class="token punctuation">.</span>io<span class="token operator">/</span>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<span class="token operator">/</span>anti<span class="token operator">-</span>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<span class="token operator">/</span>pod<span class="token operator">-</span>podaffinity<span class="token operator">-</span>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<span class="token operator">-</span>podaffinity<span class="token operator">-</span>required   1<span class="token operator">/</span>1     Running   0          6s
</code></pre> 
  <blockquote> 
   <p>关于PodAffinity的 preferredDuringSchedulingIgnoredDuringExecution,这里不再演示。</p> 
  </blockquote> 
  <blockquote> 
   <h4>PodAntiAffinity</h4> 
   <p>PodAntiAffinity主要实现以运行的Pod为参照,让新创建的Pod跟参照pod不在一个区域中的功能。</p> 
   <p>它的配置方式和选项跟PodAffinty是一样的,这里不再做详细解释,直接做一个测试案例。</p> 
  </blockquote> 
  <blockquote> 
   <p><strong>继续使用上个案例中目标pod</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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<span class="token operator">-</span>podaffinity<span class="token operator">-</span>required   1<span class="token operator">/</span>1     Running   0          4m36s   10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>7   k8s<span class="token operator">-</span>node<span class="token operator">-</span>01   <none>           <none>            <none>
pod<span class="token operator">-</span>podaffinity<span class="token operator">-</span>target     1<span class="token operator">/</span>1     Running   0          27m     10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>6   k8s<span class="token operator">-</span>node<span class="token operator">-</span>01   <none>           <none>            podenv=pro
</code></pre> 
  <blockquote> 
   <p><strong>创建pod-podantiaffinity-required.yaml,内容如下:</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell">apiVersion: v1
kind: Pod
metadata:
  name: pod<span class="token operator">-</span>podantiaffinity<span class="token operator">-</span>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<span class="token operator">/</span>hostname
</code></pre> 
  <blockquote> 
   <p>上面配置表达的意思是:新Pod必须要与拥有标签nodeenv=pro的pod不在同一Node上,运行测试一下。</p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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<span class="token operator">/</span>pod<span class="token operator">-</span>podantiaffinity<span class="token operator">-</span>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<span class="token operator">-</span>podantiaffinity<span class="token operator">-</span>required   1<span class="token operator">/</span>1     Running   0          5m28s   10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>2<span class="token punctuation">.</span>2   k8s<span class="token operator">-</span>node<span class="token operator">-</span>02   <none>           <none>
</code></pre> 
  <h3>污点和容忍</h3> 
  <blockquote> 
   <h4>污点(Taints)</h4> 
   <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> <a href="http://img.e-com-net.com/image/info8/8de419546a8a45d59e4c1f67688e24cf.jpg" target="_blank"><img src="http://img.e-com-net.com/image/info8/8de419546a8a45d59e4c1f67688e24cf.jpg" alt="Pod详解_第5张图片" width="650" height="138" style="border:1px solid black;"></a></p> </li> 
   </ul> 
  </blockquote> 
  <blockquote> 
   <p>使用kubectl设置和去除污点的命令示例如下:</p> 
   <pre><code># 设置污点
kubectl taint nodes node1 key=value:effect

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

# 去除所有污点 
kubectl taint nodes node1 key-
</code></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><code class="prism language-powershell"><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<span class="token operator">/</span>k8s<span class="token operator">-</span>node<span class="token operator">-</span>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<span class="token operator">/</span>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<span class="token operator">/</span>1     Running   0          6s    10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>2   k8s<span class="token operator">-</span>node<span class="token operator">-</span>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<span class="token operator">/</span>k8s<span class="token operator">-</span>node<span class="token operator">-</span>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<span class="token operator">/</span>k8s<span class="token operator">-</span>node<span class="token operator">-</span>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<span class="token operator">/</span>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<span class="token operator">/</span>1     Running   0          41s   10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>2   k8s<span class="token operator">-</span>node<span class="token operator">-</span>01   <none>           <none>
taint2   0<span class="token operator">/</span>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<span class="token operator">/</span>k8s<span class="token operator">-</span>node<span class="token operator">-</span>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<span class="token operator">/</span>k8s<span class="token operator">-</span>node<span class="token operator">-</span>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<span class="token operator">/</span>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<span class="token operator">/</span>1     Pending   0          7s    <none>   <none>   <none>           <none>
</code></pre> 
  <blockquote> 
   <p><strong>注意: 使用kubeadm搭建的集群,默认就会给master节点添加一个污点标记,所以pod就不会调度到master节点上。</strong></p> 
  </blockquote> 
  <blockquote> 
   <h4>容忍(Toleration)</h4> 
   <p>上面介绍了污点的作用,我们可以在node上添加污点用于拒绝pod调度上来,但是如果就是想<strong>将一个pod调度到一个有污点的node上去</strong>,这时候应该怎么做呢?这就要使用到容忍。<br> <a href="http://img.e-com-net.com/image/info8/e0982898f37e4adeae61c5c9e42c8e93.jpg" target="_blank"><img src="http://img.e-com-net.com/image/info8/e0982898f37e4adeae61c5c9e42c8e93.jpg" alt="Pod详解_第6张图片" width="650" height="257" style="border:1px solid black;"></a><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><code class="prism language-powershell">apiVersion: v1
kind: Pod
metadata:
  name: pod<span class="token operator">-</span>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>
</code></pre> 
  <blockquote> 
   <p><strong>创建pod,观察效果</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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<span class="token operator">/</span>pod<span class="token operator">-</span>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<span class="token operator">-</span>toleration   1<span class="token operator">/</span>1     Running   0          11s     10<span class="token punctuation">.</span>244<span class="token punctuation">.</span>1<span class="token punctuation">.</span>4   k8s<span class="token operator">-</span>node<span class="token operator">-</span>01   <none>           <none>
taint3           0<span class="token operator">/</span>1     Pending   0          4m43s   <none>       <none>        <none>           <none>
</code></pre> 
  <blockquote> 
   <p><strong>下面看一下容忍的详细配置:</strong></p> 
  </blockquote> 
  <pre><code class="prism language-powershell"><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>
</code></pre> 
 </div> 
</div>
                            </div>
                        </div>
                    </div>
                    <!--PC和WAP自适应版-->
                    <div id="SOHUCS" sid="1575357368097652736"></div>
                    <script type="text/javascript" src="/views/front/js/chanyan.js"></script>
                    <!-- 文章页-底部 动态广告位 -->
                    <div class="youdao-fixed-ad" id="detail_ad_bottom"></div>
                </div>
                <div class="col-md-3">
                    <div class="row" id="ad">
                        <!-- 文章页-右侧1 动态广告位 -->
                        <div id="right-1" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad">
                            <div class="youdao-fixed-ad" id="detail_ad_1"> </div>
                        </div>
                        <!-- 文章页-右侧2 动态广告位 -->
                        <div id="right-2" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad">
                            <div class="youdao-fixed-ad" id="detail_ad_2"></div>
                        </div>
                        <!-- 文章页-右侧3 动态广告位 -->
                        <div id="right-3" class="col-lg-12 col-md-12 col-sm-4 col-xs-4 ad">
                            <div class="youdao-fixed-ad" id="detail_ad_3"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="container">
        <h4 class="pt20 mb15 mt0 border-top">你可能感兴趣的:(kubernetes,kubernetes)</h4>
        <div id="paradigm-article-related">
            <div class="recommend-post mb30">
                <ul class="widget-links">
                    <li><a href="/article/1835448111909138432.htm"
                           title="react-intl——react国际化使用方案" target="_blank">react-intl——react国际化使用方案</a>
                        <span class="text-muted">苹果酱0567</span>
<a class="tag" taget="_blank" href="/search/%E9%9D%A2%E8%AF%95%E9%A2%98%E6%B1%87%E6%80%BB%E4%B8%8E%E8%A7%A3%E6%9E%90/1.htm">面试题汇总与解析</a><a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E5%BC%80%E5%8F%91%E8%AF%AD%E8%A8%80/1.htm">开发语言</a><a class="tag" taget="_blank" href="/search/%E4%B8%AD%E9%97%B4%E4%BB%B6/1.htm">中间件</a><a class="tag" taget="_blank" href="/search/spring/1.htm">spring</a><a class="tag" taget="_blank" href="/search/boot/1.htm">boot</a><a class="tag" taget="_blank" href="/search/%E5%90%8E%E7%AB%AF/1.htm">后端</a>
                        <div>国际化介绍i18n:internationalization国家化简称,首字母+首尾字母间隔的字母个数+尾字母,类似的还有k8s(Kubernetes)React-intl是React中最受欢迎的库。使用步骤安装#usenpmnpminstallreact-intl-D#useyarn项目入口文件配置//index.tsximportReactfrom"react";importReactDOMf</div>
                    </li>
                    <li><a href="/article/1835375621996376064.htm"
                           title="Kubernetes数据持久化" target="_blank">Kubernetes数据持久化</a>
                        <span class="text-muted">看清所苡看轻</span>
<a class="tag" taget="_blank" href="/search/kubernetes%28k8s%29/1.htm">kubernetes(k8s)</a><a class="tag" taget="_blank" href="/search/emptyDir/1.htm">emptyDir</a><a class="tag" taget="_blank" href="/search/HostPath/1.htm">HostPath</a><a class="tag" taget="_blank" href="/search/pv/1.htm">pv</a><a class="tag" taget="_blank" href="/search/pvc/1.htm">pvc</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a>
                        <div>在k8s中,Volume(数据卷)存在明确的生命周期(与包含该数据卷的容器组(pod)相同)。因此Volume的生命周期比同一容器组(pod)中任意容器的生命周期要更长,不管容器重启了多少次,数据都被保留下来。当然,如果pod不存在了,数据卷自然退出了。此时,根据pod所使用的数据卷类型不同,数据可能随着数据卷的退出而删除,也可能被真正持久化,并在下次容器组重启时仍然可以使用。从根本上来说,一个数</div>
                    </li>
                    <li><a href="/article/1835361001260806144.htm"
                           title="Kubernetes部署MySQL数据持久化" target="_blank">Kubernetes部署MySQL数据持久化</a>
                        <span class="text-muted">沫殇-MS</span>
<a class="tag" taget="_blank" href="/search/Kubernetes/1.htm">Kubernetes</a><a class="tag" taget="_blank" href="/search/MySQL%E6%95%B0%E6%8D%AE%E5%BA%93/1.htm">MySQL数据库</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/mysql/1.htm">mysql</a><a class="tag" taget="_blank" href="/search/%E5%AE%B9%E5%99%A8/1.htm">容器</a>
                        <div>一、安装配置NFS服务端1、安装nfs-kernel-server:sudoapt-yinstallnfs-kernel-server2、服务端创建共享目录#列出所有可用块设备的信息lsblk#格式化磁盘sudomkfs-text4/dev/sdb#创建一个目录:sudomkdir-p/data/nfs/mysql#更改目录权限:sudochown-Rnobody:nogroup/data/nfs</div>
                    </li>
                    <li><a href="/article/1835354574077128704.htm"
                           title="Kubernetes的3种数据持久化方式" target="_blank">Kubernetes的3种数据持久化方式</a>
                        <span class="text-muted">Seal^_^</span>
<a class="tag" taget="_blank" href="/search/%E3%80%90%E4%BA%91%E5%8E%9F%E7%94%9F%E3%80%91%E5%AE%B9%E5%99%A8%E5%8C%96%E4%B8%8E%E7%BC%96%E6%8E%92%E6%8A%80%E6%9C%AF/1.htm">【云原生】容器化与编排技术</a><a class="tag" taget="_blank" href="/search/%E6%8C%81%E7%BB%AD%E9%9B%86%E6%88%90/1.htm">持续集成</a><a class="tag" taget="_blank" href="/search/%23/1.htm">#</a><a class="tag" taget="_blank" href="/search/Kubernetes/1.htm">Kubernetes</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/%E5%AE%B9%E5%99%A8/1.htm">容器</a><a class="tag" taget="_blank" href="/search/%E4%BA%91%E5%8E%9F%E7%94%9F/1.htm">云原生</a><a class="tag" taget="_blank" href="/search/EmptyDir/1.htm">EmptyDir</a><a class="tag" taget="_blank" href="/search/%E9%9D%A2%E8%AF%95/1.htm">面试</a><a class="tag" taget="_blank" href="/search/HostPath/1.htm">HostPath</a>
                        <div>Kubernetes的3种数据持久化方式1.EmptyDir2.HostPath3.PersistentVolume(PV)TheBegin点点关注,收藏不迷路Kubernetes提供了几种数据持久化方式,以满足不同场景的需求:1.EmptyDir用途:临时数据存储,Pod内容器间共享。特点:生命周期与Pod相同,Pod删除时数据也删除。2.HostPath用途:访问宿主机特定文件或目录。特点:增</div>
                    </li>
                    <li><a href="/article/1835353689854930944.htm"
                           title="【Kubernetes】常见面试题汇总(十一)" target="_blank">【Kubernetes】常见面试题汇总(十一)</a>
                        <span class="text-muted">summer.335</span>
<a class="tag" taget="_blank" href="/search/Kubernetes/1.htm">Kubernetes</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/%E5%AE%B9%E5%99%A8/1.htm">容器</a><a class="tag" taget="_blank" href="/search/%E4%BA%91%E5%8E%9F%E7%94%9F/1.htm">云原生</a>
                        <div>目录33.简述Kubernetes外部如何访问集群内的服务?34.简述Kubernetesingress?35.简述Kubernetes镜像的下载策略?33.简述Kubernetes外部如何访问集群内的服务?(1)对于Kubernetes,集群外的客户端默认情况,无法通过Pod的IP地址或者Service的虚拟IP地址:虚拟端口号进行访问。(2)通常可以通过以下方式进行访问Kubernetes集群</div>
                    </li>
                    <li><a href="/article/1835351925957160960.htm"
                           title="k8s中Service暴露的种类以及用法" target="_blank">k8s中Service暴露的种类以及用法</a>
                        <span class="text-muted">听说唐僧不吃肉</span>
<a class="tag" taget="_blank" href="/search/K8S/1.htm">K8S</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/%E5%AE%B9%E5%99%A8/1.htm">容器</a><a class="tag" taget="_blank" href="/search/%E4%BA%91%E5%8E%9F%E7%94%9F/1.htm">云原生</a>
                        <div>一、说明在Kubernetes中,有几种不同的方式可以将服务(Service)暴露给外部流量。这些方式通过定义服务的spec.type字段来确定。二、详解1.ClusterIP定义:默认类型,服务只能在集群内部访问。作用:通过集群内部IP地址暴露服务。示例:spec:type:ClusterIPports:-port:80targetPo</div>
                    </li>
                    <li><a href="/article/1835347257772306432.htm"
                           title="Kubernetes 自定义控制器开发" target="_blank">Kubernetes 自定义控制器开发</a>
                        <span class="text-muted">IT回忆录</span>
<a class="tag" taget="_blank" href="/search/Kubenetes/1.htm">Kubenetes</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a>
                        <div>目录前言一、CRD二、创建数据库表(Mysql)二、控制器开发1.使用kubernetes的examplecontroller模板2.在controller.go中新增数据表监听方法3.修改tools工具生成资源对象结构体定义这里记录开发k8s控制器的一般方式,controller开发主要使用k8s提供的client-go库进行。前言Controller监听集群内部资源对象的变化,编辑资源对象(增</div>
                    </li>
                    <li><a href="/article/1835344356878413824.htm"
                           title="用kubedam搭建的k8s证书过期处理方法" target="_blank">用kubedam搭建的k8s证书过期处理方法</a>
                        <span class="text-muted">我滴鬼鬼呀wks</span>
<a class="tag" taget="_blank" href="/search/k8s/1.htm">k8s</a><a class="tag" taget="_blank" href="/search/1024%E7%A8%8B%E5%BA%8F%E5%91%98%E8%8A%82/1.htm">1024程序员节</a>
                        <div>kubeadm部署的k8s证书过期1、查看证书过期时间kubeadmalphacertscheck-expiration若证书已经过期无法试用kubectl命令建议修改服务器时间到未过期的时间段2、配置kube-controller-manager.yaml文件cat/etc/kubernetes/manifests/kube-controller-manager.yamlapiVersion:v</div>
                    </li>
                    <li><a href="/article/1835340955637608448.htm"
                           title="k8s证书过期问题处理" target="_blank">k8s证书过期问题处理</a>
                        <span class="text-muted">olina_qin</span>
<a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/%E5%AE%B9%E5%99%A8/1.htm">容器</a><a class="tag" taget="_blank" href="/search/%E4%BA%91%E5%8E%9F%E7%94%9F/1.htm">云原生</a>
                        <div>k8s证书过期问题处理opensslx509-in/etc/kubernetes/pki/apiserver.crt-noout-dateskubeadmcertsrenewallsystemctlrestartkubeleopensslx509-in/etc/kubernetes/pki/apiserver.crt-noout-text|grep"NotAfter"cp/etc/kubernet</div>
                    </li>
                    <li><a href="/article/1835337301350248448.htm"
                           title="Kubernetes Ingress 控制器(Nginx)安装与使用教程" target="_blank">Kubernetes Ingress 控制器(Nginx)安装与使用教程</a>
                        <span class="text-muted">农优影</span>

                        <div>KubernetesIngress控制器(Nginx)安装与使用教程kubernetes-ingressNGINXandNGINXPlusIngressControllersforKubernetes项目地址:https://gitcode.com/gh_mirrors/ku/kubernetes-ingress1.项目目录结构及介绍在nginxinc/kubernetes-ingress仓库中,</div>
                    </li>
                    <li><a href="/article/1835331377520799744.htm"
                           title="【K8s】专题十一:Kubernetes 集群证书过期处理方法" target="_blank">【K8s】专题十一:Kubernetes 集群证书过期处理方法</a>
                        <span class="text-muted">行者Sun1989</span>
<a class="tag" taget="_blank" href="/search/Kubernetes/1.htm">Kubernetes</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/%E4%BA%91%E5%8E%9F%E7%94%9F/1.htm">云原生</a><a class="tag" taget="_blank" href="/search/%E5%AE%B9%E5%99%A8/1.htm">容器</a>
                        <div>本文内容均来自个人笔记并重新梳理,如有错误欢迎指正!如果对您有帮助,烦请点赞、关注、转发、订阅专栏!专栏订阅入口Linux专栏|Docker专栏|Kubernetes专栏往期精彩文章【Docker】(全网首发)KylinV10下MySQL容器内存占用异常的解决方法【Docker】(全网首发)KylinV10下MySQL容器内存占用异常的解决方法(续)【Docker】MySQL源码构建Docker镜</div>
                    </li>
                    <li><a href="/article/1835269935358636032.htm"
                           title="Docker学习十一:Kubernetes概述" target="_blank">Docker学习十一:Kubernetes概述</a>
                        <span class="text-muted">爱打羽球的程序猿</span>
<a class="tag" taget="_blank" href="/search/Docker%E5%AD%A6%E4%B9%A0%E7%B3%BB%E5%88%97/1.htm">Docker学习系列</a><a class="tag" taget="_blank" href="/search/docker/1.htm">docker</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/%E5%AD%A6%E4%B9%A0/1.htm">学习</a>
                        <div>一、Kubernetes简介2006年,Google提出了云计算的概念,当时的云计算领域还是以虚拟机为代表的云平台。2013年,Docker横空出世,Docker提出了镜像、仓库等核心概念,规范了服务的交付标准,使得复杂服务的落地变得更加简单,之后Docker又定义了OCI标准,Docker在容器领域称为事实的标准。但是,Docker诞生只是帮助定义了开发和交付标准,如果想要在生产环境中大批量的使</div>
                    </li>
                    <li><a href="/article/1835269304761806848.htm"
                           title="Cloud Native Weekly | 华为云抢先发布Redis5.0,红帽宣布收购混合云提供商" target="_blank">Cloud Native Weekly | 华为云抢先发布Redis5.0,红帽宣布收购混合云提供商</a>
                        <span class="text-muted">weixin_34302561</span>
<a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E5%BA%93/1.htm">数据库</a><a class="tag" taget="_blank" href="/search/devops/1.htm">devops</a><a class="tag" taget="_blank" href="/search/%E5%A4%A7%E6%95%B0%E6%8D%AE/1.htm">大数据</a>
                        <div>1——华为云抢先发布Redis5.02——DigitalOceanK8s服务正式上线3——红帽宣布收购混合云提供商NooBaa4——微软发布多项AzureKubernetes服务更新1华为云抢先发布Redis5.012月17日,华为云在DCS2.0的基础上,快人一步,抢先推出了新的Redis5.0产品,这是一个崭新的突破。目前国内在缓存领域的发展普遍停留在Redis4.0阶段,华为云率先发布了Re</div>
                    </li>
                    <li><a href="/article/1835209173944594432.htm"
                           title="(k8s)Kubernetes 从0到1容器编排之旅" target="_blank">(k8s)Kubernetes 从0到1容器编排之旅</a>
                        <span class="text-muted">道不贱卖,法不轻传</span>
<a class="tag" taget="_blank" href="/search/kubernets/1.htm">kubernets</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/%E5%AE%B9%E5%99%A8/1.htm">容器</a><a class="tag" taget="_blank" href="/search/%E4%BA%91%E5%8E%9F%E7%94%9F/1.htm">云原生</a>
                        <div>一、引言在当今数字化的浪潮中,Kubernetes如同一艘强大的航船,引领着容器化应用的部署与管理。它以其卓越的灵活性、可扩展性和可靠性,成为众多企业和开发者的首选。然而,要真正发挥Kubernetes的强大威力,仅仅掌握基本操作是远远不够的。本文将带你深入探索Kubernetes使用过程中的奇技妙法,为你开启一段优雅的容器编排之旅。二、高级资源管理之精妙艺术1.资源配额与限制:雕琢资源之美•Ku</div>
                    </li>
                    <li><a href="/article/1835061888858288128.htm"
                           title="【K8S】kubernetes集群架构与组件" target="_blank">【K8S】kubernetes集群架构与组件</a>
                        <span class="text-muted">奇奇怪怪^</span>
<a class="tag" taget="_blank" href="/search/%E4%BA%91/1.htm">云</a><a class="tag" taget="_blank" href="/search/Linux/1.htm">Linux</a><a class="tag" taget="_blank" href="/search/IT/1.htm">IT</a><a class="tag" taget="_blank" href="/search/%E8%BF%90%E7%BB%B4/1.htm">运维</a><a class="tag" taget="_blank" href="/search/%E6%9C%8D%E5%8A%A1%E5%99%A8/1.htm">服务器</a><a class="tag" taget="_blank" href="/search/linux/1.htm">linux</a>
                        <div>文章目录【K8S】kubernetes集群架构与组件kubernetes组件**master组件**node组件整体流程POD终止过程【K8S】kubernetes集群架构与组件kubernetes组件K8S是属于主从设备模型(Master-slave架构),即有Master节点负责集群的调度、管理和运维,Slave节点是集群中的运算工作负载节点在K8S中,主节点一般被称为Master节点,而从节</div>
                    </li>
                    <li><a href="/article/1835058988631289856.htm"
                           title="K8S学习笔记02——K8S组件" target="_blank">K8S学习笔记02——K8S组件</a>
                        <span class="text-muted">沉淅尘</span>
<a class="tag" taget="_blank" href="/search/%23/1.htm">#</a><a class="tag" taget="_blank" href="/search/Docker/1.htm">Docker</a><a class="tag" taget="_blank" href="/search/%23/1.htm">#</a><a class="tag" taget="_blank" href="/search/K8S/1.htm">K8S</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a>
                        <div>Kubernetes组件一、控制平面组件(ControlPlaneComponents)(1)kube-apiserver(2)etcd(3)kube-scheduler(4)kube-controller-manager(5)cloud-controller-manager二、Node组件1.kubelet2.kube-proxy3.容器运行时(ContainerRuntime)三、插件(Add</div>
                    </li>
                    <li><a href="/article/1835054063679533056.htm"
                           title="Kubernetes——组件" target="_blank">Kubernetes——组件</a>
                        <span class="text-muted">窒息う</span>
<a class="tag" taget="_blank" href="/search/Kubernetes/1.htm">Kubernetes</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/%E5%AE%B9%E5%99%A8/1.htm">容器</a>
                        <div>文章目录K8S的优势核心架构角色与功能集群图例K8S的优势能管理大量跨主机容器快速部署应用快速扩展应用无缝对接新的应用节省资源,优化硬件资源的使用核心架构master(管理节点)node(计算节点)images(镜像节点)角色与功能Master功能提供集群的控制对集群进行全局决策检测和响应集群事件Master节点核心组件APIServer是整个系统的对外接口,提供客户端和其他组件调用后端元数据存储</div>
                    </li>
                    <li><a href="/article/1835046751275413504.htm"
                           title="Kubernetes组件" target="_blank">Kubernetes组件</a>
                        <span class="text-muted">汉只只</span>
<a class="tag" taget="_blank" href="/search/%E7%BD%91%E7%BB%9C/1.htm">网络</a><a class="tag" taget="_blank" href="/search/docker/1.htm">docker</a><a class="tag" taget="_blank" href="/search/%E5%A4%A7%E6%95%B0%E6%8D%AE/1.htm">大数据</a><a class="tag" taget="_blank" href="/search/%E5%88%86%E5%B8%83%E5%BC%8F/1.htm">分布式</a><a class="tag" taget="_blank" href="/search/hadoop/1.htm">hadoop</a>
                        <div>Kubernetes核心组件Kubernetes定义了一组构建块,它们可以共同提供部署、维护和扩展应用程序的机制。组成Kubernetes的组件设计为松耦合和可扩展的,这样可以满足多种不同的工作负载。可扩展性在很大程度上由KubernetesAPI提供——它被作为扩展的内部组件以及Kubernetes上运行的容器等使用。Kubernetes主要由以下几个核心组件组成:etcd保存了整个集群的状态;</div>
                    </li>
                    <li><a href="/article/1835034781197365248.htm"
                           title="【Linux 从基础到进阶】Kubernetes 集群搭建与管理" target="_blank">【Linux 从基础到进阶】Kubernetes 集群搭建与管理</a>
                        <span class="text-muted">爱技术的小伙子</span>
<a class="tag" taget="_blank" href="/search/Linux%E4%BB%8E%E5%9F%BA%E7%A1%80%E5%88%B0%E8%BF%9B%E9%98%B6/1.htm">Linux从基础到进阶</a><a class="tag" taget="_blank" href="/search/linux/1.htm">linux</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/%E8%BF%90%E7%BB%B4/1.htm">运维</a>
                        <div>Kubernetes集群搭建与管理Kubernetes(简称K8s)是一个用于自动化部署、扩展和管理容器化应用程序的开源平台。它提供了容器编排功能,能够管理大量的容器实例,并支持应用的自动扩展、高可用性和自愈能力。本文将详细介绍如何在CentOS和Ubuntu系统上安装和配置Kubernetes集群,并讲解Kubernetes的基本概念和管理操作。1.Kubernetes基础概念在了解如何搭建Ku</div>
                    </li>
                    <li><a href="/article/1834999728476090368.htm"
                           title="【Kubernetes】常见面试题汇总(十三)" target="_blank">【Kubernetes】常见面试题汇总(十三)</a>
                        <span class="text-muted">summer.335</span>
<a class="tag" taget="_blank" href="/search/Kubernetes/1.htm">Kubernetes</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/%E5%AE%B9%E5%99%A8/1.htm">容器</a><a class="tag" taget="_blank" href="/search/%E4%BA%91%E5%8E%9F%E7%94%9F/1.htm">云原生</a>
                        <div>目录39.简述KubernetesScheduler使用哪两种算法将Pod绑定到worker节点?40.简述Kuberneteskubelet的作用?41.简述Kuberneteskubelet监控Worker节点资源是使用什么组件来实现的?39.简述KubernetesScheduler使用哪两种算法将Pod绑定到worker节点?KubernetesScheduler根据如下两种调度算法将Po</div>
                    </li>
                    <li><a href="/article/1834893444238110720.htm"
                           title="Ansible自动化部署kubernetes集群" target="_blank">Ansible自动化部署kubernetes集群</a>
                        <span class="text-muted">theo.wu</span>
<a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/ansible/1.htm">ansible</a><a class="tag" taget="_blank" href="/search/%E8%87%AA%E5%8A%A8%E5%8C%96/1.htm">自动化</a>
                        <div>机器环境介绍1.1.机器信息介绍IPhostnameapplicationCPUMemory192.168.204.129k8s-master01etcd,kube-apiserver,kube-controller-manager,kube-scheduler,kubelet,kube-proxy,containerd2C4G192.168.204.130k8s-worker01etcd,kub</div>
                    </li>
                    <li><a href="/article/1834787055985586176.htm"
                           title="K8S - Emptydir - 取代ELK 使用fluentd 构建logging saidcar" target="_blank">K8S - Emptydir - 取代ELK 使用fluentd 构建logging saidcar</a>
                        <span class="text-muted">nvd11</span>
<a class="tag" taget="_blank" href="/search/K8S/1.htm">K8S</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a>
                        <div>由于k8s的无状态service通常部署在多个POD中,实现多实例面向高并发。但是k8s本身并没有提供集中查询多个pod的日志的功能其中1个常见方案就是ELK.本文的方案是利用fluentdsidecar和emptydir把多个pod的日志导向到bigquery的table中。Emptydir的简介Kubernetes中的EmptyDir是一种用于容器之间共享临时存储的空目录卷类型。EmptyDi</div>
                    </li>
                    <li><a href="/article/1834610443310690304.htm"
                           title="kubernetes里面那些事————控制器" target="_blank">kubernetes里面那些事————控制器</a>
                        <span class="text-muted">背锅攻城师</span>
<a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/%E5%AE%B9%E5%99%A8/1.htm">容器</a><a class="tag" taget="_blank" href="/search/%E4%BA%91%E5%8E%9F%E7%94%9F/1.htm">云原生</a>
                        <div>资源-控制器一,控制器作用二,控制器类型2.1,Deployment:无状态应用部署2.2,DaemonSet:确保所有Node运行同一个pod2.3,StatefulSet:有状态应用部署2.4,Job:一次性任务2.5,CronJob:定时任务2.6,pod2.7,service2.8,replicaset2.9,endpoints三,控制器yaml应用3.1,Deployment3.2,Da</div>
                    </li>
                    <li><a href="/article/1834610316504297472.htm"
                           title="kubernetes里面那些事—————存储" target="_blank">kubernetes里面那些事—————存储</a>
                        <span class="text-muted">背锅攻城师</span>
<a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/%E5%AE%B9%E5%99%A8/1.htm">容器</a><a class="tag" taget="_blank" href="/search/%E4%BA%91%E5%8E%9F%E7%94%9F/1.htm">云原生</a>
                        <div>常用数据卷类型一,emptyDir1.1,emptyDir概念1.2,应用场景1.3,yaml示例二,hostPath2.1,hostPath概念2.2,应用场景2.3,yaml示例三,congfigmap3.1,configmap的作用3.2,注意事项:3.3,使用configmap创建java项目配置文件3.4,subPath应用3.5,configmap作为环境变量四,secret4.1,s</div>
                    </li>
                    <li><a href="/article/1834610316068089856.htm"
                           title="kubernetes-flannel组件的安装方式" target="_blank">kubernetes-flannel组件的安装方式</a>
                        <span class="text-muted">背锅攻城师</span>
<a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a>
                        <div>使用flannel网络数据转发的过程数据->源容器->宿主机docker0虚拟网卡->flannel0虚拟网卡(UDP封装->etcd->目标容器所在宿主机flannel0->目标容器所在宿主机的docker0虚拟网卡->目标容器文章目录二进制安装flannelyaml文件部署flannel二进制安装flannel一,原有的二进制etcd集群添加配置信息#[Member]ETCD_NAME="et</div>
                    </li>
                    <li><a href="/article/1834546926272475136.htm"
                           title="深入理解Kubernetes:kube-scheduler源码解析" target="_blank">深入理解Kubernetes:kube-scheduler源码解析</a>
                        <span class="text-muted">mujingluo</span>
<a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/%E5%AE%B9%E5%99%A8/1.htm">容器</a><a class="tag" taget="_blank" href="/search/%E4%BA%91%E5%8E%9F%E7%94%9F/1.htm">云原生</a>
                        <div>Kubernetes的调度器(kube-scheduler)是整个系统中至关重要的组件,它负责将待调度的Pods分配到合适的节点上。本文将深入分析kube-scheduler的源码,揭示其内部工作机制。kube-scheduler的核心功能kube-scheduler的核心功能包括:监听Pod变化:通过KubernetesAPI监听所有未调度的Pods。过滤(Filtering):根据一系列规则(</div>
                    </li>
                    <li><a href="/article/1834543901835358208.htm"
                           title="开源项目 Kubernetes 源码探索与部署指南" target="_blank">开源项目 Kubernetes 源码探索与部署指南</a>
                        <span class="text-muted">邹滢朦</span>

                        <div>开源项目Kubernetes源码探索与部署指南kubernetesThisistherepothattracksallpatchestotheOpenShiftdistributionofKubernetesonbranchescorrespondingtoOpenShiftreleases.Seehttps://github.com/openshift/kubernetes/blob/maste</div>
                    </li>
                    <li><a href="/article/1834522729504665600.htm"
                           title="【2023年】云计算金砖牛刀小试4" target="_blank">【2023年】云计算金砖牛刀小试4</a>
                        <span class="text-muted">geekgold</span>
<a class="tag" taget="_blank" href="/search/%E4%BA%91%E8%AE%A1%E7%AE%97/1.htm">云计算</a><a class="tag" taget="_blank" href="/search/linux/1.htm">linux</a><a class="tag" taget="_blank" href="/search/%E5%AE%B9%E5%99%A8/1.htm">容器</a><a class="tag" taget="_blank" href="/search/jenkins/1.htm">jenkins</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a>
                        <div>容器云问题1.Kubernetes集群支持Pod优先级抢占,通过抢占式调度策略来实现同一个Node节点内部的Pod对象抢占。在master节点/root目录下编写YAML文件schedule.yaml创建一个抢占式调度策略,具体要求如下:(1)抢占式调度策略名称:high-scheduling;(2)优先级为1000000;(3)不要将该调度策略设置为默认优先调度策略。vischedule.yam</div>
                    </li>
                    <li><a href="/article/1834471045063602176.htm"
                           title="【Kubernetes】(K8S)彻底卸载详细教程" target="_blank">【Kubernetes】(K8S)彻底卸载详细教程</a>
                        <span class="text-muted">哒哒-blog</span>
<a class="tag" taget="_blank" href="/search/Kubernetes/1.htm">Kubernetes</a><a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/%E5%AE%B9%E5%99%A8/1.htm">容器</a><a class="tag" taget="_blank" href="/search/%E4%BA%91%E5%8E%9F%E7%94%9F/1.htm">云原生</a>
                        <div>以下全部操作都是使用root用户进行(非root用户可以使用sudo),并且全部命令都需要在Kubernetes集群的所有节点分别执行:第一步、停止K8S所有节点执行:123systemctlstopkubeletsystemctlstopetcdsystemctlstopdocker第二步、清空K8S集群设置所有节点执行:1kubeadmreset-f第三步、删除K8S相关软件所有节点执行:12</div>
                    </li>
                    <li><a href="/article/1834467134424576000.htm"
                           title="二进制方式部署K8s高可用集群" target="_blank">二进制方式部署K8s高可用集群</a>
                        <span class="text-muted">麻辣头马头</span>
<a class="tag" taget="_blank" href="/search/kubernetes/1.htm">kubernetes</a><a class="tag" taget="_blank" href="/search/%E5%AE%B9%E5%99%A8/1.htm">容器</a><a class="tag" taget="_blank" href="/search/%E4%BA%91%E5%8E%9F%E7%94%9F/1.htm">云原生</a><a class="tag" taget="_blank" href="/search/%E8%BF%90%E7%BB%B4/1.htm">运维</a><a class="tag" taget="_blank" href="/search/%E6%9C%8D%E5%8A%A1%E5%99%A8/1.htm">服务器</a><a class="tag" taget="_blank" href="/search/docker/1.htm">docker</a><a class="tag" taget="_blank" href="/search/%E7%BD%91%E7%BB%9C/1.htm">网络</a>
                        <div>1二进制方式部署K8s高可用集群1.1kubeadm和二进制安装k8s适用场景分析kubeadm是官方提供的开源工具,是一个开源项目,用于快速搭建kubernetes集群,目前是比较方便和推荐使用的。kubeadminit以及kubeadmjoin这两个命令可以快速创建kubernetes集群。Kubeadm初始化k8s,所有的组件都是以pod形式运行的,具备故障自恢复能力。kubeadm是工具,</div>
                    </li>
                                <li><a href="/article/127.htm"
                                       title="用MiddleGenIDE工具生成hibernate的POJO(根据数据表生成POJO类)" target="_blank">用MiddleGenIDE工具生成hibernate的POJO(根据数据表生成POJO类)</a>
                                    <span class="text-muted">AdyZhang</span>
<a class="tag" taget="_blank" href="/search/POJO/1.htm">POJO</a><a class="tag" taget="_blank" href="/search/eclipse/1.htm">eclipse</a><a class="tag" taget="_blank" href="/search/Hibernate/1.htm">Hibernate</a><a class="tag" taget="_blank" href="/search/MiddleGenIDE/1.htm">MiddleGenIDE</a>
                                    <div>推荐:MiddlegenIDE插件,   是一个Eclipse   插件.     用它可以直接连接到数据库,   根据表按照一定的HIBERNATE规则作出BEAN和对应的XML ,用完后你可以手动删除它加载的JAR包和XML文件!     今天开始试着使用</div>
                                </li>
                                <li><a href="/article/254.htm"
                                       title=".9.png" target="_blank">.9.png</a>
                                    <span class="text-muted">Cb123456</span>
<a class="tag" taget="_blank" href="/search/android/1.htm">android</a>
                                    <div>  “点九”是andriod平台的应用软件开发里的一种特殊的图片形式,文件扩展名为:.9.png 
  智能手机中有自动横屏的功能,同一幅界面会在随着手机(或平板电脑)中的方向传感器的参数不同而改变显示的方向,在界面改变方向后,界面上的图形会因为长宽的变化而产生拉伸,造成图形的失真变形。 
  我们都知道android平台有多种不同的分辨率,很多控件的切图文件在被放大拉伸后,边</div>
                                </li>
                                <li><a href="/article/381.htm"
                                       title="算法的效率" target="_blank">算法的效率</a>
                                    <span class="text-muted">天子之骄</span>
<a class="tag" taget="_blank" href="/search/%E7%AE%97%E6%B3%95%E6%95%88%E7%8E%87/1.htm">算法效率</a><a class="tag" taget="_blank" href="/search/%E5%A4%8D%E6%9D%82%E5%BA%A6/1.htm">复杂度</a><a class="tag" taget="_blank" href="/search/%E6%9C%80%E5%9D%8F%E6%83%85%E5%86%B5%E8%BF%90%E8%A1%8C%E6%97%B6%E9%97%B4/1.htm">最坏情况运行时间</a><a class="tag" taget="_blank" href="/search/%E5%A4%A7O%E9%98%B6/1.htm">大O阶</a><a class="tag" taget="_blank" href="/search/%E5%B9%B3%E5%9D%87%E6%83%85%E5%86%B5%E8%BF%90%E8%A1%8C%E6%97%B6%E9%97%B4/1.htm">平均情况运行时间</a>
                                    <div>算法的效率 
效率是速度和空间消耗的度量。集中考虑程序的速度,也称运行时间或执行时间,用复杂度的阶(O)这一标准来衡量。空间的消耗或需求也可以用大O表示,而且它总是小于或等于时间需求。 
  
以下是我的学习笔记: 
  
1.求值与霍纳法则,即为秦九韶公式。 
  
2.测定运行时间的最可靠方法是计数对运行时间有贡献的基本操作的执行次数。运行时间与这个计数成正比。 </div>
                                </li>
                                <li><a href="/article/508.htm"
                                       title="java数据结构" target="_blank">java数据结构</a>
                                    <span class="text-muted">何必如此</span>
<a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/1.htm">数据结构</a>
                                    <div>Java 数据结构 
 
Java工具包提供了强大的数据结构。在Java中的数据结构主要包括以下几种接口和类: 
 
枚举(Enumeration) 
位集合(BitSet) 
向量(Vector) 
栈(Stack) 
字典(Dictionary) 
哈希表(Hashtable) 
属性(Properties) 
以上这些类是传统遗留的,在Java2中引入了一种新的框架-集合框架(Collect</div>
                                </li>
                                <li><a href="/article/635.htm"
                                       title="MybatisHelloWorld" target="_blank">MybatisHelloWorld</a>
                                    <span class="text-muted">3213213333332132</span>

                                    <div>
//测试入口TestMyBatis   
package com.base.helloworld.test;

import java.io.IOException;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibat</div>
                                </li>
                                <li><a href="/article/762.htm"
                                       title="Java|urlrewrite|URL重写|多个参数" target="_blank">Java|urlrewrite|URL重写|多个参数</a>
                                    <span class="text-muted">7454103</span>
<a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/xml/1.htm">xml</a><a class="tag" taget="_blank" href="/search/Web/1.htm">Web</a><a class="tag" taget="_blank" href="/search/%E5%B7%A5%E4%BD%9C/1.htm">工作</a>
                                    <div> 个人工作经验! 如有不当之处,敬请指点    
1.0  web -info 目录下建立     urlrewrite.xml  文件 类似如下: 
<?xml version="1.0" encoding="UTF-8" ?>  
  <!DOCTYPE u</div>
                                </li>
                                <li><a href="/article/889.htm"
                                       title="达梦数据库+ibatis" target="_blank">达梦数据库+ibatis</a>
                                    <span class="text-muted">darkranger</span>
<a class="tag" taget="_blank" href="/search/sql/1.htm">sql</a><a class="tag" taget="_blank" href="/search/mysql/1.htm">mysql</a><a class="tag" taget="_blank" href="/search/ibatis/1.htm">ibatis</a><a class="tag" taget="_blank" href="/search/SQL+Server/1.htm">SQL Server</a>
                                    <div>--插入数据方面 
 
如果您需要数据库自增... 
那么在插入的时候不需要指定自增列. 
 
如果想自己指定ID列的值, 那么要设置 
set identity_insert  数据库名.模式名.表名; 
----然后插入数据; 
example: 
create table zhabei.test( 
id bigint identity(1,1) primary key, 
nam</div>
                                </li>
                                <li><a href="/article/1016.htm"
                                       title="XML 解析 四种方式" target="_blank">XML 解析 四种方式</a>
                                    <span class="text-muted">aijuans</span>
<a class="tag" taget="_blank" href="/search/android/1.htm">android</a>
                                    <div>XML现在已经成为一种通用的数据交换格式,平台的无关性使得很多场合都需要用到XML。本文将详细介绍用Java解析XML的四种方法。   
 
  
 
    
 
XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便。对于XML本身的语法知识与技术细节,需要阅读相关的技术文献,这里面包括的内容有DOM(Document Object </div>
                                </li>
                                <li><a href="/article/1143.htm"
                                       title="spring中配置文件占位符的使用" target="_blank">spring中配置文件占位符的使用</a>
                                    <span class="text-muted">avords</span>

                                    <div>1.类 
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.o</div>
                                </li>
                                <li><a href="/article/1270.htm"
                                       title="前端工程化-公共模块的依赖和常用的工作流" target="_blank">前端工程化-公共模块的依赖和常用的工作流</a>
                                    <span class="text-muted">bee1314</span>
<a class="tag" taget="_blank" href="/search/webpack/1.htm">webpack</a>
                                    <div>题记: 一个人的项目,还有工程化的问题嘛?       我们在推进模块化和组件化的过程中,肯定会不断的沉淀出我们项目的模块和组件。对于这些沉淀出的模块和组件怎么管理?另外怎么依赖也是个问题?   你真的想这样嘛?       var BreadCrumb = require(‘../../../../uikit/breadcrumb’); //真心ugly。     </div>
                                </li>
                                <li><a href="/article/1397.htm"
                                       title="上司说「看你每天准时下班就知道你工作量不饱和」,该如何回应?" target="_blank">上司说「看你每天准时下班就知道你工作量不饱和」,该如何回应?</a>
                                    <span class="text-muted">bijian1013</span>
<a class="tag" taget="_blank" href="/search/%E9%A1%B9%E7%9B%AE%E7%AE%A1%E7%90%86/1.htm">项目管理</a><a class="tag" taget="_blank" href="/search/%E6%B2%9F%E9%80%9A/1.htm">沟通</a><a class="tag" taget="_blank" href="/search/IT%E8%81%8C%E4%B8%9A%E8%A7%84%E5%88%92/1.htm">IT职业规划</a>
                                    <div>问题:上司说「看你每天准时下班就知道你工作量不饱和」,如何回应 
        正常下班时间6点,只要是6点半前下班的,上司都认为没有加班。 
  
Eno-Bea回答,注重感受,不一定是别人的 
        虽然我不知道你具体从事什么工作与职业,但是我大概猜测,你是从事一项不太容易出现阶段性成果的工作</div>
                                </li>
                                <li><a href="/article/1524.htm"
                                       title="TortoiseSVN,过滤文件" target="_blank">TortoiseSVN,过滤文件</a>
                                    <span class="text-muted">征客丶</span>
<a class="tag" taget="_blank" href="/search/SVN/1.htm">SVN</a>
                                    <div>环境: 
TortoiseSVN 1.8 
 
配置: 
在文件夹空白处右键 
选择  TortoiseSVN -> Settings 
在 Global ignote pattern 中添加要过滤的文件: 
多类型用英文空格分开 
*name : 过滤所有名称为 name 的文件或文件夹 
*.name : 过滤所有后缀为 name 的文件或文件夹 
 
 
 
--------</div>
                                </li>
                                <li><a href="/article/1651.htm"
                                       title="【Flume二】HDFS sink细说" target="_blank">【Flume二】HDFS sink细说</a>
                                    <span class="text-muted">bit1129</span>
<a class="tag" taget="_blank" href="/search/Flume/1.htm">Flume</a>
                                    <div>1. Flume配置 
  
a1.sources=r1  
a1.channels=c1  
a1.sinks=k1  

###Flume负责启动44444端口
  
a1.sources.r1.type=avro  
a1.sources.r1.bind=0.0.0.0  
a1.sources.r1.port=44444  
a1.sources.r1.chan</div>
                                </li>
                                <li><a href="/article/1778.htm"
                                       title="The Eight Myths of Erlang Performance" target="_blank">The Eight Myths of Erlang Performance</a>
                                    <span class="text-muted">bookjovi</span>
<a class="tag" taget="_blank" href="/search/erlang/1.htm">erlang</a>
                                    <div>erlang有一篇guide很有意思: http://www.erlang.org/doc/efficiency_guide 
里面有个The Eight Myths of Erlang Performance: http://www.erlang.org/doc/efficiency_guide/myths.html 
  
  
Myth: Funs are sl</div>
                                </li>
                                <li><a href="/article/1905.htm"
                                       title="java多线程网络传输文件(非同步)-2008-08-17" target="_blank">java多线程网络传输文件(非同步)-2008-08-17</a>
                                    <span class="text-muted">ljy325</span>
<a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E5%A4%9A%E7%BA%BF%E7%A8%8B/1.htm">多线程</a><a class="tag" taget="_blank" href="/search/socket/1.htm">socket</a>
                                    <div>利用 Socket 套接字进行面向连接通信的编程。客户端读取本地文件并发送;服务器接收文件并保存到本地文件系统中。 
使用说明:请将TransferClient, TransferServer, TempFile三个类编译,他们的类包是FileServer. 
客户端: 
修改TransferClient: serPort, serIP, filePath, blockNum,的值来符合您机器的系</div>
                                </li>
                                <li><a href="/article/2032.htm"
                                       title="读《研磨设计模式》-代码笔记-模板方法模式" target="_blank">读《研磨设计模式》-代码笔记-模板方法模式</a>
                                    <span class="text-muted">bylijinnan</span>
<a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/1.htm">设计模式</a>
                                    <div>声明: 本文只为方便我个人查阅和理解,详细的分析以及源代码请移步 原作者的博客http://chjavach.iteye.com/ 
 
 



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;</div>
                                </li>
                                <li><a href="/article/2159.htm"
                                       title="配置心得" target="_blank">配置心得</a>
                                    <span class="text-muted">chenyu19891124</span>
<a class="tag" taget="_blank" href="/search/%E9%85%8D%E7%BD%AE/1.htm">配置</a>
                                    <div>时间就这样不知不觉的走过了一个春夏秋冬,转眼间来公司已经一年了,感觉时间过的很快,时间老人总是这样不停走,从来没停歇过。 
作为一名新手的配置管理员,刚开始真的是对配置管理是一点不懂,就只听说咱们公司配置主要是负责升级,而具体该怎么做却一点都不了解。经过老员工的一点点讲解,慢慢的对配置有了初步了解,对自己所在的岗位也慢慢的了解。 
做了一年的配置管理给自总结下: 
1.改变 
从一个以前对配置毫无</div>
                                </li>
                                <li><a href="/article/2286.htm"
                                       title="对“带条件选择的并行汇聚路由问题”的再思考" target="_blank">对“带条件选择的并行汇聚路由问题”的再思考</a>
                                    <span class="text-muted">comsci</span>
<a class="tag" taget="_blank" href="/search/%E7%AE%97%E6%B3%95/1.htm">算法</a><a class="tag" taget="_blank" href="/search/%E5%B7%A5%E4%BD%9C/1.htm">工作</a><a class="tag" taget="_blank" href="/search/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/1.htm">软件测试</a><a class="tag" taget="_blank" href="/search/%E5%B5%8C%E5%85%A5%E5%BC%8F/1.htm">嵌入式</a><a class="tag" taget="_blank" href="/search/%E9%A2%86%E5%9F%9F%E6%A8%A1%E5%9E%8B/1.htm">领域模型</a>
                                    <div>2008年上半年,我在设计并开发基于”JWFD流程系统“的商业化改进型引擎的时候,由于采用了新的嵌入式公式模块而导致出现“带条件选择的并行汇聚路由问题”(请参考2009-02-27博文),当时对这个问题的解决办法是采用基于拓扑结构的处理思想,对汇聚点的实际前驱分支节点通过算法预测出来,然后进行处理,简单的说就是找到造成这个汇聚模型的分支起点,对这个起始分支节点实际走的路径数进行计算,然后把这个实际</div>
                                </li>
                                <li><a href="/article/2413.htm"
                                       title="Oracle 10g 的clusterware 32位 下载地址" target="_blank">Oracle 10g 的clusterware 32位 下载地址</a>
                                    <span class="text-muted">daizj</span>
<a class="tag" taget="_blank" href="/search/oracle/1.htm">oracle</a>
                                    <div>Oracle 10g 的clusterware 32位 下载地址 
 
http://pan.baidu.com/share/link?shareid=531580&uk=421021908 
 
 
http://pan.baidu.com/share/link?shareid=137223&uk=321552738 
 
http://pan.baidu.com/share/l</div>
                                </li>
                                <li><a href="/article/2540.htm"
                                       title="非常好的介绍:Linux定时执行工具cron" target="_blank">非常好的介绍:Linux定时执行工具cron</a>
                                    <span class="text-muted">dongwei_6688</span>
<a class="tag" taget="_blank" href="/search/linux/1.htm">linux</a>
                                    <div>Linux经过十多年的发展,很多用户都很了解Linux了,这里介绍一下Linux下cron的理解,和大家讨论讨论。cron是一个Linux 定时执行工具,可以在无需人工干预的情况下运行作业,本文档不讲cron实现原理,主要讲一下Linux定时执行工具cron的具体使用及简单介绍。 
新增调度任务推荐使用crontab -e命令添加自定义的任务(编辑的是/var/spool/cron下对应用户的cr</div>
                                </li>
                                <li><a href="/article/2667.htm"
                                       title="Yii assets目录生成及修改" target="_blank">Yii assets目录生成及修改</a>
                                    <span class="text-muted">dcj3sjt126com</span>
<a class="tag" taget="_blank" href="/search/yii/1.htm">yii</a>
                                    <div>assets的作用是方便模块化,插件化的,一般来说出于安全原因不允许通过url访问protected下面的文件,但是我们又希望将module单独出来,所以需要使用发布,即将一个目录下的文件复制一份到assets下面方便通过url访问。 
assets设置对应的方法位置 \framework\web\CAssetManager.php 
  
assets配置方法 在m</div>
                                </li>
                                <li><a href="/article/2794.htm"
                                       title="mac工作软件推荐" target="_blank">mac工作软件推荐</a>
                                    <span class="text-muted">dcj3sjt126com</span>
<a class="tag" taget="_blank" href="/search/mac/1.htm">mac</a>
                                    <div>mac上的Terminal + bash + screen组合现在已经非常好用了,但是还是经不起iterm+zsh+tmux的冲击。在同事的强烈推荐下,趁着升级mac系统的机会,顺便也切换到iterm+zsh+tmux的环境下了。 
我为什么要要iterm2 
切换过来也是脑袋一热的冲动,我也调查过一些资料,看了下iterm的一些优点: 
 
 * 兼容性好,远程服务器 vi 什么的低版本能很好兼</div>
                                </li>
                                <li><a href="/article/2921.htm"
                                       title="Memcached(三)、封装Memcached和Ehcache" target="_blank">Memcached(三)、封装Memcached和Ehcache</a>
                                    <span class="text-muted">frank1234</span>
<a class="tag" taget="_blank" href="/search/memcached/1.htm">memcached</a><a class="tag" taget="_blank" href="/search/ehcache/1.htm">ehcache</a><a class="tag" taget="_blank" href="/search/spring+ioc/1.htm">spring ioc</a>
                                    <div>本文对Ehcache和Memcached进行了简单的封装,这样对于客户端程序无需了解ehcache和memcached的差异,仅需要配置缓存的Provider类就可以在二者之间进行切换,Provider实现类通过Spring IoC注入。 
cache.xml 
 
<?xml version="1.0" encoding="UTF-8"?>
</div>
                                </li>
                                <li><a href="/article/3048.htm"
                                       title="Remove Duplicates from Sorted List II" target="_blank">Remove Duplicates from Sorted List II</a>
                                    <span class="text-muted">hcx2013</span>
<a class="tag" taget="_blank" href="/search/remove/1.htm">remove</a>
                                    <div>Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. 
For example,Given 1->2->3->3->4->4->5,</div>
                                </li>
                                <li><a href="/article/3175.htm"
                                       title="Spring4新特性——注解、脚本、任务、MVC等其他特性改进" target="_blank">Spring4新特性——注解、脚本、任务、MVC等其他特性改进</a>
                                    <span class="text-muted">jinnianshilongnian</span>
<a class="tag" taget="_blank" href="/search/spring4/1.htm">spring4</a>
                                    <div>Spring4新特性——泛型限定式依赖注入 
Spring4新特性——核心容器的其他改进 
Spring4新特性——Web开发的增强 
Spring4新特性——集成Bean Validation 1.1(JSR-349)到SpringMVC  
Spring4新特性——Groovy Bean定义DSL 
Spring4新特性——更好的Java泛型操作API  
Spring4新</div>
                                </li>
                                <li><a href="/article/3302.htm"
                                       title="MySQL安装文档" target="_blank">MySQL安装文档</a>
                                    <span class="text-muted">liyong0802</span>
<a class="tag" taget="_blank" href="/search/mysql/1.htm">mysql</a>
                                    <div>  工作中用到的MySQL可能安装在两种操作系统中,即Windows系统和Linux系统。以Linux系统中情况居多。 
  安装在Windows系统时与其它Windows应用程序相同按照安装向导一直下一步就即,这里就不具体介绍,本文档只介绍Linux系统下MySQL的安装步骤。 
  Linux系统下安装MySQL分为三种:RPM包安装、二进制包安装和源码包安装。二</div>
                                </li>
                                <li><a href="/article/3429.htm"
                                       title="使用VS2010构建HotSpot工程" target="_blank">使用VS2010构建HotSpot工程</a>
                                    <span class="text-muted">p2p2500</span>
<a class="tag" taget="_blank" href="/search/HotSpot/1.htm">HotSpot</a><a class="tag" taget="_blank" href="/search/OpenJDK/1.htm">OpenJDK</a><a class="tag" taget="_blank" href="/search/VS2010/1.htm">VS2010</a>
                                    <div>1. 下载OpenJDK7的源码: 
     
http://download.java.net/openjdk/jdk7 
     
http://download.java.net/openjdk/ 
     
2. 环境配置 
    ▶</div>
                                </li>
                                <li><a href="/article/3556.htm"
                                       title="Oracle实用功能之分组后列合并" target="_blank">Oracle实用功能之分组后列合并</a>
                                    <span class="text-muted">seandeng888</span>
<a class="tag" taget="_blank" href="/search/oracle/1.htm">oracle</a><a class="tag" taget="_blank" href="/search/%E5%88%86%E7%BB%84/1.htm">分组</a><a class="tag" taget="_blank" href="/search/%E5%AE%9E%E7%94%A8%E5%8A%9F%E8%83%BD/1.htm">实用功能</a><a class="tag" taget="_blank" href="/search/%E5%90%88%E5%B9%B6/1.htm">合并</a>
                                    <div>1       实例解析  
由于业务需求需要对表中的数据进行分组后进行合并的处理,鉴于Oracle10g没有现成的函数实现该功能,且该功能如若用JAVA代码实现会比较复杂,因此,特将SQL语言的实现方式分享出来,希望对大家有所帮助。如下: 
表test 数据如下: 
ID,SUBJECTCODE,DIMCODE,VALUE 
1&nbs</div>
                                </li>
                                <li><a href="/article/3683.htm"
                                       title="Java定时任务注解方式实现" target="_blank">Java定时任务注解方式实现</a>
                                    <span class="text-muted">tuoni</span>
<a class="tag" taget="_blank" href="/search/java/1.htm">java</a><a class="tag" taget="_blank" href="/search/spring/1.htm">spring</a><a class="tag" taget="_blank" href="/search/jvm/1.htm">jvm</a><a class="tag" taget="_blank" href="/search/xml/1.htm">xml</a><a class="tag" taget="_blank" href="/search/jni/1.htm">jni</a>
                                    <div>Spring 注解的定时任务,有如下两种方式: 
第一种: 
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

 xmlns:xsi="http</div>
                                </li>
                                <li><a href="/article/3810.htm"
                                       title="11大Java开源中文分词器的使用方法和分词效果对比" target="_blank">11大Java开源中文分词器的使用方法和分词效果对比</a>
                                    <span class="text-muted">yangshangchuan</span>
<a class="tag" taget="_blank" href="/search/word%E5%88%86%E8%AF%8D%E5%99%A8/1.htm">word分词器</a><a class="tag" taget="_blank" href="/search/ansj%E5%88%86%E8%AF%8D%E5%99%A8/1.htm">ansj分词器</a><a class="tag" taget="_blank" href="/search/Stanford%E5%88%86%E8%AF%8D%E5%99%A8/1.htm">Stanford分词器</a><a class="tag" taget="_blank" href="/search/FudanNLP%E5%88%86%E8%AF%8D%E5%99%A8/1.htm">FudanNLP分词器</a><a class="tag" taget="_blank" href="/search/HanLP%E5%88%86%E8%AF%8D%E5%99%A8/1.htm">HanLP分词器</a>
                                    <div>本文的目标有两个: 
1、学会使用11大Java开源中文分词器 
2、对比分析11大Java开源中文分词器的分词效果 
本文给出了11大Java开源中文分词的使用方法以及分词结果对比代码,至于效果哪个好,那要用的人结合自己的应用场景自己来判断。 
11大Java开源中文分词器,不同的分词器有不同的用法,定义的接口也不一样,我们先定义一个统一的接口: 
/**
 * 获取文本的所有分词结果, 对比</div>
                                </li>
                </ul>
            </div>
        </div>
    </div>

<div>
    <div class="container">
        <div class="indexes">
            <strong>按字母分类:</strong>
            <a href="/tags/A/1.htm" target="_blank">A</a><a href="/tags/B/1.htm" target="_blank">B</a><a href="/tags/C/1.htm" target="_blank">C</a><a
                href="/tags/D/1.htm" target="_blank">D</a><a href="/tags/E/1.htm" target="_blank">E</a><a href="/tags/F/1.htm" target="_blank">F</a><a
                href="/tags/G/1.htm" target="_blank">G</a><a href="/tags/H/1.htm" target="_blank">H</a><a href="/tags/I/1.htm" target="_blank">I</a><a
                href="/tags/J/1.htm" target="_blank">J</a><a href="/tags/K/1.htm" target="_blank">K</a><a href="/tags/L/1.htm" target="_blank">L</a><a
                href="/tags/M/1.htm" target="_blank">M</a><a href="/tags/N/1.htm" target="_blank">N</a><a href="/tags/O/1.htm" target="_blank">O</a><a
                href="/tags/P/1.htm" target="_blank">P</a><a href="/tags/Q/1.htm" target="_blank">Q</a><a href="/tags/R/1.htm" target="_blank">R</a><a
                href="/tags/S/1.htm" target="_blank">S</a><a href="/tags/T/1.htm" target="_blank">T</a><a href="/tags/U/1.htm" target="_blank">U</a><a
                href="/tags/V/1.htm" target="_blank">V</a><a href="/tags/W/1.htm" target="_blank">W</a><a href="/tags/X/1.htm" target="_blank">X</a><a
                href="/tags/Y/1.htm" target="_blank">Y</a><a href="/tags/Z/1.htm" target="_blank">Z</a><a href="/tags/0/1.htm" target="_blank">其他</a>
        </div>
    </div>
</div>
<footer id="footer" class="mb30 mt30">
    <div class="container">
        <div class="footBglm">
            <a target="_blank" href="/">首页</a> -
            <a target="_blank" href="/custom/about.htm">关于我们</a> -
            <a target="_blank" href="/search/Java/1.htm">站内搜索</a> -
            <a target="_blank" href="/sitemap.txt">Sitemap</a> -
            <a target="_blank" href="/custom/delete.htm">侵权投诉</a>
        </div>
        <div class="copyright">版权所有 IT知识库 CopyRight © 2000-2050 E-COM-NET.COM , All Rights Reserved.
<!--            <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">京ICP备09083238号</a><br>-->
        </div>
    </div>
</footer>
<!-- 代码高亮 -->
<script type="text/javascript" src="/static/syntaxhighlighter/scripts/shCore.js"></script>
<script type="text/javascript" src="/static/syntaxhighlighter/scripts/shLegacy.js"></script>
<script type="text/javascript" src="/static/syntaxhighlighter/scripts/shAutoloader.js"></script>
<link type="text/css" rel="stylesheet" href="/static/syntaxhighlighter/styles/shCoreDefault.css"/>
<script type="text/javascript" src="/static/syntaxhighlighter/src/my_start_1.js"></script>





</body>

</html>