如何连接Kubernetes集群内的mysql?

问题描述 投票:0回答:1

我有一个 java 应用程序,需要连接到 Kubernetes 集群内运行的 MySQL 实例。 MySQL 实例运行良好,我可以从 shell 登录到容器。 配置如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:latest
          env:
            # Use secret in real usage
            - name: MYSQL_ROOT_PASSWORD
              value: password
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: mysql-pv-claim
---

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
    
---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
      
---

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

我的 java 应用程序是使用 Docker 构建的,我可以在本地主机上运行它;我想知道运行应用程序容器时如何传入

db.url

申请的YML如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: directory-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: directory
  template:
    metadata:
      labels:
        app: directory
    spec:
      containers:
          - name: directory-container
            image: directory:latest
            imagePullPolicy: Always
            ports:
              - containerPort: 8080
            env:
              - name: SPRING_DATASOURCE_URL
                value: jdbc:mysql://${DB_HOST}/temp?useSSL=false
                
---

apiVersion: v1
kind: Service
metadata:
  name: directory-service
spec:
  selector:
    app: directory
  ports:
      - protocol: TCP
        port: 8080
        targetPort: 8080
  type: LoadBalancer
                
---


apiVersion: apps/v1
kind: Deployment
metadata:
  name: directory-deployment
spec:
  selector:
    matchLabels:
      app: directory-mysql
  replicas: 1
  template:
    metadata:
      labels:
        app: directory-mysql
    spec:
      containers:
        - name: directory
          image: directory:latest
          ports:
            - containerPort: 8080
          env:   # Setting Enviornmental Variables
            - name: DB_HOST   # Setting Database host address from configMap
              valueFrom :
                configMapKeyRef :
                  name : db-config
                  key :  host

            - name: DB_NAME  # Setting Database name from configMap
              valueFrom :
                configMapKeyRef :
                  name : db-config
                  key :  dbName
            - name: DB_USERNAME  # Setting Database username from Secret
              valueFrom :
                secretKeyRef :
                  name : mysql-secrets
                  key :  username
            - name: DB_PASSWORD # Setting Database password from Secret
              valueFrom :
                secretKeyRef :
                  name : mysql-secrets
                  key :  password

---

apiVersion: v1 # Kubernetes API version
kind: Service # Kubernetes resource kind we are creating
metadata: # Metadata of the resource kind we are creating
  name: directory
spec:
  selector:
    app: mysql
  ports:
    - protocol: "TCP"
      port: 8080 # The port that the service is running on in the cluster
      targetPort: 8080 # The port exposed by the service
  type: NodePort # type of the service.

我想要做的是将正确的

db.url
传递给应用程序容器以连接到 MySQL pod。

java mysql kubernetes jdbc
1个回答
0
投票

以下是如何设置 SPRING_DATASOURCE_URL 环境变量以连接到 MySQL pod:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: directory-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: directory
  template:
    metadata:
      labels:
        app: directory
    spec:
      containers:
        - name: directory-container
          image: directory:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
          env:
            - name: SPRING_DATASOURCE_URL
              value: jdbc:mysql://mysql:3306/temp?useSSL=false

确保 JDBC URL 中的 DB_HOST:PORT 与您的 MySQL pod 的服务名称匹配。 另一个需要注意的要点是 MySQL 服务应该是NodePort 类型。您不需要将其暴露在集群外部,因此您可以将服务的类型更改为ClusterIP

© www.soinside.com 2019 - 2024. All rights reserved.