如何从外部访问托管在kubernetes集群上的mongodb副本集?

时间:2018-09-13 06:32:28

标签: mongodb kubernetes rancher

在Rancher中使用Amazon EKS创建的集群。

MongoDB副本集在Rancher中作为目录应用程序创建。

集群中的服务可以使用此连接字符串成功连接数据库。

mongodb://mongodb-replicaset.mongodb-replicaset.svc.cluster.local:27017/tradeit_system?replicaSet=rs

我想查看和编辑数据库中的数据。在本地数据库中,您可以通过命令mongo --port 27017轻松完成此操作。

同样,有一种方法可以连接到kubernetes上的一个。是从终端还是使用Robo 3t之类的应用程序?

编辑

复制集不显示。

kubectl get deployments --all-namespace

kubectl get pods --all-namespaces

显示它在3个容器中运行mongodb-replicaset-0,mongodb-replicaset-1,mongodb-replicaset-2。

3 个答案:

答案 0 :(得分:3)

  1. 运行kubectl get services -n <namespace>。这将列出副本集服务
  2. 执行kubectl port-forward svc/mongodb-replicaset -n mongoNamespace 27018:27017

其中

mongodb-replicaset = mongodb服务名称

mongoNamespace =名称空间

27018 =您的本地端口

作为最佳实践,您应该始终连接服务而不是Pod。由于Pod是自动重新创建/重新启动的,因此它将为您提供新的Pod名称。连接到服务可避免您重新连接和查找mongodb副本集的主容器。

答案 1 :(得分:1)

kubectl port-forward mongodb-replicaset-0 --namespace mongodb-replicaset 27017:27017

mongodb-replicaset-0-运行主要集的广告连播。

这会将流量转发到您计算机上的localhost:27017

Github discussion

Documentation on port-forward

答案 2 :(得分:0)

执行// DownloadFile AsyncTask private class DownloadFile extends AsyncTask<String, Integer, String> { ProgressDialog mProgressDialog; String filepath; @Override protected void onPreExecute() { super.onPreExecute(); // Create progress dialog mProgressDialog = new ProgressDialog(context); // Set your progress dialog Title mProgressDialog.setTitle("Downloading Updates!"); // Set your progress dialog Message mProgressDialog.setMessage("Click Install when done..."); mProgressDialog.setIndeterminate(false); mProgressDialog.setMax(100); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setCancelable(false); // Show progress dialog mProgressDialog.show(); } @Override protected String doInBackground(String... Url) { try { String app_url = Url[0]; String f_name = ""; if (app_url.contains("/")) { String temp[] = app_url.split("/"); f_name = temp[temp.length - 1]; } URL url = new URL(app_url); URLConnection connection = url.openConnection(); connection.connect(); // Detect the file length int fileLength = connection.getContentLength(); // Locate storage location filepath = Environment.getExternalStorageDirectory() .getPath() + "/" + f_name; // Download the file InputStream input = new BufferedInputStream(url.openStream()); // Save the downloaded file OutputStream output = new FileOutputStream(filepath); byte data[] = new byte[1024]; long total = 0; int count; while ((count = input.read(data)) != -1) { total += count; // Publish the progress publishProgress((int) (total * 100 / fileLength)); output.write(data, 0, count); } // Close connection output.flush(); output.close(); input.close(); } catch (Exception e) { // Error Log Log.e("Error", e.getMessage()); e.printStackTrace(); } return null; } @Override protected void onProgressUpdate(Integer... progress) { super.onProgressUpdate(progress); mProgressDialog.setProgress(progress[0]); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); mProgressDialog.dismiss(); if (filepath != null) { File file = new File(filepath); Uri mUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider",file); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(mUri, "application/vnd.android.package-archive"); startActivity(intent); } } } 后,您将看到豆荚的名称。

您可以使用以下命令连接到任何吊舱:kubectl get pods。 还要检查文档中是否有Execute a command in a container

此外,在连接到kubectl exec -it POD_NAME /bin/bash之后,您可以使用POD检查mongo在哪里。

我认为默认安装位于whereis mongo,因此在连接到群集时,您可以使用/usr/bin/mongo从喜欢的任何Pod中直接调用mongo

相关问题