I have Nodejs application running in K8S ( Minikube running in hyperv) , the application connecting to DB.
This DB is running also in k8S as MySQL.
Nodejs DB Connection
var mysql = require('mysql');
var connection = mysql.createConnection({
host: "mysql" ,//work only if you get the ip of the mysql running pod ! need to exec the pod the get the ip
user:'root',
password:'root',
database:'crud'
});
connection.connect(function(error){
if(!!error) {
console.log(error);
} else {
console.log('Connected..!');
}
});
module.exports = connection;
as you can see I use "host: "mysql"
and the nodejs logs show me that the NodeJs application recognize what it "mysql" dns name becuse i have clusterIP service for it
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '10.102.140.203',
port: 3306,
fatal: true
this IP address (10.102.140.203) that I see in the log is the CluserIP address of the MySQL service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
app ClusterIP 10.111.211.9 <none> 3000/TCP 62m
appnodeport NodePort 10.99.179.176 <none> 3000:30958/TCP 62m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 71m
mysql ClusterIP 10.102.140.203 <none> 3306/TCP 62m
mysqlnode NodePort 10.106.157.92 <none> 3306:31186/TCP 57m
BUT if I enter the pod himself of the SQL and get the IP address , 172.x.x.x and enter it to the NodeJS DB connection
host: "172.x.x.x.x"
user:'root',
password:'root',
database:'crud'
the NodeJS application connected fine , but i dont want to do it in this way becuse the ip of the pod himself changes sometimes if the pod is restarting , i want to use service name or something that is not changes.
NodeJS Deployment Yaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
kompose.cmd: C:\Users\xx\Desktop\xx\kompose.exe convert
kompose.version: 1.24.0 (7c629530)
creationTimestamp: null
labels:
io.kompose.service: app
name: app
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: app
strategy: {}
template:
metadata:
annotations:
kompose.cmd: C:\Users\xx\Desktop\xx\kompose.exe convert
kompose.version: 1.24.0 (7c629530)
creationTimestamp: null
labels:
io.kompose.service: app
spec:
containers:
- image: xxx/app:6
name: employees-app
ports:
- containerPort: 3000
imagePullPolicy: Always
resources: {}
hostname: app
restartPolicy: Always
status: {}
NodeJS Service yaml
apiVersion: v1
kind: Service
metadata:
annotations:
kompose.cmd: C:\Users\xxx\Desktop\xxx\kompose.exe convert
kompose.version: 1.24.0 (7c629530)
creationTimestamp: null
labels:
io.kompose.service: app
name: app
spec:
ports:
- name: "3000"
port: 3000
targetPort: 3000
selector:
io.kompose.service: app
status:
loadBalancer: {}
MySQL deployment yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
# Use secret in real usage
- name: MYSQL_ROOT_PASSWORD
value: "root"
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- mountPath: '/var/lib/mysql'
name: mysql-persistent-storage
hostname: mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
mySQL service
apiVersion: v1
kind: Service
metadata:
annotations:
kompose.cmd: C:\Users\xxx\Desktop\xxx\kompose.exe convert
kompose.version: 1.24.0 (7c629530)
creationTimestamp: null
labels:
io.kompose.service: mysql
name: mysql
spec:
ports:
- name: "3306"
port: 3306
targetPort: 3306
selector:
io.kompose.service: mysql
status:
loadBalancer: {}
2条答案
按热度按时间6psbrbz91#
Found a Solution , just used "Endpoint" the Endpoint its the IP addr of the running pod himself , so i used the name of it :)
b1payxdu2#
It will also work if while defining your mysql-service.yaml, you use ClusterIP: none. That will expose the endpoint as endpoint rather than as VirtualIP.