如何在kubernetes集群中将google cloud功能连接到cassandra数据库

时间:2019-03-28 12:50:00

标签: cassandra kubernetes google-cloud-functions

我在带有kubernetes(带有terraform)的Google云平台中部署了一个Cassandra集群。

cassandra集群运行良好,我在kubectl exec -it cassandra-0 bash上运行了一些命令,数据正在传播到所有其他节点,一切运行良好。

当我运行host cassandra时,我看到以下内容:

cassandra.default.svc.cluster.local has address 10.32.1.4
cassandra.default.svc.cluster.local has address 10.32.1.5
cassandra.default.svc.cluster.local has address 10.32.3.12

直到这里,一切都正确。

但是现在我有一个我不知道如何解决的问题。 我有一个Google Cloud Function,我想连接到该数据库并插入一些数据。

做到这一点的最佳方法是什么?我想我不应该将数据库公开给互联网。但是,当我尝试将云功能连接到cassandra.default.svc.cluster.local时,看不到服务器。

我在cassandra服务上使用cluster_ip = "None"

已更新:添加了以下信息:

我的Google Cloud Function是这样的:

exports.helloGET = function helloGET (req, res) {
    const cassandra = require('cassandra-driver');
    const client = new cassandra.Client({ contactPoints: ['cassandra.default.svc.cluster.local'], localDataCenter: 'DC1', keyspace: 'my_bd_name' });

    const query = 'SELECT * FROM User';
    client.execute(query)
        .then(result => console.log('Users: ', dump(result.rows)));

    res.send('Cassandra loaded... :' + dump(result.rows));
};

1 个答案:

答案 0 :(得分:0)

期望完整的SRV record

  

SRV记录

     

SRV记录是为属于普通或无头服务的命名端口创建的。对于每个命名端口,SRV记录的格式为pom.xml。对于常规服务,它解析为端口号和域名:org.codehaus.gmaven.gmaven-plugin。对于无头服务,这可以解析为多个答案,每个答案都支持该服务的Pod,其中包含端口号和<plugin> <groupId>org.codehaus.gmaven</groupId> <artifactId>gmaven-plugin</artifactId> <version>1.4</version> <executions> <execution> <phase>generate-resources</phase> <goals> <goal>execute</goal> </goals> <configuration> <properties> <script>docker image inspect ${project.docker.image}:${project.version} --format='{{.Size}}</script> </properties> <source> def command = project.properties.script def process = command.execute() process.waitFor() def text = process.in.text.trim() // Remove single quotes that surround number output def number = text.substring(1, text.length()-1); project.properties.dockerImageSize = number </source> </configuration> </execution> </executions> </plugin> 格式的Pod域名。

在您的情况下,您创建了一个无头服务(没有使用<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>2.4.0</version> <extensions>true</extensions> <executions> <execution> <id>bundle-manifest</id> <phase>process-classes</phase> <goals> <goal>manifest</goal> </goals> </execution> </executions> <configuration> <instructions> <Bundle-Category>Thing</Bundle-Category> <Bundle-Activator>com.company.thing.impl.Activator</Bundle-Activator> <Bundle-Vendor>${company.vendor}</Bundle-Vendor> <Bundle-ContactAddress>${company.contactAddress}</Bundle-ContactAddress> <Bundle-Copyright>${company.copyright}</Bundle-Copyright> <Bundle-LicenseType>${company.licenseType}</Bundle-LicenseType> <Bundle-Description>${company.description}</Bundle-Description> <Bundle-DockerImageSize>${dockerImageSize}</Bundle-DockerImageSize> <Import-Package> com.company.thing.api*;version="[0.0.10,1.0.0)", * </Import-Package> </instructions> </configuration> </plugin> 的群集IP),并且需要找到_my-port-name._my-port-protocol.my-svc.my-namespace.svc.cluster.local中的my-svc.my-namespace.svc.cluster.local