OS名称 | Microsoft Windows 11 家庭中文版 |
---|---|
系统类型 | x64-based PC |
Docker版本 | Docker version 24.0.6, build ed223bc |
minikube版本 | v1.32.0 |
你可以通过 kustomization.yaml 中的生成器创建一个 Secret存储密码或密钥敏感数据对象。
kustomization.yaml
cat <>./kustomization.yaml
secretGenerator:
- name: mysql-pass
literals:
- password=YOUR_PASSWORD #自定义密码
EOF
MySQL 容器将 PersistentVolume 挂载在 /var/lib/mysql。 MYSQL_ROOT_PASSWORD 环境变量根据 Secret 设置数据库密码。
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
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: 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
将内容追加到 kustomization.yaml
:
cat <>>./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 6m52s
rkun1@LAPTOP-TUS5FU0D MINGW64 ~/minikube-env
$ kubectl get secrets
NAME TYPE DATA AGE
mysql-pass-km27c7kb5t Opaque 1 7m5s
rkun1@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-948164f82d6a
pv.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-claim
HostPathType:
Events: <none>
Name: pvc-e25bb604-379b-4883-bc5c-fa3fe03c6301
Labels: <none>
Annotations: hostPathProvisionerIdentity: bb8975c2-5e31-4b2b-829f-948164f82d6a
pv.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-claim
HostPathType:
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_001
auto.cnf client-key.pem performance_schema undo_002
binlog.000001 ib_buffer_pool private_key.pem wordpress
binlog.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.yaml
rkun1@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