简体   繁体   English

kubernetes-如何将 mysql 服务暴露给另一个 pod?

[英]kubernetes-How do I expose mysql service to another pod?

I have a mysql service with pv, and a depoyment of a custom karaf image image.我有一个带有 pv 的 mysql 服务,以及一个自定义 karaf 图像图像的部署。

I have hooks in my karaf, and when I install the kar liquibase should fill the databases, but I realise that the databases renain empty, so karaf cant access mysql.我的 karaf 中有钩子,当我安装 kar liquibase 时应该填充数据库,但我意识到数据库是空的,所以 karaf 无法访问 mysql。

I want to access in my machine, localhost.我想在我的机器 localhost 中访问。

Here are my .yaml configurations:这是我的 .yaml 配置:

mysql pv: mysql 光伏:

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

mysql-deploy: mysql 部署:

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.7
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: mysql-root-password
        - name: MYSQL_USER
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: mysql-user
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: mysql-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

the secret with mysql pass: mysql pass 的秘密:

apiVersion: v1
kind: Secret
metadata:
  name: db-secret
type: Opaque
data:
  mysql-password: cm9vdA==
  mysql-root-password: cm9vdA==
  mysql-user: eGNvcmVyb290

karaf deploy:卡拉夫部署:

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: karaf-deployment
spec:
  selector:
    matchLabels:
      app: karaf
  replicas: 1 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: karaf
    spec:
      containers:
      - name: karaf
        image: myuser/karaf:v1

Note: to connect to mysql locally I had to expose doing: kubectl expose deployment mysql --type=LoadBalancer --name=my-service注意:要在本地连接到 mysql,我必须公开这样做: kubectl expose deployment mysql --type=LoadBalancer --name=my-service

Not sure if that is right...不知道这样对不对...

and I did the same for karaf, adding --port=8101 so I could ssh to karaf and install the kar...我对 karaf 做了同样的事情,添加--port=8101这样我就可以 ssh 到 karaf 并安装 kar ...

I tried some sugestions I saw on the internet, such as create a ingress controller, or this one: Minikube expose MySQL running on localhost as service我尝试了一些我在互联网上看到的建议,例如创建一个入口控制器,或者这个: Minikube 将运行在本地主机上的 MySQL 作为服务公开

but didnt work :(但没有用:(

thanks for your time!谢谢你的时间!

In Your Karaf application.what is the connection source url?在您的 Karaf 应用程序中。连接源 url 是什么? For example in a Spring boot application deploy in kubernetes and that use a mysql service the connection source url will be : jdbc:mysql://mysqlservice:3306/${DB_NAME}.例如,在 kubernetes 中部署的 Spring boot 应用程序中,并且使用 mysql 服务,连接源 url 将是:jdbc:mysql://mysqlservice:3306/${DB_NAME}。 Where "mysqlservice" will be the name of the service mysql define in kubernetes其中“mysqlservice”将是在 kubernetes 中定义的服务 mysql 的名称

Your service is already expose in the Kubernetes to other pods on port 3306 .您的服务已经在 Kubernetes 中暴露给端口3306上的其他 pod。 You can expose mysql to the host by using a service of type NodePort .您可以使用NodePort类型的服务将 mysql 公开给主机。

kubectl expose svc/mysql --name mysql-np --port 3306 --type NodePort
kubectl get svc mysql-np -ojsonpath='{.spec.ports[0].nodePort}'

For the data, are you deleting and recreating PV by any chance?对于数据,你是否有任何机会删除并重新创建PV? The configuration you have shared should work.您共享的配置应该可以工作。

As you are working the same namespace the pods created after you have a service called MySQL will have environment variables named:当您使用相同的命名空间时,在您拥有名为 MySQL 的服务后创建的 pod 将具有名为的环境变量:

  • MYSQL_SERVICE_HOST MYSQL_SERVICE_HOST
  • MYSQL_SERVICE_PORT MYSQL_SERVICE_PORT

You can use them in the second deployment to get access to the MySQL deployment.您可以在第二个部署中使用它们来访问 MySQL 部署。

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

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