k8s_centos7 安装部署coredns
1.简单来说,服务发现橘色服务(应用)之间相互定位的过程
2.服务发现并非云主机时代独有,传统的单体架构时代也会用到。以下应用场景下,更需要服务发现:
服务(应用)的动态性强
服务(应用)更新发布频繁
服务(应用)支持自动伸缩
3.在K8S集群里,POD的IP不断变化的,如何“以不变应万变”呢
抽象出了Service资源,通过标签选择器,关联一组pod
抽象出了集群网络,通过相对固定的“集群IP”,使服务接入点固定
4.如何自动关联Service资源的“名称”和“集群网络IP”,从而达到服务被集群自动发现的目的呢
传统的DNS模型:hdss7-14.host.com -> 192.168.16.14
能否在k8s里建立这样的模型:nginx-ds -> 10.254.0.5
5.K8S里服务发现的方式 - DNS
6.实现K8S里DNS功能的插件(软件)
kube-dns-kuberbetes-v1.2至kuberbetes-v1.10
Coredns-kuberbetes-v1.11至今
注意:
K8S里的DNS不是万能的!它应该只负责自动维护“服务名” -> “集群网络IP”之间的关系
K8S的服务发现插件 - CoreDNS
部署K8S的内网资源配置清单http服务
在运维主机HDSS7-12.host.com上,配置一个nginx虚拟主机,用以提供K8S统一的资源配置清单访问入口
配置nginx
[[email protected] ~]# mkdir /data/k8s-yaml
[[email protected] ~]# vi /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;
}
}
[[email protected] ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] ~]# nginx -s reload
配置内网DNS解析
[[email protected] ~]# vi /var/named/od.com.zone
$ORIGIN od.com.
$TTL 600 ; 10 minutes
@ IN SOA dns.od.com. dnsadmin.od.com. (
2019120903 ; serial # 03,***做下前滚
10800 ; refresh (3 hours)
900 ; retry (15 minutes)
604800 ; expire (1 week)
86400 ; minimum (1 day)
)
NS dns.od.com.
$TTL 60 ; 1 minute
dns A 192.168.16.11
harbor A 192.168.16.12
k8s-yaml A 192.168.16.12 # 添加地址解析
[[email protected] ~]# systemctl restart named
[[email protected] ~]# dig -t A k8s-yaml.od.com @192.168.16.11 +short
[[email protected] ~]# cd /data/k8s-yaml/
[[email protected] k8s-yaml]# mkdir coredns
制作上传coredns的镜像
[[email protected] k8s-yaml]# docker pull coredns/coredns:1.6.1
[[email protected] k8s-yaml]# docker tag coredns/coredns:1.6.1 harbor.od.com/public/coredns:v1.6.1
[[email protected] k8s-yaml]# docker push harbor.od.com/public/coredns:v1.6.1
创建资源管理配置清单
[[email protected] k8s-yaml]# cd coredns/
[[email protected] coredns]# vi 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
[[email protected] coredns]# vi cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
data:
Corefile: |
.:53 {
errors
log
health
ready
kubernetes cluster.local 10.254.0.0/16 # service网段
forward . 192.168.16.11 # 物理机安装dns服务的地址
cache 30
loop
reload
loadbalance
}
[[email protected] coredns]# vi 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
[[email protected] coredns]# vi 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: 10.254.0.2 # dns服务的ip
ports:
- name: dns
port: 53
protocol: UDP - name: dns-tcp
port: 53 - name: metrics
port: 9153
protocol: TCP
官方coredns资源配置清单下载地址
https://github.com/kubernetes/kubernetes/blob/master/cluster/addons/dns/coredns/coredns.yaml.base
在mater上运行
[[email protected] ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/rbac.yaml
[[email protected] ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/cm.yaml
[[email protected] ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/dp.yaml
[[email protected] ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/svc.yaml
[[email protected] ~]# kubectl get svc -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
coredns ClusterIP 10.254.0.2 53/UDP,53/TCP,9153/TCP 26s
[[email protected] ~]# kubectl get all -n kube-system
NAME READY STATUS RESTARTS AGE
pod/coredns-6b6c4f9648-srsht 1/1 Running 0 114s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/coredns ClusterIP 10.254.0.2 53/UDP,53/TCP,9153/TCP 101s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/coredns 1/1 1 1 114s
NAME DESIRED CURRENT READY AGE
replicaset.apps/coredns-6b6c4f9648 1 1 1 114s
[[email protected] bin]# cat kubelet.sh # 里面定义了dns的ip地址
验证dns服务
[[email protected] ~]# dig -t A www.baidu.com @10.254.0.2 +short
www.a.shifen.com.
180.101.49.12
180.101.49.11
[[email protected] ~]# dig -t A hdss7-11.host.com @10.254.0.2 +short
192.168.16.11
[[email protected] ~]# kubectl create deployment nginx-dp --image=harbor.od.com/public/nginx:v1.7.9 -n kube-public
[[email protected] ~]# kubectl expose deployment nginx-dp --port=80 -n kube-public
[[email protected] ~]# dig -t A nginx-dp.kube-public.svc.cluster.local. @10.254.0.2 +short
10.254.127.91
[[email protected] ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-dp-5dfc689474-xzfgq 1/1 Running 0 6m57s
[[email protected] ~]# kubectl exec -it nginx-dp-5dfc689474-xzfgq /bin/bash -n kube-public
[email protected]:/# cat /etc/resolv.conf
nameserver 10.254.0.2
search kube-public.svc.cluster.local svc.cluster.local cluster.local host.com
options ndots:5
CoreDNS:Kubernetes内部域名解析原理、弊端及优化方式
http://ccnuo.com/
上一篇: k8s篇-使用yaml部署coredns