一個(gè)關(guān)于K8S的故事
為什么要自定義Router
- Openshift的Router節(jié)點(diǎn)其實(shí)就是一個(gè)Haproxy容器應(yīng)用泉唁,這個(gè)已經(jīng)不是什么秘密了烛卧。我們平常使用時(shí)予颤,不需要關(guān)心Haproxy的配置踢步,因?yàn)樗哪J(rèn)配置完全滿足絕大多數(shù)情況的需求绞佩。
- 但是它的默認(rèn)配置并沒(méi)有把最大的性能發(fā)揮出來(lái)悠咱,這時(shí)就需要我們對(duì)Haproxy的配置進(jìn)行自定義了蒸辆。
- 如果你需要實(shí)現(xiàn)一些新的功能,比如說(shuō)將Mysql通過(guò)Router對(duì)外提供服務(wù)等
怎樣自定義Router
- Router Pod根據(jù)haproxy-config模板創(chuàng)建haproxy.conf配置文件析既,默認(rèn)文件為
/var/lib/haproxy/conf/haproxy-config.template
- Router Haproxy鏡像啟動(dòng)時(shí)會(huì)根據(jù)環(huán)境變量
TEMPLATE_FILE
來(lái)選擇最終使用的模板文件 - 向Router Pod中放入自定義的模板文件躬贡,并將它的路徑傳給環(huán)境變量
TEMPLATE_FILE
- 模板文件中的相關(guān)函數(shù)
函數(shù) | 說(shuō)明 |
---|---|
processEndpointsForAlias(alias ServiceAliasConfig, svc ServiceUnit, action string) []Endpoint | 返回所有可用的endpoints,如果action為shuffle眼坏,那返回結(jié)果會(huì)打亂順序 |
env(variable, default …?string) string | 獲取環(huán)境變量拂玻,如果沒(méi)有環(huán)境變量,使用第二個(gè)參數(shù)的值宰译,如果第二個(gè)參數(shù)沒(méi)設(shè)置檐蚜,返回空字符串 |
matchPattern(pattern, s string) bool | 第一個(gè)參數(shù)是正則表達(dá)式,第二個(gè)參數(shù)為校驗(yàn)字符串沿侈,如果第二個(gè)參數(shù)滿足正則規(guī)則闯第,則返回True,否則返回False |
isInteger(s string) bool | 檢查字符串是否為數(shù)字格式 |
firstMatch(s string, allowedValues …?string) bool | 將一個(gè)字符串在允許的字符串列表中從左到右匹配缀拭,返回最新匹配的字符串 |
matchValues(s string, allowedValues …?string) bool | 如果字符串與給定的一組字符串中的字符串有匹配咳短,則返回True,否則返回False |
generateRouteRegexp(hostname, path string, wildcard bool) string | 生成一個(gè) 與主機(jī)名和路徑匹配的正則表達(dá)式 |
genCertificateHostName(hostname string, wildcard bool) string | 為匹配證書(shū)生成主機(jī)名 |
isTrue(s string) bool | 如果給定的字符串中有'true'則返回True,否則False |
自定義Router配置實(shí)戰(zhàn)
- 導(dǎo)出Router默認(rèn)的模板文件
$ oc rsh router-1-pod -n default cat haproxy-config.template > haproxy-config.template
- 更改haproxy-config.template,添加nbthread支持
3.10以上版本已添加了該變量
...
global
nbthread {{env "ROUTER_NBTHREAD" "1"}}
cpu-map auto:1/1-{{env "ROUTER_NBTHREAD" "1"}} 1-{{env "ROUTER_NBTHREAD" "1"}}
...
環(huán)境變量蛛淋,3.11版本主要更新以下兩個(gè)
ROUTER_THREADS
ROUTER_MAX_CONNECTIONS
默認(rèn)值是20000
- 創(chuàng)建ConfigMap
$ oc create configmap customrouter --from-file=haproxy-config.template
- 掛載ConfigMap咙好,設(shè)置環(huán)境變量
#### *3.9* 版本
$ oc volumes dc/router --add --overwrite \
--name=config-volume \
--mount-path=/var/lib/haproxy/conf/custom \
--source='{"configMap": { "name": "customrouter"}}'
$ oc set env dc/router \
TEMPLATE_FILE=/var/lib/haproxy/conf/custom/haproxy-config.template
#### *3.11* 版本
$ oc set volumes dc/router --add --overwrite \
--name=config-volume \
--mount-path=/var/lib/haproxy/conf/custom \
--source='{"configMap": { "name": "customrouter"}}'
$ oc set env dc/router \
TEMPLATE_FILE=/var/lib/haproxy/conf/custom/haproxy-config.template
- 等待Router自動(dòng)重啟,使用新的配置模板
$ oc rsh router-2-pod -n default cat haproxy.conf
測(cè)試結(jié)果
通過(guò)設(shè)置環(huán)境變量ROUTER_NBTHREAD的值褐荷,使用Jmeter對(duì)應(yīng)用進(jìn)行壓力測(cè)試
ROUTER_NBTHREAD | Avg(ms) | Min(ms) | Max(ms) | Error | TPS(/s) |
---|---|---|---|---|---|
1 | 194 | 0 | 8383 | 0 | 18322 |
2 | 133 | 0 | 15602 | 0 | 31160 |
4 | 127 | 0 | 7553 | 0 | 47049 |
8 | 138 | 0 | 14259 | 0 | 41995 |
16 | 163 | 0 | 10069 | 1(0%) | 23979 |
從中可以看出脂新,不同的nbthread對(duì)Router容量的影響非常大耗式,當(dāng)nbthread=4時(shí)枝笨,容量達(dá)到默認(rèn)的nbthread=1的2倍谐丢。
參考文章:
Openshift 自定義Router的haproxy配置
Openshift 容器平臺(tái)負(fù)載測(cè)試
Haproxy多線程
Haproxy性能優(yōu)化