在 Kubernetes 中,可以使用 Persistent Volumes (PV) 和 Persistent Volume Claims (PVC) 来持久化存储数据,例如在部署 WordPress 和 MySQL 这样的应用程序时。以下是一个简单的示例,演示如何使用 Persistent Volumes 部署 WordPress 和 MySQL。

1. 创建 MySQL 的 Persistent Volume 和 Persistent Volume Claim:
   # mysql-pv.yaml
   apiVersion: v1
   kind: PersistentVolume
   metadata:
     name: mysql-pv
   spec:
     capacity:
       storage: 1Gi
     accessModes:
       - ReadWriteOnce
     hostPath:
       path: "/mnt/data/mysql"

   ---

   # mysql-pvc.yaml
   apiVersion: v1
   kind: PersistentVolumeClaim
   metadata:
     name: mysql-pvc
   spec:
     accessModes:
       - ReadWriteOnce
     resources:
       requests:
         storage: 1Gi

   这个示例中,我们使用 hostPath 来表示在宿主机上的路径为 /mnt/data/mysql,你可以根据实际情况使用其他持久化存储解决方案。

2. 创建 MySQL 的 Deployment 和 Service:
   # mysql-deployment.yaml
   apiVersion: apps/v1
   kind: Deployment
   metadata:
     name: mysql-deployment
   spec:
     replicas: 1
     selector:
       matchLabels:
         app: mysql
     template:
       metadata:
         labels:
           app: mysql
       spec:
         containers:
         - name: mysql
           image: mysql:latest
           env:
           - name: MYSQL_ROOT_PASSWORD
             value: rootpassword
           ports:
           - containerPort: 3306
           volumeMounts:
           - name: mysql-persistent-storage
             mountPath: /var/lib/mysql

   ---

   # mysql-service.yaml
   apiVersion: v1
   kind: Service
   metadata:
     name: mysql-service
   spec:
     selector:
       app: mysql
     ports:
       - protocol: TCP
         port: 3306

   在这个示例中,MySQL 使用了上述创建的 Persistent Volume 和 Persistent Volume Claim。MYSQL_ROOT_PASSWORD 是 MySQL 的根密码,这里设置为了 rootpassword。

3. 创建 WordPress 的 Persistent Volume 和 Persistent Volume Claim:
   # wordpress-pv.yaml
   apiVersion: v1
   kind: PersistentVolume
   metadata:
     name: wordpress-pv
   spec:
     capacity:
       storage: 1Gi
     accessModes:
       - ReadWriteOnce
     hostPath:
       path: "/mnt/data/wordpress"

   ---

   # wordpress-pvc.yaml
   apiVersion: v1
   kind: PersistentVolumeClaim
   metadata:
     name: wordpress-pvc
   spec:
     accessModes:
       - ReadWriteOnce
     resources:
       requests:
         storage: 1Gi

4. 创建 WordPress 的 Deployment 和 Service:
   # wordpress-deployment.yaml
   apiVersion: apps/v1
   kind: Deployment
   metadata:
     name: wordpress-deployment
   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-service
           - name: WORDPRESS_DB_PASSWORD
             value: rootpassword
           ports:
           - containerPort: 80
           volumeMounts:
           - name: wordpress-persistent-storage
             mountPath: /var/www/html

   ---

   # wordpress-service.yaml
   apiVersion: v1
   kind: Service
   metadata:
     name: wordpress-service
   spec:
     selector:
       app: wordpress
     ports:
       - protocol: TCP
         port: 80

   在这个示例中,WordPress 使用了上述创建的 Persistent Volume 和 Persistent Volume Claim。WORDPRESS_DB_HOST 是 MySQL 的服务名称,WORDPRESS_DB_PASSWORD 是 MySQL 的根密码。

5. 应用配置:

   应用上述 YAML 文件到 Kubernetes 集群:
   kubectl apply -f mysql-pv.yaml
   kubectl apply -f mysql-pvc.yaml
   kubectl apply -f mysql-deployment.yaml
   kubectl apply -f mysql-service.yaml

   kubectl apply -f wordpress-pv.yaml
   kubectl apply -f wordpress-pvc.yaml
   kubectl apply -f wordpress-deployment.yaml
   kubectl apply -f wordpress-service.yaml

以上配置将创建一个运行 WordPress 和 MySQL 的 Kubernetes 集群。确保你的集群已经配置正确,并且根据实际需要修改密码和存储路径等配置。这只是一个基础示例,实际部署可能需要更多的配置和安全性措施。


转载请注明出处:http://www.pingtaimeng.com/article/detail/9981/Kubernetes