我有一个Kubernetes集群,我想从Kubernetes中的容器连接到外部世界
例如,我在远程服务器上有一个数据库,其IP地址是10.1.1.111,可以从我的笔记本电脑连接到数据库。
现在,我将应用程序部署在笔记本电脑上的Kubernetes集群中,简化后的结构如下:
------------------------------------------------------
| laptop |
| ---------------- (Kubernetes) -------------- |
| | | |
| | ------ deployment ------ | |
| | | ------- pod -------- | | | ---(remote server)---
| | | | -- contaniner -- | | | | | (http://10.1.1.111) |
| | | | | | | | | | | |
user ----- ingress --- | | App | | |------------------- Database |
| | | | |________________| | | | | | |
| | | ____________________ | | | |_____________________|
| | ________________________ | |
| |____________________________________________| |
_______________________________________________________
如果从笔记本电脑上,我可以连接到远程服务器,
------------------------------------------------------
| laptop * ----------------------------------------------------------
| ---------------- (kubernete) ---------------- | |
| | | | | connect ok !
| | ------ deployment ------ | | |
| | | ------- pod -------- | | | ---(remote server)---
| | | | -- contaniner -- | | | | | (http://10.1.1.111) |
| | | | | | | | | | | |
user ----- ingress --- | | App | | |------------------- Database |
| | | | |________________| | | | | | |
| | | ____________________ | | | |_____________________|
| | ________________________ | |
| |____________________________________________| |
_______________________________________________________
如果我使用eval $(minikube docker-env)
进入Kubernetes集群并执行curl
命令,它可以连接到远程服务器:
------------------------------------------------------
| laptop |
| ---------------- (Kubernetes) -------------- | connect ok !
| | * ----------------
| | ------ deployment ------ | | |
| | | ------- pod -------- | | | ---(remote server)---
| | | | -- contaniner -- | | | | | (http://10.1.1.111) |
| | | | | | | | | | | |
user ----- ingress --- | | App | | |------------------- Database |
| | | | |________________| | | | | | |
| | | ____________________ | | | |_____________________|
| | ________________________ | |
| |____________________________________________| |
_______________________________________________________
然后我使用docker exec -it (docker id) /bin/bash
然后执行curl
,它无法连接到远程服务器:
------------------------------------------------------
| laptop |
| ---------------- (Kubernetes) --------------- |
| | | | cannot connect !
| | ------ deployment ------ | |
| | | ------- pod -------- | | | ---(remote server)---
| | | | -- contaniner -- | | | | | (http://10.1.1.111) |
| | | | | *----------------------> | |
user ----- ingress --- | | App | | |------------------- Database |
| | | | |________________| | | | | | |
| | | ____________________ | | | |_____________________|
| | ________________________ | |
| |____________________________________________| |
_______________________________________________________
以下是我对Kubernetes的配置:
入口服务:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-service
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- http:
paths:
- path: /?(.*)
backend:
serviceName: qs-ts-cluster-ip-service
servicePort: 8092
ip服务:
apiVersion: v1
kind: Service
metadata:
name: qs-ts-cluster-ip-service
spec:
type: ClusterIP
selector:
component: qs-service-ts
ports:
- port: 8092
targetPort: 8092
部署:
apiVersion: apps/v1
kind: Deployment
metadata:
name: qs-service-ts-deployment
spec:
replicas: 1
selector:
matchLabels:
component: qs-service-ts
template:
metadata:
labels:
component: qs-service-ts
spec:
containers:
- name: qs-service-ts
image: app/image
command: ["sbt"]
args: ["runMain com.app.http.appService"]
ports:
- containerPort: 8092
imagePullSecrets:
- name: supersecret
有什么好的方法可以从Kubernetes中的容器连接到远程数据库吗?