简体   繁体   English

控制通过Kubernetes部署的POD的主机名

[英]Control the hostname of the POD deployed via Kubernetes

I am trying to deploy my application using helm charts. 我正在尝试使用掌舵图表来部署我的应用程序。 I have defined a statefulSet as a kind under deployment.yaml and provided a headless service under spec.serviceName. 我已经在deployment.yaml下将statefulSet定义为一种,并在spec.serviceName下提供了无头服务。 Code snippet from deployment.yaml is given below. 下面给出了deployment.yaml的代码片段。

Post deployment, when I fire "kubectl get pods", its shows POD name as "MyApp-100-deployment-n" where n >=0, based on replicas. 部署后,当我启动“ kubectl get pods”时,其基于副本显示POD名称为“ MyApp-100-deployment-n”,其中n> = 0。

If I go inside the pod using kubectl exec, and fire "hostname" command , I get "MyApp-100-deployment-n" as hostname and when I fire "hostname --fqdn" , I get something like below: 如果我使用kubectl exec进入吊舱,并启动“ hostname”命令,则获得“ MyApp-100-deployment-n”作为主机名,而当我启动“ hostname --fqdn”时,则得到如下信息:

MyApp-100-deployment-n.<name of Service>.<Namespace>.svc.cluster.local

These results are fine and good but when my application written in JAVA and which is deployed now tries to get the hostname using InetAddress.getLocalHost().getHostName() , It gets the entire fqdn of pods , not the hostname of pod. 这些结果很好,但是当我用JAVA编写并已部署的应用程序现在尝试使用InetAddress.getLocalHost().getHostName()获取主机名时,它将获取pod的整个fqdn,而不是pod的主机名。 This is something which is bothering me. 这让我很困扰。 Why Java InetAddress is not able to get the hostname only? 为什么Java InetAddress无法仅获取主机名? Is there any way related to configuration in yaml files to not allow the headless service to modify the hostnames? 是否有任何与yaml文件中的配置有关的方法,以允许无头服务修改主机名? Eventually, I need only hostname not fqdn in Java code to do processing. 最终,我只需要Java代码中的主机名而不是fqdn即可进行处理。

If I remove the headless service name from the deployment.yaml and deploy the application and fire the hostname and hostname --fqdn command from inside the container, I get only "MyApp-100-deployment-n" as results for both. 如果我从Deployment.yaml中删除无头服务名称并部署该应用程序,然后从容器内部启动主机名和主机名--fqdn命令,则两者的结果均为“ MyApp-100-deployment-n”。 Adding the headless service is only introducing the fqdn. 添加无头服务只是引入了fqdn。

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: "{{ .Chart.Name }}-{{ .Chart.AppVersion | replace "." "" }}-deployment"
  labels:
    app: Myapp-deployment
{{ include "metadata.labels.standard" . | indent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: "{{ .Chart.Name }}-{{ .Chart.AppVersion | replace "." "" }}"
  serviceName: "{{ .Chart.Name }}-{{ .Chart.AppVersion | replace "." "" }}"

You can use the env variable HOSTNAME that is present on each container and gives you only the hostname. 您可以使用每个容器中存在的env变量HOSTNAME ,并且仅提供主机名。

Also, try to execute env on any pod to see all available variables 另外,尝试在任何Pod上执行env以查看所有可用变量

kubectl exec <pod-name> env

An explanation of why using InetAddress.getLocalHost().getHostName() is not the right way of retrieving the hostname is here 为什么使用的说明InetAddress.getLocalHost().getHostName()不是检索主机的正确的方法是在这里

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

相关问题 Spring 引导无法连接到部署在另一个 kubernetes pod 上的 postgre 数据库 - Spring boot cannot connect to postgre database deployed on another kubernetes pod 尝试通过 kubernetes DNS 访问 kubernetes pod 时连接被拒绝 - Connection refused when trying to access a kubernetes pod via kubernetes DNS 在访问部署在 kubernetes pod 上的 Java 应用程序时获取 502 Bad Gateway (Nginx) - Getting 502 Bad Gateway (Nginx) while accessing Java application deployed on kubernetes pod 我想在pod内使用fabric8 kubernetes客户端(java)。 我如何获取其所部署集群的kubernetes客户端? - I want to use fabric8 kubernetes client (java) inside a pod. How do I obtain the kubernetes client for the cluster it is deployed on? 在Kubernetes中绑定应用程序Pod和数据库Pod - Bind application pod and database pod in kubernetes 以编程方式使用 Kubernetes pod 内存 - Kubernetes pod memory usage programatically 到达 Eureka Server pod Kubernetes - Reaching Eureka Server pod Kubernetes 读取部署在 Kubernetes 上的 Springboot 中的秘密 - Read secrets in Springboot deployed on Kubernetes 通过主机名连接到Java套接字 - Connect to Java socket via hostname 如何在不崩溃 pod 的情况下杀死 Kubernetes pod 中的命令进程 - How to kill a command process inside Kubernetes pod without crashing pod
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM