5、kubernetes资源清单之Pod应用190709
一、Pod镜像及端口
- 获取帮助文档
# kubectl explain pod.spec.containers
spec.containers <[]object>
- pod.spec.containers.imagePullPolicy:镜像的拉取策略
- name <string>
image <string>
imagePullPolicy: <string> #如果标签是latest则默认值是Always,如果是其他标签则默认值是IfNotPresent
Always:总是去仓库下载,latest标签的镜像用
Never:本地有就用,没有就不用
IfNotPresent:本地有用本地的,本地没有去仓库下载
- pod.spec.containers.ports:端口的暴露
ports:
- name: http
containerPort: 80
- name: https
containerPort: 443
- pod.spec.containers.command和args:相当于dockerfile中的ENTRYPOINT和CMD,将args当做参数传递给command;变量引用:$(VAR_NAME);command和args的关系参考:https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
二、Pod标签及标签选择器和注解
- pod标签
metadata:
name: pod-demo
namespace: default
labels:
app: myapp
tier: frontend
# kubectl get pods --show-labels #查看所有pod的标签
# kubectl get pods --show-labels -L app #显示拥有app标签的值
# kubectl get pods --show-labels -L app,run #显示多个标签的标签值
# kubectl get pods --show-labels -l app #过滤拥有app标签的pod
# kubectl get pods --show-labels -l app=myapp #基于等值的标签选择器(=, ==, !=)
# kubectl get pods --show-labels -l "app in (myapp,noapp)" #基于集合关系的标签选择器(in, ontin)
# kubectl label pods pod-demo release=canary #给pod打标
# kubectl label pods pod-demo release=stable --overwrite #修改标签的值
- node标签
# kubectl get nodes --show-labels #基于nodeSelector节点选择器
# kubectl label node node01 disktype=ssd #给node01增加disktype=ssd的标签
- nodeSelector
spec:
nodeSelector: #使其pod只能运行在拥有disktype=ssd标签的node上
disktype: ssd
- nodeName
spec:
nodeName: node01 #使其pod只能运行在node01上
- annotations:与label不同的地方在于它不能用于挑选资源对象,仅用于为对象提供“元数据”
metadata:
annotations:
dongfei.tech/created-by: "cluster admin"
# kubectl describe pods pod-demo |grep Annotations
三、Pod生命周期
1、Pod生命周期中的行为
- init container:初始化容器,为主容器准备环境,可以有多个初始化容器(串行执行)
- main container:主容器
- post start:主容器启动后执行的程序
- liveness probe:存活状态监测,监测主进程是否正在运行
- readiness probe:就绪状态监测,监测主进程提供的服务是否就绪
- pre stop:主容器结束前执行的程序
2、Pod生命周期的状态
- Pending:挂起状态
- Running:运行状态
- Failed:失败状态
- Succeeded:成功状态
- Unknown:未知状态
3、Pod重启策略
spec:
restartPolicy:
Always:默认,总是重启
OnFailure:Pod失败则会重启
Never:不会重启
四、Pod容器存活性探测和就绪性探测
- 三种探针类型:ExecAction、TCPSocketAction、HTTPGetAction
1、存活性探测
- pods.spec.containers.livenessProbe.exec:存活性探测之exec探针
# cat liveness-exec.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-exec-pod
namespace: default
spec:
containers:
- name: liveness-exec-container
image: busybox:latest
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "-c","touch /tmp/healthy; sleep 10; rm -rf /tmp/healthy; sleep 3600"]
livenessProbe:
exec:
command: ["test","-e","/tmp/healthy"] #探测命令
initialDelaySeconds: 1 #初始化延迟时间,默认0s
periodSeconds: 3 #隔多长时间探测一次,默认10s
failureThreshold: 3 #探测失败3次为失败,默认3次
successThreshold: 1 #探测成功1次为成功
restartPolicy: Always #探测失败时的重启策略
# kubectl get pods -w #监控POD状态
# kubectl describe pods liveness-exec-pod |grep "Restart Count" #查看Pod重启次数
- pods.spec.containers.livenessProbe.tcpSocket:存活性探测之tcpSocket探针
- pods.spec.containers.livenessProbe.httpGet:存活性探测之httpGet探针
# cat liveness-httpget.yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-httpget-pod
namespace: default
spec:
containers:
- name: liveness-httpget-container
image: dongfeimg/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
# kubectl exec -it liveness-httpget-pod -- /bin/sh #手动连入pod
/ # rm -f /usr/share/nginx/html/index.html #删除index.html文件,探测失败会重启
2、就绪性探测
- pods.spec.containers.readinessProbe.httpGet:就绪性探测之httpGet探针
# cat readiness-httpget.yaml
apiVersion: v1
kind: Pod
metadata:
name: readiness-httpget-pod
namespace: default
spec:
containers:
- name: readiness-httpget-container
image: dongfeimg/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
readinessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
- 其他参考存活性探测
五、启动后和终止前钩子
- pods.spec.containers.lifecycle.postStart:启动后钩子
# cat poststart-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: poststart-pod
namespace: default
spec:
containers:
- name: busybox-httpd
image: busybox:latest
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command: ["mkdir","-p","/data/web/html"] #在command命令后执行此命令
command: ["/bin/sh","-c","sleep 3600"]
- pods.spec.containers.lifecycle.preStop:终止前钩子
5、kubernetes资源清单之Pod应用190709的更多相关文章
- kubernetes资源清单之pod
什么是pod? Pod是一组一个或多个容器(例如Docker容器),具有共享的存储/网络,以及有关如何运行这些容器的规范. Pod的内容始终位于同一地点,并在同一时间安排,并在共享上下文中运行. Po ...
- 6、kubernetes资源清单之Pod控制器190714
一.Pod控制器的类别 ReplicationController:早期唯一的控制器,已废弃 ReplicaSet:控制Pod满足用户期望副本:标签选择器选择由自己管理的Pod副本:Pod资源模板完成 ...
- Kubernetes中资源清单与Pod的生命周期(二)
一.资源清单 1,定义: 在k8s中一般使用yaml格式的文件来创建符合我们预期的资源,这样的yaml被称为资源清单. 使用资源清单创建Pod: kubectl apply -f nginx.yaml ...
- kubernetes 资源清单定义入门
k8s中的资源 什么叫资源? k8s中所有的内容都抽象为资源, 资源实例化之后,叫做对象 在k8s中有哪些资源? 工作负载型资源(workload): Pod ReplicaSet Deploymen ...
- kubernetes系列06—kubernetes资源清单定义入门
本文收录在容器技术学习系列文章总目录 1.认识kubernetes资源 1.1 常用资源/对象 workload工作负载型资源:pod,ReplicaSet,Deployment,StatefulSe ...
- 【04】Kubernets:资源清单(pod)
写在前面的话 前面我们提到过,纯手敲 K8S 名称管理 K8S 服务只是作为我们了解 K8S 的一种方案,而我们最终管理 K8S 的方法还是通过接下来的资源清单的方式进行管理. 所以从本章节开始,将会 ...
- (四)Kubernetes 资源清单定义
Kubernetes常用资源对象 依据资源的主要功能作为分类标准,Kubernetes的API对象大体可分为五个类别,如下: 类型 名称 工作负载(Workload) Pod.ReplicaSet.D ...
- Kubernetes 学习5 kubernetes资源清单定义入门
一.kubernetes是有一个restful风格的 API,把各种操作对象都一律当做资源来管理.并且可通过标准的HTTP请求的方法 GET,PUT,DELETE,POST,等方法来完成操作,不过是通 ...
- 4、kubernetes资源清单快速入门190625
一.资源清单概念 资源/对象的类型 工作负载型资源:Pod, ReplicaSet, Deployment, StatefulSet, DaemonSet, Job, Cronjob, ... 服务发 ...
随机推荐
- 错误:SyntaxError: identifier starts immediately after numeric literal
转载:http://blog.csdn.net/shalousun/article/details/39995443在用JavaScript时,当你使用一个字符传作为函数的参数常常会看到语法错误,在f ...
- Git复习(八)之快速理解Git结构
git pull:拉取远程服务器最新代码到本地(会自动merge) git add:将本地代码添加到暂存区 git commit:将暂存区的所有内容提交到当前分支(git会自动为我们创建第一个分支 ...
- weex animation模块 使用指南
本节学习目标 掌握内置组件animation的使用 我们在开发应用的时候,常常需要增加一些动画效果,来提高用户体验,经常用到的一些动画效果如下 平移 旋转 缩放 背景颜色改变 组件透明图 weex 提 ...
- 禁止antd Input.Password浏览器自动回传
设置autoComplete为new-password
- Django 使用request获取浏览器发送的参数(Django编程-5)
1.url:需要正则去匹配 url(r'^index/(num)/$',view.index) 匹配到的参数会自动传入对应的视图函数 也可以给匹配到的参数起名字?P url(r'^index/(?P& ...
- Python(os和sys)理解
Python(os和sys)理解 os模块负责程序与操作系统的交互,提供了访问操作系统底层的接口; sys模块负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python的运行时 ...
- java的移位和异或运算
Java移位运算种类 基础:我们知道在Java中int类型占32位,可以表示一个正数,也可以表示一个负数.正数换算成二进制后的最高位为0,负数的二进制最高为为1 例子: -5换算成二进制后为:1111 ...
- mybatis原理解析
本文是结合spring-mybatis整合进行的分析 1.先看看依赖的jar包: <dependency> <groupId>org.mybatis</groupId&g ...
- js事件总汇
Mouse 事件 描述onClick 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击 onDblClick 鼠标双击事件 on ...
- Summer training round2 #10(Training 30)
A:签到题 B!:搜索+DP #include<bits/stdc++.h> #define mp make_pair #define pi pair<int,int> usi ...