Is there a way to add arbitrary records to kube-dns?

后端 未结 5 1346
遥遥无期
遥遥无期 2020-12-28 21:27

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...

Say, there is a MongoDB replica se

5条回答
  •  囚心锁ツ
    2020-12-28 22:09

    UPDATE: 2017-07-03 Kunbernetes 1.7 now support Adding entries to Pod /etc/hosts with HostAliases.


    The solution is not about kube-dns, but /etc/hosts. Anyway, following trick seems to work so far...

    EDIT: Changing /etc/hosts may has race condition with kubernetes system. Let it retry.

    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) 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
    

    Don't forget chmod a+x ensure_hosts.sh.

    3) Add a wrapper script start.sh your image

    #!/bin/sh
    $(dirname "$(realpath "$0")")/ensure_hosts.sh &
    exec your-app args...
    

    Don't forget chmod a+x 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
            ...
    

提交回复
热议问题