k8s部署Mysql连接断断续续一会连接成功,一会连接失败

问题场景:

        三台机器:

                master ip为192.168.1.100

                node1 ip为192.168.1.101

                node2 ip为192.168.1.102

        需要部署其中一台节点机上部署mysql、nacos、oracle、redis随机指定即可,但需要访问matser能访问到,所以在master上部署了一个Nodport类型的service来供以端口外部访问,端口为:30306。

问题描述:

        部署完毕,出现了连接192.168.1.100:30306连接失败,再次尝试成功,但查看数据库下表的列表的时候又出现连接失败。

        随即使用telnet 192.168.1.100 30306:

[root@master ~]# telnet 192.168.1.100 3306
Trying 192.168.1.100...
telnet: connect to address 192.168.1.100: Connection refused
telnet: Unable to connect to remote host
[root@master ~]# telnet 192.168.1.100 30306
Trying 192.168.1.100...
telnet: connect to address 192.168.1.100: Connection refused
telnet: Unable to connect to remote host
[root@master ~]# telnet 192.168.1.100 30306
Trying 192.168.1.100...
Connected to 192.168.1.100.
Escape character is '^]'.
J
8.0.27w�j"`>)uxT
                caching_sha2_password^[[A
!#08S01Got packets out of orderConnection closed by foreign host.
[root@master ~]# telnet 192.168.1.100 30306
Trying 192.168.1.100...
telnet: connect to address 192.168.1.100: Connection refused
telnet: Unable to connect to remote host
[root@master ~]# telnet 192.168.1.100 30306
Trying 192.168.1.100...
telnet: connect to address 192.168.1.100: Connection refused
telnet: Unable to connect to remote host
[root@master ~]# telnet 192.168.1.100 30306
Trying 192.168.1.100...
Connected to 192.168.1.100.
Escape character is '^]'.
J
8.0.27ICO+v;X�GcGN&dbZMTcaching_sha2_password^CConnection closed by foreign host.
[root@master ~]# 

问题解决:

        后来发现是service的endpoint节点把三个node都囊括进来了,因为其他节点还有nacos等等的pod,只需要规定pod label,在Service.spec.selector规定相关selector即可。

# 获取service列表
[root@master k8s]# kubectl get svc -n jees-ns
NAME              TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)                                                       AGE
jees-mysql-svc    NodePort   10.96.90.157             3306:30306/TCP                                                20s
jees-nacos-svc    NodePort   10.111.142.165           7848:30748/TCP,8848:30848/TCP,9848:30948/TCP,9849:30949/TCP   20s
jees-oracle-svc   NodePort   10.100.198.246           1521:30521/TCP                                                20s
jees-redis-svc    NodePort   10.103.240.127           6379:30379/TCP                                                20s

# 查看service详情,可以看到Selector下生效的selector只有key为app
# Endpoints有N > 1个
[root@master k8s]# kubectl describe svc jees-mysql-svc -n jees-ns
Name:                     jees-mysql-svc
Namespace:                jees-ns
Labels:                   app=small-shop
                          use-for=mysql
Annotations:              desc: mysql使用的service,通过域名(jees-mysql-svc.jees-ns.svc.cluster.local)形式访问
Selector:                 app=small-shop
Type:                     NodePort
IP:                       10.96.90.157
Port:                     3306-out-port  3306/TCP
TargetPort:               3306/TCP
NodePort:                 3306-out-port  30306/TCP
Endpoints:                10.244.1.97:3306,10.244.1.98:3306,10.244.2.106:3306 + 1 more...
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   

# 查看pods列表详情信息包含IP,结合上面Endpoints可以看到nacos也进去了...
[root@master k8s]# kubectl get pods -n jees-ns -o wide
NAME                                     READY   STATUS    RESTARTS   AGE    IP             NODE    NOMINATED NODE   READINESS GATES
jees-mysql-deployment-6d74966d4-dv4l2    1/1     Running   0          2m1s   10.244.1.97    node1              
jees-nacos-deployment-6c88cd7ff6-vvmqh   1/1     Running   0          2m1s   10.244.2.106   node2              
jees-oracle-deployment-6b675dc7d-6fknr   1/1     Running   0          2m1s   10.244.1.98    node1              
jees-redis-deployment-8dfdccd6-5wbdl     1/1     Running   0          2m1s   10.244.2.107   node2              

        所以查看YAML内容,确实因为不小心底部有个覆盖了:

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: small-shop
    use-for: mysql
  annotations:
    desc: mysql使用的service,通过域名(jees-mysql-svc.jees-ns.svc.cluster.local)形式访问
  name: jees-mysql-svc
  namespace: jees-ns
spec:
  selector: # -------- 此处和下面重复,覆盖了此处
    app: small-shop
    use-for: mysql
  type: NodePort
  # clusterIP: None # 将clusterIP设置为None,即可创建headliness Service
  ports:
    - name: 3306-out-port
      targetPort: 3306 # POD控制器中定义的端口
      nodePort: 30306
      port: 3306 # 集群内服务访问端口
  selector:
    app: small-shop # -------- 此处和上面重复,覆盖了上面

修改过来重新delete,再create即可。

你可能感兴趣的:(#,k8s,kubernetes,容器,云原生,k8s部署mysql断断续续)