有没有一种方法可以向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...
说,在Kubernetes集群之外但在网络中有一个MongoDB副本集.复制集的所有成员的ip地址已由应用服务器和数据库服务器中的/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容器访问那些mongo db服务器. 但是,kubernetes似乎不允许在pods/容器中的/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
推荐答案
更新:2017年7月3日,Kunbernetes 1.7现在支持
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屋!