I have frontend container running with below php code;
<?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();
?>
I have hostname name and password saved in text file as mentioned above on frontend container. I have metioned hostname as SeriveName. Please find below sample backend service, here backend service name is "userdatabase-service". Same I have mentioned in hostname.txt on frontend container.
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
But while accessing frontend service which pulls record from database gives message: Unable to connect to database! Please try again later.
I tried with Ingress Ip but still it gives same error. Can you please guide here? Regards, Vikas
This was solved in the comments by @Matthew L Daniel. The connection string was being formed with 'userdatabase-service' but without any explicit port. It worked when the port 80 was added to the connection string.