1. The problem I’m having:
I’m trying to build a web application with front-end and back-end separation using k8s, and I am planning to use Caddy as its reverse proxy.
The back-end Flask application has been deployed with backups using a k8s deployment and exposed by a k8s service.
I use wireshark to capture the traffic. From my observation, Caddy uses a long-lived connection to handle all requests to the back-end, regardless of who initiates the request. In this way, all requests will be routed via the long-lived connection to a single pod, and load balancing will not be performed. I think Caddy should initiate a connection to the back-end for each connection from the client, so that Kubernetes can perform load balancing.
I’m new in caddy, and I’m wondering if there is any configuration can meet my requirement or if there’s an issue with my current configuration
2. Error messages and/or full log output:
no error message here
3. Caddy version:
i use docker image caddy:alpine
/srv # caddy version
v2.6.4 h1:2hwYqiRwk1tf3VruhMpLcYTg+11fCdr8S3jhNAdnPy8=
4. How I installed and ran Caddy:
a. System environment:
ubuntu22.04 vps
using kind to deploy k8s
kind version 0.17.0
b. Command:
Dockerfile
FROM caddy:alpine
COPY ./Caddyfile /etc/caddy/Caddyfile
RUN echo "hello" > /srv/index.html
c. Service/unit/compose file:
caddy-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: caddy
template:
metadata:
labels:
app: caddy
spec:
containers:
- name: caddy
image: docker.io/library/front:v1
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
flask-app-service.yaml
apiVersion: v1
kind: Service
metadata:
name: flaskapp
spec:
selector:
app: flask-app
ports:
- name: flask-port
protocol: TCP
port: 3000
targetPort: 3000
d. My complete Caddy config:
:80 {
root * /srv
try_files {path} {path}/ /index.html
file_server
handle_path /api/* {
reverse_proxy flaskapp:3000
}
}
e.What I already tried
I changed Caddyfile to this
handle_path /api/* {
reverse_proxy flaskapp:3000 {
transport http {
keepalive off
}
}
}
In this case, each request becomes a new connection, even if the request header Connection: keep-alive is added.
What I need is for all long-lived connections to be treated as such, and for each connection to be distinguished so that load balancing can take effect.