我是Kube的初学者,我试图连接2个不同的前端(具有不同的DNS名称)和我的Kube集群上的postgres数据库。
示例:前1 = test1.mysite.com前2 = test2.mysite.com
两个DNS -> 162.145.23.123
我希望我的postgres服务在同一个IP上
前排座椅1
kind: Deployment
apiVersion: apps/v1
metadata:
name: front-app
labels:
app: front-app
spec:
selector:
matchLabels:
app: front-app
replicas: 1
template:
metadata:
labels:
app: front-app
spec:
containers:
- name: front-app
image: nginx
ports:
- containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
name: front-service
spec:
selector:
app: front-app
ports:
# Default port used by the image
- port: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: demo-localhost
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: test1.mysite.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: front-service
port:
number: 80
status:
loadBalancer:
ingress:
- ip: 162.145.23.123
前部2
kind: Deployment
apiVersion: apps/v1
metadata:
name: front-app2
labels:
app: front-app2
spec:
selector:
matchLabels:
app: front-app2
replicas: 1
template:
metadata:
labels:
app: front-app2
spec:
containers:
- name: front-app2
image: nginx
ports:
- containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
name: front-service2
spec:
selector:
app: front-app2
ports:
# Default port used by the image
- port: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: demo-localhost2
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: test2.mysite.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: front-service2
port:
number: 80
status:
loadBalancer:
ingress:
- ip: 162.145.23.123
Postgres
apiVersion: v1
kind: Service
metadata:
name: hello-pg
labels:
app: hello-pg
spec:
type: LoadBalancer
ports:
- port: 5432
targetPort: 5432
protocol: TCP
selector:
app: hello-pg
---
apiVersion: v1
kind: ConfigMap
metadata:
name: db-secret-credentials
labels:
app: postgresdb
data:
POSTGRES_DB: testDB
POSTGRES_USER: testUser
POSTGRES_PASSWORD: testPassword
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-pg-deployment
labels:
app: hello-pg
spec:
replicas: 1
selector:
matchLabels:
app: hello-pg
template:
metadata:
labels:
app: hello-pg
spec:
containers:
- name: hello-pg
image: postgres
ports:
- containerPort: 5432
envFrom:
- configMapRef:
name: db-secret-credentials
谢谢你的帮助
1条答案
按热度按时间k10s72fa1#
根据你的入口注解,我可以看到你正在使用
ingress nginx
控制器。因此,如果您希望通过与应用程序相同的负载均衡器公开数据库,则需要添加具有TCP
流量路由配置的额外ConfigMap
对象,并修改现有的Service
,以提供LB
。您可以在ingress nginx documentation中找到有关此类配置的更多信息。