--- title: 将 CoreDNS 设置为联邦集群的 DNS 提供者 content_template: templates/tutorial weight: 130 --- {{% capture overview %}} {{< deprecationfilewarning >}} {{< include "federation-deprecation-warning-note.md" >}} {{< /deprecationfilewarning >}} 此页面显示如何配置和部署 CoreDNS,将其用作联邦集群的 DNS 提供者 {{% /capture %}} {{% capture objectives %}} * 配置和部署 CoreDNS 服务器 * 使用 CoreDNS 作为 dns 提供者设置联邦 * 在 nameserver 查找链中设置 CoreDNS 服务器 {{% /capture %}} {{% capture prerequisites %}} * 你需要有一个正在运行的 Kubernetes 集群(作为主机集群引用)。请参阅[入门指南](/docs/setup/),了解平台的安装说明。 * 必须在联邦的集群成员中支持 `LoadBalancer` 服务,用来支持跨联邦集群的 `CoreDNS` 服务发现。 {{% /capture %}} {{% capture lessoncontent %}} ## 部署 CoreDNS 和 etcd 图表 CoreDNS 可以部署在各种配置中。下面解释的是一个参考,可以根据平台和联邦集群的需要进行调整。 为了部署 CoreDNS,我们将利用图表。 CoreDNS 将部署 [etcd](https://coreos.com/etcd) 作为后端,并且应该预先安装。etcd 也可以使用图表进行部署。下面显示了部署 etcd 的说明。 helm install --namespace my-namespace --name etcd-operator stable/etcd-operator helm upgrade --namespace my-namespace --set cluster.enabled=true etcd-operator stable/etcd-operator *注意:etcd 默认部署配置可以被覆盖,适合主机集群。* 部署成功后,可以使用主机集群中的 [http://etcd-cluster.my-namespace:2379](http://etcd-cluster.my-namespace:2379) 端点访问 etcd。 应该定制 CoreDNS 默认配置适应联邦。 下面显示的是 Values.yaml,它覆盖了 CoreDNS 图表上的默认配置参数。 ```yaml isClusterService: false serviceType: "LoadBalancer" plugins: kubernetes: enabled: false etcd: enabled: true zones: - "example.com." endpoint: "http://etcd-cluster.my-namespace:2379" ``` 以上配置文件需要说明: - `isClusterService` 指定是否应该将 CoreDNS 部署为集群服务,这是默认值。 你需要将其设置为 false,以便将 CoreDNS 部署为 Kubernetes 应用程序服务。 - `serviceType` 指定为核心用户创建的 Kubernetes 服务的类型。 你需要选择 `LoadBalancer` 或 `NodePort`,以便在 Kubernetes 集群之外访问 CoreDNS 服务。 - 禁用 `plugins.kubernetes`,默认情况下通过设置 `plugins.kubernetes.enabled` 为 false。 - 启用 `plugins.etcd`,通过设置 `plugins.etcd.enabled` 为 true。 - 通过设置 `plugins.etcd.zones` 来配置 CoreDNS 具有权威性的 DNS 域(联邦域)。如上所示。 - 通过设置 `plugins.etcd.endpoint` 来配置早期部署的 etcd 端点 现在部署 CoreDNS 来运行 helm install --namespace my-namespace --name coredns -f Values.yaml stable/coredns 验证 etcd 和 CoreDNS,pod 都按预期运行。 ## 使用 CoreDNS 作为 DNS 提供者部署联邦 可以使用 `kubefed init` 部署联邦控制平面。通过指定两个附加参数,可以选择 CoreDNS 作为 DNS 提供者。 --dns-provider=coredns --dns-provider-config=coredns-provider.conf coredns-provider.conf 的格式如下: [Global] etcd-endpoints = http://etcd-cluster.my-namespace:2379 zones = example.com. coredns-endpoints = : - `etcd-endpoints` 是访问 etcd 的端点。 - `zones` 是 CoreDNS 具有权威性的联邦域,它与 `kubefed init` 的 --dns-zone-name 参数相同。 - `coredns-endpoints` 是访问 CoreDNS 服务器的端点。这是从 v1.7 开始引入的一个可选参数。 {{< note >}} CoreDNS 配置中的 `plugins.etcd.zones` 和 `kubefed init` 的 `--dns-zone-name` 参数应该匹配。 {{< /note >}} ## 在 nameserver resolv.conf 链中设置 CoreDNS 服务器 {{< note >}} 下面的部分只适用于 v1.7 之前的版本,如果 `coredns-endpoint` 参数是 在 `coredns-provider.conf` 中配置的,就会自动处理。 {{< /note >}} 一旦部署了联邦控制平面并将联邦集群连接到联邦, 你需要将 CoreDNS 服务器添加到所有联邦集群中 pod 的 nameserver resolv.conf 链,因为这个自托管的 CoreDNS 服务器是不可公开发现的。 这可以通过在 `kube-dns` 部署中将下面的行添加到 `dnsmasq` 容器的参数中来实现。 --server=/example.com./ 将上面的 `example.com` 替换为联邦域。 现在联邦集群已经为跨集群服务发现做好了准备! {{% /capture %}}