docker with ansible等待数据库

Vla*_*sov 8 ansible docker ansible-playbook

我尝试用ansible部署docker.我有一个docker数据库容器,而在其他容器中是我的web应用程序,我尝试链接这两个容器.问题是数据库容器没有时间自行配置,并且Web容器已经启动.我的ansible剧本看起来像:

...
- name: run mysql in docker container
  docker:
    image: "mysql:5.5"
    name: database
    env: "MYSQL_ROOT_PASSWORD=password"
    state: running

- name: run application containers
  docker:
    name: "application"
    image: "myapp"
    ports:
      - "8080:8080"
    links:
      - "database:db"
    state: running
Run Code Online (Sandbox Code Playgroud)

如何确定数据库是否启动?我尝试使用wait_for模块,但这不起作用.我不想设置超时,这对我来说不是一个好选择.

etr*_*nok 13

wait_for不适用于MySQL docker容器,因为它只检查端口是否可连接(对于Docker容器,这是正确的).但是,wait_for不检查容器内的服务是否侦听端口并将响应发送到客户端.

这就是我在ansible playbook中等待MySQL服务在Docker容器内完全运行的方式:

- name: Start MySQL container
  docker:
    name: some-name
    image: mysql:latest
    state: started
    ports:
    - "8306:3306" # it's important to expose the port for waiting requests
    env:
        MYSQL_ROOT_PASSWORD: "{{ mysql_root_password }}"

- template: mode="a+rx,o=rwx" src=telnet.sh.j2 dest=/home/ubuntu/telnet.sh

# wait while MySQL is starting
- action: shell /home/ubuntu/telnet.sh
  register: result
  until: result.stdout.find("mysql_native_password") != -1
  retries: 10
  delay: 3
Run Code Online (Sandbox Code Playgroud)

而telnet.sh.j2是

#!/bin/bash -e

telnet localhost 8306 || true
Run Code Online (Sandbox Code Playgroud)

  • 您在单独的脚本中运行telnet命令的任何原因? (3认同)

Fre*_*Pow 7

为了避免sh和我通常没有安装telnet ...

- name: Wait for database to be available
  shell: docker run --rm --link mysql:mysql mysql sh -c 'mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p{{mysql_password}} || true'
  register: result
  until: result.stderr.find("Can't connect to MySQL") == -1
  retries: 10
  delay: 3
Run Code Online (Sandbox Code Playgroud)

  • 这对我来说效果很好,我喜欢它避免使用脚本或telnet。我正在使用带有mysqladmin ping和docker exec的版本。我将`command`模块与docker exec {{container_name}} mysqladmin ping一起使用,然后直到直到`result.rc == 0`。 (2认同)

tim*_*els 7

正如etrubenok所说

wait_for 不适用于 MySQL docker 容器,因为它只检查端口是否可连接(这对于 Docker 容器来说是正确的)。但是,wait_for 不会检查容器内的服务是否侦听端口并向客户端发送响应。

使用Andy Shinn 对 FreshPow 的回答的建议,您可以等待而无需 shell 脚本或 telnet:

- name: Wait for mariadb
  command: >
    docker exec {{ container|quote }}
    mysqladmin ping -u{{ superuser|quote }} -p{{ superuser_password|quote }}
  register: result
  until: not result.rc  # or result.rc == 0 if you prefer
  retries: 20
  delay: 3
Run Code Online (Sandbox Code Playgroud)

这将mysqladmin ping ...一直运行直到成功(返回代码 0)。通常超级用户是root. 我使用 podman 而不是 docker 进行了测试,但我相信无论如何命令都是一样的。|quoteshell 转义,根据 Ansible 文档,在使用时也应该这样做command:


Kas*_*yap 1

使用wait_for模块。我不是 MySQL 专家,但我假设在某些日志文件等中会有一些端口或文件或消息的存在。您可以检查数据库是否已启动。

以下是wait_for从上面的链接复制的示例。

# wait 300 seconds for port 8000 to become open on the host, don't start checking for 10 seconds
- wait_for: port=8000 delay=10

# wait 300 seconds for port 8000 of any IP to close active connections, don't start checking for 10 seconds
- wait_for: host=0.0.0.0 port=8000 delay=10 state=drained

# wait 300 seconds for port 8000 of any IP to close active connections, ignoring connections for specified hosts
- wait_for: host=0.0.0.0 port=8000 state=drained exclude_hosts=10.2.1.2,10.2.1.3

# wait until the file /tmp/foo is present before continuing
- wait_for: path=/tmp/foo

# wait until the string "completed" is in the file /tmp/foo before continuing
- wait_for: path=/tmp/foo search_regex=completed

# wait until the lock file is removed
- wait_for: path=/var/lock/file.lock state=absent

# wait until the process is finished and pid was destroyed
- wait_for: path=/proc/3466/status state=absent

# wait 300 seconds for port 22 to become open and contain "OpenSSH", don't assume the inventory_hostname is resolvable
# and don't start checking for 10 seconds
- local_action: wait_for port=22 host="{{ ansible_ssh_host | default(inventory_hostname) }}" search_regex=OpenSSH delay=10
Run Code Online (Sandbox Code Playgroud)