The persistent storage of kubernetes is implemented through distributed storage, so that stateful services such as mysql can run and save data in kubernetes. This looks wonderful, but in actual production environments, the performance problems of disks implemented by distributed storage in IO-intensive applications such as mysql will become very prominent. Therefore, in practical applications, applications such as mysql are generally not directly managed in kubernetes, but are deployed independently using dedicated servers. Stateless applications like web will still run in kubernetes. At this time, the web server needs to connect to the database outside the management of kubernetes. There are two ways: one is to directly connect to the IP of the physical server where the database is located, and the other is to use kubernetes. The endpoints directly map the external server to a service inside kubernetes.
Example:
apiVersion: v1 kind: Service metadata: name: plat-dev spec: ports: - port: 3306 protocol: TCP targetPort: 3306 --- apiVersion: v1 kind: Endpoints metadata: name: plat-dev subsets: - addresses: - ip: "10.5.10.109" ports: - port: 3306
This example defines two resource objects, Service and Endpoints. The definition of Service does not use the tag selector, but defines an Endpoints with the same name as the Service, so that they can be automatically associated. The IP and port of the external server that needs to be connected are specified in the subsets of Endpoints.
We can check it with kubectl get svc:
[root@server-116 test]# kubectl get svc NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE plat-dev 10.254.4.76 <none> 3306/TCP 20m
We can start another example container and do the following in the container to try to connect to the external service:
[root@server-116 test]# kubectl exec -it nginx /bin/bash [root@nginx nginx]# nslookup plat-dev Server: 10.254.0.100 Address: 10.254.0.100#53 Name: plat-dev.default.svc.cluster.local Address: 10.254.4.76 [root@nginx nginx]# mysql -uxxx -pxxx -hplat-dev Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 349446 Server version: 5.6.14 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases;
Reprinted to: https://www.cnblogs.com/breezey/p/6586962.html