一、服务发现
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
dig -t A k8s-yaml.od.com @192.168.112.20 +short
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
华为云开年送福利:
福利1:免费试用海外云主机和云原生容器
进入免费试用专区 【实名才可领取】:
福利2:超低折扣
原价1244.76元一年的1C2G1M带宽的服务器现在只需要:99元,赠送主机安全
福利三:
终极大奖,华为云永久VIP(实名注册第59、119、189...以此类推)
购买华为云永久8折权限(除云专线,硬件产品,混托类产品,域名,竞价实例,储值卡,云市场产品,国际云联盟节点,HCS Online解决文案以上特定产品),均可享永久8折。
参与攻略:
1,长按识别下方二维码关联注册 网页连接
2,实名认证
接下来,就可以静等开奖了,此活动持续到月底结束,我们会每隔五个工作日后进行开奖,请小伙伴注意查收。