【问题标题】:Kubernetes service in HostAliasesHostAliases 中的 Kubernetes 服务
【发布时间】:2019-07-08 05:50:27
【问题描述】:

在 Kubernetes 中是否可以在 hostAliases 下拥有服务名称?我想将一个不存在的域名指向服务而不是 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"

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

【问题讨论】:

    标签: kubernetes


    【解决方案1】:

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

    您不能使用域名而不是 IP 的原因是文件 /etc/hosts 应该只包含 IP 和主机名。有关更多信息,您可能需要阅读此Creating alias to domain name with /etc/hosts 主题。

    【讨论】:

    • 是的,我实际上想出了如何在 CoreDNS 中为 Kubernetes 注入 CNAME。您的解释是正确答案,所以我会标记它并稍后发布我的解决方法。
    【解决方案2】:

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

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

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

    $ 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
        }
    

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

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

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

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

    【讨论】:

    • 感谢您的技巧。也可以使用 kubectl -n kube-system delete pod <coredns_pod_1> <coredns_pod_2> 重新启动 CoreDNS pod
    • 我实际上觉得这种方式更优雅:kubectl rollout restart -n kube-system deployment/coredns
    猜你喜欢
    • 2020-06-19
    • 2020-09-27
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多