ZooKeeper客户端与服务端建立的是TCP长连接,显然是基于TCP协议,因此想要使用Nginx代理ZooKeeper集群,需要Nginx基于TCP协议来实现。本文修改ingress-nginx设置tcp/udp转发,转发tcp请求到zookeeper。满足开发本地调试的需求。
更改ingress-nginx的deployment启动参数,开启tcp的支持
添加--tcp-services-configmap和--udp-services-configmap参数,开启tcp的支持
kubectl edit pod ingress-ingress-nginx-controller-hj8pq -n kube-system
containers:
- args:
- /nginx-ingress-controller
- --tcp-services-configmap=kube-system/ingress-ingress-nginx-tcp
修改ingress-nginx的service,声明tcp和udp用的端口号
kubectl edit svc ingress-ingress-nginx-controller -n kube-system
- name: proxy-zookeeper-2181
nodePort: 40303
port: 2181
protocol: TCP
targetPort: 2181
定义configmap
格式为
<ingress-controller-svc-port>:"<namespace>/<service-name>:<port>"
下面配置的data第一行表示将default命名空间下的zookeeper-client服务的2181端口映射到ingress-controller service的2181端口,即可通过ingress-controller的service ip(下图的External-IP)加2181端口访问到zookeeper-client服务
apiVersion: v1
data:
"2181": default/zookeeper-client:2181
kind: ConfigMap
metadata:
labels:
k8s-app: ingress-ingress-nginx-tcp
qcloud-app: ingress-ingress-nginx-tcp
name: ingress-ingress-nginx-tcp
namespace: kube-system
selfLink: /api/v1/namespaces/kube-system/configmaps/ingress-ingress-nginx-tcp