【问题标题】:How to get the hostname of a service in Kubernetes?如何在 Kubernetes 中获取服务的主机名?
【发布时间】:2021-05-04 08:41:34
【问题描述】:

我需要服务 lenspostgres-postgresql 的主机名,但出现错误:

$ kubectl get services -n default                                                                                              
NAME                                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
kubernetes                           ClusterIP   ........     <none>        443/TCP    20m
lensespostgres-postgresql            ClusterIP   ........     <none>        5432/TCP   14m
lensespostgres-postgresql-headless   ClusterIP   None         <none>        5432/TCP   14m
$ ping lensespostgres-postgresql.default.svc.cluster.local
ping: lensespostgres-postgresql.default.svc.cluster.local: Name or service not known

为什么?

【问题讨论】:

  • 您无法 ping 包含“http://”的地址。 ~$ ping 8.8.8.8 PING 8.8.8.8 (8.8.8.8) 56(84) 字节数据。来自 8.8.8.8 的 64 个字节:icmp_seq=1 ttl=118 time=11.8 ms 来自 8.8.8.8 的 64 个字节:icmp_seq=2 ttl=118 time=11.7 ms ^C ~$ ping http://8.8.8.8 ping: http://8.8.8.8: 名称或服务未知
  • @sarlacii 如果没有 http://,我也会遇到同样的错误
  • kubectl 获取 svc clusterip
  • @sarlacii 我得到:来自服务器的错误(未找到):找不到服务“xx.xx.xx.xx”
  • 嗯,这超出了我的范围...如果您检查 pod,您会得到什么:kubectl get pod -o wide,以及 --all-namespaces 列出所有命名空间中的所有 pod?

标签: kubernetes google-kubernetes-engine


【解决方案1】:

要获取服务的主机名,您需要 dnsutils

如果没有安装 dnsutils,请按照以下步骤操作

   sudo apt-get install dnsutils
   sudo apt-get update

link中提到的dnsutils创建yaml文件

使用以下命令应用创建的 yaml 文件

kubectl apply -f dnsutils.yaml

要获取服务的主机名,请使用以下命令:

kubectl exec -ti dnsutils -- nslookup <service-name>

有关yaml文件的详细信息和步骤,您可以参考link

我已经在我的项目中尝试过,它对我有用

【讨论】:

    猜你喜欢
    • 2021-07-23
    • 2016-03-24
    • 2014-08-09
    • 2011-01-09
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    相关资源
    最近更新 更多