Kubernetes集群添加主机名解析
在Kubernetes集群中添加主机名解析分以下几种情况:
- 单个Pod添加直接主机别名
- 在CoreDNS添加主机名解析
1、pod中配置hosts解析域名
编辑deployment.yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "2"
creationTimestamp: "2020-09-11T08:35:00Z"
generation: 2
labels:
k8s-app: nginx-hosts-alis
qcloud-app: nginx-hosts-alis
name: nginx-hosts-alis
namespace: test
resourceVersion: "12673987137"
selfLink: /apis/apps/v1beta2/namespaces/test/deployments/nginx-hosts-alis
uid: 61493e8e-b0c9-4b5d-a031-6ea2799e9de8
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: nginx-hosts-alis
qcloud-app: nginx-hosts-alis
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
k8s-app: nginx-hosts-alis
qcloud-app: nginx-hosts-alis
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: nginx-hosts-alis
resources:
limits:
cpu: 500m
memory: 1Gi
requests:
cpu: 250m
memory: 256Mi
securityContext:
privileged: false
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
hostAliases:
- hostnames:
- foo.local
- bar.local
ip: 127.0.0.1
- hostnames:
- foo.remote
- bar.remote
ip: 10.1.2.3
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
2、CoreDNS添加主机名解析
如果node本地有dns插件,如NodeLocalDNS,需要先对NodeLocalDNS进行转发。比如域名解析出来的ip为169.254.25.10,需要将此请求现转发到coredns中,以coredns ip10.233.0.3为例,如下:
apiVersion: v1
kind: ConfigMap
metadata:
creationTimestamp: "2024-09-15T01:02:02Z"
labels:
addonmanager.kubernetes.io/mode: EnsureExists
name: nodelocaldns
namespace: kube-system
resourceVersion: "55695455"
uid: 8b2c7245-6563-4a5f-bb11-cbdc5bb9e17c
data:
Corefile: |
cluster.local:53 {
errors
cache {
success 9984 30
denial 9984 5
}
reload
loop
bind 169.254.25.10
forward . 10.233.0.3 {
force_tcp
}
prometheus :9253
health 169.254.25.10:9254
}
in-addr.arpa:53 {
errors
cache 30
reload
loop
bind 169.254.25.10
forward . 10.233.0.3 {
force_tcp
}
prometheus :9253
}
ip6.arpa:53 {
errors
cache 30
reload
loop
bind 169.254.25.10
forward . 10.233.0.3 {
force_tcp
}
prometheus :9253
}
.:53 {
errors
cache 30
reload
loop
bind 169.254.25.10
forward . 10.233.0.3 {
force_tcp
}
prometheus :9253
}
CoreDNS配置添加hosts主机名解析。
apiVersion: v1
kind: ConfigMap
metadata:
creationTimestamp: "2024-09-15T01:01:58Z"
labels:
addonmanager.kubernetes.io/mode: EnsureExists
name: coredns
namespace: kube-system
resourceVersion: "55644208"
uid: 7e4fb70b-bac6-4c13-ad1a-8e9c0dbc0861
data:
Corefile: |
.:53 {
errors
health {
lameduck 5s
}
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
}
hosts {
10.65.10.91 jingxiang.oss-cn-lasa-xzns-d01-a.ops.sukai.com.cn
10.65.10.91 oss-cn-lasa-xzns-d01-a.ops.sukai.com.cn
fallthrough
}
prometheus :9153
forward . 114.114.114.114 {
prefer_udp
max_concurrent 1000
}
cache 30
loop
reload
loadbalance
}