What I have:

Two servers running Wheezy/OpenVZ with

  • One MySQL container on each host master/master replicated (mysql1/mysql2)
  • Replicated DNS on each host (dns1/dns2)
  • different web-containers on each host but regulary backuped to the other.

What I want:

Each container should use the "local" MySQL-Server (the one which runs on the same hardware-node). I'd like to be able to move the web-containers between the to hosts. Each container should choose the MySQL-Server (semi) automatically. This scheme should continue working if one host is down.

What I tried:

Currently I'm keeping track on which container should run on which host by DNS entries which are queries by scripts e.g. for questions like: "Which container should be backuped on/to which host."

For choosing the right MySQL server I have one extra entry like "mysql.container_abc" which resolves to either mysql1/mysql2. So in the applications in the container I can use "mysql.container_abc" for e.g. mysql_connect and if I want to move the container around I just need to change the dns.

Now I notices one problem with this approach: Every mysql_connect generates one DNS query because the dns is not cached and this slows the request down unnecessarily.

What I would like better:

Some way of passing the information on which host we are running to the container and using it directly instead of using DNS. E.g. some way of setting a custom /etc/hosts entry in the container. Or any other great idea. Doesn't have to include DNS but shouldn't require to much special "magic" inside the container. Perhaps it is possible to create /etc/hosts on container-startup from the outside?


You should read a bit about the vzctl command, you can set /etc/hosts, or even give it a custom hostname, at startup, before startup, etc.

It's all up to you what or how you set it, and it is scriptable.

  • Yes. I know vzctl. (How can I not). But I don't see anything which lets me e.g. add an entry to /etc/hosts nor an option to run scripts on startup. (Am I holding the manual wrong?) – Scheintod Oct 23 '13 at 11:52
  • So, you've never used vzctl to change values in a running container? – NickW Oct 23 '13 at 12:58
  • for example, 'vzctl exec abc /bin/echo con1 >> /etc/hosts' (where abc is your container name, could be numbers, etc etc) – NickW Oct 23 '13 at 13:01
  • Yes. Thanks. I know this. But this is not going to do any configuration automatically but needs to be executed manually every time I start the container. (By the way: This can be done easily without vzctl exec. Just echo abc >> /vz/private/1234/etc/hosts. But done automatically (how?) this will still not work because it will append abc every time to the end of the file.) – Scheintod Oct 24 '13 at 18:49
  • It was first only an example :) Second, it's been a while since I've set up openvz containers. Why not wrap your vzctl start command in a script, you can call what you want at that time. Pass the vz number to the script, use it in a little routine which sleeps for 20 seconds, then adds a custom line to /etc/hosts. – NickW Oct 25 '13 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.