Wordpress 使用 kubernetes 建立数据库连接时出错

Wordpress Error establishing a database connection using kubernetes

我是 Kubernetes 的新手,正在尝试使用 Kubernetes pod 容器部署 WordPress 和 MySQL,但在 运行 Kubernetes

时抛出错误 "Error establishing a database connection"

下面是我的 pod 和服务定义文件

secret.yaml

apiVersion: v1
kind: Secret
metadata:
  name: mysql-pass
type: Opaque
data:
  username: XXXXX
  password: XXXXX

mysql-pod-定义-file.yaml

apiVersion: v1
kind: Pod
metadata:
 name: mysql-app
 labels:
  name: mysql-app-label
spec:
 containers:
  - image: mysql:5.6
    env:
     - name: MYSQL_ROOT_PASSWORD
       valueFrom:
        secretKeyRef:
         name: mysql-pass
         key: password
    name: mysql-image
    ports:
     - containerPort: 3306

wordpress-pod-定义-file.yaml

apiVersion: v1
kind: Pod
metadata:
 name: wordpress-app
 labels:
  name: wordpress-app-label
spec:
 containers:
  - image: wordpress:apache
    name: wordpress-image
    env:
        - name: WORDPRESS_DB_HOST
          value: db
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
    ports:
        - containerPort: 80 

mysql-服务反抗-file.yaml

apiVersion: v1
kind: Service
metadata:
 name: db
 labels:
  name: db-service
  app: demo-wordpress-app
spec: 
 ports: 
  - port: 3306
 selector:
    name: mysql-app-label

wordpress-service-defination-file-yaml

apiVersion: v1
kind: Service
metadata:
 name: wordpress-service
 labels:
  name: wordpress-service
  app: demo-wordpress-app
spec:
 type: NodePort 
 ports: 
  - port: 80
    targetPort: 80
 selector:
    name: wordpress-app-label

我能够从 minikube Pod 容器内部成功连接到 mysql 数据库

我的定义文件有什么问题吗?

在 mysql 服务 YAML

中包含以下行

目标端口:3306

您需要在 mysql 服务 yaml 定义中添加 targetPort

apiVersion: v1
kind: Service
metadata:
 name: db
 labels:
  name: db-service
  app: demo-wordpress-app
spec: 
 ports: 
  - port: 3306
    targetPort: 3306
 selector:
    name: mysql-app-label