[英]Is it possible to have under Kubernetes the Pod hostname be the hosting Node hostname?
Moving from Docker to K8s, today we run some containers with: docker run --hostname %H ... that causes the container to get the Host machine hostname as its own, can I have similar behavior running under K8s? 从Docker迁移到K8s,今天,我们使用以下命令运行一些容器: docker run --hostname%H ...导致容器将主机的主机名作为自己的主机名,我可以在K8s下运行类似的行为吗? (having the pod hostname as the Node hostname)
(具有pod主机名作为Node主机名)
From what I have seen so far: 从到目前为止我所看到的:
spec.hostname seems not to support values from env vars. spec.hostname似乎不支持env vars中的值。
Using hostAliases seems to put a hardcoded name... 使用hostAliases似乎会加上一个硬编码的名称...
BTW, the pods are deployed as a DeamonSet 顺便说一句,吊舱已部署为DeamonSet
If you use hostNetwork: true
for pod's spec, it will share the network with the node. 如果您对pod的规格使用
hostNetwork: true
,它将与节点共享网络。 Here is a simple test: 这是一个简单的测试:
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
hostNetwork: true
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "sh", "-c"]
args:
- while true; do
echo -en '\n';
printenv HOSTNAME
sleep 10;
done;
restartPolicy: Never
That shows that the pod's host name is the same as its node. 这表明Pod的主机名与其节点相同。
Note though that this setup is general not recommended : 请注意,尽管一般不建议使用此设置:
Avoid using
hostNetwork
, for the same reasons ashostPort
出于与
hostPort
相同的原因,请避免使用hostNetwork
。
However, maybe in your case that's not an issue. 但是,也许就您而言,这不是问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.