有没有办法向 kube-dns 添加任意记录? [英] Is there a way to add arbitrary records to kube-dns?
问题描述
我会用一种非常具体的方式来解释我的问题,但我认为这比用抽象的方式解释要具体一些...
I will use a very specific way to explain my problem, but I think this is better to be specific than explain in an abstract way...
假设有一个 MongoDB 副本集位于 Kubernetes 集群之外但位于网络中.副本集所有成员的ip地址由app服务器和db服务器中的/etc/hosts解析.
Say, there is a MongoDB replica set outside of a Kubernetes cluster but in a network. The ip addresses of all members of the replica set were resolved by /etc/hosts in app servers and db servers.
在实验/过渡阶段,我需要从 kubernetes pod 访问那些 mongo db 服务器.但是,kubernetes 似乎不允许向 pods/containers 中的/etc/hosts 添加自定义条目.
In an experiment/transition phase, I need to access those mongo db servers from kubernetes pods. However, kubernetes doesn't seem to allow adding custom entries to /etc/hosts in pods/containers.
MongoDB 副本集已经在处理大数据集,不能在集群中创建新的副本集.
The MongoDB replica sets are already working with large data set, creating a new replica set in the cluster is not an option.
因为我使用 GKE,所以我想应该避免更改 kube-dns 命名空间中的任何资源.配置或替换 kube-dns 以适合我的需要是最后一件事.
Because I use GKE, changing any of resources in kube-dns namespace should be avoided I suppose. Configuring or replace kube-dns to be suitable for my need are last thing to try.
有没有办法在 Kubernetes 集群中解析自定义主机名的 ip 地址?
Is there a way to resolve ip address of custom hostnames in a Kubernetes cluster?
这只是一个想法,但如果 kube2sky 可以读取 configmap 的一些条目并将它们用作 dns 记录,那就太好了.例如repl1.mongo.local: 192.168.10.100
.
It is just an idea, but if kube2sky can read some entries of configmap and use them as dns records, it colud be great.
e.g. repl1.mongo.local: 192.168.10.100
.
我从 https://github.com/kubernetes/kubernetes/issues 引用了这个问题/12337
推荐答案
UPDATE: 2017-07-03 Kunbernetes 1.7 现在支持 使用 HostAliases 将条目添加到 Pod/etc/hosts.
UPDATE: 2017-07-03 Kunbernetes 1.7 now support Adding entries to Pod /etc/hosts with HostAliases.
解决方案不是关于 kube-dns,而是/etc/hosts.无论如何,到目前为止,以下技巧似乎有效......
The solution is not about kube-dns, but /etc/hosts. Anyway, following trick seems to work so far...
更改/etc/hosts 可能与 kubernetes 系统存在竞争条件.让它重试.
Changing /etc/hosts may has race condition with kubernetes system. Let it retry.
1) 创建一个 configMap
1) create a configMap
apiVersion: v1
kind: ConfigMap
metadata:
name: db-hosts
data:
hosts: |
10.0.0.1 db1
10.0.0.2 db2
2) 添加一个名为 ensure_hosts.sh
的脚本.
2) Add a script named ensure_hosts.sh
.
#!/bin/sh
while true
do
grep db1 /etc/hosts > /dev/null || cat /mnt/hosts.append/hosts >> /etc/hosts
sleep 5
done
不要忘记chmod a+x ensure_hosts.sh
.
3) 添加包装脚本 start.sh
您的图像
3) Add a wrapper script start.sh
your image
#!/bin/sh
$(dirname "$(realpath "$0")")/ensure_hosts.sh &
exec your-app args...
不要忘记chmod a+x start.sh
4) 使用 configmap 作为卷并运行 start.sh
4) Use the configmap as a volume and run start.sh
apiVersion: extensions/v1beta1
kind: Deployment
...
spec:
template:
...
spec:
volumes:
- name: hosts-volume
configMap:
name: db-hosts
...
containers:
command:
- ./start.sh
...
volumeMounts:
- name: hosts-volume
mountPath: /mnt/hosts.append
...
这篇关于有没有办法向 kube-dns 添加任意记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!