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, ... 服务发 ...
随机推荐
- 小程序存emoji表情 不改变数据库
1.小程序:提交前先编码 encodeURIComponent(data) 2.服务端解码(PHP) urldecode(data) 3.如果有空格字符串的,保存之前先对空格进行处理,不然空格在页面会 ...
- resulting in duplicate entry '1' for key 'primary'
现在有一个标签表,里面已经填入了一些数据了,想把主键生成策略改成自增的: ALTER TABLE `tags` CHANGE COLUMN `Id` `Id` INT(11) NOT NULL AUT ...
- Linux下svn服务器的安装与配置-备份-恢复-计划任务
简介:SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subvers ...
- idea自动抽取变量快捷键设置
file---setting---keymap---搜索variable 如下图:默认是ctrl+alt+v,这里修改成自己比较方便的快捷键即可,我这里设置的是alt+e
- 从FBV到CBV三(权限)
丛FBC到CBV三(权限) span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0 ...
- Hive入门指南
转自:http://blog.csdn.net/zhoudaxia/article/details/8842576 1.安装与配置 Hive是建立在Hadoop上的数据仓库软件,用于查询和管理存放在分 ...
- centos 7 安装 Oracle 12c
#!/bin/bash #!/usr/bin/expect -f #/etc/sysctl.conf --bash-srcipts-- echo 'net.ipv6.conf.all.disable_ ...
- python中的else语句
python语言和其它语言一样在支持else语句,通常else语句和if语句合用,完成程序的分支选择功能. 例如如下打印学成成绩代码: score = int(input("请输入成绩:&q ...
- mongodb aggregate
$project:修改输入文档的结构.可以用来重命名.增加或删除域,也可以用于创建计算结果以及嵌套文档. $match:用于过滤数据,只输出符合条件的文档.$match使用MongoDB的标准查询操作 ...
- python继承小demo
# -*- coding: utf-8 -*- """ 继承的意义:实现代码重用,数据函数都可以重用 子类覆盖,子类与父类同名 选择性继承 super().__init_ ...