kubernetes1.11版本開始,kubernetes集群內部的DNS解析主要由coredns完成
kubelet中指定DNS解析地址
kubelet-config.yml中添加配置
clusterDNS: # DNS服務的clusterIP
- 11.0.0.2
clusterDomain: cluster.local # 在DNS服務中設置的域名
部署coredns服務
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
- apiGroups:
- ""
resources:
- nodes
verbs:
- get
---
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
labels:
addonmanager.kubernetes.io/mode: EnsureExists
data:
Corefile: |
.:53 {
errors
health
ready
kubernetes cluster.local in-addr.arpa ip6.arpa { # cluster.local為指定的域名,與kubelet中配置的clusterDomain要一致
pods insecure
fallthrough in-addr.arpa ip6.arpa
ttl 30
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: coredns
namespace: kube-system
labels:
k8s-app: kube-dns
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
kubernetes.io/name: "CoreDNS"
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
selector:
matchLabels:
k8s-app: kube-dns
template:
metadata:
labels:
k8s-app: kube-dns
annotations:
seccomp.security.alpha.kubernetes.io/pod: 'docker/default'
spec:
priorityClassName: system-cluster-critical
serviceAccountName: coredns
tolerations:
- key: "CriticalAddonsOnly"
operator: "Exists"
nodeSelector:
beta.kubernetes.io/os: linux # 選擇Linux操作系統部署
containers:
- name: coredns
image: coredns/coredns:1.6.1
imagePullPolicy: IfNotPresent
resources:
limits:
memory: 512Mi
requests:
cpu: 100m
memory: 70Mi
args: [ "-conf", "/etc/coredns/Corefile" ]
volumeMounts:
- name: config-volume
mountPath: /etc/coredns
readOnly: true
- name: date-config
mountPath: /etc/localtime
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: 180
timeoutSeconds: 10
successThreshold: 1
failureThreshold: 5
readinessProbe:
httpGet:
path: /ready
port: 8181
scheme: HTTP
securityContext:
allowPrivilegeEscalation: false
capabilities:
add:
- NET_BIND_SERVICE
drop:
- all
readOnlyRootFilesystem: true
dnsPolicy: Default
volumes:
- name: config-volume
configMap:
name: coredns
items:
- key: Corefile
path: Corefile
- name: date-config
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
---
apiVersion: v1
kind: Service
metadata:
name: kube-dns
namespace: kube-system
annotations:
prometheus.io/port: "9153"
prometheus.io/scrape: "true"
labels:
k8s-app: kube-dns
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile
kubernetes.io/name: "CoreDNS"
spec:
selector:
k8s-app: kube-dns
clusterIP: 11.0.0.2 # 指定service的clusterIP,與kubelet中配置的要一值
ports:
- name: dns
port: 53
protocol: UDP
- name: dns-tcp
port: 53
protocol: TCP
- name: metrics
port: 9153
protocol: TCP
coredns的配置說明
coredns的主要功能是通過插件系統實現的。coredns實現一種鏈式插件結構,將DNS的邏輯抽象成一個個插件,能夠靈活的使用。
常用的插件如下:
| 插件名稱 | 功能描述 |
|---|---|
| loadbalance | 提供基於DNS的負載均衡 |
| loop | 檢測在DNS解析過程中出現的簡單循環問題 |
| cache | 提供前端緩存功能 |
| health | 對Endpoint進行健康檢查 |
| kubernetes | 從kubernetes中讀取zone數據 |
| etcd | 從etcd讀取zone數據,可用於自定義域名 |
| file | 從RFC1035格式文件中讀取zone數據 |
| hosts | 使用/etc/hosts文件或者其他文件讀取zone數據,可以用戶自定義域名 |
| auto | 從磁盤中自動加載區域文件 |
| reload | 定時自動重新加載Corefile配置文件的內容 |
| forward | 轉發域名查詢到上游DNS服務器 |
| proxy | 轉發特定的域名查詢到其他DNS服務器,同時提供多個DNS服務器的負載均衡功能 |
| prometheus | 為prometheus系統提供采集性能指標的url |
| pprof | 在url路徑/debug/pprof下提供運行時的性能數據 |
| log | 對dns查詢進行日志記錄 |
| errors | 對錯誤信息進行日志記錄 |
| 在上述事例中域名"cluster.local"的域名設置了一系列插件,域名解析時這些插件將從上往下依次執行: |
.:53 {
errors
health
ready
kubernetes cluster.local in-addr.arpa ip6.arpa { # 指定域名,可指定多個,要包含kubelet中配置的clusterDomain
pods insecure
fallthrough in-addr.arpa ip6.arpa
ttl 30
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
另外,etcd和hosts插件都可以用於用戶自定義域名記錄
下面是使用etcd插件的配置示例,將已.com結尾的域名配置為從etcd中獲取,並將域名記錄保存在/skydns路徑下:
{
etcd com {
path /skydns
endpoint http://10.0.1.111:2379
upstream /etc/resolve.conf
}
cache 160 com
loadbalance
proxy . /etc/resolve.conf
}
如果用戶在etcd中插入一條dns記錄,客戶端應用應該就可以解析這個域名了:
etcdctl put /skydns/com/test '{"host": "10.1.1.1", "ttl": 60}'
客戶端解析域名:
nslookup test.com
forward和proxy插件都可以用於配置上游DNS服務器或其他DNS服務器,當coredns中查詢不到域名時,會到其他DNS服務器去查詢。
coredns域名解析的格式
service的ip:
<svc name>.<namespace>.svc.cluster.local # cluster.local為上面定義的域名
statefulset內的pod域名解析
<pod名>.<headless service名>.<namespace>.svc.cluster.local # 有狀態的應用的pod可以進行域名解析,無狀態應用不可以,域名解析需要配置一個headless service
pod級別的DNS配置
除了使用集群范圍的DNS服務(coredns),pod內也可以直接設置dns的相關策略和配置。主要由兩個參數來設置:spec.dnsPolicy和spec.dnsConfig
示例:
dnsPolicy: None # 清除dns規則,Default:與宿主機一致,ClusterFirst:使用coredns的,如設置了 hostNetwork = true 時,ClusterFirst 會被強制轉化為 Default
dnsConfig: # 額外配置host文件dns
nameservers:
- 172.32.1.201
searches:
- ns1.svc.cluster.local
options:
- name: ndots
value: "2"
- name: edns0
部署pod后,可從pod的/etc/resolve.conf查看配置的域名
dnsPolicy配置
| 參數 | 說明 |
|---|---|
| Default | 繼承pod所在宿主機的DNS配置 |
| ClusterFirst | 優先使用k8s環境的dns服務(coredns),無法解析的域名轉發到從宿主機繼承的DNS服務器,如設置了 hostNetwork = true 時,ClusterFirst 會被強制轉化為 Default |
| ClusterFirstWithHostNet | 與ClusterFirst相同,對使用hostNetwork模式運行的pod,應明確指定改策略 |
| None | 清除dns規則,通過dnsConfig配置 |
dnsConfig配置
| 參數 | 說明 |
|---|---|
| nameservers | 一組DNS服務器的列表,最多可以設置3個 |
| searches | 一組用戶域名搜索的DNS域名后綴,最多可以設置6個 |
| options | 配置其他可選DNS參數,例如ndots、timeout等,以name或name/value對的形式表示 |
