kubernetes之coredns玩法

1、概述html

新版本的kubernetes默認使用了coredns,這裏就不贅述了。直達車:https://coredns.io/https://kubernetes.io/docs/tasks/administer-cluster/dns-custom-nameservers/#corednsnode

2、coredns配置api

在以前的博客裏面介紹過使用dnsmasq做爲coredns的上游dns的玩法,這裏介紹直接修改coredns的配置,coredns的配置是Corefile,能夠經過configmap控制修改。默認的配置以下:app

# kubectl get cm coredns -n kube-system -o yamldom

apiVersion: v1 kind: ConfigMap metadata: name: coredns namespace: kube-system data: Corefile: | .:53 { errors health kubernetes cluster.local in-addr.arpa ip6.arpa { pods insecure upstream fallthrough in-addr.arpa ip6.arpa } prometheus :9153 proxy . /etc/resolv.conf cache 30 loop reload loadbalance }

errors、health等都是cordns的插件,更多插件參考:https://coredns.io/plugins/curl

kuberneets1.10開始,支持將kube-dns的配置轉換成coredns的配置。kube-dns裏面使用stubDomains來指定存根域,即oop

apiVersion: v1 kind: ConfigMap metadata: name: kube-dns namespace: kube-system data: stubDomains: | {"demo.local": ["10.0.0.1"]}

 

使用upstreamNameservers來指定非集羣dns查找使用外部dns解析,若是設置default默認只會使用node節點dns解析。測試

apiVersion: v1 kind: ConfigMap metadata: name: kube-dns namespace: kube-system data: upstreamNameservers: | ["10.0.0.1"]

 

在coredns裏面使用upstream和proxy實現kube-dns的stubDomains、upstreamNameservers的功能。ui

一、coredns使用consul做爲dns查詢url

修改coredns的configmap:

# kubectl -n kube-system edit  cm coredns

 
 
apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns
  namespace: kube-system
data:
  Corefile: |
    .:53 {
        errors
        health
        kubernetes cluster.local in-addr.arpa ip6.arpa {
           pods insecure
           upstream
           fallthrough in-addr.arpa ip6.arpa
        }
        prometheus :9153
        proxy . /etc/resolv.conf
        cache 30
        loop
        reload
        loadbalance
    }
 service.hq:53 { 
errors
cache
30
proxy .
10.4.9.6
}

service.hq是在consul裏面設置的domain,consul使用參考我可博客:http://www.javashuo.com/article/p-ornwlmdi-em.html,個人consul設置的dns端口是53,默認是8600. 10.4.9.6是consul的監聽地址。

從新調度pod使配置生效:

kubectl get pods -n kube-system | grep coredns | awk '{print $1}' | xargs kubectl -n kube-system delete pod

 

測試:

activity是咱們在consul裏面註冊的服務,sleep是包含curl和ping的pod。

# kubectl exec -it -n istio-system sleep-754684654f-c6mct -- ping activity.service.hq

 

附錄:

sleep的yaml:

apiVersion: v1 kind: ServiceAccount metadata: name: sleep
--- apiVersion: v1 kind: Service metadata: name: sleep labels: app: sleep spec: ports: - port: 80 name: http selector: app: sleep
--- apiVersion: extensions/v1beta1 kind: Deployment metadata: name: sleep spec: replicas: 1 template: metadata: labels: app: sleep spec: serviceAccountName: sleep containers: - name: sleep image: pstauffer/curl command: ["/bin/sleep", "3650d"] imagePullPolicy: IfNotPresent ---
相關文章
相關標籤/搜索