k8s_centos7 安裝部署coredns

zz891422822發表於2020-12-14

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
[root@hdss7-12 ~]# mkdir /data/k8s-yaml
[root@hdss7-12 ~]# 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;
}

}
[root@hdss7-12 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@hdss7-12 ~]# nginx -s reload


配置內網DNS解析
[root@hdss7-11 ~]# 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 # 新增地址解析

[root@hdss7-11 ~]# systemctl restart named

[root@hdss7-11 ~]# dig -t A k8s-yaml.od.com @192.168.16.11 +short


[root@hdss7-12 ~]# cd /data/k8s-yaml/
[root@hdss7-12 k8s-yaml]# mkdir coredns


製作上傳coredns的映象
[root@hdss7-12 k8s-yaml]# docker pull coredns/coredns:1.6.1
[root@hdss7-12 k8s-yaml]# docker tag coredns/coredns:1.6.1 harbor.od.com/public/coredns:v1.6.1
[root@hdss7-12 k8s-yaml]# docker push harbor.od.com/public/coredns:v1.6.1

建立資源管理配置清單
[root@hdss7-12 k8s-yaml]# cd coredns/

[root@hdss7-12 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

[root@hdss7-12 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
}

[root@hdss7-12 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

[root@hdss7-12 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上執行
[root@hdss7-14 ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/rbac.yaml
[root@hdss7-14 ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/cm.yaml
[root@hdss7-14 ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/dp.yaml
[root@hdss7-14 ~]# kubectl apply -f http://k8s-yaml.od.com/coredns/svc.yaml

[root@hdss7-14 ~]# 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

[root@hdss7-14 ~]# 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


[root@hdss7-11 bin]# cat kubelet.sh # 裡面定義了dns的ip地址


驗證dns服務
[root@hdss7-14 ~]# dig -t A www.baidu.com @10.254.0.2 +short
www.a.shifen.com.
180.101.49.12
180.101.49.11
[root@hdss7-14 ~]# dig -t A hdss7-11.host.com @10.254.0.2 +short
192.168.16.11


[root@hdss7-14 ~]# kubectl create deployment nginx-dp --image=harbor.od.com/public/nginx:v1.7.9 -n kube-public

[root@hdss7-14 ~]# kubectl expose deployment nginx-dp --port=80 -n kube-public

[root@hdss7-14 ~]# dig -t A nginx-dp.kube-public.svc.cluster.local. @10.254.0.2 +short
10.254.127.91

[root@hdss7-14 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-dp-5dfc689474-xzfgq 1/1 Running 0 6m57s

[root@hdss7-14 ~]# kubectl exec -it nginx-dp-5dfc689474-xzfgq /bin/bash -n kube-public

root@nginx-dp-5dfc689474-xzfgq:/# 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/