一每篷、問題背景
在采用 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
- 部署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