k3s中的Traefik是可以被代理的,需要进行以下步骤:
- 确认Traefik的Service type为LoadBalancer或NodePort
$ kubectl get svc -n kube-system traefik
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
traefik LoadBalancer 10.43.72.35 <pending> 80:31000/TCP,443:32063/TCP 2d23h
- 在Traefik的Deployment或DaemonSet中添加
--externalTrafficPolicy=Local
参数
apiVersion: apps/v1
kind: Deployment
metadata:
name: traefik
namespace: kube-system
spec:
replicas: 1
selector:
matchLabels:
app: traefik
template:
metadata:
labels:
app: traefik
spec:
containers:
- args:
- --api.insecure=true
- --accesslog=true
- --entrypoints.web.address=:80
- --providers.kubernetescrd
- --configfile=/config/traefik.toml
image: traefik:v2.4
name: traefik
ports:
- containerPort: 80
protocol: TCP
- containerPort: 8080
protocol: TCP
volumeMounts:
- mountPath: /config
name: config
# 添加参数
- "--externalTrafficPolicy=Local"
volumes:
- configMap:
defaultMode: 420
name: traefik-config
name: config
- 配置代理服务器的负载均衡规则,将请求转发到Traefik的Service IP或NodePort上。
例如,使用Nginx作为代理服务器时,可以在Nginx的配置文件中添加以下配置:
upstream traefik {
server 10.43.72.35:80; # Traefik的Service IP
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://traefik;
}
}