一、服务发现
1、概念
简单来说,服务发现就是服务(应用)之间相互定位的过程。
服务发现并非云计算时代独有的,传统的单体架构时代也会用到。以下应用场景下,更需要服务发现
服务(应用)的动态性强
服务(应用)更新发布频繁
服务(应用)支持自动伸缩
在k8s集群里,POD的IP是不断变化的,如何“以不变应万变”
抽象出了service资源,通过标签选择器,关联一组POD
抽象出了集群网络,通过相对固定的“集群IP”,使服务接入点固定
那么如何自动关联Service资源的“名称”和“集群网络IP”,从而达到服务被集群自动发现的目的呢?
考虑传统DNS的模型:hdss7-21.host.com->10.4.7.21
能否在k8s里建立这样的模型:nginx-ds->192.168.0.5
2、k8s里服务发现的方式-DNS
3、实现k8s里DNS功能的插件(软件)
kube-dns-kubernetes-v1.2至kubernetes-v1.10
Coredns-kubernetes-v1.11至今
注意:k8s里的DNS不是万能的!它应该只负责自动维护“服务名”->"集群网络IP"之间的关系
二、部署k8s的核心组件(CoreDNS)
1、编写配置文件
vim /etc/nginx/conf.d/k8s-yaml.od.com.conf server { listen 80; server_name k8s-yaml.od.com; location / { autoindex on; default_type text/plain; root /data/k8s-yaml; } } mkdir /data/k8s-yaml/coredns -p
2、创建yaml文件
vim /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 --- apiVersion: v1 kind: ConfigMap metadata: name: coredns namespace: kube-system data: Corefile: | .:53 { errors log health kubernetes cluster.local 182.168.0.0/16 #鏍规嵁kubelet鐨勯厤缃�枃浠朵慨鏀规�澶勪俊鎭� forward . 192.168.112.60 cache 30 loop reload loadbalance } vim /data/k8s-yaml/coredns/cm.yaml apiVersion: v1 kind: ConfigMap metadata: name: coredns namespace: kube-system data: Corefile: | .:53 { errors log health kubernetes cluster.local 192.168.0.0/16 #鏍规嵁kubelet鐨勯厤缃�枃浠朵慨鏀规�澶勪俊鎭� forward . 192.168.112.60 cache 30 } vim /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 vim /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
3、修改dns
[root@slave1 ~]# vim /var/named/od.com.zone k8s-yaml A 192.168.112.24 systemctl restart named
4、docker下载镜像
docker pull docker.io/coredns/coredns:1.6.1 docker tag c0f6e815079e harbor.od.com/public/coredns:v1.6.1 docker push harbor.od.com/public/coredns:v1.6.1
5、用陈述式
kubectl apply -f http://k8s-yaml.od.com/coredns/rbac.yaml kubectl apply -f http://k8s-yaml.od.com/coredns/cm.yaml kubectl apply -f http://k8s-yaml.od.com/coredns/dp.yaml kubectl apply -f http://k8s-yaml.od.com/coredns/svc.yaml
6、测试
dig -t A k8s-yaml.bbdops.com @192.168.0.2 +short 10.61.10.43dig -t A www.baidu.com @192.168.0.2 +short www.a.shifen.com. 110.242.68.dig -t A k8s-yaml.bbdops.com @192.168.0.2 +short 10.61.10.43