在Kubernetes(k8s)中部署WordPress通常涉及创建一个Deployment来管理WordPress的Pod,以及一个Service来暴露WordPress应用。此外,由于WordPress需要数据库支持,你还需要部署一个MySQL或MariaDB的Pod和Service。
以下是一个基本的示例,展示了如何使用YAML文件在Kubernetes中部署WordPress和MySQL。
- MySQL Deployment 和 Service
首先,我们创建MySQL的Deployment和Service。
mysql-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata: name: mysql
spec: replicas: 1 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - name: mysql image: mysql env: - name: MYSQL_ROOT_PASSWORD value: "root" - name: MYSQL_DATABASE value: wordpress - name: MYSQL_USER value: wordpress - name: MYSQL_PASSWORD value: "root" 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: Service
metadata: name: mysql
spec: ports: - port: 3306 selector: app: mysql
注意:这里使用了PersistentVolumeClaim(PVC)来持久化MySQL数据。你需要提前创建相应的PersistentVolume(PV)。
2. WordPress Deployment 和 Service
接下来,我们创建WordPress的Deployment和Service。
wordpress-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata: name: wordpress
spec: replicas: 1 selector: matchLabels: app: wordpress template: metadata: labels: app: wordpress spec: containers: - name: wordpress image: wordpress:latest env: - name: WORDPRESS_DB_HOST value: mysql - name: WORDPRESS_DB_USER value: wordpress - name: WORDPRESS_DB_PASSWORD value: "root" - name: WORDPRESS_DB_NAME value: wordpress ports: - containerPort: 80 name: wordpress
---
apiVersion: v1
kind: Service
metadata: name: wordpress
spec: type: NodePort ports: - port: 80 targetPort: 80 nodePort: 30008selector: app: wordpress
注意:这里Service的类型设置为LoadBalancer,这适用于大多数云环境(如AWS, GCP, Azure等),它们会自动为你创建一个负载均衡器。如果你是在本地环境(如Minikube)上运行,可能需要将类型更改为NodePort。
部署
使用kubectl部署这些YAML文件:
kubectl apply -f mysql-deployment.yaml kubectl apply -f wordpress-deployment.yaml