🗓️实验环境
OS名称 | Microsoft Windows 11 家庭中文版 |
---|---|
系统类型 | x64-based PC |
Docker版本 | Docker version 24.0.6, build ed223bc |
minikube版本 | v1.32.0 |
🖇️创建 kustomization.yaml
你可以通过 kustomization.yaml 中的生成器创建一个 Secret存储密码或密钥敏感数据对象。
kustomization.yaml
cat <<EOF >./kustomization.yaml
secretGenerator:
- name: mysql-passliterals:- password=YOUR_PASSWORD #自定义密码
EOF
🗒️MySQL 和 WordPress 的资源配置
MySQL 容器将 PersistentVolume 挂载在 /var/lib/mysql。 MYSQL_ROOT_PASSWORD 环境变量根据 Secret 设置数据库密码。
mysql-deployment.yaml
apiVersion: v1
kind: Service
metadata:name: wordpress-mysqllabels:app: wordpress
spec:ports:- port: 3306selector:app: wordpresstier: mysqlclusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: mysql-pv-claimlabels:app: wordpress
spec:accessModes:- ReadWriteOnceresources:requests:storage: 20Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:name: wordpress-mysqllabels:app: wordpress
spec:selector:matchLabels:app: wordpresstier: mysqlstrategy:type: Recreatetemplate:metadata:labels:app: wordpresstier: mysqlspec:containers:- image: mysql:8.0name: mysqlenv:- name: MYSQL_ROOT_PASSWORDvalueFrom:secretKeyRef:name: mysql-passkey: password- name: MYSQL_DATABASEvalue: wordpress- name: MYSQL_USERvalue: wordpress- name: MYSQL_PASSWORDvalueFrom:secretKeyRef:name: mysql-passkey: passwordports:- containerPort: 3306name: mysqlvolumeMounts:- name: mysql-persistent-storagemountPath: /var/lib/mysqlvolumes:- name: mysql-persistent-storagepersistentVolumeClaim:claimName: mysql-pv-claim
PersistentVolume 挂载到 /var/www/html,用于保存网站数据文件。 WORDPRESS_DB_HOST 环境变量设置上面定义的 MySQL Service 的名称,WordPress 将通过 Service 访问数据库。 WORDPRESS_DB_PASSWORD 环境变量根据使用 kustomize 生成的 Secret 设置数据库密码。
wordpress-deployment.yaml
apiVersion: v1
kind: Service
metadata:name: wordpresslabels:app: wordpress
spec:ports:- port: 80selector:app: wordpresstier: frontendtype: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: wp-pv-claimlabels:app: wordpress
spec:accessModes:- ReadWriteOnceresources:requests:storage: 20Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:name: wordpresslabels:app: wordpress
spec:selector:matchLabels:app: wordpresstier: frontendstrategy:type: Recreatetemplate:metadata:labels:app: wordpresstier: frontendspec:containers:- image: wordpress:6.2.1-apachename: wordpressenv:- name: WORDPRESS_DB_HOSTvalue: wordpress-mysql- name: WORDPRESS_DB_PASSWORDvalueFrom:secretKeyRef:name: mysql-passkey: password- name: WORDPRESS_DB_USERvalue: wordpressports:- containerPort: 80name: wordpressvolumeMounts:- name: wordpress-persistent-storagemountPath: /var/www/htmlvolumes:- name: wordpress-persistent-storagepersistentVolumeClaim:claimName: wp-pv-claim
将内容追加到 kustomization.yaml
:
cat <<EOF >>./kustomization.yaml
resources:- mysql-deployment.yaml- wordpress-deployment.yaml
EOF
应用目录下文件:
rkun1@LAPTOP-TUS5FU0D MINGW64 ~/minikube-env
$ ls
kustomization.yaml mysql-deployment.yaml wordpress-deployment.yaml rkun1@LAPTOP-TUS5FU0D MINGW64 ~/minikube-env
$ kubectl apply -k ./
secret/mysql-pass-km27c7kb5t 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
📌验证信息
rkun1@LAPTOP-TUS5FU0D MINGW64 ~/minikube-env
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8d
wordpress LoadBalancer 10.111.241.246 <pending> 80:32583/TCP 6m52s
wordpress-mysql ClusterIP None <none> 3306/TCP 6m52srkun1@LAPTOP-TUS5FU0D MINGW64 ~/minikube-env
$ kubectl get secrets
NAME TYPE DATA AGE
mysql-pass-km27c7kb5t Opaque 1 7m5srkun1@LAPTOP-TUS5FU0D MINGW64 ~/minikube-env
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
wordpress-79669d56c6-ww62t 1/1 Running 0 7m12s
wordpress-mysql-6cdf6b85df-7dq9h 1/1 Running 0 7m12s
本地minikube只能使用NodePort,运行以下命令以获取 WordPress 服务的 IP 地址
minikube service wordpress --url
$ minikube service wordpress --url
W1119 20:15:12.068839 25968 main.go:291] Unable to resolve the current Docker CLI context "default": context "default": context not found: open C:\Users\rkun1\.docker\contexts\meta\37a8eec1ce19687d132fe29051dca629d164e2c4958ba141d5f4133a33f0688f\meta.json: The system cannot find the path specified.
http://127.0.0.1:49401
❗ 因为你正在使用 windows 上的 Docker 驱动程序,所以需要打开终端才能运行它。
使用浏览器访问:
数据存储在mysql服务中,mysql使用的存储资源是从环境获取的
查看PV信息:
rkun1@LAPTOP-TUS5FU0D MINGW64 ~
$ kubectl describe pv
Name: pvc-30474999-b5e8-4b3a-8a47-135c469cb7c1
Labels: <none>
Annotations: hostPathProvisionerIdentity: bb8975c2-5e31-4b2b-829f-948164f82d6apv.kubernetes.io/provisioned-by: k8s.io/minikube-hostpath
Finalizers: [kubernetes.io/pv-protection]
StorageClass: standard
Status: Bound
Claim: default/mysql-pv-claim
Reclaim Policy: Delete
Access Modes: RWO
VolumeMode: Filesystem
Capacity: 20Gi
Node Affinity: <none>
Message:
Source:Type: HostPath (bare host directory volume)Path: /tmp/hostpath-provisioner/default/mysql-pv-claimHostPathType:
Events: <none>Name: pvc-e25bb604-379b-4883-bc5c-fa3fe03c6301
Labels: <none>
Annotations: hostPathProvisionerIdentity: bb8975c2-5e31-4b2b-829f-948164f82d6apv.kubernetes.io/provisioned-by: k8s.io/minikube-hostpath
Finalizers: [kubernetes.io/pv-protection]
StorageClass: standard
Status: Bound
Claim: default/wp-pv-claim
Reclaim Policy: Delete
Access Modes: RWO
VolumeMode: Filesystem
Capacity: 20Gi
Node Affinity: <none>
Message:
Source:Type: HostPath (bare host directory volume)Path: /tmp/hostpath-provisioner/default/wp-pv-claimHostPathType:
Events: <none>
进入minikube虚拟环境,可以查看到pv所挂载的目录:
rkun1@LAPTOP-TUS5FU0D MINGW64 ~
$ minikube ssh
W1119 20:23:02.886020 3824 main.go:291] Unable to resolve the current Docker CLI context "default": context "default": context not found: open C:\Users\rkun1\.docker\contexts\meta\37a8eec1ce19687d132fe29051dca629d164e2c4958ba141d5f4133a33f0688f\meta.json: The system cannot find the path specified.
docker@minikube:~$ cd /tmp/hostpath-provisioner/default
docker@minikube:/tmp/hostpath-provisioner/default$ ls
mysql-pv-claim wp-pv-claim
进入目录可以查看到mysql的一些配置文件以及数据:
docker@minikube:/tmp/hostpath-provisioner/default$ cd mysql-pv-claim/
docker@minikube:/tmp/hostpath-provisioner/default/mysql-pv-claim$ ls
'#ib_16384_0.dblwr' binlog.index ibtmp1 server-cert.pem
'#ib_16384_1.dblwr' ca-key.pem mysql server-key.pem
'#innodb_redo' ca.pem mysql.ibd sys
'#innodb_temp' client-cert.pem mysql.sock undo_001auto.cnf client-key.pem performance_schema undo_002binlog.000001 ib_buffer_pool private_key.pem wordpressbinlog.000002 ibdata1 public_key.pem
🗑️清理资源
kubectl delete -k ./
rkun1@LAPTOP-TUS5FU0D MINGW64 ~
$ cd minikube-env/rkun1@LAPTOP-TUS5FU0D MINGW64 ~/minikube-env
$ ls
kustomization.yaml mysql-deployment.yaml wordpress-deployment.yamlrkun1@LAPTOP-TUS5FU0D MINGW64 ~/minikube-env
$ kubectl delete -k ./
secret "mysql-pass-km27c7kb5t" 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