HostAliases 中的 Kubernetes 服务

Nee*_*koy 8 kubernetes

hostAliasesKubernetes下是否可以有服务名称?我想将一个不存在的域名指向一个服务而不是一个 IP。

类似于以下内容:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: deployment-name
spec:
  replicas: 1
  template:
    spec:
      containers:
        hostAliases:
        - ip: "my-service-name"
          hostnames:
          - "my.domain.com"
Run Code Online (Sandbox Code Playgroud)

如果没有,你们将如何为服务的 pod 设置本地主机文件条目?我需要将不存在的域解析为服务。

Vit*_*Vit 6

不可以,您不能在部署定义中使用服务名称来代替ipunder 。spec.template.spec您可以做的是获取您的服务 IP kubectl get services,并在部署中使用它,将其映射到不存在的域。

您无法使用域名而不是 IP 的原因是文件 /etc/hosts 应仅包含 IP 和主机名。有关更多信息,您可能需要阅读此使用 /etc/hosts 创建域名别名主题。


Nee*_*koy 5

正如@VKR 在另一条评论中所解释的那样,HostAliases 基本上只是注入到 /etc/hosts 中,它只允许 A 记录类型条目。

对于 CNAME 条目,解决方法是将别名注入 CoreDNS。

这可以通过编辑 CoreDNS 的 ConfirmMap 来完成:

$ kubectl edit configmap coredns -n kube-system

apiVersion: v1
data:
  Corefile: |
    .:53 {
        errors
        health
        rewrite name orderer0.example.com orderer0-example-com.orbix-mvp.svc.cluster.local
        rewrite name peer0.example.com peer0-example-com.orbix-mvp.svc.cluster.local
        kubernetes cluster.local {
          pods insecure
          upstream
          fallthrough in-addr.arpa ip6.arpa
        }
        prometheus :9153
        proxy . /etc/resolv.conf
        cache 30
    }
Run Code Online (Sandbox Code Playgroud)

我添加了以 开头的两行,rewrite name一旦 CoreDNS 重新启动,新条目将在整个集群中可用。

可以使用以下命令重新启动 CoreDNS:

kubectl exec -n kube-system coredns-980047985-g2748 -- kill -SIGUSR1 1

上面需要为两个 CoreDNS pod 运行。