我有一个裸机集群,有几个nodeport部署的服务(http和https)。我想从一个单一的url,如myservices.local和(sub)paths访问它们。
配置可以是下面这样的(伪代码)。
/app1
http://10.100.22.55:30322
http://10.100.22.56:30322
# browser access: myservices.local/app1
/app2
https://10.100.22.55:31432
https://10.100.22.56:31432
# browser access: myservices.local/app2
/...
我尝试了一些东西与haproxy和nginx,但没有什么真正的工作(对于没有经验的在这个webserverlb的东西有点混乱的语法配置风格在我看来)。
对于这样的情况,最简单的解决方案是什么?
最简单、最常用的方法是使用NGINX Ingress。NGINX的Ingress是围绕着 Kubernetes Ingress资源,使用 配置图 来存储NGINX配置。
在 文件 我们可以阅读。
侵入 将HTTP和HTTPS路由从集群外部暴露给 服务 集群内。流量路由由定义在 Ingress 资源上的规则控制。
internet | [ Ingress ] --|-----|-- [ Services ]
Ingress 可被配置为向服务提供外部可访问的 URL、负载平衡流量、终止 SSL TLS 以及提供基于名称的虚拟主机。一个 入口控制器 负责完成Ingress,通常使用负载均衡器,尽管它也可能配置边缘路由器或额外的前端来帮助处理流量。
Ingress不会暴露任意端口或协议。将HTTP和HTTPS以外的服务暴露到互联网上,通常使用类型如下的服务 Service.Type=NodePort 或 Service.Type=LoadBalancer.
这正是你想要实现的。
你需要做的第一件事就是在你的集群中安装NGINX Ingress Controller。你可以按照官方的 安装指南.
一个入口总是会指向一个服务。所以你需要有一个部署、一个服务和一个NGINX Ingress。
下面是一个与你的例子类似的应用程序的例子。
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: app1
name: app1
spec:
replicas: 1
selector:
matchLabels:
app: app1
strategy:
type: Recreate
template:
metadata:
labels:
app: app1
spec:
containers:
- name: app1
image: nginx
imagePullPolicy: Always
ports:
- containerPort: 5000
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: app2
name: app2
spec:
replicas: 1
selector:
matchLabels:
app: app2
strategy:
type: Recreate
template:
metadata:
labels:
app: app2
spec:
containers:
- name: app2
image: nginx
imagePullPolicy: Always
ports:
- containerPort: 5001
---
apiVersion: v1
kind: Service
metadata:
name: app1
labels:
app: app1
spec:
type: ClusterIP
ports:
- port: 5000
protocol: TCP
targetPort: 5000
selector:
app: app1
---
apiVersion: v1
kind: Service
metadata:
name: app2
labels:
app: app2
spec:
type: ClusterIP
ports:
- port: 5001
protocol: TCP
targetPort: 5001
selector:
app: app2
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress #ingress resource
metadata:
name: myservices
labels:
app: myservices
spec:
rules:
- host: myservices.local #only match connections to myservices.local.
http:
paths:
- path: /app1
backend:
serviceName: app1
servicePort: 5000
- path: /app2
backend:
serviceName: app2
servicePort: 5001