Init Container for probing dependant services in Kubernetes
In certain use cases you would want to delay starting up your deployment by checking if dependent services are up either through http or tcp probing. This helps your services to boot up in the correct order, for reference check out this example. Particular use cases include service discovery and fetching startup configs for spring boot applications.
- HTTP probe
- TCP Probe
- DNS Probe
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
namespace: default
labels:
app: frontend
spec:
replicas: 1
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
# Add your init container here
initContainers:
- name: init-c
image: jdube/init-c
args: ['http', '-u', "http://backend.default"]
containers:
- name: frontend
image: jdube/frontend
ports:
- containerPort: 8000docker run --rm jdube/init-c http -u https://www.google.comTo use the DNS probe, specify dns as the probe type and use the -h or --hostname argument to provide the hostname to probe.
initContainers:
- name: init-c
image: jdube/init-c
args: ['dns', '-h', "google.com"]docker run --rm jdube/init-c dns -h google.comTo use the TCP probe, specify tcp as the probe type and use the -i or --ip argument to provide the IP address and -p or --port argument to provide the port to probe.
initContainers:
- name: init-c
image: jdube/init-c
args: ['tcp', '-i', "database.default", '-p', "5432"]initContainers:
- name: init-c
image: jdube/init-c
args: ['tcp', '-i', "redis.default", '-p', "6379"]initContainers:
- name: init-c
image: jdube/init-c
args: ['tcp', '-i', "mysql.default", '-p', "3306"]initContainers:
- name: init-c
image: jdube/init-c
args: ['tcp', '-i', "mongodb.default", '-p', "27017"]initContainers:
- name: init-c
image: jdube/init-c
args: ['tcp', '-i', "backend-service.default", '-p', "8080"]You can specify a custom timeout using the -t or --timeout argument:
initContainers:
- name: init-c
image: jdube/init-c
args: ['tcp', '-i', "database.default", '-p', "5432", '-t', "30"]# Test local service
docker run --rm jdube/init-c tcp -i 127.0.0.1 -p 80
# Test database connection
docker run --rm jdube/init-c tcp -i 127.0.0.1 -p 5432
# Test with custom timeout (30 seconds)
docker run --rm jdube/init-c tcp -i 127.0.0.1 -p 3306 -t 30
# Test external service
docker run --rm jdube/init-c tcp -i google.com -p 80Here's a complete example showing how to use TCP probes in a real deployment scenario:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-application
namespace: default
labels:
app: web-application
spec:
replicas: 3
selector:
matchLabels:
app: web-application
template:
metadata:
labels:
app: web-application
spec:
# Wait for database and cache to be ready
initContainers:
- name: wait-for-db
image: jdube/init-c
args: ['tcp', '-i', "postgres.default", '-p', "5432", '-t', "60"]
- name: wait-for-cache
image: jdube/init-c
args: ['tcp', '-i', "redis.default", '-p', "6379", '-t', "30"]
containers:
- name: web-app
image: myapp/web-application:latest
ports:
- containerPort: 8080
env:
- name: DATABASE_URL
value: "postgres://postgres.default:5432/myapp"
- name: REDIS_URL
value: "redis://redis.default:6379"Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.