BS问题:k8s部署 mysql navicat 连接失败

服务器版本:

 k8s版本:

docker 版本: 

 

MySQL yaml文件:

apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
  labels:
    name: mysql
spec:
  replicas: 1
  selector:
    name: mysql
  template:
    metadata:
      labels:
        name: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:8.0
        securityContext:
          runAsUser: 1000 
          allowPrivilegeEscalation: false
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: mysql-data
          mountPath: /var/lib/mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "root"
        - name: MYSQL_DATABASE
          value: "nacos_devtest"
        - name: MYSQL_USER
          value: "nacos"
        - name: MYSQL_PASSWORD
          value: "nacos"
      volumes:
      - name: mysql-data
        nfs:
          server: 192.168.3.51
          path: /data/mysql
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    name: mysql
spec:
  ports:
  - port: 3306
    targetPort: 3306
  selector:
    name: mysql

部署 成功了 

BS问题:k8s部署 mysql navicat 连接失败_第1张图片

 查看服务

 查看 ingress

BS问题:k8s部署 mysql navicat 连接失败_第2张图片

 查看本地 hosts

BS问题:k8s部署 mysql navicat 连接失败_第3张图片

 这个地址是ingressController 部署的地址

Navicat 连接 

经过一番查找资料

MySQL的 service 采用 NodePort类型 

apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
  labels:
    name: mysql
spec:
  replicas: 1
  selector:
    name: mysql
  template:
    metadata:
      labels:
        name: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:8.0
        securityContext:
          runAsUser: 1000 
          allowPrivilegeEscalation: false
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: mysql-data
          mountPath: /var/lib/mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "root"
        - name: MYSQL_DATABASE
          value: "nacos_devtest"
        - name: MYSQL_USER
          value: "nacos"
        - name: MYSQL_PASSWORD
          value: "nacos"
      volumes:
      - name: mysql-data
        nfs:
          server: 192.168.3.51
          path: /data/mysql
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    name: mysql
spec:
  ports:
  - port: 3306
    targetPort: 3306
    nodePort: 30301
  selector:
    name: mysql
  type: NodePort

spec:
  ports:
  - port: 3306
    targetPort: 3306
    nodePort: 30301  #映射的端口
  selector:
    name: mysql 
  type: NodePort  #服务类型

BS问题:k8s部署 mysql navicat 连接失败_第4张图片

 是映射到w2这个ip的端口 30301 ,w2如果开启了防火墙,需要打开端口,我这里直接关闭了防火墙,直接能用Navicat 连接

 BS问题:k8s部署 mysql navicat 连接失败_第5张图片

但是如果重启了mysql服务,MySQL服务跑到别的节点上去了,navicat 就需要重新换地址了

你可能感兴趣的:(mysql,kubernetes,数据库)