Kubernetes 服务自动发现CoreDNS

前言

Service服务,是一个概念,逻辑通过selector标签代理指定后端pod。众所周知,pod生命周期短,状态不稳定,pod错误异常后新生成的Pod IP会发生变化,之前Pod的访问方式均不可达。通过service对pod做代理,service有固定的IP和Port,IP:Port组合自动通过selector标签关联后端Pod,即使Pod发生改变,kubernetes内部更新这组关联关系,使得service能够匹配到新的Pod。这样,通过service提供的固定Ip,用户再也不用关心需要访问哪个Pod,以及Pod是否发生改变,大大提高了服务质量。如果Pod使用Deployment创建了多个副本,那么service就能代理多个相同的Pod,通过kube-proxy,实现负载均衡

服务发现在微服务架构里,各个服务之间经常进行通信,服务发现就是解决不同服务之间通信的问题。比如一个nginx的pod,要访问一个mysql服务,就需要知道mysql服务的Ip和Port,获取Ip和Port的过程就是服务发现。

服务发现

1.环境变量:
Pod创建的时候,服务的IP和Port会以环境变量的形式注入到pod里
2.dns:
K8s集群部署一个Dns服务器,Service创建成功后,会在Dns服务器里写入一些记录,想要访问某个服务,通过Dns服务器解析出对应的Ip和Port,从而实现k8s集群内部的服务访问
 
CoreDns可以解决Service的发现问题,k8s将Service的名称当做域名注册到CoreDns中,通过Service的名称就可以访问其提供的服务
 

CoreDNS简介

CoreDNS是一个Go语言实现的链式插件DNS服务端, 实现了集群内部通过服务名进行可以访问,是CNCF成员,是一个 高性能、易扩展的DNS服务端。可以很方便的部署在k8s集群中,用来代替kube-dns

 

CoreDNS部署 

使用yaml方式部署CoreDNS

1、yaml资源配置清单

• rbac

$ vi /data/k8s-yaml/coredns/rbac.yaml
apiVersion: v1 kind: ServiceAccount metadata: name: coredns namespace: kube-system labels: kubernetes.io/cluster-service: "true" addonmanager.kubernetes.io/mode: Reconcile --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: labels: kubernetes.io/bootstrapping: rbac-defaults addonmanager.kubernetes.io/mode: Reconcile name: system:coredns rules: - apiGroups: - "" resources: - endpoints - services - pods - namespaces verbs: - list - watch --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: annotations: rbac.authorization.kubernetes.io/autoupdate: "true" labels: kubernetes.io/bootstrapping: rbac-defaults addonmanager.kubernetes.io/mode: EnsureExists name: system:coredns roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: system:coredns subjects: - kind: ServiceAccount name: coredns namespace: kube-system

 

• ConfigMap

$ vi /data/k8s-yaml/coredns/cm.yaml
apiVersion: v1 kind: ConfigMap metadata: name: coredns namespace: kube-system data: Corefile: | .:53 { errors log health ready kubernetes cluster.local 192.168.0.0/16 forward . 10.4.7.11 cache 30 loop reload loadbalance }

关键参数解释:

kubernetes cluster.local 192.168.0.0/16:Cluster网段(该网段就是service ip地址范围)

forward . 10.4.7.11:制定上级dns服务器地址

 

• Deployment

$ vi /data/k8s-yaml/coredns/dp.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: coredns namespace: kube-system labels: k8s-app: coredns kubernetes.io/name: "CoreDNS" spec: replicas: 1 selector: matchLabels: k8s-app: coredns template: metadata: labels: k8s-app: coredns spec: priorityClassName: system-cluster-critical serviceAccountName: coredns containers: - name: coredns image: harbor.od.com/public/coredns:v1.6.1 args: - -conf - /etc/coredns/Corefile volumeMounts: - name: config-volume mountPath: /etc/coredns 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

 

• Server

$ vi /data/k8s-yaml/coredns/svc.yaml
apiVersion: v1 kind: Service metadata: name: coredns namespace: kube-system labels: k8s-app: coredns kubernetes.io/cluster-service: "true" kubernetes.io/name: "CoreDNS" spec: selector: k8s-app: coredns clusterIP: 192.168.0.2 ports: - name: dns port: 53 protocol: UDP - name: dns-tcp port: 53 - name: metrics port: 9153 protocol: TCP

关键参数解释:

clusterIP: 192.168.0.2  指定了coredns的server cluster ip地址(必须要和kubelet的--cluster-dns ip一致)

 

2、创建资源

$ kubectl apply -f rbac.yaml
$ kubectl apply -f cm.yaml
$ kubectl apply -f dp.yaml
$ kubectl apply -f svc.yaml

 

3、查看CoreDns是否运行

$ kubectl get pods -n kube-system
NAME                       READY   STATUS    RESTARTS   AGE
coredns-6dd977c48b-rvb7h   1/1     Running   0          31s

$ kubectl get svc -n kube-system
NAME      TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)                  AGE
coredns   ClusterIP   192.168.0.2   <none>        53/UDP,53/TCP,9153/TCP   3m20s

 

4、通过core dns解析svc名称获取到cluster-ip

# svc名称.命名空间.svc.cluster.local
$ dig -t A nginx-ds.default.svc.cluster.local @192.168.0.2 +short
192.168.155.175

 

5、进入pod查看域名解析文件

$ kubectl exec -it nginx-ds-gbt9g bash
root@nginx-ds-gbt9g:/# cat /etc/resolv.conf
nameserver 192.168.0.2   # CoreDns地址
search default.svc.cluster.local svc.cluster.local cluster.local 4
options ndots:5

在容器内部是可以访问到本k8s集群里的所有svc

 

posted @ 2020-02-02 03:45  jasonminghao  阅读(1477)  评论(0编辑  收藏  举报