So the best way I believe this can be done is via an initContainer
So let me use a contrived example. I want to start any app named some-app-that-uses-redis
which uses alpine
in my example before redis starts.
I would use a pod with an initContainer
to do a little check to see if redis is up and running:
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: some-app-that-uses-redis
name: some-app-that-uses-redis
spec:
initContainers:
- name: wait-for-redis
image: goodsmileduck/redis-cli
imagePullPolicy: IfNotPresent
args: [ 'sh', '-c', 'until redis-cli -h redis-service.default.svc.cluster.local -p 6379 get hello; do echo "Sleeping a bit"; sleep 1; done; echo "ready!"; ' ]
containers:
- args:
- sleep
- "1000"
image: alpine
name: some-app-that-uses-redis
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
The meat of the check is the following bit, which is just a for loop that does a redis query to the service where redis should work.
initContainers:
- name: wait-for-redis
image: goodsmileduck/redis-cli
imagePullPolicy: IfNotPresent
args: [ 'sh', '-c', 'until redis-cli -h redis-service.default.svc.cluster.local -p 6379 get hello; do echo "Sleeping a bit"; sleep 1; done; echo "ready!"; ' ]
Then I'd make sure redis is part of a service so that this test will always be true:
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: redis
name: redis
spec:
replicas: 1
selector:
matchLabels:
app: redis
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: redis
spec:
containers:
- image: redis:5.0.9
name: redis
resources: {}
status: {}
---
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: redis
name: redis-service
spec:
ports:
- port: 6379
protocol: TCP
targetPort: 6379
selector:
app: redis
type: NodePort
status:
loadBalancer: {}
If I understand things correctly, pods should only speak to other pods by means of services anyway.