目录
1、Pod 概述
2、Pod的生命周期和重启策略
3、Pod健康检查
3.1、 LivenessProbe探针:判断容器是否存活(running)
3.1.1、 ExecAction:在容器内部执行一个命令,如果返回码为0,则表示健康
3.1.2 、TcpAction:通过IP 和port ,如果能够和容器建立连接则表示容器健康
3.1.3、 HttpGetAction:发送一个http Get请求(ip+port+请求路径)如果返回状态吗在200-400之间则表示健康
3.2、ReadinessProbe探针: 用于判断容器是否启动完成(ready)
4、Pod 调度
5、Pod 模块定义详解
6、Pod实战
6.1 、创建Pod容器和外部访问服务
6.2 、集群外部如何访问Pod
6.3、Pod中多个容器
1、Pod 概述
Pod是kubernetes中你可以创建和部署的最小也是最简的单位。Pod代表着集群中运行的进程。
Pod中封装着应用的容器(有的情况下是好几个容器),存储、独立的网络IP,管理容器如何运行的策略选项。Pod代表着部署的一个单位:kubernetes中应用的一个实例,可能由一个或者多个容器组合在一起共享资源。
在Kubrenetes集群中Pod有如下两种使用方式:
2、Pod的生命周期和重启策略
pod一共有四种生命状态:
状态值 |
描述 |
Pending |
APIserver已经创建该server,但pod内有一个或多个容器的镜像还未创建,可能在下载中。 |
Running |
pod正常启动 |
Failed |
Pod内所有容器都已退出,其中至少有一个容器退出失败。 |
Unknown |
由于某种原因无法获取Pod的状态比如网络不通。 |
pod一共有三种重启策略:
重启策略 |
描述 |
Always |
容器失效时,即重启 |
OnFailure |
容器终止运行,且退出码不为0 时重启 |
Never |
Pod不重启 |
3、Pod健康检查
Kubernetes内部通过2种探针,实现了对Pod健康的检查
3.1、 LivenessProbe探针:判断容器是否存活(running)
LivenessProbe探针通过三种方式来检查容器是否健康
3.1.1、 ExecAction:在容器内部执行一个命令,如果返回码为0,则表示健康
apiVersion: v1 kind: Pod metadata: name: liveness spec: containers: - name : liveness image: liveness livenessProbe: exec: command: - cat - /tmp/health initialDelaySeconds: 30 periodSeconds: 60 timeoutSeconds: 5 |
3.1.2 、TcpAction:通过IP 和port ,如果能够和容器建立连接则表示容器健康
apiVersion: v1 kind: Pod metadata: name: pod-with-healthcheck spec: containers: - name : nginx image: nginx ports: - containerPort : 80 livenessProbe: tcpSocket: port: 80 initialDelaySeconds: 30 periodSeconds: 60 timeoutSeconds: 5 |
3.1.3、 HttpGetAction:发送一个http Get请求(ip+port+请求路径)如果返回状态吗在200-400之间则表示健康
apiVersion: v1 kind: Pod metadata: name: pod-with-healthcheck spec: containers: - name : nginx image: nginx ports: - containerPort : 80 livenessProbe: httpGet: path: / port: 80 initialDelaySeconds: 30 periodSeconds: 60 timeoutSeconds: 5 |
3.2、ReadinessProbe探针: 用于判断容器是否启动完成(ready)
ReadinessProbe探针与livenessProbe一样也支持exec、httpGet、TCP的探测方式,配置方式相同,只不过是将livenessProbe字段修改为ReadinessProbe。
4、Pod 调度
在kubernetes系统中,pod在大部分场景下都只是容器的载体而已,通常需要通过ReplicaSet, Deployment,DaemonSet,Job等对象来完成Pod的调度与自动控制功能。
全自动调度–controller 控制pod
RC的主要功能之一就是自动部署一个容器应用的多份副本,以及持续监控,保持集群内有一定数量的副本数量(配置文件指定了副本数量)。
定向调度
kubernetes中的Schduler 负责实现pode的调度,他会根据一些复杂的算法,把pod调度到某一个Node上。
后期详细讲解Pod全自动调度Controller
5、Pod 模块定义详解
apiVersion: v1 //版本 kind: pod //类型,pod metadata: //元数据 name: String //元数据,pod的名字 namespace: String //元数据,pod的命名空间 labels: //元数据,标签列表 - name : String //元数据,标签的名字 annotations: //元数据 , 自定义注解列表 - name : String //元数据 , 自定义注解名字 spec: //pod中容器的详细定义 containers: //pod中的容器列表,可以有多个容器 - name : String image: String //容器中的镜像 imagesPullPolicy: [ Always|Never|IfNotPresent ] //获取镜像的策略 command: [ String ] //容器的启动命令列表(不配置的话使用镜像内部的命令) args: [ String ] //启动参数列表 workingDir: String //容器的工作目录 volumeMounts: //挂载到到容器内部的存储卷设置 - name : String mountPath: String readOnly: boolean ports: //容器需要暴露的端口号列表 - name : String containerPort: int //容器要暴露的端口 hostPort: int //容器所在主机监听的端口(容器暴露端口映射到宿主机的端口) protocol: String env: //容器运行前要设置的环境列表 - name : String value: String resources: //资源限制 limits: cpu: Srting memory: String requeste: cpu: String memory: String livenessProbe: //pod内容器健康检查的设置 exec: command: [ String ] httpGet: //通过httpget检查健康 path: String port: number host: String scheme: Srtring httpHeaders: - name : Stirng value: String tcpSocket: //通过tcpSocket检查健康 port: number initialDelaySeconds: 0//首次检查时间 timeoutSeconds: 0 //检查超时时间 periodSeconds: 0 //检查间隔时间 successThreshold: 0 failureThreshold: 0 securityContext: //安全配置 privileged: falae restartPolicy: [ Always|Never|OnFailure ] //重启策略 nodeSelector: object //节点选择 imagePullSecrets: - name : String hostNetwork: false //是否使用主机网络模式,默认否 volumes: //在该pod上定义共享存储卷 - name : String meptyDir: { } hostPath: path: string secret: //类型为secret的存储卷 secretName: String item: - key : String path: String configMap: //类型为configMap的存储卷 name: String items: - key : String path: String |
6、Pod实战
在创建Pod容器之前,我们应该将我们项目打成镜像,上传到我们的仓库。
将项目编译打包成jar文件或者war文件,编写对应的Dockerfile文件 上传镜像到docker仓库 第一步:登陆(注意:Linux环境! 特殊符号需要转义 K2pass\!\!) docker login -u admin -p K2pass!! harbor.pd.k2software.com.cn 或者 docker login harbor.pd.k2software.com.cn 第二步:创建镜像( Dockerfile 文件 ) docker build . -t harbor.pd.k2software.com.cn/test/dockerdemo:v1. 0 第三步: docker push harbor.pd.k2software.com.cn/test/dockerdemo:v1. 0 |
6.1 、创建Pod容器和外部访问服务
编写 demo-test.yaml 文件
apiVersion: v1 kind: Pod metadata: name: demo-test spec: hostNetwork: true containers: - name : demo-test image: harbor.pd.k2software.com.cn/test/dockerdemo : v1.0 ports: - name : http containerPort: 8080 protocol: TCP livenessProbe: httpGet: path: /api/show port: http initialDelaySeconds: 30 periodSeconds: 30 timeoutSeconds: 5 successThreshold: 1 failureThreshold: 5 readinessProbe: httpGet: path: /api/show port: http initialDelaySeconds: 30 periodSeconds: 10 timeoutSeconds: 5 successThreshold: 1 failureThreshold: 5 resources: limits: cpu: 500m memory: 1Gi requests: cpu: 200m memory: 512Mi |
执行kubectl apply 命令创建 pod:
kubectl apply -f demo-pod-test.yaml |
查看创建好的Pod:
kubectl get pod NAME READY STATUS RESTARTS AGE demo-test 1 / 1 Running 0 23m |
6.2 、集群外部如何访问Pod
如果想要让外部客户端访问容器,可以将Pod的端口映射到宿主机。
第一种方式:
通过设置容器级别的hostPort,将容器应用的端口号映射到物理机上:
第二种方式:
通过设置Pod级别的hostNetWork=true,该Pod中所有容器的端口号都将被直接映射到物理机上。特别注意:如果设置hostNetWork=true时,在容器的ports定义部分如果不定义
hostPort,则hostPort默认等于containerPort。如果定义了hostPort,则hostPort必须等于containerPort的值
通过物理机的IP地址和hostPort端口号访问Pod内容器服务:
查看Pod安装到那台物理机上:
kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE demo-test 1 / 1 Running 0 43m 172.18 . 10.42 172.18 . 10.42 |
访问 :172.18.10.42:8080/api/show
6.3、Pod中多个容器
一个Pod中包含两个容器: tomcat 和 busybox
编写duplit-pod.yaml 文件如下: apiVersion: v1 kind: Pod metadata: name: duplit-pod spec: containers: - name : tomcat image: tomcat ports: - containerPort : 8080 volumeMounts: - name : app-logs mountPath: /usr/local/tomcat/logs - name : busybox image: busybox command: [ "sh" , "-c" , "tail -f /logs/catalina*.log" ] volumeMounts: - name : app-logs mountPath: /logs volumes: - name : app-logs emptyDir: { } 一个Pod中多个容器共享存储卷Volume. tomcat容器共享卷写日志,busybox读取共享卷日志。 kubectl get pod NAME READY STATUS RESTARTS AGE duplit-pod 2/2 Running 0 9m 可以看到duplit-pod启动了两个容器。 |