模板中暴露的kubernetes statefulsets索引/序号 [英] kubernetes statefulsets index/ordinal exposed in template

查看:162
本文介绍了模板中暴露的kubernetes statefulsets索引/序号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Statefulsets指示kubectl/kubernetes创建带有Pod名称后的索引或序号的Pod.例如,如果实体的名称为redis并且复制计数为3,则我们将获得redis-0,redis-1和redis-2.有某种方法可以访问索引或序数吗?传递给容器的启动脚本将非常有用.如果它在向下的api中可用,那就太好了.尝试将PVC与PV对齐时使用它真的非常好.

Statefulsets direct kubectl/kubernetes to create pods with an index or ordinal following the pod name. If the entity has a name of redis for example and the replication count is 3, then we will get redis-0, redis-1 and redis-2. Is there some way to get access to the index or ordinal? It would be very useful to pass to the startup script for a container. It would be nice if it were available in the downward api. It would be really really nice to use it when trying to line up PVC's to PV's.

是否有可能为此提供一些秘密模板变量?

Is it possible that there is some secret template variable for this?

请注意,我已经找到解决方法.我正在为命令使用脚本,并使用主机名将其解压缩.但这似乎需要大量工作(以及将来的维护).

Note that I have found a way around it. I am using a script for my command and use the hostname to extract it. But it just seems like a lot of work (and future maintenance).

推荐答案

最终,这应该通过将清单env添加进来(我从1.9起被告知,但尚未确认):

Eventually this should work by adding to the manifest env (I'm told 1.9 onwards but not confirmed yet):

   - name: KAFKA_BROKER_ID
     valueFrom:
       fieldRef:
         fieldPath: metadata.annotations['spec.pod.beta.kubernetes.io/statefulset-index'] 

现在,您可以像使用脚本或使用生命周期事件一样黑客"它:

For now you can 'hack' it like you did using script or using lifecycle events like following:

    lifecycle:
      postStart:
        exec:
          command:
            - "/bin/sh"
            - "-c"
            - "export KAFKA_BROKER_ID=${HOSTNAME##*-}"

这篇关于模板中暴露的kubernetes statefulsets索引/序号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆