Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

420
Visualizações
How to use an init container to check if MySQL is ready for connections?

Friends, I am learning here and trying to implement an init container which checks if MySQL is ready for connections. I have a pod running MySQL and another pod with an app which will connect to the MysSQL pod when its ready.

No success so far. The error I am getting is the following: sh: mysql: not found. This is how I am trying:

initContainers:
- name: {{ .Values.initContainers.name }}
  image: {{ .Values.initContainers.image }}
  command:
  - "sh"
  - "-c"
  - "until mysql --host=mysql.default.svc.cluster.local --user={MYSQL_USER} 
  --password={MYSQL_PASSWORD} --execute=\"SELECT 1;\"; do echo waiting for mysql; sleep 2; done;" 

Any idea how I could make this work?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Do absolutely nothing; even delete the initContainers: you have now.

Let's say your application starts up before the database is ready. It tries to connect, fails, and exits. Kubernetes notices this, so it will try to start the application again; if it fails repeatedly, it will start waiting progressively longer between retries (you will see CrashLoopBackOff state). Eventually the cluster will have waited long enough that the database is ready, and the next retry will be successful.

Note that this is probably the same thing that will happen if the database restarts after the application is already running; "at startup time" doesn't need to be a special case.

over 4 years ago · Santiago Trujillo Relatório

0

Please try using this.

initContainers:

    - name: init-cont

      image: busybox:1.31

      command: ['sh', '-c', 'echo -e "Checking for the availability of MySQL Server deployment"; while ! nc -z mysql 3306; do sleep 1; printf "-"; done; echo -e "  >> MySQL DB Server has started";']
over 4 years ago · Santiago Trujillo Relatório

0

Your initContainer is missing the mysql client binary.

There are a few ways to solve this:

  1. Use an official mysql docker image, and use what you have as the command.
  2. Create your own docker image from a base image (e.g. Alpine), install mysql-client, and then modify your initContainer to work with Alpine.

Assuming Alpine is your base image you would have a dockerfile like this:

FROM alpine:3.14
RUN apk add mysql-client

Then your initContainer command would be:

command:
  - "/bin/ash"
  - "-c"
  - "until mysql --host=mysql.default.svc.cluster.local --user={MYSQL_USER} 
  --password={MYSQL_PASSWORD} --execute=\"SELECT 1;\"; do echo waiting for mysql; sleep 2; done;" 

My recommendation, use the official mysql docker image

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda