kubectl get pods
kubectl get deploy
kubectl delete deploy nginx
4. 虽然删掉了 Pod,但是这是时候还有 service,我们可以也删掉
kubectl get services
kubectl delete svc nginx
mkdir pods
touch nginx-demo.yaml
参数名 | 类型 | 字段说明 |
---|---|---|
apiVersion | String | K8S APl 的版本,可以用 kubectl api versions 命令查询 |
kind | String | yam 文件定义的资源类型和角色 |
metadata | Object | 元数据对象,下面是它的属性 |
metadata.name | String | 元数据对象的名字,比如 pod 的名字 |
metadata.namespace | String | 元数据对象的命名空间 |
Spec | Object | 详细定义对象 |
spec.containers[] | list | 定义 Spec 对象的容器列表 |
spec.containers[].name | String | 为列表中的某个容器定义名称 |
spec.containers[].image | String | 为列表中的某个容器定义需要的镜像名称 |
spec.containers[].imagePullPolicy | string | 定义镜像拉取策略,有 Always、Never、IfNotPresent 三个值可选 - Always(默认):意思是每次都尝试重新拉取镜像 - Never:表示仅适用本地镜像 - IfNotPresent:如果本地有镜像就使用本地镜像,没有就拉取在线镜像。 |
spec.containers[].command[] | list | 指定容器启动命令,因为是数组可以指定多个,不指定则使用镜像打包时使用的启动命令。 |
spec.containers[].args[] | list | 指定容器启动命令参数,因为是数组可以指定多个。 |
spec.containers[].workingDir | string | 指定容器的工作目录 |
spec.containers[].volumeMounts[] | list | 指定容器内部的存储卷配置 |
spec.containers[].volumeMounts[].name | string | 指定可以被容器挂载的存储卷的名称 |
spec.containers[].volumeMounts[].mountPath | string | 指定可以被容器挂载的存储卷的路径 |
spec.containers[].volumeMounts[].readOnly | string | 设置存储卷路径的读写模式,ture 或者 false,默认是读写模式 |
spec.containers[].ports[] | list | 指定容器需要用到的端口列表 |
spec.containers[].ports[].name | string | 指定端口的名称 |
spec.containers[].ports[].containerPort | string | 指定容器需要监听的端口号 |
spec.containers[].ports[].hostPort | string | 指定容器所在主机需要监听的端口号,默认跟上面 containerPort 相同,注意设置了 hostPort 同一台主机无法启动该容器的相同副本(因为主机的端口号不能相同,这样会冲突) |
spec.containers[].ports[].protocol | string | 指定端口协议,支持 TCP 和 UDP,默认值为 TCP |
spec.containers[].env[] | list | 指定容器运行前需设置的环境变量列表 |
spec.containers[].env[].name | string | 指定环境变量名称 |
spec.containers[].env[].value | string | 指定环境变量值 |
spec.containers[].resources | Object | 指定资源限制和资源请求的值(这里开始就是设置容器的资源上限) |
spec.containers[].resources.limits | Object | 指定设置容器运行时资源的运行上限 |
spec.containers[].resources.limits.cpu | string | 指定 CPU 的限制,单位为 Core 数,将用于 docker run –cpu-shares 参数 |
spec.containers[].resources.limits.memory | string | 指定 mem 内存的限制,单位为 MIB、GiB |
spec.containers[].resources.requests | Object | 指定容器启动和调度时的限制设置 |
spec.containers[].resources.requests.cpu | string | CPU请求,单位为core数,容器启动时初始化可用数量 |
spec.containers[].resources.requests.memory | string | 内存请求,单位为MIB、GiB,容器启动的初始化可用数量 |
spec.restartPolicy | string | 定义 pod 的重启策略,可选值为 Always、OnFailure、Never,默认值为 Always。 - Always:pod 一旦终止运行,则无论容器是如何终止的,kubelet 服务都将重启它。 - OnFailure:只有 pod 以非零退出码终止时,kubelet 才会重启该容器。如果容器正常结束(退出码为0),则 kubectl 将不会重启它。 - Never:Pod 终止后,kubelet 将退出码报告给 master,不会重启该 pod |
spec.nodeSelector | Object | 定义 Node 的 label 过滤标签,以 key:value 格式指定 |
spec.imagePullSecrets | Object | 定义 pull 镜像时使用 secret 名称,以 name:secretkey 格式指定 |
spec.hostNetwork | Boolean | 定义是否使用主机网络模式,默认值为 false。设置 true 表示使用宿主机网络,不使用 docker 网桥,同时设置了 true将无法在同一台宿主机上启动第二个副本 |
apiVersion: v1 # api 文档版本
kind: Pod # 资源对象类型,可以配置为像Deployment StatefulSet这一类的对象
metadata: # Pod相关的元数据,用于描述Pod的数据
name: nginx-demo #Pod的名称
labels: # 定义Pod的标签,这个标签可以自己任意指定,是无所谓的
type: app # 自定义label标签,名字为type,值为app
version: 1.0.0 # 自定义label标签,描述版本号
namespace: 'default' # 命名空间的配置
spec: # 期望Pod按照这里面的描述进行创建
containers: # 对于Pod中的容器描述
- name: nginx # 容器的名称,这个是可以乱取的
image: nginx:1.7.9 # 指定容器的镜像,docker会去search
imagePullPolicy: IfNotPresent # 镜像拉去策略, 如果本地有就用本地的,如果本地没有就拉去远程的
command: # 指定容器启动时执行的命令
- nginx
- -g
- 'daemon off;' # nginx -g 'daemon off;'
workingDir: /usr/share/nginx/html/ # 定义容器启动后的工作目录
ports:
- name: http # 端口名称,随便起
containerPort: 80 # 描述容器要暴露什么端口
protocol: TCP # 描述端口是用那种通信协议
env: # 环境变量
- name: JVM_OPTS # 环境变量的名称
value: '-Xms128m -Xmx128m'
resources:
requests: # 最少需要多少资源
cpu: 100m # 限制cpu最少使用0.1个核心,一个核心用满是1000m
memory: 128Mi # 限制内存最少使用128兆
limits: # 最多可以使用多少资源
cpu: 200m # 限制cpu最多使用0.2个核心
memory: 256Mi # 限制最多使用256兆
restartPolicy: OnFailure #Secrets 重启策略,只有失败的情况才会重启
kubectl create -f nginx-demo.yaml
kubectl get po
kubectl describe po nginx-demo
kubectl get po -o wide
curl 10.244.169.135
route -n
docker ps
startupProbe:
httpGet:
path: /api/startup
port: 80
livenessProbe:
failureThreshold: 5
httpGet:
path: /health
port: 8080
scheme: HTTP
initialDelaySeconds: 60
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 5
readinessProbe:
failureThreshold: 3 # 错误次数
httpGet:
path: /ready
port: 8181
scheme: HTTP
periodSeconds: 10 # 间隔时间
successThreshold: 1
timeoutSeconds: 1
livenessProbe:
exec:
command:
- cat
- /health
livenessProbe:
tcpSocket:
port: 80
livenessProbe:
failureThreshold: 5
httpGet:
path: /health
port: 8080
scheme: HTTP
httpHeaders:
- name: xxx
value: xxx
# 删除 po
kubectl delete po nginx-demo
# 重新启动容器
kubectl create -f nginx-demo.yaml
# 观察 pod
kubectl describe po nginx-demo
kubectl get po
...
spec: # 期望Pod按照这里面的描述进行创建
containers: # 对于Pod中的容器描述
- name: nginx # 容器的名称,这个是可以乱取的
image: nginx:1.7.9 # 指定容器的镜像,docker会去search
imagePullPolicy: IfNotPresent # 镜像拉去策略, 如果本地有就用本地的,如果本地没有就拉去远程的
# ===== 这里才是新添加 =====
startupProbe:
httpGet:
path: /index.html # http 请求路径
port: 80 # 请求端口
failureThreshold: 3 # 失败多少次才算失败
periodSeconds: 10 # 间隔时间
successThreshold: 1 # 多少次检测成功算成功
timeoutSeconds: 5 # 请求的超时时间
# =====
command: # 指定容器启动时执行的命令
- nginx
- -g
- 'daemon off;' # nginx -g 'daemon off;'
workingDir: /usr/share/nginx/html/ # 定义容器启动后的工作目录
...
...
spec: # 期望Pod按照这里面的描述进行创建
containers: # 对于Pod中的容器描述
- name: nginx # 容器的名称,这个是可以乱取的
image: nginx:1.7.9 # 指定容器的镜像,docker会去search
imagePullPolicy: IfNotPresent # 镜像拉去策略, 如果本地有就用本地的,如果本地没有就拉去远程的
# ===== 这里才是新添加 =====
startupProbe:
#httpGet:
# path: /index.html # http 请求路径
tcpSocket:
port: 80 # 请求端口
failureThreshold: 3 # 失败多少次才算失败
periodSeconds: 10 # 间隔时间
successThreshold: 1 # 多少次检测成功算成功
timeoutSeconds: 5 # 请求的超时时间
# =====
command: # 指定容器启动时执行的命令
- nginx
- -g
- 'daemon off;' # nginx -g 'daemon off;'
...
...
spec: # 期望Pod按照这里面的描述进行创建
containers: # 对于Pod中的容器描述
- name: nginx # 容器的名称,这个是可以乱取的
image: nginx:1.7.9 # 指定容器的镜像,docker会去search
imagePullPolicy: IfNotPresent # 镜像拉去策略, 如果本地有就用本地的,如果本地没有就拉去远程的
# ===== 这里才是新添加 =====
startupProbe:
#httpGet:
# path: /index.html # http 请求路径
#tcpSocket:
# port: 80 # 请求端口
exec:
command:
- sh
- -c
- "sleep 3; echo successaaa > /inited"
failureThreshold: 3 # 失败多少次才算失败
periodSeconds: 10 # 间隔时间
successThreshold: 1 # 多少次检测成功算成功
timeoutSeconds: 5 # 请求的超时时间
# =====
command: # 指定容器启动时执行的命令
- nginx
- -g
- 'daemon off;' # nginx -g 'daemon off;'
...
kubectl exec -it nginx-demo -c nginx -- cat /inited
...
spec: # 期望Pod按照这里面的描述进行创建
containers: # 对于Pod中的容器描述
- name: nginx # 容器的名称,这个是可以乱取的
image: nginx:1.7.9 # 指定容器的镜像,docker会去search
imagePullPolicy: IfNotPresent # 镜像拉去策略, 如果本地有就用本地的,如果本地没有就拉去远程的
startupProbe:
exec:
command:
- sh
- -c
- "sleep 3; echo successaaa > /inited"
failureThreshold: 3 # 失败多少次才算失败
periodSeconds: 10 # 间隔时间
successThreshold: 1 # 多少次检测成功算成功
timeoutSeconds: 5 # 请求的超时时间
# ===== 这里才是新添加 =====
livenessProbe:
httpGet:
path: /started.html # http 请求路径,这个路径肯定不存在
port: 80
failureThreshold: 3 # 失败多少次才算失败
periodSeconds: 10 # 间隔时间
successThreshold: 1 # 多少次检测成功算成功
timeoutSeconds: 5 # 请求的超时时间
# =====
command: # 指定容器启动时执行的命令
- nginx
- -g
- 'daemon off;' # nginx -g 'daemon off;'
...
echo 'started' > started.html
kubectl cp started.html nginx-demo:/usr/share/nginx/html/
...
spec: # 期望Pod按照这里面的描述进行创建
containers: # 对于Pod中的容器描述
- name: nginx # 容器的名称,这个是可以乱取的
image: nginx:1.7.9 # 指定容器的镜像,docker会去search
imagePullPolicy: IfNotPresent # 镜像拉去策略, 如果本地有就用本地的,如果本地没有就拉去远程的
startupProbe:
#httpGet:
# path: /index.html # http 请求路径
#tcpSocket:
# port: 80 # 请求端口
exec:
command:
- sh
- -c
- "sleep 3; echo successaaa > /inited"
failureThreshold: 3 # 失败多少次才算失败
periodSeconds: 10 # 间隔时间
successThreshold: 1 # 多少次检测成功算成功
timeoutSeconds: 5 # 请求的超时时间
# ===== 这里才是新添加 =====
readinessProbe:
httpGet:
path: /started.html # http 请求路径,这个路径肯定不存在
port: 80
failureThreshold: 5 # 失败多少次才算失败
periodSeconds: 10 # 间隔时间
successThreshold: 1 # 多少次检测成功算成功
timeoutSeconds: 3 # 请求的超时时间
# =====
command: # 指定容器启动时执行的命令
- nginx
- -g
- 'daemon off;' # nginx -g 'daemon off;'
...
echo 'started' > started.html
kubectl cp started.html nginx-demo:/usr/share/nginx/html/
# 配置参数:
# 作用于 pod 中的所有容器(默认)
terminationGracePeriodSeconds: 30
containers:
- xxx
...
spec: # 期望Pod按照这里面的描述进行创建
# ===== 这里才是新添加 =====
# 一定要注意,如果 preStop 的任务执行超过 30s,一定要修改这个配置同时间,否则会直接结束容器。同时,如果 preStop 任务提前结束,容器也会提前结束,而不是限定 30s
terminationGracePeriodSeconds: 30
# =====
containers: # 对于Pod中的容器描述
- name: nginx # 容器的名称,这个是可以乱取的
image: nginx:1.7.9 # 指定容器的镜像,docker会去search
imagePullPolicy: IfNotPresent # 镜像拉去策略, 如果本地有就用本地的,如果本地没有就拉去远程的
# ===== 这里才是新添加 =====
lifecycle: # 生命周期配置
postStart: # 生命周期启动阶段做的事情,不一定在容器的 command 之前运行
exec:
command:
- sh
- -c
- "echo 'pre stop
' > /usr/share/nginx/html/prestop.html"
preStop:
exec:
command:
- sh
- -c
- "echo 'sleep finished...' >> /usr/share/nginx/html/prestop.html; sleep 50"
# =====
command: # 指定容器启动时执行的命令
- nginx
- -g
- 'daemon off;' # nginx -g 'daemon off;'
...
kubectl get po -w
time kubectl delete po nginx-demo