5.6. OpenShift Container Platform 中的 DNS Operator
在 OpenShift Container Platform 中,DNS Operator 部署和管理 CoreDNS 实例,为集群中的 pod 提供名称解析服务,启用基于 DNS 的 Kubernetes 服务发现,并解析内部 cluster.local 名称。
5.6.1. 检查 DNS Operator 的状态 复制链接链接已复制到粘贴板!
DNS Operator 从 operator.openshift.io API 组实现 dns API。Operator 使用守护进程集部署 CoreDNS,为守护进程集创建一个服务,并将 kubelet 配置为指示 pod 使用 CoreDNS 服务 IP 地址进行名称解析。
流程
在安装过程中使用 Deployment 对象部署 DNS Operator。
使用
oc get命令查看部署状态:$ oc get -n openshift-dns-operator deployment/dns-operator输出示例
NAME READY UP-TO-DATE AVAILABLE AGE dns-operator 1/1 1 1 23h使用
oc get命令来查看 DNS Operator 的状态:$ oc get clusteroperator/dns输出示例
NAME VERSION AVAILABLE PROGRESSING DEGRADED SINCE MESSAGE dns 4.1.15-0.11 True False False 92mAVAILABLE、PROGRESSING和DEGRADED提供了有关 Operator 状态的信息。当 CoreDNS 守护进程中至少有一个 pod 被设置为Available状态时,AVAILABLE为True,DNS 服务有一个集群 IP 地址。
5.6.2. 查看默认 DNS 复制链接链接已复制到粘贴板!
每个 OpenShift Container Platform 新安装都有一个名为 default 的 dns.operator。
流程
使用
oc describe命令来查看默认dns:$ oc describe dns.operator/default输出示例
Name: default Namespace: Labels: <none> Annotations: <none> API Version: operator.openshift.io/v1 Kind: DNS ... Status: Cluster Domain: cluster.local1 Cluster IP: 172.30.0.102 ...要查找集群的服务 CIDR 范围,如
172.30.0.0/16,请使用oc get命令:$ oc get networks.config/cluster -o jsonpath='{$.status.serviceNetwork}'
5.6.3. 使用 DNS 转发 复制链接链接已复制到粘贴板!
您可以使用以下方法使用 DNS 转发来覆盖 /etc/resolv.conf 文件中的默认转发配置:
-
为每个区指定名称服务器 (
spec.servers)。如果转发区是 OpenShift Container Platform 管理的 ingress 域,则必须为域授权上游名称服务器。 -
提供上游 DNS 服务器列表 (
spec.upstreamResolvers)。 - 更改默认转发策略。
默认域的 DNS 转发配置可以同时在 /etc/resolv.conf 文件和上游 DNS 服务器中指定默认服务器。
流程
修改名为
default的 DNS Operator 对象:$ oc edit dns.operator/default发出上一命令后,Operator 会根据
spec.servers创建并更新名为dns-default的配置映射,并使用额外的服务器配置块。如果任何服务器都没有与查询匹配的区域,则名称解析会返回上游 DNS 服务器。配置 DNS 转发
apiVersion: operator.openshift.io/v1 kind: DNS metadata: name: default spec: cache: negativeTTL: 0s positiveTTL: 0s logLevel: Normal nodePlacement: {} operatorLogLevel: Normal servers: - name: example-server1 zones: - example.com2 forwardPlugin: policy: Random3 upstreams:4 - 1.1.1.1 - 2.2.2.2:5353 upstreamResolvers:5 policy: Random6 protocolStrategy: ""7 transportConfig: {}8 upstreams: - type: SystemResolvConf9 - type: Network address: 1.2.3.410 port: 5311 status: clusterDomain: cluster.local clusterIP: x.y.z.10 conditions: ...- 1
- 必须符合
rfc6335服务名称语法。 - 2
- 必须符合
rfc1123服务名称语法中的子域的定义。集群域cluster.local是对zones字段的无效子域。 - 3
- 定义用于选择
forwardPlugin中列出的上游解析器的策略。默认值为Random。您还可以使用RoundRobin, 和Sequential值。 - 4
- 每个
forwardPlugin最多允许 15 个upstreams。 - 5
- 您可以使用
upstreamResolvers覆盖默认转发策略,并将 DNS 解析转发到默认域的指定 DNS 解析器(上游解析器)。如果没有提供任何上游解析器,DNS 名称查询将进入/etc/resolv.conf中声明的服务器。 - 6
- 决定选择上游中列出的
upstreams服务器进行查询的顺序。您可以指定这些值之一:Random、RoundRobin或Sequential。默认值为Sequential。 - 7
- 如果被省略,平台会选择一个默认值,通常是原始客户端请求的协议。设置为
TCP,以指定平台应该对所有上游 DNS 请求使用 TCP,即使客户端请求使用了 UDP。 - 8
- 用于配置传输类型、服务器名称和可选自定义 CA 或 CA 捆绑包,以便在将 DNS 请求转发到上游解析器时使用。
- 9
- 您可以指定两个类型的
upstreams:SystemResolvConf或Network。SystemResolvConf将上游配置为使用/etc/resolv.conf和Network定义一个Networkresolver。您可以指定其中一个或两者都指定。 - 10
- 如果指定类型是
Network,则必须提供 IP 地址。address字段必须是有效的 IPv4 或 IPv6 地址。 - 11
- 如果指定类型是
Network,您可以选择性地提供端口。port字段必须是1到65535之间的值。如果您没有为上游指定端口,则默认端口为 853。
5.6.4. 检查 DNS Operator 状态 复制链接链接已复制到粘贴板!
您可以使用 oc describe 命令来检查状态并查看 DNS Operator 的详情。
流程
查看 DNS Operator 的状态:
$ oc describe clusteroperators/dns虽然在特定版本中的信息和拼写可能有所不同,但预期的状态输出如下:
Status: Conditions: Last Transition Time: <date> Message: DNS "default" is available. Reason: AsExpected Status: True Type: Available Last Transition Time: <date> Message: Desired and current number of DNSes are equal Reason: AsExpected Status: False Type: Progressing Last Transition Time: <date> Reason: DNSNotDegraded Status: False Type: Degraded Last Transition Time: <date> Message: DNS default is upgradeable: DNS Operator can be upgraded Reason: DNSUpgradeable Status: True Type: Upgradeable
5.6.5. 查看 DNS Operator 日志 复制链接链接已复制到粘贴板!
您可以使用 oc logs 命令来查看 DNS Operator 日志。
流程
查看 DNS Operator 的日志:
$ oc logs -n openshift-dns-operator deployment/dns-operator -c dns-operator
5.6.6. 设置 CoreDNS 日志级别 复制链接链接已复制到粘贴板!
CoreDNS 和 CoreDNS Operator 的日志级别使用不同的方法设置。您可以配置 CoreDNS 日志级别来确定日志记录错误信息中的详情量。CoreDNS 日志级别的有效值为 Normal、Debug 和 Trace。默认 logLevel 为 Normal。
CoreDNS 错误日志级别总是被启用。以下日志级别设置报告不同的错误响应:
-
logLevel:Normal启用 "errors" 类:log . { class error }. -
loglevel:Debug启用 "denial" 类:log . { class denial error }。 -
logLevel:Trace启用 "all" 类:log . { class all }.
流程
要将
logLevel设置为Debug,输入以下命令:$ oc patch dnses.operator.openshift.io/default -p '{"spec":{"logLevel":"Debug"}}' --type=merge要将
logLevel设置为Trace,输入以下命令:$ oc patch dnses.operator.openshift.io/default -p '{"spec":{"logLevel":"Trace"}}' --type=merge
验证
要确保设置了所需的日志级别,请检查配置映射:
$ oc get configmap/dns-default -n openshift-dns -o yaml例如,在将
logLevel设置为Trace后,您应该在每个 server 块中看到这个小节:errors log . { class all }
5.6.7. 查看 CoreDNS 日志 复制链接链接已复制到粘贴板!
您可以使用 oc logs 命令查看 CoreDNS 日志。
流程
输入以下命令来查看特定 CoreDNS pod 的日志:
$ oc -n openshift-dns logs -c dns <core_dns_pod_name>输入以下命令遵循所有 CoreDNS pod 的日志:
$ oc -n openshift-dns logs -c dns -l dns.operator.openshift.io/daemonset-dns=default -f --max-log-requests=<number>1 - 1
- 指定要流传输日志的 DNS pod 数量。最大值为 6。
5.6.8. 设置 CoreDNS Operator 的日志级别 复制链接链接已复制到粘贴板!
CoreDNS 和 CoreDNS Operator 的日志级别使用不同的方法设置。集群管理员可以配置 Operator 日志级别来更快地跟踪 OpenShift DNS 问题。operatorLogLevel 的有效值为 Normal、Debug 和 Trace。Trace 具有更详细的信息。默认 operatorlogLevel 为 Normal。Operator 问题有七个日志记录级别: Trace, Debug, Info, Warning, Error, Fatal, 和 Panic。设置了日志级别后,具有该严重性级别或以上级别的所有内容都会记录为日志条目。
-
operatorLogLevel: "Normal"设置logrus.SetLogLevel("Info")。 -
operatorLogLevel: "Debug"设置logrus.SetLogLevel("Debug")。 -
operatorLogLevel: "Trace"设置logrus.SetLogLevel("Trace")。
流程
要将
operatorLogLevel设置为Debug,请输入以下命令:$ oc patch dnses.operator.openshift.io/default -p '{"spec":{"operatorLogLevel":"Debug"}}' --type=merge要将
operatorLogLevel设置为Trace,请输入以下命令:$ oc patch dnses.operator.openshift.io/default -p '{"spec":{"operatorLogLevel":"Trace"}}' --type=merge
验证
要查看生成的更改,请输入以下命令:
$ oc get dnses.operator -A -oyaml您应该会看到两个日志级别条目。
operatorLogLevel适用于 OpenShift DNS Operator 问题,logLevel适用于 CoreDNS pod 的 daemonset:logLevel: Trace operatorLogLevel: Debug要查看 daemonset 的日志,请输入以下命令:
$ oc logs -n openshift-dns ds/dns-default
5.6.9. 调整 CoreDNS 缓存 复制链接链接已复制到粘贴板!
对于 CoreDNS,您可以配置成功或不成功缓存的最长持续时间,也称为正缓存或负缓存。调整 DNS 查询响应的缓存持续时间可减少任何上游 DNS 解析器的负载。
将 TTL 字段设为低值可能会导致集群、任何上游解析器或两者中负载的增加。
流程
运行以下命令来编辑名为
default的 DNS Operator 对象:$ oc edit dns.operator.openshift.io/default修改生存时间 (TTL) 缓存值:
配置 DNS 缓存
apiVersion: operator.openshift.io/v1 kind: DNS metadata: name: default spec: cache: positiveTTL: 1h1 negativeTTL: 0.5h10m2
验证
要查看更改,请运行以下命令再次查看配置映射:
$ oc get configmap/dns-default -n openshift-dns -o yaml验证您是否看到类似以下示例的条目:
cache 3600 { denial 9984 2400 }
5.6.10. 高级任务 复制链接链接已复制到粘贴板!
5.6.10.1. 更改 DNS Operator managementState 复制链接链接已复制到粘贴板!
DNS Operator 管理 CoreDNS 组件,为集群中的 pod 和服务提供名称解析服务。默认情况下,DNS Operator 的 managementState 设置为 Managed,这意味着 DNS Operator 会主动管理其资源。您可以将其更改为 Unmanaged,这意味着 DNS Operator 不管理其资源。
以下是更改 DNS Operator managementState 的用例:
-
您是一个开发者,希望测试配置更改来查看它是否解决了 CoreDNS 中的问题。您可以通过将
managementState设置为Unmanaged来停止 DNS Operator 覆盖配置更改。 -
您是一个集群管理员,报告了 CoreDNS 的问题,但在解决这个问题前需要应用一个临时解决方案。您可以将 DNS Operator 的
managementState字段设置为Unmanaged以应用临时解决方案。
流程
在 DNS Operator 中将
managementState更改为Unmanaged:oc patch dns.operator.openshift.io default --type merge --patch '{"spec":{"managementState":"Unmanaged"}}'使用
jsonpath命令行 JSON 解析器查看 DNS Operator 的managementState:$ oc get dns.operator.openshift.io default -ojsonpath='{.spec.managementState}'注意当
managementState设置为Unmanaged时,您无法升级。
5.6.10.2. 控制 DNS pod 放置 复制链接链接已复制到粘贴板!
DNS Operator 有两个守护进程集:一个用于 CoreDNS,名为 dns-default,另一个用于管理 名为 node-resolver 的 /etc/hosts 文件。
您可能会发现,需要控制哪些节点已分配并运行了 CoreDNS pod,尽管这不是一个常见操作。例如,如果集群管理员配置了可以禁止节点对节点间通信的安全策略,这需要限制 CoreDNS 运行 daemonset 的节点集合。如果 DNS pod 在集群中的某些节点上运行,且没有运行 DNS pod 的节点与运行 DNS pod 的节点具有网络连接,则 DNS 服务将适用于所有 pod。
node-resolver 守护进程集必须在每个节点主机上运行,因为它为集群镜像 registry 添加一个条目来支持拉取镜像。node-resolver pod 只有一个作业:查找 image-registry.openshift-image-registry.svc 服务的集群 IP 地址,并将其添加到节点主机上的 /etc/hosts 中,以便容器运行时可以解析服务名称。
作为集群管理员,您可以使用自定义节点选择器将 CoreDNS 的守护进程集配置为在某些节点上运行或不运行。
先决条件
-
已安装
ocCLI。 -
以具有
cluster-admin权限的用户身份登录集群。 -
您的 DNS Operator
managementState设置为Managed。
流程
要允许 CoreDNS 的守护进程集在特定节点上运行,请配置污点和容限:
修改名为
default的 DNS Operator 对象:$ oc edit dns.operator/default为污点指定污点键和一个容忍度:
spec: nodePlacement: tolerations: - effect: NoExecute key: "dns-only" operators: Equal value: abc tolerationSeconds: 36001 - 1
- 如果污点是
dns-only,它可以无限期地被容许。您可以省略tolerationSeconds。
5.6.10.3. 使用 TLS 配置 DNS 转发 复制链接链接已复制到粘贴板!
在高度监管的环境中工作时,您可能需要在将请求转发到上游解析器时保护 DNS 流量,以便您可以确保额外的 DNS 流量和数据隐私。
请注意,CoreDNS 缓存在 10 秒内转发连接。如果没有请求,CoreDNS 将为该 10 秒打开 TCP 连接。
对于大型集群,请确保您的 DNS 服务器知道可能有多个新的连接来保存打开,因为您可以在每个节点上启动连接。相应地设置 DNS 层次结构以避免性能问题。
流程
修改名为
default的 DNS Operator 对象:$ oc edit dns.operator/default集群管理员可以配置传输层安全(TLS)来转发 DNS 查询。
使用 TLS 配置 DNS 转发
apiVersion: operator.openshift.io/v1 kind: DNS metadata: name: default spec: servers: - name: example-server1 zones: - example.com2 forwardPlugin: transportConfig: transport: TLS3 tls: caBundle: name: mycacert serverName: dnstls.example.com4 policy: Random5 upstreams:6 - 1.1.1.1 - 2.2.2.2:5353 upstreamResolvers:7 transportConfig: transport: TLS tls: caBundle: name: mycacert serverName: dnstls.example.com upstreams: - type: Network8 address: 1.2.3.49 port: 5310 - 1
- 必须符合
rfc6335服务名称语法。 - 2
- 必须符合
rfc1123服务名称语法中的子域的定义。集群域cluster.local是对zones字段的无效子域。集群域cluster.local不是zones中的一个有效的subdomain。 - 3
- 在为转发 DNS 查询配置 TLS 时,将
transport字段设置为具有值TLS。 - 4
- 当为转发 DNS 查询配置 TLS 时,这是用作服务器名称的一部分(SNI)的强制服务器名称来验证上游 TLS 服务器证书。
- 5
- 定义用于选择上游解析器的策略。默认值为
Random。您还可以使用RoundRobin, 和Sequential值。 - 6
- 必需。使用它提供上游解析器。每个
forwardPlugin条目最多允许 15 个upstreams条目。 - 7
- 可选。您可以使用它来覆盖默认策略,并将 DNS 解析转发到默认域的指定 DNS 解析器(上游解析器)。如果没有提供任何上游解析器,DNS 名称查询将进入
/etc/resolv.conf中的服务器。 - 8
- 在使用 TLS 时,只允许
网络类型,且您必须提供 IP 地址。网络类型表示,该上游解析器应该独立于/etc/resolv.conf中列出的上游解析器单独处理转发请求。 - 9
address字段必须是有效的 IPv4 或 IPv6 地址。- 10
- 您可以选择提供端口。
port必须是1到65535之间的值。如果您没有为上游指定端口,则默认端口为 853。
注意如果
servers未定义或无效,则配置映射只包括默认服务器。
验证
查看配置映射:
$ oc get configmap/dns-default -n openshift-dns -o yaml基于 TLS 转发示例的 DNS ConfigMap 示例
apiVersion: v1 data: Corefile: | example.com:5353 { forward . 1.1.1.1 2.2.2.2:5353 } bar.com:5353 example.com:5353 { forward . 3.3.3.3 4.4.4.4:54541 } .:5353 { errors health kubernetes cluster.local in-addr.arpa ip6.arpa { pods insecure upstream fallthrough in-addr.arpa ip6.arpa } prometheus :9153 forward . /etc/resolv.conf 1.2.3.4:53 { policy Random } cache 30 reload } kind: ConfigMap metadata: labels: dns.operator.openshift.io/owning-dns: default name: dns-default namespace: openshift-dns- 1
- 对
forwardPlugin的更改会触发 CoreDNS 守护进程集的滚动更新。