Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法通过Flask app Pod连接到MongoDB Pod_Python_Flask_Kubernetes - Fatal编程技术网

Python 无法通过Flask app Pod连接到MongoDB Pod

Python 无法通过Flask app Pod连接到MongoDB Pod,python,flask,kubernetes,Python,Flask,Kubernetes,我正在尝试连接到我的MongoDB吊舱,但失败了。以前,我只是使用一个在线资源来托管我的MongoDB。现在我想用Kubernetes部署我的DB。但是,我在通过Flask应用程序连接DB pod时遇到问题,并且找不到任何使用Minikube或python的示例 这就是我尝试连接到我的pod并填充它的方式: be_host = os.getenv('MONGO-DB_SERVICE_HOST', 'mongo-db') be_port = os.getenv('MONGO-DB_SERVICE_

我正在尝试连接到我的MongoDB吊舱,但失败了。以前,我只是使用一个在线资源来托管我的MongoDB。现在我想用Kubernetes部署我的DB。但是,我在通过Flask应用程序连接DB pod时遇到问题,并且找不到任何使用Minikube或python的示例

这就是我尝试连接到我的pod并填充它的方式:

be_host = os.getenv('MONGO-DB_SERVICE_HOST', 'mongo-db')
be_port = os.getenv('MONGO-DB_SERVICE_PORT', '27017')
url = 'http://{}:{}/rba-db'.format(be_host, be_port)

app.config['MONGO_DBNAME'] = 'pymongo_db'
app.config['MONGO_URI'] = url

mongo = PyMongo(app)

@app.route('/populate_db')
def populate_db():
    patient = mongo.db.patients
    patient.insert({'id': 1, 'fname': 'Jill', 'lname': 'Smith', 'age': '50', 'weight': '63.3', 'conditions': ['Stage 2 Diabetes', 'Cancer', 'Aids']})
    patient.insert({'id': 2, 'fname': 'John', 'lname': 'Smith', 'age': '52', 'weight': '86.2', 'conditions': ['Heart Disease', 'Cancer']})
    patient.insert({'id': 3, 'fname': 'Ryan', 'lname': 'Gosling', 'age': '25', 'weight': '75', 'conditions': ['Flu']})
    patient.insert({'id': 4, 'fname': 'Sean', 'lname': 'Winnot', 'age': '21', 'weight': '82', 'conditions': ['Lupis']})
    return "Patients Added."
这是我的部署:

kind: Service
apiVersion: v1
metadata:
  name: mongo-db
spec:
  type: NodePort
  selector:
    app: mongo-db
  ports:
  - protocol: TCP
    nodePort: 31003
    port: 27017
    targetPort: 27017
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo-db
  labels:
    app: mongo-db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongo-db
  template:
    metadata:
      labels:
        app: mongo-db
    spec:
      containers:
      - name: mongo-db
        image: mongo:latest
        ports:
        - containerPort: 27017
我试过:

app.config["MONGO_URI"] = "mongodb://localhost:27017/myDatabase"
正如所建议的,但我在尝试通过/populate\u db添加到我的数据库时收到错误
pymongo.errors.OperationFailure:Authentication失败。

我也试过:

mongo = MongoClient("mongodb://mongo:27017/patients")
与后者的结果相同

编辑:

我的docker映像更新不正确时出现问题

mongo=MongoClient(“mongodb://mongo:27017/patients“”

工作正常。

url='http://{}:{}/rba db'。格式(be_主机,be_端口)
http://
是这样吗

app.config[“MONGO_URI”]=”mongodb://localhost:27017/myDatabase"

据我所知,
mongourl=”mongodb://localhost:27017/myDatabase“

我之前试过,但得到了错误
pymongo.errors.OperationFailure:身份验证失败。
这个例子只是一个例子,试试这个例子
uri=“mongodb://{0}:{1}@{2}:{3}/{4}”。format(用户、密码、主机、端口、数据库名)app.config[“MONGO\uri”]=uri