问题描述:开发服务器在内网,开发人员有时候会在家里或在其他地方进行开发,用的服务器端口转发到内网,更多的时候在同一个地方进行开发,频繁的设置更改开发地址非常不方便,那么有没有更好的解决方法呢?
解决方法:统一开发地址为域名地址,通过自定义解析域名,就可以解决改开发地址的问题。
环境说明
k8s 1.15
centos 7.x
域名:server.dev.foxwho.com
网卡IP:192.168.0.254 10.1.99.99
centos 开发服务器 安装 dnsmasq 域名解析服务
yum -y install dnsmasq
修改 dnsmasq 配置
文件:/etc/dnsmasq.conf
修改内容如下
resolv-file=/etc/resolv.dnsmasq.conf
strict-order
listen-address=127.0.0.1,10.1.99.99,192.168.0.254
addn-hosts=/etc/dnsmasq.hosts
参数解释:
resolv-file
定义dnsmasq
从哪里获取上游DNS服务器的地址, 默认是从/etc/resolv.conf
获取。在此我们定义的是从/etc/resolv.dnsmasq.conf
文件中获得。
strict-order
表示严格按照resolv-file
文件中的顺序从上到下进行DNS解析,直到第一个解析成功为止。
listen-address
定义dnsmasq
监听的地址,默认是监控本机的所有网卡上
addn-hosts
添加主机记录,就和/etc/hosts
文件一样,添加物理机hostname
和ip
映射
新建配置文件 /etc/resolv.dnsmasq.conf
根据你自己的 服务器填写
nameserver 192.168.0.1
nameserver 223.5.5.5
nameserver 223.6.6.6
新建配置文件 /etc/dnsmasq.hosts
根据你自己的 服务器hosts 填写
127.0.0.1 localhost
192.168.0.254 server.dev.foxwho.com
启动 dnsmasq 服务
systemctl enable dnsmasq --now
或
systemctl enable dnsmasq && systemctl start dnsmasq
查看状态
systemctl status dnsmasq
修改 服务器 nameserver 地址
修改服务器中的/etc/resolv.conf
里面 nameserver
地址换成dnsmasq
的服务地址,默认监听53端口。
nameserver 192.168.0.254
更新k8s dns 解析
查询 k8s dns
kubectl -n kube-system get pods
输出
coredns-8686dcc4fd-4bpqs 1/1 Running 0 20m
coredns-8686dcc4fd-xsd5h 1/1 Running 0 20m
etcd-foxserver 1/1 Running 4 80d
kube-apiserver-foxserver 1/1 Running 41 80d
kube-controller-manager-foxserver 1/1 Running 6 80d
kube-flannel-ds-amd64-jdxx4 1/1 Running 4 80d
kube-proxy-x79qh 1/1 Running 4 80d
kube-scheduler-foxserver 1/1 Running 6 80d
kubernetes-dashboard-76f6bf8c57-x5ph8 1/1 Running 4 80d
删除 coredns 让 k8s 重新创建新的 coredns
kubectl -n kube-system delete pod coredns-8686dcc4fd-4bpqs
kubectl -n kube-system delete pod coredns-8686dcc4fd-xsd5h
等待几秒钟,再次kubectl -n kube-system get pods
查看 就会看到新的已经创建成功并运行
测试dns
服务器测试
ping server.dev.foxwho.com
输出
PING server.dev.foxwho.com (192.168.0.252) 56(84) bytes of data.
64 bytes from foxserver (192.168.0.252): icmp_seq=1 ttl=64 time=0.025 ms
64 bytes from foxserver (192.168.0.252): icmp_seq=2 ttl=64 time=0.056 ms
64 bytes from foxserver (192.168.0.252): icmp_seq=3 ttl=64 time=0.057 ms
64 bytes from foxserver (192.168.0.252): icmp_seq=4 ttl=64 time=0.058 ms
k8s 测试
sentinel-0 为 是 你自己创建的 pod
kubectl exec -it sentinel-0 ping server.dev.foxwho.com
输出
PING server.dev.foxwho.com (192.168.0.252) 56(84) bytes of data.
64 bytes from 192-168-0-252.kubernetes.default.svc.cluster.local (192.168.0.252): icmp_seq=1 ttl=64 time=0.036 ms
64 bytes from 192-168-0-252.kubernetes.default.svc.cluster.local (192.168.0.252): icmp_seq=2 ttl=64 time=0.089 ms
64 bytes from 192-168-0-252.kubernetes.default.svc.cluster.local (192.168.0.252): icmp_seq=3 ttl=64 time=0.069 ms
64 bytes from 192-168-0-252.kubernetes.default.svc.cluster.local (192.168.0.252): icmp_seq=4 ttl=64 time=0.069 ms
64 bytes from 192-168-0-252.kubernetes.default.svc.cluster.local (192.168.0.252): icmp_seq=5 ttl=64 time=0.069 ms
64 bytes from 192-168-0-252.kubernetes.default.svc.cluster.local (192.168.0.252): icmp_seq=6 ttl=64 time=0.073 ms
域名绑定
假设,外部端口转发的服务器IP 为 47.52.41.70
,那么在域名解析中 把 server.dev.foxwho.com
的IP指向为 47.52.41.70
公司内部网络
如果有条件的(公司路由器支持的),在路由器中设置 域名 server.dev.foxwho.com
指向IP 192.168.0.254
没有条件的话,在每个开发人员的电脑上设置 /etc/hosts
,如果开发人员的电脑会带外出,那么记得 把 该 hosts
给删除
192.168.0.254 server.dev.foxwho.com
至此,内网,外网都可以正常访问,不用再频繁更改 开发服务器Ip了
kubernetes coredns 添加自定义DNS解析记录
自定义解析看如下
https://blog.csdn.net/kunyus/article/details/88841159
来源
https://www.cnblogs.com/cuishuai/p/9856843.html