openshift tcp-router


https://github.com/wangzheng422/docker_env/blob/master/redhat/ocp4/4.3/4.3.haproxy.md

openshift tcp-router

本文描述,如何通過定制化haproxy template, 通過給route添加annotation,就可以向外界開放tcp路由。本文相關腳本和文件,在scripts目錄中。

初衷和原理

經常會在openshift的poc中遇到L4負載均衡的測試,我們知道默認ocp router是haproxy做的,而且默認只支持http, https, 雖然tls/sni 也算是支持tcp的一種方式,但是這個也還是7層的。官方文檔只是簡單的說,如果有其他的需求,就定制haproxy template來滿足,不過定制說的很少,例子也不多。本文就是一個通過定制化haproxy template,來達到動態監聽route配置,並動態開放tcp端口。

定制haproxy template需要了解openshift router的一些原理要點

  • openshift router不僅僅是haproxy,它還有一個go程序,監聽了openshift的配置,並且寫入了一堆的map文件,這個文件是非常關鍵的配置haproxy template的配置文件。
  • openshift router里面的tls passthrough方式,對應到haproxy的配置里面,就是tcp的模式,我們的定制點就是在這里。
  • 定制過程集中在,屏蔽掉http/https 的edge和reencrypt部分,對於打annotation 的route,開放tls passthrough的frontend
  • route annotation 配置形式是 haproxy.router.openshift.io/external-tcp-port: "13306"
  • 當然,ocp4現在還不支持定制化route template,所以本文直接創建了一個route的deployment。
  • 現場實施的時候,注意更改router的image,每個版本的image可以去release.txt文件中找到。

既然是面向poc的,就肯定有局限

  • route annotation 定義的開放tcp端口,是手動定義,而且面向整個集群各個project開放,必然會導致tcp端口沖突。需要已有端口管理方案,這個就交給GPS吧。

以下是route的配置示例

kind: Route
apiVersion: route.openshift.io/v1
metadata:
  name: ottcache-002
  annotations:
    haproxy.router.openshift.io/wzh-router-name: "wzh-router-1"
    haproxy.router.openshift.io/external-tcp-port: "6620"
spec:
  to:
    kind: Service
    name: ottcache-002-service
  port:
    targetPort: 6620
  tls:
    termination: passthrough
    insecureEdgeTerminationPolicy: None

以下是template里面,關鍵的定制點

{{/*try to add tcp support*/}}

{{- if eq (env "WZH_ROUTER_NAME" "wzh-router-name") (index $cfg.Annotations "haproxy.router.openshift.io/wzh-router-name") }}
  {{- if (isInteger (index $cfg.Annotations "haproxy.router.openshift.io/external-tcp-port")) }} 
  frontend tcp-{{ (index $cfg.Annotations "haproxy.router.openshift.io/external-tcp-port") }}
    bind *:{{ (index $cfg.Annotations "haproxy.router.openshift.io/external-tcp-port") }}
    mode tcp
    default_backend {{genBackendNamePrefix $cfg.TLSTermination}}:{{$cfgIdx}}

  {{- end}}{{/* end haproxy.router.openshift.io */}}
{{- end}}{{/* end WZH_ROUTER_NAME */}}

{{/*end try to add tcp support*/}}

測試步驟

測試步驟不復雜,就是創建一個新的router,然后就可以去其他project創建應用,給route打annotation就可以了。

本文的例子,包含兩個應用,一個是web應用,一個是mysql,都通過tcp端口對外開放。

# tcp-router will install in the same project with openshift router
oc project openshift-ingress

# install the tcp-router and demo
oc create configmap customrouter-wzh --from-file=haproxy-config.template
oc apply -f haproxy.router.yaml

oc apply -f haproxy.demo.yaml

# test your tcp-router, replace ip with router ip, both command will success.
curl 192.168.7.18:18080

podman run -it --rm registry.redhat.ren:5443/docker.io/mysql mysql -h 192.168.7.18 -P 13306 -u user -D db -p

# if you want to delete the tcp-router and demo
oc delete -f haproxy.router.yaml
oc delete configmap customrouter-wzh

oc delete -f haproxy.demo.yaml

# oc set volume deployment/router-wzh --add --overwrite \
#     --name=config-volume \
#     --mount-path=/var/lib/haproxy/conf/custom \
#     --source='{"configMap": { "name": "customrouter-wzh"}}'

# oc set env dc/router \
#     TEMPLATE_FILE=/var/lib/haproxy/conf/custom/haproxy-config.template

參考

https://docs.openshift.com/container-platform/3.11/install_config/router/customized_haproxy_router.html#go-template-actions

https://www.haproxy.com/blog/introduction-to-haproxy-maps/

https://access.redhat.com/solutions/3495011

https://blog.zhaw.ch/icclab/openshift-custom-router-with-tcpsni-support/

以下是彎路

分析源碼,我們可以看到,openshift router還是對haproxy做了擴展的,那些map文件,都是router的擴展生成的,目的是對接endpoint,繞過service。所以我們想做tcp轉發,可以借助sni-tcp來實現tcp轉發。

分析源碼,我們可以看到,openshift router還是對haproxy做了擴展的,那些map文件,都是router的擴展生成的,目的是對接endpoint,繞過service。所以我們想做tcp轉發,可以借助sni-tcp來實現tcp轉發。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM