【问题标题】:"container started" event of a pod from kubernetes using pythons kubernetes library使用 pythons kubernetes 库的 kubernetes 的 pod 的“容器启动”事件
【发布时间】:2019-09-09 01:21:30
【问题描述】:

我有一个部署,其中一个容器具有 postStart 钩子,如下所示

      containers:
      - name: openvas
        image: my-image:test
        lifecycle:
            postStart:
              exec:
                 command:
                 - /usr/local/tools/is_service_ready.sh

我正在使用 python 的 kubernetes 库监视 pod 的事件。 当 pod 被部署时,容器会启动并执行 postStart 脚本,直到 postStart 脚本成功退出。当 CONTAINER 出现时,我想使用 pythons kubernetes 库从 kubernetes 获取事件。 我尝试观看该事件,只有当 postStart 完成并且 POD 出现时,我才会收到状态为“containersReady”的事件,如下所示。

 'status': {'conditions': [{'last_probe_time': None,
                            'last_transition_time': datetime.datetime(2019, 4, 18, 16, 25, 3, tzinfo=tzlocal()),
                            'message': None,
                            'reason': None,
                            'status': 'True',
                            'type': 'Initialized'},
                           {'last_probe_time': None,
                            'last_transition_time': datetime.datetime(2019, 4, 18, 16, 26, 51, tzinfo=tzlocal()),
                            'message': None,
                            'reason': None,
                            'status': 'True',
                            'type': 'Ready'},
                           {'last_probe_time': None,
                            'last_transition_time': None,
                            'message': None,
                            'reason': None,
                            'status': 'True',
                            'type': 'ContainersReady'},
                           {'last_probe_time': None,
                            'last_transition_time': datetime.datetime(2019, 4, 18, 16, 25, 3, tzinfo=tzlocal()),
                            'message': None,
                            'reason': None,
                            'status': 'True',
                            'type': 'PodScheduled'}],
                       'container_statuses': [{'container_id': 'docker://1c39e13dc777a34c38d4194edc23c3668697223746b60276acffe3d62f9f0c44',
                                    'image': 'my-image:test',
                                    'image_id': 'docker://sha256:9903437699d871c1f3af7958a7294fe419ed7b1076cdb8e839687e67501b301b',
                                    'last_state': {'running': None,
                                                   'terminated': None,
                                                   'waiting': None},
                                    'name': 'samplename',
                                    'ready': True,
                                    'restart_count': 0,
                                    'state': {'running': {'started_at': datetime.datetime(2019, 4, 18, 16, 25, 14, tzinfo=tzlocal())},
                                              'terminated': None,
                                              'waiting': None}}],


在此之前,我的状态“podScheduled”为“True”

 'status': {'conditions': [{'last_probe_time': None,
                            'last_transition_time': datetime.datetime(2019, 4, 18, 16, 25, 3, tzinfo=tzlocal()),
                            'message': None,
                            'reason': None,
                            'status': 'True',
                            'type': 'Initialized'},
                           {'last_probe_time': None,
                            'last_transition_time': datetime.datetime(2019, 4, 18, 16, 25, 3, tzinfo=tzlocal()),
                            'message': 'containers with unready status: [openvas]',
                            'reason': 'ContainersNotReady',
                            'status': 'False',
                            'type': 'Ready'},
                           {'last_probe_time': None,
                            'last_transition_time': None,
                            'message': 'containers with unready status: [openvas]',
                            'reason': 'ContainersNotReady',
                            'status': 'False',
                            'type': 'ContainersReady'},
                           {'last_probe_time': None,
                            'last_transition_time': datetime.datetime(2019, 4, 18, 16, 25, 3, tzinfo=tzlocal()),
                            'message': None,
                            'reason': None,
                            'status': 'True',
                            'type': 'PodScheduled'}],
            'container_statuses': [{'container_id': None,
                                    'image': 'ns-openvas:test',
                                    'image_id': '',
                                    'last_state': {'running': None,
                                                   'terminated': None,
                                                   'waiting': None},
                                    'name': 'openvas',
                                    'ready': False,
                                    'restart_count': 0,
                                    'state': {'running': None,
                                              'terminated': None,
                                              'waiting': {'message': None,
                                                          'reason': 'ContainerCreating'}}}],

当 CONTAINER 出现时,我可以尝试任何方法来获取事件。

【问题讨论】:

    标签: python-2.7 kubernetes


    【解决方案1】:

    显然,用当前的方法你永远不会让它工作,因为正如here 描述的那样:

    postStart 处理程序相对于容器的异步运行 代码,但 Kubernetes 对容器的管理 blocks 直到 postStart 处理程序完成。 容器的状态未设置为 运行直到 postStart 处理程序完成。

    也许您应该使用 is_service_ready.sh 脚本创建另一个 pod,它将监视主 pod 的事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-19
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-26
      相关资源
      最近更新 更多