简体   繁体   中英

How to set hostname of mysql url in kubernetes Spring application as evnironment variable?

I have a spring application on kube.netes which connects to mysql database. This is my spring-deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: spring
spec:
  selector:
    matchLabels:
      app: spring
  replicas: 1
  template:
    metadata:
      labels:
        app: spring
    spec:
      containers:
      - name: spring
        image: docker/spring
        imagePullPolicy: IfNotPresent
        ports:
          - containerPort: 8091
        env:
        - name: SPRING_DATASOURCE_URL
          value: jdbc:mysql://172.17.0.7:3306/buddyto_mstr_local?useSSL=false&allowPublicKeyRetrieval=true
        - name: SPRING_DATASOURCE_USERNAME
          value: root
        - name: SPRING_DATASOURCE_PASSWORD
          value: root
---
apiVersion: v1
kind: Service
metadata:
  name: spring
spec:
  ports:
    - protocol: "TCP"
      port: 8091
      targetPort: 8091
  selector:
    app: sample-service
  type: NodePort

This value: jdbc:mysql://172.17.0.7:3306/buddyto_mstr_local?useSSL=false&allowPublicKeyRetrieval=true contains the mysql pods IP adress which I got after running kubectl get pods -o wide

Every time I delete the mysql deployment and create a new one, a new IP address is assigned to the mysql deployment so I have to change it here from 172.17.0.7 to the new IP.

I want to set it in a way so that I don't have to keep changing the IP every time. What should I do for that?

Kube.netes creates DNS entries for your services so instead of using the IP use the service name.

Please read more here: https://kube.netes.io/docs/concepts/services.networking/dns-pod-service/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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