我有运行下面的php代码的前端容器;
<?php
$hn=file_get_contents('/var/secrets/hostname.txt');
$hn=str_replace("\n", "", $hn);
$pass=file_get_contents('/var/secrets/password.txt');
$pass=str_replace("\n", "", $pass);
$cname=$_POST['name'];
$email=$_POST['email'];
$hostname=$hn;
$username='root';
$password=$pass;
$dbname='test';
$usertable='testuser';
$con=mysqli_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
mysqli_select_db($con,$dbname);
$query = "select * from testuser";
$result = $con->query($query) or die("Not Updated!");
while($row = $result->fetch_assoc()) {
echo "<tr><td> " . $row["name"] . "<td> " . $row["email"] . "</tr> ";
}
$con->close();
?>
我在前端容器中将主机名和密码保存在文本文件中,如上所述。我已将主机名设置为SeriveName 。请在下面的示例后端服务中找到,此处的后端服务名称为“ userdatabase-service”。我在前端容器上的hostname.txt中提到过。
apiVersion: v1
kind: Service
metadata:
creationTimestamp: 2018-11-13T12:01:25Z
labels:
app: userdatabase
name: userdatabase-service
namespace: default
resourceVersion: "32448"
selfLink: /api/v1/namespaces/default/services/userdatabase-service
uid: d85cf471-e73b-11e8-8506-42010aa60fca
spec:
clusterIP: 10.7.255.80
externalTrafficPolicy: Cluster
ports:
- nodePort: 30198
port: 80
protocol: TCP
targetPort: 3306
selector:
app: userdatabase
sessionAffinity: None
type: LoadBalancer
status:
loadBalancer:
ingress:
- ip: 35.228.105.176
但是在访问从数据库中提取记录的前端服务时,出现消息:无法连接到数据库!请稍后再试。
我尝试使用Ingress Ip,但仍然给出相同的错误。 你能在这里指导吗? 问候, 维卡斯
答案 0 :(得分:1)
@ Matthew L Daniel在注释中解决了此问题。连接字符串是由“ userdatabase-service”形成的,但没有任何显式端口。当端口80添加到连接字符串时,它起作用。