简体   繁体   English

如何在Kubernetes上访问MySQL即服务?

[英]How do I access MySQL as a Service on Kubernetes?

I have the following deployment... 我有以下部署...

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-data-disk
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:5.7
          ports:
            - containerPort: 3306
          volumeMounts:
            - mountPath: "/var/lib/mysql"
              subPath: "mysql"
              name: mysql-data
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-secrets
                  key: ROOT_PASSWORD
      volumes:
        - name: mysql-data
          persistentVolumeClaim:
            claimName: mysql-data-disk

This works great I can access the db like this... 这很棒,我可以像这样访问数据库...

kubectl exec -it mysql-deployment-<POD-ID> -- /bin/bash

Then I run... 然后我跑...

mysql -u root -h localhost -p

And I can log into it. 而且我可以登录。 However, when I try to access it as a service by using the following yaml... 但是,当我尝试使用以下yaml将其作为服务访问时...

---
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  selector:
    app: mysql
  ports:
    - protocol: TCP
      port: 3306
      targetPort: 3306

I can see it by running this kubectl describe service mysql-service I get... 我可以通过运行此kubectl describe service mysql-service来查看它...

Name:              mysql-service
Namespace:         default
Labels:            <none>
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"mysql-service","namespace":"default"},"spec":{"ports":[{"port":33...
Selector:          app=mysql
Type:              ClusterIP
IP:                10.101.1.232
Port:              <unset>  3306/TCP
TargetPort:        3306/TCP
Endpoints:         172.17.0.4:3306
Session Affinity:  None
Events:            <none>

and I get the ip by running kubectl cluster-info 我通过运行kubectl cluster-info获得IP

#kubectl cluster-info
Kubernetes master is running at https://192.168.99.100:8443
KubeDNS is running at https://192.168.99.100:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

but when I try to connect using Oracle SQL Developer like this... 但是当我尝试使用Oracle SQL Developer这样连接时...

在此处输入图片说明

It says it cannot connect. 它说它无法连接。

在此处输入图片说明

How do I connect to the MySQL running on K8s? 如何连接到在K8s上运行的MySQL?

Service type ClusterIP will not be accessible outside of Pod network. 在Pod网络外部将无法访问服务类型ClusterIP If you don't have LoadBalancer option, then you have to use either Service type NodePort or kubectl port-forward 如果没有LoadBalancer选项,则必须使用服务类型NodePortkubectl port-forward

  1. You need your mysql service to be of Type NodePort instead of ClusterIP to access it outside Kubernetes. 您需要mysql服务的类型为NodePort而不是ClusterIP才能在Kubernetes之外访问它。

  2. Use the Node Port in your client config 在客户端配置中使用节点端口

Example Service: 服务示例:

apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  type: NodePort
  selector:
    app: mysql
  ports:
    - protocol: TCP
      port: 3306
      nodePort: 30036
      targetPort: 3306

So then you can use the port: 30036 in your client. 因此,您可以在客户端中使用端口:30036。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM