k8s单机部署wordpress
在前文k8s部署有状态应用中,简单介绍了StatefulSet的使用。
本教程将展示如何使用 Minikube 部署 WordPress 站点和 MySQL 数据库。这两个应用程序都使用 PersistentVolumes 和 PersistentVolumeClaims 来存储数据。
PersistentVolume (PV) 是集群中的一块存储,由管理员手动配置,或者由 Kubernetes 使用 StorageClass动态配置。PersistentVolumeClaim (PVC )是用户提出的存储请求,可以通过 PV 来满足。PersistentVolumes 和 PersistentVolumeClaims 独立于 Pod 生命周期,并通过重新启动、重新调度甚至删除 Pod 来保留数据。
本文目标
- 创建pv和pvc
- 创建一个包含Secret generator 和mysql与wordpress配置的yaml文件
- 部署
- 清除
创建kustomization
Secret是存储密码或密钥等敏感数据的对象。从 1.14 开始,kubectl支持使用 kustomization 文件管理 Kubernetes 对象。
执行以下命令将Secret添加到kustomization.yaml。记得更换password
cat <<EOF >./kustomization.yaml
secretGenerator:
- name: mysql-pass
literals:
- password=YOUR_PASSWORD
EOF
创建MySQL yaml
vim mysql-deployment.yaml
MySQL 容器将 PersistentVolume 挂载在 /var/lib/mysql 处。环境MYSQL_ROOT_PASSWORD 变量设置来自 Secret 的数据库密码。
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
创建Wordpress yaml
vim wordpress-deployment.yaml
wordpress-deployment.yaml描述了单实例 WordPress 容器将PersistentVolume挂载在/var/www/html 作为网站数据文件 。WORDPRESS_DB_HOST环境变量设置是上面定义的MySQL Service的名称。WORDPRESS_DB_PASSWORD 环境变量在kustomization中设置。
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
mysql 和wordpress的配置文件也可以从网上下载下来。
curl -LO https://k8s.io/examples/application/wordpress/mysql-deployment.yaml
curl -LO https://k8s.io/examples/application/wordpress/wordpress-deployment.yaml
将配置文件添加到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
验证 Secret 是否存在
kubectl get secrets
------
NAME TYPE DATA AGE
mysql-pass-kkcc2b926b Opaque 1 26s
验证 PersistentVolume 是否已动态配置
kubectl get pvc
------
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-pv-claim Bound pvc-2675097d-aec0-4f96-9e80-047588bcfd9a 20Gi RWO standard 94s
wp-pv-claim Bound pvc-d5d19a5c-d9c2-41d7-8de8-f7a1bec03fc1 20Gi RWO standard 94s
验证 Pod 是否正在运行
kubectl get pods
------
NAME READY STATUS RESTARTS AGE
wordpress-65dcc8b9cd-bz875 1/1 Running 0 2m5s
wordpress-mysql-7646676cfc-98pxd 1/1 Running 0 2m5s
验证服务是否正在运行
kubectl get services wordpress
------
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress LoadBalancer 10.110.228.240 <pending> 80:30692/TCP 3m2s
获取 WordPress 服务的 IP 地址
minikube service wordpress --url
------
http://127.0.0.1:52551
? 因为你正在使用 darwin 上的 Docker 驱动程序,所以需要打开终端才能运行它。
复制 IP 地址,然后在浏览器中加载页面以访问wordpress站点。
清除
清理所有服务执行kubectl delete -k ./
即可。
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
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!