I'm pushing my Phoenix app to a Kubernetes cluster for review. I use GitLab to create a service for the web server and another service for a temporary postgres pod.
What I would like to do is automate mix ecto.create
and mix ecto.migrate
. However, there is a timing issue - there's a short period of time when the postgres server is not ready yet.
I could poll the postgres service in my deployment script before creating the web application service. But is this the most practical method?
Kubernetes has something called init containers which may help you.
From the documentation, here is an example of an app container which waits for a dB container.
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox
command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
- name: init-mydb
image: busybox
command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']