Kubernetes的有状态应用示例:用PV部署WordPress和MySQL

文章目录

  • 环境
  • PVC和PV
    • 创建PVC和PV
  • 创建kustomization.yaml
    • 添加secret生成器
  • 为MySQL和WordPress添加资源配置
  • 部署和验证
  • 清理
  • 参考

环境

  • RHEL 9.3
  • Docker Community 24.0.7
  • minikube v1.32.0

PVC和PV

PersistentVolume(PV)是在集群里由管理员手动provision或Kubernetes通过StorageClass动态provision的存储。PersistentVolumeClaim(PVC)是用户的存储请求,该请求可由PV来实现。PV和 PVC独立于pod生命周期,在pod重启、重新调度甚至删除时仍然会保留数据。

创建PVC和PV

MySQL和Wordpress都需要PV来存储数据。它们的PVC将在部署时创建。

很多集群环境都安装了默认的StorageClass。如果在PVC中没有指定StorageClass,则使用默认的StorageClass。

在创建PVC时,将基于StorageClass配置动态provision PV。

注意:在本地集群中,默认的StorageClass使用 hostPath provisioner。 hostPath 卷仅适用于开发和测试。使用 hostPath 卷时,数据位于pod所在node的 /tmp 中,且不会在node间移动。如果pod死亡并被调度到集群中的另一个node,或者该node被重启,则数据将丢失。

如果要建立使用 hostPath provisioner的集群,则必须在 controller-manager 组件中设置 --enable-hostpath-provisioner 选项。

创建kustomization.yaml

添加secret生成器

Secret是存储敏感数据(比如密码或密钥)的对象。从1.14开始, kubectl 支持使用kustomization文件来管理Kubernetes对象。你可以通过 kustomization.yaml 中的生成器来创建secret。

可通过以下命令在 kustomization.yaml 中添加secret生成器。把 YOUR_PASSWORD 替换为自己的密码。

cat <./kustomization.yaml
secretGenerator:
- name: mysql-pass
  literals:
  - password=YOUR_PASSWORD
EOF

比如:本例中,把密码设置为 123456

为MySQL和WordPress添加资源配置

创建文件 mysql-deployment.yaml 如下:

apiVersion: v1
kind: Service
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  ports:
    - port: 3306
  selector:
    app: wordpress
    tier: mysql
  clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: mysql
    spec:
      containers:
      - image: mysql:8.0
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        - name: MYSQL_DATABASE
          value: wordpress
        - name: MYSQL_USER
          value: wordpress
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: 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

也可以直接下载:

curl -LO https://k8s.io/examples/application/wordpress/mysql-deployment.yaml

创建文件 wordpress-deployment.yaml 如下:

apiVersion: v1
kind: Service
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress
    tier: frontend
  type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: frontend
    spec:
      containers:
      - image: wordpress:6.2.1-apache
        name: wordpress
        env:
        - name: WORDPRESS_DB_HOST
          value: wordpress-mysql
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        - name: WORDPRESS_DB_USER
          value: wordpress
        ports:
        - containerPort: 80
          name: wordpress
        volumeMounts:
        - name: wordpress-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: wp-pv-claim

也可以直接下载:

curl -LO https://k8s.io/examples/application/wordpress/wordpress-deployment.yaml

添加到 kustomization.yaml file 文件里:

cat <>./kustomization.yaml
resources:
  - mysql-deployment.yaml
  - wordpress-deployment.yaml
EOF

最终 kustomization.yaml 文件如下:

secretGenerator:
- name: mysql-pass
  literals:
  - password=123456
resources:
  - mysql-deployment.yaml
  - wordpress-deployment.yaml

部署和验证

$ kubectl apply -k ./
secret/mysql-pass-kkcc2b926b created
service/wordpress created
service/wordpress-mysql created
persistentvolumeclaim/mysql-pv-claim created
persistentvolumeclaim/wp-pv-claim created
deployment.apps/wordpress created
deployment.apps/wordpress-mysql created

注:使用 -k 选项时,默认的文件名是 kustomization.yaml

验证secret:

$ kubectl get secrets
NAME                    TYPE     DATA   AGE
mysql-pass-kkcc2b926b   Opaque   1      47s

验证PVC:

$ kubectl get pvc
NAME             STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-pv-claim   Bound    pvc-580c45ff-ff05-4a51-b1f9-0d2ca6d76034   20Gi       RWO            standard       98s
wp-pv-claim      Bound    pvc-40afa185-98e1-4efe-9076-2ff01d6e3774   20Gi       RWO            standard       98s

验证pod:

$ kubectl get pods
NAME                               READY   STATUS    RESTARTS   AGE
wordpress-65dcc8b9cd-jsd6v         1/1     Running   0          4m42s
wordpress-mysql-7646676cfc-rbhm9   1/1     Running   0          4m42s

验证service:

$ kubectl get services wordpress
NAME        TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
wordpress   LoadBalancer   10.101.89.106        80:30851/TCP   5m33s

注意:Minikube只能通过 NodePort 暴露服务。 EXTERNAL-IP 始终处于 pending 状态。

获取URL:

$ minikube service wordpress --url
http://192.168.49.2:30851

注:如果不加 --url 选项,则会自动启动浏览器,访问该URL。

打开浏览器,访问该URL:

Kubernetes的有状态应用示例:用PV部署WordPress和MySQL_第1张图片

按照其提示,安装配置一番,就可以使用了:

Kubernetes的有状态应用示例:用PV部署WordPress和MySQL_第2张图片

清理

$ kubectl delete -k ./
secret "mysql-pass-kkcc2b926b" deleted
service "wordpress" deleted
service "wordpress-mysql" deleted
persistentvolumeclaim "mysql-pv-claim" deleted
persistentvolumeclaim "wp-pv-claim" deleted
deployment.apps "wordpress" deleted
deployment.apps "wordpress-mysql" deleted

参考

  • https://kubernetes.io/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume

你可能感兴趣的:(Kubernetes,kubernetes,statefulset)