Kubernetes - Endpoint访问外部服务

k8s访问集群外独立的服务最好的方式是采用Endpoint方式,以mysql服务为例:

  • 创建mysql-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: mysql-production
spec:
  ports:
    - port: 3306
  • 创建mysql-endpoints.yaml
kind: Endpoints
apiVersion: v1
metadata:
  name: mysql-production
  namespace: default
subsets:
  - addresses:
      - ip: 192.168.1.25
    ports:
      - port: 3306
  • 测试连接数据库
[root@k8s-master endpoint]# kubectl exec -it mysql-client-j3fr3 bash
bash-4.1# mysql -hmysql-production -u user -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 496257557
Server version: 5.5.52-MariaDB-1ubuntu0.14.04.1 (Ubuntu)

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> exit
Bye
  • 查看这个service
[root@k8s-master endpoint]# kubectl describe svc mysql-production
Name:           mysql-production
Namespace:      default
Labels:         
Annotations:        
Selector:       
Type:           ClusterIP
IP:         10.254.218.165
Port:            3306/TCP
Endpoints:      192.168.1.25:3306
Session Affinity:   None
Events:         

你可能感兴趣的:(kubernetes)