场景:
部署完容器集群之后,发现连不上erueka和其他的一些外部域名
Caused by: java.net.UnknownHostException: eureka-0.discovery
环境中有,阿里云的privatezone,有内网的dns服务器

处理思路
一、
直接去coredns的conf里面加host信息
kubectl edit cm coredns -nkube-system
hosts {
192.168.1.1 eureka-0.discovery.test.svc.cluster.local
fallthrough
}
单独添加发现是可以ping通的,问题是应用依赖比较多的中间件服务,几十个域名全部这里也不太合理。
二、修改coredns conf里面的信息,直接写死dns服务器
#forward . /etc/resolv.conf
forward . 10.0.0.10:53 10.0.0.11:1053 [2003::1]:53
然后问题来了,虽然可以访问到dns上面定义的域名,但是内部service之间不能访问和解析了。
三、重新安装coredns
原版本升级到了最新版本
删除已有pod
kubectl delete --namespace=kube-system deployment coredns
COPY
重新安装
wget https://raw.githubusercontent.com/coredns/deployment/master/kubernetes/coredns.yaml.sed
wget https://raw.githubusercontent.com/coredns/deployment/master/kubernetes/deploy.sh
chmod +x deploy.sh
./deploy.sh | kubectl apply -f -
重新安装发现问题并没有解决,继续,,
四、先更改coredns的cm,然后再重新安装coredns。
当环境里面有namserver有多个时,coredns内部默认采用的时random的方式随机转发,失败后就返回错误。
由于是在云上的环境,我们添加上公司的nameserver后,云环境自己也有对于的nameserver在resolv.conf文件中配置,导致coredns在转发的时候,random方式选择到云环境自己的dns nameserver的时候,就解析不了我们内部使用的存储的域名,所以解决方式是修改resolv.conf文件,将我们自己的nameserver添加到宿主机resolv.conf第一个nameserver,然后修改coredns配置,将其forward的policy设置为sequential,然后重建coredns的pod。
coredns在复用宿主机/etc/resolv.conf时,未严格按照nameserver的顺序去解析域名,修改方式,添加sequential策略:
forward . /etc/resolv.conf {
policy sequential
}
验证之后,处理掉了域名解析时好时坏的问题。nice

配置模板
查看 coredns 的配置文件如下
apiVersion: v1
data:
Corefile: |
.:53 {
errors
health {
lameduck 5s
}
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
reload
loadbalance
}
kind: Configap
metadata:
name: coredns
namespace: kube-system
其中 forward . /etc/resolv.conf 配置表示使用当coredns内部不能解析的时候,
向宿主机上的resolv.conf文件中配置的nameserver转发dns解析请求,
当宿主机上namserver有多个时,默认采用的时random的方式随机转发,失败后就返回错误。

报错处理
发现问题,确实是由于Readiness probe failed健康检查的时候报错,导致不能ready,最后有这样的描述信息: Warning Unhealthy 36m (x4 over 3h16m) kubelet Readiness probe failed: Get “http://10.20.32.131:8181/ready”: dial tcp 10.20.32.131:8181: connect: connection refused

导出deploy
kubectl get deploy coredns -nkube-system -o yaml >/tmp/test.yml
注释以下内容
readinessProbe:
failureThreshold: 3
httpGet:
path: /ready
port: 8181
scheme: HTTP
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
解决报错

其他知识了解
Pod dns策略
Default: 继承Pod所在宿主机的DNS设置
ClusterFirst:优先使用kubernetes环境的dns服务,将无法解析的域名转发到从宿主机继承的dns服务器
ClusterFirstWithHostNet:和ClusterFirst相同,对于以hostNetwork模式运行的Pod应明确知道使用该策略
None: 忽略kubernetes环境的dns配置,通过spec.dnsConfig自定义DNS配置
我们环境中使用了ClusterFirst,如不修改则是默认的ClusterFirst

自定义Dns配置可以通过spec.dnsConfig字段进行设置,可以设置如下信息
nameservers:一组dns服务器的列表,最多可设置3个
searchs:一组用于域名搜索的dns域名后缀,最多6个
options:配置其他可选参数,例如ndots、timeout等

coredns解析策略
policy specifies the policy to use for selecting upstream servers. The default is random.
random is a policy that implements random upstream selection.
round_robin is a policy that selects hosts based on round robin ordering.
sequential is a policy that selects hosts based on sequential ordering.
health_check configure the behaviour of health checking of the upstream servers

命令
kubectl edit cm coredns -n kube-system -o yaml
kubectl edit deployment coredns -n kube-system -o yaml
kubectl logs -f coredns-57ffb6f49-qnksq -n kube-system

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐