这可行,这里的假设是服务<service_name>.example.com 映射到<service_name>.svc.cluster.local。通常会涉及命名空间,因此重写看起来更像{1}.{1}.svc.cluster.local(其中<service_name> 也是<namespace_name>),或者命名空间可以根据需要硬编码{1}.<namespace_name>.svc.cluster.local。
请记住不要将kubernetes.io/cluster-service: "true" 设置为true,因此它会被注释掉,否则如果它设置为true,GKE 会继续删除该服务。我没有研究为什么会这样。
CoreDNS proxy plugin 不会采用 DNS 名称,它采用 IP、IP:PORT 或 FILENAME(例如 /etc/resolv.conf)。
需要代理/上游,因为一旦将 DNS 解析交给 CoreDNS 并且 CoreDNS 将其重写到本地集群服务,则必须解析本地集群服务 DNS 条目,请参阅 kubernetes 文档中的effects on pods。最终解析到 IP 发生在代理上,甚至可能使用指向 kube-dns.kube-system.svc.cluster.local 的上游服务器。
apiVersion: v1
kind: ConfigMap
metadata:
name: internal-dns
namespace: kube-system
data:
Corefile: |
example.com:53 {
log
errors
health
prometheus :9153
rewrite name regex (.*).example.com {1}.svc.cluster.local
proxy . 10.10.10.10 ### ip of kube-dns.kube-system.svc.cluster.local
}
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: internal-dns
namespace: kube-system
labels:
k8s-app: internal-dns
kubernetes.io/name: "CoreDNS"
spec:
replicas: 1
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
selector:
matchLabels:
k8s-app: internal-dns
template:
metadata:
labels:
k8s-app: internal-dns
spec:
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
- key: "CriticalAddonsOnly"
operator: "Exists"
containers:
- name: coredns
image: coredns/coredns:1.2.6
imagePullPolicy: IfNotPresent
resources:
limits:
memory: 170Mi
requests:
cpu: 100m
memory: 70Mi
args: [ "-conf", "/etc/coredns/Corefile" ]
volumeMounts:
- name: config-volume
mountPath: /etc/coredns
readOnly: true
ports:
- containerPort: 53
name: dns
protocol: UDP
- containerPort: 53
name: dns-tcp
protocol: TCP
- containerPort: 9153
name: metrics
protocol: TCP
livenessProbe:
httpGet:
path: /health
port: 8080
scheme: HTTP
initialDelaySeconds: 60
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 5
dnsPolicy: Default
volumes:
- name: config-volume
configMap:
name: coredns
items:
- key: Corefile
path: Corefile
---
apiVersion: v1
kind: Service
metadata:
name: internal-dns
namespace: kube-system
annotations:
prometheus.io/port: "9153"
prometheus.io/scrape: "true"
labels:
k8s-app: internal-dns
#kubernetes.io/cluster-service: "true"
kubernetes.io/name: "CoreDNS"
spec:
selector:
k8s-app: internal-dns
ports:
- name: dns
port: 53
protocol: UDP
- name: dns-tcp
port: 53
protocol: TCP
正如@patrick-w 和@danny-l 在上面的 cmets 中指出的那样,需要将存根域插入 kube-dns,然后将调用委托给 example.com 或 CoreDNS。
apiVersion: v1
kind: ConfigMap
metadata:
name: kube-dns
namespace: kube-system
data:
stubDomains: |
{"example.com": ["10.20.20.20"]} ### ip of internal-dns.kube-system.svc.cluster.local.
存根域具有获取 DNS 名称的能力,internal-dns.kube-system.svc.cluster.local 本来可以工作,但由于 bug in kube-dns (dnsmasq),dnsmasq 容器无法启动并最终出现 CrashLoopBackOff。
internal-dns.kube-system.svc.cluster.local 是 CoreDNS/internal-dns 服务的名称。
dnsmasq 错误:
I1115 17:19:20.506269 1 main.go:74] opts: {{/usr/sbin/dnsmasq [-k --cache-size=1000 --no-negcache --log-facility=- --server=/cluster.local/127.0.0.1#10053 --server=/in-addr.arpa/127.0.0.1#10053 --server=/ip6.arpa/127.0.0.1#10053] true} /etc/k8s/dns/dnsmasq-nanny 10000000000}
I1115 17:19:20.506570 1 sync.go:167] Updated stubDomains to map[example.com:[internal-dns.kube-system.svc.cluster.local]]
I1115 17:19:20.506734 1 nanny.go:94] Starting dnsmasq [-k --cache-size=1000 --no-negcache --log-facility=- --server=/cluster.local/127.0.0.1#10053 --server=/in-addr.arpa/127.0.0.1#10053 --server=/ip6.arpa/127.0.0.1#10053 --server /example.com/internal-dns.kube-system.svc.cluster.local]
I1115 17:19:20.507923 1 nanny.go:116]
I1115 17:19:20.507952 1 nanny.go:116] dnsmasq: bad command line options: bad address
I1115 17:19:20.507966 1 nanny.go:119]
W1115 17:19:20.507970 1 nanny.go:120] Got EOF from stderr
I1115 17:19:20.507978 1 nanny.go:119]
W1115 17:19:20.508079 1 nanny.go:120] Got EOF from stdout
F1115 17:19:20.508091 1 nanny.go:190] dnsmasq exited: exit status 1
在stubdomain中使用ip时dnsmasq成功:
I1115 17:24:18.499937 1 main.go:74] opts: {{/usr/sbin/dnsmasq [-k --cache-size=1000 --no-negcache --log-facility=- --server=/cluster.local/127.0.0.1#10053 --server=/in-addr.arpa/127.0.0.1#10053 --server=/ip6.arpa/127.0.0.1#10053] true} /etc/k8s/dns/dnsmasq-nanny 10000000000}
I1115 17:24:18.500605 1 sync.go:167] Updated stubDomains to map[example.com:[10.20.20.20]]
I1115 17:24:18.500668 1 nanny.go:94] Starting dnsmasq [-k --cache-size=1000 --no-negcache --log-facility=- --server=/cluster.local/127.0.0.1#10053 --server=/in-addr.arpa/127.0.0.1#10053 --server=/ip6.arpa/127.0.0.1#10053 --server /example.com/10.20.20.20]
I1115 17:24:18.850687 1 nanny.go:119]
W1115 17:24:18.850726 1 nanny.go:120] Got EOF from stdout
I1115 17:24:18.850748 1 nanny.go:116] dnsmasq[15]: started, version 2.78 cachesize 1000
I1115 17:24:18.850765 1 nanny.go:116] dnsmasq[15]: compile time options: IPv6 GNU-getopt no-DBus no-i18n no-IDN DHCP DHCPv6 no-Lua TFTP no-conntrack ipset auth no-DNSSEC loop-detect inotify
I1115 17:24:18.850773 1 nanny.go:116] dnsmasq[15]: using nameserver 10.20.20.20#53 for domain example.com
I1115 17:24:18.850777 1 nanny.go:116] dnsmasq[15]: using nameserver 127.0.0.1#10053 for domain ip6.arpa
I1115 17:24:18.850780 1 nanny.go:116] dnsmasq[15]: using nameserver 127.0.0.1#10053 for domain in-addr.arpa
I1115 17:24:18.850783 1 nanny.go:116] dnsmasq[15]: using nameserver 127.0.0.1#10053 for domain cluster.local
I1115 17:24:18.850788 1 nanny.go:116] dnsmasq[15]: reading /etc/resolv.conf
I1115 17:24:18.850791 1 nanny.go:116] dnsmasq[15]: using nameserver 10.20.20.20#53 for domain example.com
I1115 17:24:18.850796 1 nanny.go:116] dnsmasq[15]: using nameserver 127.0.0.1#10053 for domain ip6.arpa
I1115 17:24:18.850800 1 nanny.go:116] dnsmasq[15]: using nameserver 127.0.0.1#10053 for domain in-addr.arpa
I1115 17:24:18.850803 1 nanny.go:116] dnsmasq[15]: using nameserver 127.0.0.1#10053 for domain cluster.local
I1115 17:24:18.850850 1 nanny.go:116] dnsmasq[15]: read /etc/hosts - 7 addresses