溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

理解OpenShift(1):網(wǎng)絡(luò)之 Router 和 Route

發(fā)布時間:2020-07-19 12:14:29 來源:網(wǎng)絡(luò) 閱讀:1019 作者:我是SammyLiu 欄目:云計算


** 本文基于 OpenShift 3.11,Kubernetes 1.11 進行測試 ***

 

1. OpenShift 為什么需要 Router 和 Route?

顧名思義,Router 是路由器,Route 是路由器中配置的路由。OpenShift 中的這兩個概念是為了解決從集群外部(就是從除了集群節(jié)點以外的其它地方)訪問服務(wù)的需求。不曉得為什么OpenShift 要將Kubernetes 中的 Ingress 改為 Router,我倒是覺得 Ingress 名字更貼切。

從外部通過 router 和從內(nèi)部通過 servide 訪問 pod 中的應(yīng)用兩個過程的簡單的示意圖如下:

理解OpenShift(1):網(wǎng)絡(luò)之 Router 和 Route

上圖中,某個應(yīng)用的三個pod 分別位于 node1,node2 和 node3 上。OpenShift 中有三層IP地址概念:

  • pod 自己的 IP 地址,可以類比為 OpenStack 中虛擬機的固定IP。它只有在集群內(nèi)才有意義。

  • service 的 IP 地址。Service 通常有 ClusterIP,這也是一種集群內(nèi)部的IP 地址。

  • 應(yīng)用的外部 IP 地址,可以類比為OpenStack 中的浮動IP,或者IDC IP(和浮動IP 之間是NAT 映射關(guān)系)。

因此,要從集群外部訪問 pod 中的應(yīng)用,無非兩種方式:

  • 一種是利用一個代理(proxy),把外部 IP 地址轉(zhuǎn)化為后端的 Pod IP 地址。這就是 OpenShift router/route 的思路。OpenShift 中的 router 服務(wù),是一個運行在特定節(jié)點(通常是基礎(chǔ)架構(gòu)節(jié)點)上的集群基礎(chǔ)服務(wù),由集群管理員負(fù)責(zé)創(chuàng)建和管理。它可以有多個副本(pod)。router 中可有多個 route,每個 route 能通過外部HTTP 請求的域名找出其后端的 pod 列表,并進行網(wǎng)絡(luò)包的轉(zhuǎn)發(fā)。也就是將pod 中的應(yīng)用暴露到外網(wǎng)域名,使得用戶可以外面通過域名訪問到應(yīng)用。這實際上是一種七層負(fù)載均衡器。OpenShift 默認(rèn)采用 HAProxy 來實現(xiàn),當(dāng)然也支持其它實現(xiàn),比如 F5.

  • 另一種是將服務(wù)直接暴露到集群外。這種方式具體會在『服務(wù) Service』那一篇文章中詳細(xì)解釋。

2. OpenShift 如何利用 HAProxy 實現(xiàn) router 和 route?

2.1 Router 部署

使用 ansible 采用默認(rèn)配置部署 OpenShift 集群時,在集群 Infra 節(jié)點上,會以 Host networking 方式運行一個 HAProxy 的 pod,它會在所有網(wǎng)卡的 80 和 443 端口上進行監(jiān)聽。

[root@infra-node3 cloud-user]# netstat -lntp | grep haproxy
tcp        0      0 127.0.0.1:10443         0.0.0.0:*               LISTEN      583/haproxy         
tcp        0      0 127.0.0.1:10444         0.0.0.0:*               LISTEN      583/haproxy         
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      583/haproxy         
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      583/haproxy

其中,172.0.0.1 上的 10443 和 10444 是HAproxy 自己使用的。下文會有解釋。

因此,在每個 infra 節(jié)點上,只能有一個 HAProxy pod,因為這些端口只能被占用一次。如果調(diào)度器找不到滿足要求的節(jié)點,則router 服務(wù)的調(diào)度就會失?。?/p>

0/7 nodes are available: 2 node(s) didn't have free ports for the requested pod ports, 5 node(s) didn't match node selector

OpenShift HAProxy Router 支持兩種部署方式:

  • 一種是常見的單Router 服務(wù)部署,它有一個或多個實例(pod),分布在多個節(jié)點上,負(fù)責(zé)整個集群上部署的服務(wù)的對外訪問。

  • 另一種是分片(sharding)部署。此時,會有多個 Router 服務(wù),每個Router 服務(wù)負(fù)責(zé)指定的若干project,兩者之間采用標(biāo)簽(label)進行映射。這是為了解決單個 Router 的性能不夠問題而提出的解決方案。

OpenShift 提供了 oc adm router 命令來創(chuàng)建 router 服務(wù)。

創(chuàng)建router:

[root@master1 cloud-user]# oc adm router router2 --replicas=1 --service-account=routerinfo: password for stats user admin has been set to J3YyPjlbqf--> Creating router router2 ...
    warning: serviceaccounts "router" already exists
    clusterrolebinding.authorization.openshift.io "router-router2-role" created
    deploymentconfig.apps.openshift.io "router2" created
    service "router2" created--> Success

詳細(xì)的部署方法請參見官方文檔 https://docs.openshift.com/container-platform/3.11/install_config/router/default_haproxy_router.html。

2.2 Router pod 中的 HAProxy 進程

在 Router 服務(wù)的每個 pod 之中,openshift-router 進程啟動了一個 haproy 進程:

UID        PID  PPID  C STIME TTY          TIME CMD1000000+     1     0  0 Nov21 ?        00:14:27 /usr/bin/openshift-router1000000+ 16011     1  0 12:42 ?        00:00:00 /usr/sbin/haproxy -f /var/lib/haproxy/conf/haproxy.config -p /var/lib/haproxy/run/haproxy.pid -x /var/lib/haproxy/run/haproxy.sock -sf 16004

查看 haproxy 使用的配置文件(只是部分):

-base /etc/-base /etc/-forwarded-.: /var/lib/haproxy/conf/error-page---keep--request inspect--request content accept -uri /
  http-request del- insensitive (RFC ), we need to convert the -request set-header Host % we need to redirect//var/lib/haproxy/conf/os_route_http_redirect.map) -%[base,map_reg(/var/lib/haproxy/conf/# determined by the next backend  the chain -request  inspect--request content accept  { req_ssl_hello_type  the connection is SNI and the route is a passthrough don
  #  the SNI , we also need to compare it  -insensitive mode (by converting it to lowercase) as RFC -/var/lib/haproxy/conf/os_sni_passthrough.map) -%[req.ssl_sni,lower,map_reg(/var/lib/haproxy/conf/os_tcp_be.map)] -request set-header X-Forwarded-Host %-request set-header X-Forwarded-Port %-request set-header X-Forwarded-Proto http  !-request set-header X-Forwarded-Proto https -request set-header X-Forwarded-Proto-Version h3  { ssl_fc_alpn --request add-header Forwarded =%[src];host=%[req.hdr(host)];proto=%[req.hdr(X-Forwarded-Proto)];proto-version=%[req.hdr(X-Forwarded-Proto---84nrt:jenkins:.: .: cookie 8669a19afc9f0fed6824feb9fb1cf4ac weight

為了簡單期間,上面只是配置文件的部分內(nèi)容,它主要包括三種類型:

  • 全局配置,比如最大連接數(shù) maxconn,超時時間 timeout 等;以及front部分,即前端配置,HAProxy 默認(rèn)會在 443 和 80 兩個端口上分別監(jiān)聽外部 https 和 http 請求。

  • backend,即每個服務(wù)的后端配置,里面有很多關(guān)鍵內(nèi)容,比如后端協(xié)議(mode)、負(fù)載均衡方法(balance)、后端列表(server,這里是pod,包括其IP 地址和端口)、證書等。

因此,OpenShift 的路由器功能需要能對這三部分進行管理和控制。

關(guān)于負(fù)載均衡器和 HAProxy 的詳細(xì)介紹,可以參考 Neutron 理解 (7): Neutron 是如何實現(xiàn)負(fù)載均衡器虛擬化的 這篇文章。

2.3 全局配置管理

要指定或修改 HAProxy 的全局配置,OpenShift 有提供兩種方式:

(1)第一種是使用 oc adm router 命令在創(chuàng)建 router 時候指定各種參數(shù),比如 --max-connections 用于設(shè)置最大連接數(shù)。比如:

oc adm router --max-connections=200000 --ports='81:80,444:443' router3

創(chuàng)建出來的HAProxy 的 maxconn 將是 20000,router3 這個服務(wù)對外暴露出來的端口是 81 和 444,但是 HAProxy pod 的端口依然是 80 和 443.

(2)通過設(shè)置 dc/<dc router名> 的環(huán)境變量來設(shè)置 router 的全局配置。

在官方文檔 https://docs.openshift.com/container-platform/3.4/architecture/core_concepts/routes.html#haproxy-template-router 中有完整的環(huán)境變量列表。比如運行以下命令后,

 oc set env dc/router3 ROUTER_SERVICE_HTTPS_PORT=444 ROUTER_SERVICE_HTTP_PORT=81 STATS_PORT=1937

router3 會重新部署,新部署的HAProxy 的 https 監(jiān)聽端口是 444,http 監(jiān)聽端口是 80,統(tǒng)計端口是 1937.

 2.4 OpenShift passthrough 類型的 route 與 HAProxy backend

(1)通過OpenShift Console 或者 oc 命令創(chuàng)建一條 route,它將 sit 項目的 jenkins 服務(wù)暴露到域名 sitjenkins.com.cn:

在界面上創(chuàng)建 route:

理解OpenShift(1):網(wǎng)絡(luò)之 Router 和 Route

理解OpenShift(1):網(wǎng)絡(luò)之 Router 和 Route

結(jié)果:

Name:                   sitjenkins.com.cn
Namespace:              sitLabels:                 app=jenkins-ephemeral
                        template=jenkins-ephemeral-template
Annotations:            <none>Requested Host:         sitjenkins.com.cnPath:                   <none>TLS Termination:        passthroughEndpoint Port:          web

Service:        jenkins
Weight:         100 (100%)
Endpoints:      10.128.2.15:8080, 10.131.0.10:8080

這里,service name 起了一個中介作用,把 route 和服務(wù)的端點(也就是pod)連接了起來。

(2)router 服務(wù)的兩個 pod 中的 HAProxy 進程的配置文件中多了一個backend:

# Secure backend, pass through
backend be_tcp:sit:sitjenkins.com.cn
  balance source

  hash-type consistent
  timeout check 5000ms}
  server pod:jenkins-1-bqhfj:jenkins:10.128.2.15:8080 10.128.2.15:8080 weight 256 check inter 5000ms
  server pod:jenkins-1-h3fff:jenkins:10.131.0.10:8080 10.131.0.10:8080 weight 256 check inter 5000ms

其中,這些后端 server 其實就是 pod,它們是 openshift 通過步驟(1)中的 service name 找到的。balance 是負(fù)載均衡策略,后文會解釋。

(3)文件 /var/lib/haproxy/conf/os_sni_passthrough.map 中多了一條記錄

sh-4.2$ cat /var/lib/haproxy/conf/os_sni_passthrough.map^sitjenkins\.com\.cn(:[0-9]+)?(/.*)?$ 1

(4)文件 /var/lib/haproxy/conf/os_tcp_be.map 中多了一條記錄

sh-4.2$ cat /var/lib/haproxy/conf/os_tcp_be.map^sitjenkins\.com\.cn(:[0-9]+)?(/.*)?$ be_tcp:sit:sitjenkins.com.cn

(5)HAProxy 根據(jù)上面的 map 文件為該條 route 選擇第(2)步中增加的 backend的邏輯如下

frontend public_ssl  #解釋:前端協(xié)議 https,

  bind :443  ##前端端口 443
  tcp-request  inspect-delay 5s
  tcp-request content accept if { req_ssl_hello_type 1 }

  # if the connection is SNI and the route is a passthrough don't use the termination backend, just use the tcp backend
  # for the SNI case, we also need to compare it in case-insensitive mode (by converting it to lowercase) as RFC 4343 says
  acl sni req.ssl_sni -m found ##檢查 https request 支持 sni
  acl sni_passthrough req.ssl_sni,lower,map_reg(/var/lib/haproxy/conf/os_sni_passthrough.map) -m found ##檢查通過 sni 傳來的 hostname 在 os_sni_patthrough.map 文件中
  use_backend %[req.ssl_sni,lower,map_reg(/var/lib/haproxy/conf/os_tcp_be.map)] if sni sni_passthrough ##從 oc_tcp_be.map 中根據(jù) sni hostname 獲取 backend name

  # if the route is SNI and NOT passthrough enter the termination flow
  use_backend be_sni if sni

  # non SNI requests should enter a default termination backend rather than the custom cert SNI backend since it
  # will not be able to match a cert to an SNI host
  default_backend be_no_sni

(6)HAPorxy 進程會重啟,從而應(yīng)用修改了的配置文件。

理解(5)中的腳本需要的一些背景知識:

  • SNI:TLS Server Name Indication (SNI) ,這是 TLS 網(wǎng)絡(luò)協(xié)議的一種擴展,會在 TLS 握手前由客戶端(client)告知服務(wù)器端(server)它將會連接的域名(hostname),使得服務(wù)器端可以根據(jù)該hostname 向客戶端段返回指定的證書,從而使得服務(wù)器端能夠支持多個hostname 需要的多個證書。詳情請參閱 https://en.wikipedia.org/wiki/Server_Name_Indication。

  • OpenShift passthrough route:這種 route 的 SSL 連接不會在 router 上被 TLS 終止(termination),而是router 會將 TLS 鏈接透傳到后端。下文有解釋。

  • HAProxy 對 SNI 的支持:HAProxy 會根據(jù) SNI 的信息中的 hostname 去選擇特定的 backend。詳情請參閱 https://www.haproxy.com/blog/enhanced-ssl-load-balancing-with-server-name-indication-sni-tls-extension/。

  • HAProxy ACL:詳情請參閱 https://www.haproxy.com/documentation/aloha/10-0/traffic-management/lb-layer7/acls/

從上面的藍色注釋中,我們能看到 HAProxy 進程通過 https 請求中通過 SNI 傳入的域名 sitjenkins.com.cn ,在 os_tcp_be.map 文件中獲取到了 backend 名稱 be_tcp:sit:sitjenkins.com.cn,這樣就和(2)步驟中的 backend 對應(yīng)上了。

OpenShift 的 router 使用的 HAProxy 采用基于域名的負(fù)載均衡路由方式,示例如下,具體說明請參加官方文檔。

理解OpenShift(1):網(wǎng)絡(luò)之 Router 和 Route

2.5 OpenShift edge 和 re-encrypt 類型的 route 與 HAProxy

HAProxy 前端:前端依然是在 443 端口監(jiān)聽外部 HTTPS 請求

 sni

但是,當(dāng) TLS 終止類型不是 passthrough (edge 或者 re-encrypt)時,會使用backend be_sni。

backend be_sni
  server fe_sni 127.0.0.1:10444 weight 1 send-prox

而這個后端是由本機的 127.0.0.1:10444 提供服務(wù),因此又轉(zhuǎn)到了前端 fe_sni:

frontend fe_sni
  # terminate ssl on edge
  bind 127.0.0.1:10444 ssl no-sslv3 crt /var/lib/haproxy/router/certs/default.pem crt-list /var/lib/haproxy/conf/cert_config.map accept-proxy
  mode http。。。。。。

  # map to backend
  # Search from most specific to general path (host case).
  # Note: If no match, haproxy uses the default_backend, no other
  #       use_backend directives below this will be processed.
  use_backend %[base,map_reg(/var/lib/haproxy/conf/os_edge_reencrypt_be.map)]

  default_backend openshift_default

map 映射文件:

sh-4.2$ cat /var/lib/haproxy/conf/os_edge_reencrypt_be.map^edgejenkins\.com\.cn(:[0-9]+)?(/.*)?$ be_edge_http:sit:jenkins-edge

Edge 類型 route 的 HAProxy 后端:

backend be_edge_http:sit:jenkins-edge
  mode http
  option redispatch
  option forwardfor
  balance leastconn

  timeout check 5000ms
  .....
  server pod:jenkins-1-bqhfj:jenkins:10.128.2.15:8080 10.128.2.15:8080 cookie 71c6bd03732fa7da2f1b497b1e4c7993 weight 256 check inter 5000ms
  server pod:jenkins-1-h3fff:jenkins:10.131.0.10:8080 10.131.0.10:8080 cookie fa8d7fb72a46958a7add1406e6d26cc8 weight 256 check inter 5000ms

Re-encrypt 類型 route 的 HAProxy 后端:

-

    http-request set-header X-Forwarded-Host %[req.hdr(host)]
    http-request set-header X-Forwarded-Port %[dst_port]
    http-request set-header X-Forwarded-Proto http if !{ ssl_fc }
    http-request set-header X-Forwarded-Proto https if { ssl_fc }
    http-request set-header X-Forwarded-Proto-Version h3 if { ssl_fc_alpn -i h3 }

  server pod:jenkins-1-bqhfj:jenkins:10.128.2.15:8080 10.128.2.15:8080 cookie ... weight 256 ssl verifyhost jenkins.sit.svc verify required ca-file /var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt check inter 5000ms #與后端的鏈路采用 ssl 加密,并且要檢查hostname
  server pod:jenkins-1-h3fff:jenkins:10.131.0.10:8080 10.131.0.10:8080 cookie ... weight 256 ssl verifyhost jenkins.sit.svc verify required ca-file /var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt check inter 5000ms

這里可以看出來重新使用密鑰對連接進行加密,但是不知道為何 mode 依然是 http,而不是 https。

 2.6 設(shè)置和修改 route 配置

route 配置主要有以下幾個比較重要的:

(1)SSL 終結(jié)方式。共三種:

  • edge:TLS 在 router 上被終結(jié),然后非SSL網(wǎng)絡(luò)包被轉(zhuǎn)發(fā)給后端 pod。因此需要在 router 上安裝 TLS 證書。不安裝的話,會使用 router 的默認(rèn)證書。

  • passthrough:加密網(wǎng)絡(luò)包直接被發(fā)給 pod,router 上不做TLS 終結(jié),因為不需要在 router 上配置證書或密鑰。

  • Re-encryption:是 edge 的一種變種。首先 router 上會使用一個證書做 TSL 終結(jié),然后使用另外的證書再進行加密,然后發(fā)給后端 pod。因此,整個網(wǎng)絡(luò)路徑都是加密的。

設(shè)置:

  • 可以在創(chuàng)建 route 時設(shè)置,也可以通過修改 route 的 termination 配置項來修改其 SSL 終結(jié)方式。

  • 具體請參考官方文檔 https://docs.okd.io/latest/architecture/networking/routes.html#edge-termination

(2)負(fù)載均衡策略。也有三種:

  • roundrobin:根據(jù)權(quán)重輪流使用所有后端。

  • leastconn:選擇最少連接的后端接收請求。

  • source:將源IP進行哈希,確保來自同一個源IP的請求發(fā)給同一個后端。

設(shè)置:

  • 要修改整個 router 的負(fù)載均衡策略,可使用 ROUTER_TCP_BALANCE_SCHEME 環(huán)境變量,為該 router 的所有 passthrough 類型的 route設(shè)置負(fù)載均衡策略,使用 ROUTER_LOAD_BALANCE_ALGORITHM 為其它類型的 route 設(shè)置策略。

  • 可以使用 haproxy.router.openshift.io/balance 為某個 route 設(shè)置負(fù)載均衡策略。

 舉例:

  • 設(shè)置整個 router 的環(huán)境變量:oc set env dc/router ROUTER_TCP_BALANCE_SCHEME=roundrobin.改完以后,該 router 實例會重新部署,所有 passthrough 的 route 都是 roundrobin 類型的了。默認(rèn)為 source 類型。

  • 修改某個 route 的負(fù)載均衡的策略:oc edit route aaaa.svc.cluster.local.修改完成后,HAProxy 中對應(yīng)該 route 的 backend 中的 balance 值會被修改為 leastconn。

2.7 一個 route 將流量分給多個后端服務(wù)

該功能常用于一些開發(fā)測試流程,比如做A/B 測試。

在下面的配置中,有一個應(yīng)用三個版本的部署,前端一個 route,各服務(wù)使用不同的權(quán)重。

理解OpenShift(1):網(wǎng)絡(luò)之 Router 和 Route

下面是 HAProxy 配置文件中的 backend 配置,采用 roundrobin 負(fù)載均衡模式:

理解OpenShift(1):網(wǎng)絡(luò)之 Router 和 Route

3. OpenShift router 服務(wù)如何實現(xiàn)高可用?

OpenShift router 服務(wù)支持兩種高可用模式。

3.1 單 router 服務(wù)多副本,并利用和DNS/LB 實現(xiàn)高可用

這種模式只部署一個 router 服務(wù),它支持集群的所有對外暴露的服務(wù)。要實現(xiàn)HA,需要設(shè)置副本數(shù)(replicas)大于1,使得會在超過一臺服務(wù)器上創(chuàng)建pod,然后再通過DNS輪詢或者四層負(fù)載均衡。 

理解OpenShift(1):網(wǎng)絡(luò)之 Router 和 Route

因為 router/pod 中的 HAProxy 要實現(xiàn)本地配置文件,因此實際上它們是有狀態(tài)容器。OpenShift 采用 etcd 作為配置的統(tǒng)一存儲,openshift-router 進程應(yīng)該是采取某種機制(被通知或定時拉?。?etcd 中獲取 router 和 route 的配置,然后再修改本地的配置文件,再重啟 HAPorxy 進程來應(yīng)用新修改了的配置文件。 要深入了解這里面的工作原理,可以去看源代碼。

3.2 多 router 服務(wù)通過分片(sharding)實現(xiàn)高可用

這種模式下,管理員需要創(chuàng)建和部署多個 router 服務(wù),每個router 服務(wù)支持一個或幾個 project/namespace。router 和 project/namespace 之間的映射使用標(biāo)簽(label)來實現(xiàn)。具體的配置請參考官網(wǎng) https://docs.openshift.com/container-platform/3.11/install_config/router/default_haproxy_router.html。實際上,和一些產(chǎn)品(比如mysql,memedcache)的分片功能類似,該功能更多地是為了解決性能問題,而無法完全解決高可用問題。

理解OpenShift(1):網(wǎng)絡(luò)之 Router 和 Route

4. 常見問題如何排查?

從上面的分析可以看出,要使得 router 和 route 都正常工作,至少要確保以下幾個環(huán)節(jié)都是沒問題的:

  1. 客戶端使用 route 中配置的域名和端口來訪問服務(wù)。

  2. DNS 能將域名解析到目標(biāo) router 所在的服務(wù)器(在使用分片配置時比較復(fù)雜,尤其需要注意)。

  3. 如有采用另外的四層負(fù)載均衡器的話,它得配置正確、工作正常。

  4. HAProxy 能通過域名匹配到正確的backend。

  5. router 和 route 的配置被正確地反映到了 HAProxy 的配置文件中了。

  6. HAProxy 進程重啟了,從而讀取了新修改的配置文件。

  7. 后端 pod 列表正確,并且至少有一個 pod 正常工作。

如果您看到如下的錯誤頁面,則說明上面的第3到7點至少有一處不能正常功能。此時,進行有針對性的排查即可。

理解OpenShift(1):網(wǎng)絡(luò)之 Router 和 Route

 

感謝您的閱讀,歡迎關(guān)注我的微信公眾號:

理解OpenShift(1):網(wǎng)絡(luò)之 Router 和 Route


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI