美文网首页
Kubernetes配置Pod和容器(十一) 容器生命周期时间附

Kubernetes配置Pod和容器(十一) 容器生命周期时间附

作者: YiQinGuo | 来源:发表于2017-06-01 16:20 被阅读704次

    容器生命周期时间附加操作器

    这个章节展示了如果给容器生命周期时间附加操作器。Kubernetes支持postStart和preStop事件。当容器启动以后Kubernetes立即发送一个postStart事件,并且在容器退出之前理解发送一个preStop事件。

    定义postStart和preStop操作器

    在本次试验,创建包含一个容器的pod。这个容器包含postStart和preStop事件操作器。

    下面是这个Pod的配置文件:

    apiVersion: v1
    kind: Pod
    metadata:
      name: lifecycle-demo
    spec:
      containers:
      - name: lifecycle-demo-container
        image: nginx
    
        lifecycle:
          postStart:
            exec:
              command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
          preStop:
            exec:
              command: ["/usr/sbin/nginx","-s","quit"]
    

    在这个配置文件里面,可以看到postStart命令写了一个message文件在容器的/usr/share目录里面。preStop命令优雅的退出nginx。如果容器因为失败而退出这是非常有用的。

    创建一个pod:

    kubectl create -f handler.yaml
    

    验证pod中的容器是否运行:

    kubectl get pod lifecycle-demo
    

    用shell进入运行中的容器里面:

    kubectl exec -it lifecycle-demo -- /bin/bash
    

    在shell里面验证postStart操作器创建的message文件 :

    root@lifecycle-demo:/# cat /usr/share/message
    

    输出展示了postStart操作器写入的文本:

    Hello from the postStart handler
    

    相关文章

      网友评论

          本文标题:Kubernetes配置Pod和容器(十一) 容器生命周期时间附

          本文链接:https://www.haomeiwen.com/subject/yteefxtx.html