我目前正在Ubuntu 22.10上的kubertes (MicroK8s)中测试Wordpress。现在我总是有一个问题,当我访问"test.com"时,我被重定向到IP地址"192.168.210.200"。我的YAML文件
apiVersion: v1
kind: Secret
metadata:
name: mysql-creds
namespace: wordpress
type: Opaque
data:
MYSQL_ROOT_PASSWORD: ZXhhbXBsZWRhdGE=
MYSQL_DATABASE: ZXhhbXBsZWRhdGE=
MYSQL_PASSWORD: ZXhhbXBsZWRhdGE=
MYSQL_USER: ZXhhbXBsZWRhdGE=
---
apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql-service
namespace: wordpress
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-mysql-deployment
namespace: wordpress
labels:
app: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
tier: mysql
minReadySeconds: 20
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: wordpress
tier: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
envFrom:
- secretRef:
name: mysql-creds
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-pv-volume
mountPath: /var/lib/mysql
securityContext:
runAsUser: 1000
volumes:
- name: mysql-pv-volume
persistentVolumeClaim:
claimName: mysql-pv-claim
---
apiVersion: v1
kind: Secret
metadata:
name: wordpress-creds
namespace: wordpress
type: Opaque
data:
WORDPRESS_DB_HOST: d29yZHByZXNzLW15c3FsLXNlcnZpY2U= # wordpress-mysql-service
WORDPRESS_DB_USER: ZXhhbXBsZWRhdGE=
WORDPRESS_DB_PASSWORD: ZXhhbXBsZWRhdGE=
WORDPRESS_DB_NAME: ZXhhbXBsZWRhdGE=
---
apiVersion: v1
kind: Service
metadata:
name: wordpress-frontend-service
namespace: wordpress
labels:
app: wordpress
spec:
selector:
app: wordpress
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
- port: 443
protocol: TCP
name: https
targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
namespace: wordpress
name: wordpress-ingress
labels:
app: wordpress
spec:
rules:
- host: test.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: wordpress-frontend-service
port:
number: 80
port:
number: 443
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-frontend-deployment
namespace: wordpress
labels:
app: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
tier: frontend
minReadySeconds: 20
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: wordpress
tier: frontend
spec:
containers:
- image: wordpress:4.8-apache
name: wordpress
envFrom:
- secretRef:
name: wordpress-creds
ports:
- containerPort: 80
name: wordpress
volumeMounts:
- name: wordpress-pv-volume
mountPath: /var/www/html
securityContext:
runAsUser: 1000
volumes:
- name: wordpress-pv-volume
persistentVolumeClaim:
claimName: wordpress-pv-claim
文件不包含命名空间,没有PersistentVolumes和persistentvolumecclaims,因为这与解决问题无关。
我的子网:192.168.210.0/24
我启用了入口"microk8s启用了入口"并且nginx入口控制器pod也正常运行。当然,我还在hosts文件(etc/hosts)中为test.com做了一个条目。Wordpress和MySQL pod也都在运行。这有什么不对吗?
听起来你的WordPress应用程序重定向到IP地址,而不是使用域名。这是WordPress运行在反向代理(如Nginx入口控制器)后的常见问题。
你可以尝试在WordPress容器的wp-config.php文件中添加以下几行:
define('WP_HOME','http://test.com');
define('WP_SITEURL','http://test.com');
这将告诉WordPress使用test.com作为站点URL而不是IP地址。
另外,你可以检查你的Nginx入口控制器配置,以确保它正确地转发流量到你的WordPress服务。您应该有一个入口资源,为您的WordPress应用程序指定主机名和路径,如下所示:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: wordpress-ingress
spec:
rules:
- host: test.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: wordpress-service
port:
name: http
确保主机字段与您正在使用的域名匹配,并且后端部分指向您的WordPress服务。