k8s

四、Pod进阶

2021-11-22  本文已影响0人  jan29

1、通过yaml文件创建pod

apiVersion: v1
kind: Pod
metadata:
  name: base-pod
spec:
  containers:
  - name: nginx
    image: nginx:1.14

apiVersion:api的版本,可以通过kubectl api-versions查看
kind:资源的类型
metadata:元数据描述,描述kind定义的资源类型的元数据
spec:用户期望的状态
status:容器创建后系统的状态

2、metadata字段

2.1namespace

apiVersion: v1
kind: Pod
metadata:
  name: base-pod
  namespace: default

命名空间,不同类型的pod运行在创建出来的不同的命名空间当中

2.2labels

apiVersion: v1
kind: Pod
metadata:
  name: base-pod
  namespace: default
  labels:
    app: nginx
    test: busybox

标签,pod的调度和其它资源选择pod时通过标签进行选择
通过key/value键值对的形式进行定义

2.3annotations

apiVersion: v1
kind: Pod
metadata:
  name: base-pod
  namespace: default
  labels:
    app: nginx
    test: busybox
  annotations:
    release: beta
    author: jan29

注解 与Label类似,也使用key/value键值对的形式进行定义。
用Annotation来记录的信息包括但不限于
1、build信息、release信息、Docker镜像信息等,例如时间戳、release id号、PR号、镜像hash值、docker registry地址等;
2、日志库、监控库、分析库等资源库的地址信息;
3、程序调试工具信息,例如工具名称、版本号等;
4、团队的联系信息,例如电话号码、负责人名称、网址等。

3、spec字段

3.1containers

用于定义容器,可以定义多个

3.1.1ports

spec:
  containers:
  - name: nginx
    image: nginx:1.14
    ports:
    - name: nginx
      containerPort: 80
      hostPort: 80

用于定义port
containerPort:用于定义容器开放的端口
hostIP:用于定义主机IP地址
hostPort:用于映射主机的端口
name:同于定义容器名字
protocol:用于定义端口协议

3.1.2imagePullPolicy

spec:
  containers:
  - name: nginx
    image: nginx:1.14
    ports:
    - name: nginx
      containerPort: 80
      hostPort: 80
    env:
    - name: LANG
      value: en_us.UTF-8
    - name: USER
      value: root
    imagePullPolicy: IfNotPresent

镜像拉取有三种策略
Always:总是从镜像仓库拉取
Never:重来不从镜像仓库拉取,只使用本地镜像
IfNotPresent:如果本地有就使用本地镜像,如果没有就从镜像仓库拉取

3.1.3livenessProbe存活性探测

spec:
  containers:
  - name: nginx
    image: nginx:1.14
    ports:
    - name: nginx
      containerPort: 80
      hostPort: 80
    env:
    - name: LANG
      value: en_us.UTF-8
    - name: USER
      value: root
    imagePullPolicy: IfNotPresent
    livenessProbe:
      tcpSocket:
        port: 80

用于探测容器是否存活
探测的策略有三种
exec:通过命令探测
httpGet:向指定地址发送get请求探测
tcpSocket:向指定端口发送请求探测

3.1.4readinessProbe就绪性探测

spec:
  containers:
  - name: nginx
    image: nginx:1.14
    ports:
    - name: nginx
      containerPort: 80
      hostPort: 80
    env:
    - name: LANG
      value: en_us.UTF-8
    - name: USER
      value: root
    imagePullPolicy: IfNotPresent
    livenessProbe:
      tcpSocket:
        port: 80
    readinessProbe:
      initialDelaySeconds: 5
      failureThreshold: 3
      successThreshold: 3
      periodSeconds: 1
      timeoutSeconds: 3
      httpGet:
        path: /
        port: 80

用于探测容器是否就绪
failureThreshold:连续探测失败几次认为该容器没有存活或就绪
successThreshold:连续探测成功几次认为该容器存活或就绪
initialDelaySeconds:容器启动几秒后开始探测
periodSeconds:探测周期,几秒钟探测一次
timeoutSeconds:探测超过多长时间没有相应认为超时

3.1.5lifecycle生命周期钩子

容器启动后或者容器推出前都可以进行相应的操作被称为钩子

3.1.5.1postStart启动后配置钩子
spec:
  containers:
  - name: busybox
    image: busybox
    command:
     - "/bin/sh"
    args:
     - "-c"
     - "sleep 3600"
    lifecycle:
      postStart:
        exec:
          command:
            - "/bin/sh"
            - "-c"
            - "ping -c 4 baidu.com"

创建钩子也有三种方式
exec,httpGet和tcpSocket

3.1.5.2prestop停止前配置钩子
spec:
  containers:
  - name: busybox
    image: busybox
    command:
     - "/bin/sh"
    args:
     - "-c"
     - "sleep 3600"
    lifecycle:
      postStart:
        exec:
          command:
            - "/bin/sh"
            - "-c"
            - "ping -c 4 baidu.com"
      preStop:
        exec:
          command:
            - "/bin/sh"
            - "-c"
            - "wget -O - -q https://baidu.com"

和prostStart相同配置

3.2restartPolicy

spec:
  containers:
  - name: nginx
    image: nginx:1.14
  restartPolicy: OnFailure

Pod重启策略也有三种
Always:只要退出就会重启
OnFailure:退出状态码不为1重启,也就是非正常退出重启
Never:从来不会重启

3.3nodeSelector

spec:
  containers:
  - name: nginx
    image: nginx:1.14
 restartPolicy: OnFailure
 nodeSelector:
     kubernetes.io/hostname: node2

选择一个标签匹配的节点进行调度
也可以通过nodeName进行指定

标签查看与创建分别可以使用以下命令

  • kubectl get <资源类型> --show-labels
  • kubectl label <资源类型> <资源名字> <key>=<value>
上一篇 下一篇

猜你喜欢

热点阅读