[英]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.