【k8s】k8s ingress 代理集群外的服務(wù)

一每篷、問題背景

在采用 k8s 后,一些遺留系統(tǒng)或者因?yàn)檫w移不方便或者因?yàn)闉榱送瑫r服務(wù)于多個環(huán)境蚯瞧,而仍然以原來的方式運(yùn)行著(不受 k8s 管理)待讳。
如果想讓 k8s 內(nèi)的 pods 訪問這些遺留的服務(wù),怎么辦蛮原?

現(xiàn)在有集群外的1個微服務(wù)(多實(shí)例):

upstream upsmicroservice {
    server 192.168.26.141:12345;
    server 192.168.26.142:12345;
    server 192.168.26.143:12345;
}


location  /microservice {
        proxy_pass http://upsmicroservice;
}

如何在ingress中配置 ,訪問 https://www.example.com/microservice 接口 能訪問到對應(yīng)的后端實(shí)例另绩?

二儒陨、 k8s ingress 代理操作

如果 upsmicroservice 有多個實(shí)例花嘶,可以通過 Endpoints 資源來配置這些服務(wù)。
以下是如何配置它們蹦漠,以確保流量能分發(fā)到所有實(shí)例椭员。

1. 更新 Service 配置

service-microservice.yaml

apiVersion: v1
kind: Service
metadata:
  name: microservice
  namespace: test
spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: 123456

2. 創(chuàng)建 Endpoints

創(chuàng)建 Endpoints 資源,以便指向所有實(shí)例的 IP笛园。

endpoints-microservice.yaml

apiVersion: v1
kind: Endpoints
metadata:
  name: microservice
  namespace: test
subsets:
  - addresses:
      - ip: 192.168.26.141
      - ip: 192.168.26.142
      - ip: 192.168.26.143
    ports:
      - port: 123456

3. 為該服務(wù)創(chuàng)建Ingress 代理規(guī)則

test-ingress-nginx-microservice.yaml

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: test-ingress-nginx-outer
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
  creationTimestamp: "2023-08-24T07:56:43Z"
  generation: 55
  name: test-ingress-nginx-microservice
  namespace: test
  resourceVersion: "111101388"
  uid: 81e435c1-3f03-4303-xxxx-c56cb1d50d8a

spec:
  rules:
  - host: www.example.com
    http:
      paths:
      - path: /microservice
        pathType: Prefix
        backend:
          service:
            name: microservice
            port:
              number: 80
  1. 部署Service隘击、Endpoints和Ingress
4.1. 部署服務(wù):

kubectl apply -f service-microservice.yaml
kubectl desribe  service   microservice -n  test 

4.2. 部署端點(diǎn):

kubectl apply -f endpoints-microservice.yaml
kubectl desribe  endpoints   microservice -n  test 


4.3. 部署 Ingress:

kubectl apply -f  test-ingress-nginx-microservice.yaml
kubectl desribe  ingress   test-ingress-nginx-microservice -n  test 
$ kubectl describe  service microservice  -n  test 
Name:              microservice 
Namespace:         test
Labels:            <none>
Annotations:       <none>
Selector:          <none>
Type:              ClusterIP
IP Families:       <none>
IP:                10.96.80.213
IPs:               10.96.80.213
Port:              <unset>  80/TCP
TargetPort:        12345/TCP
Endpoints:         192.168.26.141:12345,192.168.26.142:12345,192.168.26.143:12345
Session Affinity:  None
Events:            <none>


$ kubectl describe  endpoints    microservice  -n  test 
Name:         microservice 
Namespace:    test
Labels:       <none>
Annotations:  <none>
Subsets:
  Addresses:          192.168.26.141,192.168.26.142,192.168.26.143
  NotReadyAddresses:  <none>
  Ports:
    Name     Port   Protocol
    ----     ----   --------
    <unset>  12345  TCP

Events:  <none>


$ kubectl describe ingress  test-ingress-nginx-microservice  -n test 
Name:             test-ingress-nginx-microservice 
Namespace:        test
Address:          10.96.92.163
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host                  Path  Backends
  ----                  ----  --------
  www.example.com  
                        /microservice           microservice :80 (192.168.26.141:12345,192.168.26.142:12345,192.168.26.143:12345)
                        /ai-embedding   ai-embedding:80 (192.168.26.141:11633,192.168.26.142:11633,192.168.26.143:11633)
Annotations:            kubernetes.io/ingress.class: test-ingress-nginx-outer
                        nginx.ingress.kubernetes.io/force-ssl-redirect: false
                        nginx.ingress.kubernetes.io/ssl-redirect: false
Events:                 <none>

三、 訪問測試

curl   -vvv  https://www.example.com/microservice   

看 ingress 日志研铆,請求是否打到了 microservice 后端實(shí)例埋同。

四、參考

Ingress 代理集群外服務(wù)
https://mp.weixin.qq.com/s/F9s__YGqG5Jjzb0SnWXVAg

Kubernetes使用ingress反向代理外部IP
https://zahui.fan/posts/0ad6df1b/

圖解 Kubernetes Ingress
https://www.qikqiak.com/post/visually-explained-k8s-ingress/

如何將外部服務(wù)納入到k8s集群內(nèi)
https://beloved.family/wx/%E5%A6%82%E4%BD%95%E5%B0%86%E5%A4%96%E9%83%A8%E6%9C%8D%E5%8A%A1%E7%BA%B3%E5%85%A5%E5%88%B0k8s%E9%9B%86%E7%BE%A4%E5%86%85

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末棵红,一起剝皮案震驚了整個濱河市凶赁,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌逆甜,老刑警劉巖虱肄,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異交煞,居然都是意外死亡咏窿,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進(jìn)店門素征,熙熙樓的掌柜王于貴愁眉苦臉地迎上來集嵌,“玉大人,你說我怎么就攤上這事稚茅≈交矗” “怎么了?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵亚享,是天一觀的道長咽块。 經(jīng)常有香客問我,道長欺税,這世上最難降的妖魔是什么侈沪? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮晚凿,結(jié)果婚禮上亭罪,老公的妹妹穿的比我還像新娘。我一直安慰自己歼秽,他們只是感情好应役,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般箩祥。 火紅的嫁衣襯著肌膚如雪院崇。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天袍祖,我揣著相機(jī)與錄音底瓣,去河邊找鬼。 笑死蕉陋,一個胖子當(dāng)著我的面吹牛捐凭,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播凳鬓,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼茁肠,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了村视?” 一聲冷哼從身側(cè)響起官套,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蚁孔,沒想到半個月后奶赔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡杠氢,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年站刑,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鼻百。...
    茶點(diǎn)故事閱讀 40,030評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡绞旅,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出温艇,到底是詐尸還是另有隱情因悲,我是刑警寧澤,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布勺爱,位于F島的核電站晃琳,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏琐鲁。R本人自食惡果不足惜卫旱,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望围段。 院中可真熱鬧顾翼,春花似錦、人聲如沸奈泪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至取逾,卻和暖如春耗绿,著一層夾襖步出監(jiān)牢的瞬間苹支,已是汗流浹背砾隅。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留债蜜,地道東北人晴埂。 一個月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像寻定,于是被迫代替她去往敵國和親儒洛。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評論 2 355

推薦閱讀更多精彩內(nèi)容