简体   繁体   English

在Kubernetes下是否可以将Pod主机名作为托管Node主机名?

[英]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: 从到目前为止我所看到的:

  1. spec.hostname seems not to support values from env vars. spec.hostname似乎不支持env vars中的值。

  2. 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 as hostPort 出于与hostPort相同的原因,请避免使用hostNetwork

However, maybe in your case that's not an issue. 但是,也许就您而言,这不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 控制通过Kubernetes部署的POD的主机名 - Control the hostname of the POD deployed via Kubernetes Kubernetes pod 将 coredns 中的外部 kafka 主机名解析为 pod 内的 hostaliases - Kubernetes pod resolve external kafka hostname in coredns not as hostaliases inside pod 如何在 Kubernetes 的单个 pod 中为各个容器提供唯一的主机名? - How to give individual containers a unique hostname within a single pod in Kubernetes? Kubernetes:主机名正则表达式失败 - Kubernetes : hostname regex failed Spring 启动微服务正在以 pod 名称作为 Kubernetes 集群中的主机名注册到 Eureka - docker 桌面 - Spring boot microservices are getting registered to Eureka with the pod name as the hostname in the Kubernetes cluster - docker desktop 设置在Kubernetes中运行的容器的主机名 - Setting the hostname for a container running in Kubernetes 如何更改docker节点主机名 - How to change docker node hostname 我可以通过它在Kubernetes中另一个节点上运行的另一个容器的主机名来访问容器吗? - Can I reach a container by it's hostname from another container running on another node in Kubernetes? 如何解析 kubernetes 集群中 pod 之间的主机名? - How to resolve the hostname between the pods in the kubernetes cluster? 我可以为 kubernetes 中的每个容器提供主机名吗 - can I give hostname for each container in kubernetes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM