1、部署MySQL,将数据存储在OpenEBS Jiva卷上;
#部署 openebs 基础环境
root@k8s-master01:~kubectl apply -f https://openebs.github.io/charts/openebs-operator.yaml
#导入sig-storage.tar文件
ctr -n k8s.io image import sig-storage.tar
# node节点部署 scsi-client插件
sudo apt-get update
sudo apt-get install open-iscsi
sudo systemctl enable --now iscsid
#部署jiva卷组件
root@k8s-master01:~kubectl apply -f https://openebs.github.io/charts/jiva-operator.yaml
#jiva卷策略,定义了副本数
root@k8s-master01:~/wordpress# vim openebs-jivavolumepolicy-mysql.yaml
apiVersion: openebs.io/v1alpha1
kind: JivaVolumePolicy
metadata:
name: jivavolumepolicy-mysql
namespace: openebs
spec:
replicaSC: openebs-hostpath
target:
# This sets the number of replicas for high-availability
# replication factor <= no. of (CSI) nodes
replicationFactor: 2
root@k8s-master01:~/wordpress#kubectl apply -f openebs-jivavolumepolicy-mysql.yaml
root@k8s-master01:~/wordpress# cat openebs-jiva-csi-storageclass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: openebs-jiva-csi-mysql
provisioner: jiva.csi.openebs.io
#允许卷扩展
allowVolumeExpansion: true
parameters:
#定义数据引擎为jiva
cas-type: "jiva"
#策略为刚定义的卷策略文件openebs-jivavolumepolicy-mysql中name字段的名称
policy: "jivavolumepolicy-mysql"
root@k8s-master01:~/wordpress#kubectl apply -f openebs-jiva-csi-storageclass.yaml
root@k8s-master01:~/wordpress# cat openebs-jiva-csi-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: openebs-jiva-csi-pvc-mysql
spec:
#sc中的name的值
storageClassName: openebs-jiva-csi-mysql
#定义读写模式为多路只读
accessModes:
- ReadWriteOnce
#定义资源需求为5Gi
resources:
requests:
storage: 5Gi
root@k8s-master01:~/wordpress#kubectl apply -f openebs-jiva-csi-storageclass.yaml
root@k8s-master01:~/wordpress# vim mysql-jiva.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: mysql
name: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: mysql
spec:
containers:
- image: mysql:8.0
name: mysql
#定义挂载/var/lib/mysql的数据在local-storage卷上
volumeMounts:
- mountPath: /var/lib/mysql
name: local-storage
env:
- name: MYSQL_RANDOM_ROOT_PASSWORD
value: '1'
- name: MYSQL_DATABASE
value: 'wordpress'
- name: MYSQL_USER
value: 'wpuser'
- name: MYSQL_PASSWORD
value: '123456'
resources:
requests:
memory: "64Mi"
cpu: "200m"
limits:
memory: "512Mi"
cpu: "1"
#定义local-storage的卷
volumes:
- name: local-storage
#定义请求的PVC名称
persistentVolumeClaim:
claimName: openebs-jiva-csi-pvc-mysql
status: {}
root@k8s-master01:~/wordpress#kubectl apply -f mysql-jiva.yaml
root@k8s-master01:~/wordpress# kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql-jiva-6c7dd7f9dd-kzmjg 1/1 Running 0 138m
2、部署Wordpress,将数据存储在nfs-csi卷上;
#创建nfs名称空间
root@k8s-master01:~/wordpress#kubectl create namespace nfs
root@k8s-master01:~/wordpress#kubectl create -f https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/master/deploy/example/nfs-provisioner/nfs-server.yaml -n nfs
#使用代理方式部署nfs-server驱动
root@k8s-master01:~/wordpress#kubectl create -f https://mirror.ghproxy.com/https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/master/deploy/example/nfs-provisioner/nfs-server.yaml -n nfs
root@k8s-master01:~/wordpress#curl -skSL https://mirror.ghproxy.com/https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/v4.5.0/deploy/install-driver.sh | bash -s v4.5.0 --
root@k8s-master01:~/wordpress# cat storageclass-nfs.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-csi
annotations:
storageclass.kubernetes.io/is-defalut: "true"
provisioner: nfs.csi.k8s.io
parameters:
#名称空间为nfs
server: nfs-server.nfs.svc.cluster.local
share: /
# csi.storage.k8s.io/provisioner-secret is only needed for providing mountOptions in DeleteVolume
# csi.storage.k8s.io/provisioner-secret-name: "mount-options"
# csi.storage.k8s.io/provisioner-secret-namespace: "default"
#重新申请策略为删除
reclaimPolicy: Delete
#卷绑定策略为立即
volumeBindingMode: Immediate
mountOptions:
- nfsvers=4.1
root@k8s-master01:~/wordpress# cat wordpress-pvc-nfs-csi-dynamic.yaml
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wordpress-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 2Gi
#定义SC文件的name的值
storageClassName: nfs-csi
root@k8s-master01:~/wordpress# cat wordpress-nfspv.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: wordpress
name: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: wordpress
spec:
containers:
- image: wordpress:5.7
name: wordpress
#定义将wordpress Pod中 /var目录挂载在 wordpressdata卷上
volumeMounts:
- name: wordpressdata
mountPath: /var
env:
- name: WORDPRESS_DB_HOST
value: '10.244.1.45'
- name: WORDPRESS_DB_NAME
value: 'wordpress'
- name: WORDPRESS_DB_USER
value: 'wpuser'
- name: WORDPRESS_DB_PASSWORD
value: '123456'
volumes:
- name: wordpressdata
#定义PVC的名称
persistentVolumeClaim:
claimName: wordpress-pvc
status: {}
root@k8s-master01:~/wordpress# kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql-jiva-6c7dd7f9dd-kzmjg 1/1 Running 0 162m
wordpress-nfs-5f4dcd9f7c-j2wvq 1/1 Running 0 46s
3、使用Secret配置MySQL,提供几个敏感数据;同时使用ConfigMap为MySQL提供配置文件,设定使用UTF8字符集,并且每表使用单独表空间;
innodb_file_per_table = ON
#将mysql字符集设置为utf8mb4,每表使用单独表空间的配置以命令式指令方式测试运行,并写入 cm-mysql-cfg.yaml文件
root@k8s-master01:~/wordpress#kubectl create configmap mysql-cfg --from-literal=character_set_server=utf8mb4 --from-literal=innodb_file_per_table=ON --dry-run=client -o yaml > /root/wordpress/cm-mysql-cfg.yaml
#部署myql的configmap
root@k8s-master01:~/wordpress#kubectl apply -f /root/wordpress/cm-mysql-cfg.yaml
#将mysql的root密码,用户密码,数据库名称,用户名称 以 base64编码保存为 mysql-secret.yaml文件
root@k8s-master01:~/wordpress#kubectl create secret generic mysql-secret --from-literal=root.pass='magedu.com' --from-literal=wp.user='wpuser' --from-literal=wp.pass='WPp@ss' --from-literal=db.name='wpdb' --dry-run=client -o yaml > mysql-secret.yaml
root@k8s-master01:~/wordpress#kubectl apply -f mysql-secret.yam
root@k8s-master01:~/wordpress# cat mysql-cm.yaml
apiVersion: v1
kind: Pod
metadata:
name: mysql
spec:
containers:
- name: mysql
image: mysql:8.0
env:
#环境变量方式引用secret文件
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
#secret的名称
name: mysql-secret
#sercret中的键
key: root.pass
#不可选,必须使用
optional: false
- name: MYSQL_DATABASE
valueFrom:
secretKeyRef:
name: mysql-secret
key: db.name
optional: false
- name: MYSQL_USER
valueFrom:
secretKeyRef:
name: mysql-secret
key: wp.user
optional: false
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: wp.pass
optional: false
- name: CHARACTER
valueFrom:
configMapKeyRef:
name: mysql-cfg
key: character_set_server
optional: false
- name: innodb_file_per_table
valueFrom:
configMapKeyRef:
name: mysql-cfg
key: innodb_file_per_table
optional: false
root@k8s-master01:~/wordpress# kubectl apply -f mysql-cm.yaml
#wordpress同样以环境变量引用
root@k8s-master01:~/wordpress# cat wordpress-cm.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: wordpress
name: wordpress-cm
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: wordpress
spec:
containers:
- image: wordpress:5.7
name: wordpress-cm
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 5
periodSeconds: 5
readinessProbe:
httpGet:
path: '/'
port: 80
scheme: HTTP
initialDelaySeconds: 15
timeoutSeconds: 2
periodSeconds: 5
failureThreshold: 3
#restartPolicy: Always
env:
- name: WORDPRESS_DB_HOST
value: '10.244.3.12'
- name: WORDPRESS_DB_NAME
#value: 'wordpress'
valueFrom:
secretKeyRef:
name: mysql-secret
key: db.name
optional: false
- name: WORDPRESS_DB_USER
valueFrom:
secretKeyRef:
name: mysql-secret
key: wp.user
optional: false
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: wp.pass
optional: false
resources:
requests:
memory: "90Mi"
cpu: "1"
limits:
memory: "300Mi"
cpu: "1"
status: {}
root@k8s-master01:~/wordpress# kubectl apply -f wordpress-cm.yaml
root@k8s-master01:~/wordpress# kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql 1/1 Running 0 24h
mysql-7c47d655ff-ts44b 1/1 Running 0 3d2h
mysql-jiva-6c7dd7f9dd-kzmjg 1/1 Running 0 4h38m
redis-with-openebs-local-hostpath 1/1 Running 0 4d2h
wordpress-5f4dcd9f7c-6gxw8 1/1 Running 0 2d2h
wordpress-cm-57fc474c87-sqmth 1/1 Running 0 19m
wordpress-nfs-5f4dcd9f7c-j2wvq 1/1 Running 0 116m
评论(0)