应用介绍
部署考虑
镜像下载
docker pull mysql:5.7.34 Docker pull wordpress:4.8-apache
mysql的部署
service文件配置
apiVersion v1 kind Service metadata name mysql-sts labels app mysql-sts spec portsport3306 selector app mysql-sts clusterIP None
Secret文件配置
apiVersion v1 kind Secret metadata name mysecret type Opaque data password MTIzNDU2 username cm9vdA== #user/pass=root/123456
StatefulSet 文件配置
apiVersion apps/v1 kind StatefulSet metadata name mysql-sts spec serviceName"mysql-sts" replicas1 volumeClaimTemplatesmetadata name mysql-local-storage annotations volume.beta.kubernetes.io/storage-class"nfs-csi" spec accessModes"ReadWriteOnce" resources requests storage 25Gi selector matchLabels app mysql-sts template metadata labels app mysql-sts spec nodeSelector wordpress mysql containersimage mysql5.7.34 name mysql envname MYSQL_ROOT_PASSWORD valueFrom secretKeyRef name mysecret key password #- name: SECRET_USERNAME# valueFrom:# secretKeyRef:# name: mysecret# key: username portscontainerPort3306 name mysql volumeMountsname mysql-local-storage readOnlyfalse mountPath /var/lib/mysql initContainersname init-wordpress-mysql image busybox imagePullPolicy IfNotPresent command'sh''-c''rm -rf /data/*''until nslookup mysql-sts; do echo waiting for mysql-sts; sleep 2; done;' volumeMountsname mysql-local-storage readOnlyfalse mountPath /data
配置说明:
volumeClaimTemplatesmetadata name mysql-local-storage annotations volume.beta.kubernetes.io/storage-class"nfs-csi" spec accessModes"ReadWriteOnce" resources requests storage 25Gi
volumeClaimTemplates:PVC 的配置模块,存储类型是nfs, 驱动插件为 CSI
accessModes: ReadWriteOnce 模式
spec nodeSelector wordpress mysql containersimage mysql5.7.34 name mysql envname MYSQL_ROOT_PASSWORD valueFrom secretKeyRef name mysecret key password
* spec.nodeSelector: 节点选择器。 作用:当前pod会部署到,含有wordpress=mysql标签的node。
如果让pod部署到指定的node, 可以使用nodeSelector指定标签。前提需要在node 节点上创建label。命令: kubectl label node node-name label-key=label-value。
* spec.env容器中使用的环境变量配置, 由Secret文件配置
完整配置
mysql-sts.yaml
apiVersion v1 kind Service metadata name mysql-sts labels app mysql-sts spec portsport3306 selector app mysql-sts clusterIP None ---apiVersion v1 kind Secret metadata name mysecret type Opaque data password MTIzNDU2 username cm9vdA== #user/pass=root/123456---apiVersion apps/v1 kind StatefulSet metadata name mysql-sts spec serviceName"mysql-sts" replicas1 volumeClaimTemplatesmetadata name mysql-local-storage annotations volume.beta.kubernetes.io/storage-class"nfs-csi" spec accessModes"ReadWriteOnce" resources requests storage 25Gi selector matchLabels app mysql-sts template metadata labels app mysql-sts spec nodeSelector wordpress mysql containersimage mysql5.7.34 name mysql envname MYSQL_ROOT_PASSWORD valueFrom secretKeyRef name mysecret key password #- name: SECRET_USERNAME# valueFrom:# secretKeyRef:# name: mysecret# key: username portscontainerPort3306 name mysql volumeMountsname mysql-local-storage readOnlyfalse mountPath /var/lib/mysql initContainersname init-wordpress-mysql image busybox imagePullPolicy IfNotPresent command'sh''-c''rm -rf /data/*''until nslookup mysql-sts; do echo waiting for mysql-sts; sleep 2; done;' volumeMountsname mysql-local-storage readOnlyfalse mountPath /data
安装mysql
kubectl create ns wp # 创建命名空间kubectl apply -f mysql-sts.yaml -n wp # 部署mysql
部署wordpress
PVC配置
apiVersion v1 kind PersistentVolumeClaim metadata name pvc-wp spec storageClassName nfs-csi # 存储类型为nfs, 驱动插件为CSI accessModes# 访问模式:支持多个service 读写 ReadWriteMany resources requests storage 12G
Service 配置
apiVersion v1 kind Service metadata name wordpress labels app wordpress spec portsport80 targetPort80 nodePort30180 selector app wordpress type NodePort # 类型为NodePort
ConfigMap 配置定义
apiVersion v1 data db-host mysql-sts # 使用mysql的serviceName作为hostkind ConfigMap metadata name mycm
Deployment 配置定义
apiVersion apps/v1 kind Deployment metadata name wordpress labels app wordpress spec selector matchLabels app wordpress template metadata labels app wordpress spec nodeSelector wordpress mysql containersimage wordpress 4.8-apache name wordpress imagePullPolicy IfNotPresent envname WORDPRESS_DB_HOST valueFrom configMapKeyRef name mycm key db-host # 读取configmap中定义的hostname WORDPRESS_DB_USER valueFrom secretKeyRef name mysecret # 使用mysecret配置,参考 mysql-sts.yaml 中定义的 Secret key username name WORDPRESS_DB_PASSWORD valueFrom secretKeyRef name mysecret # 使用mysecret配置,参考 mysql-sts.yaml 中定义的 Secret key password portscontainerPort80 name wordpress resources requests cpu0.5 volumeMountsname wordpress-local-storage readOnlyfalse mountPath /var/www/html # 容器内部的卷path volumesname wordpress-local-storage persistentVolumeClaim# 使用的PVC 为 pvc-wp claimName pvc-wp
完整的配置
wp-deploy.yaml
apiVersion v1 kind PersistentVolumeClaim metadata name pvc-wp spec storageClassName nfs-csi accessModes ReadWriteMany resources requests storage 12G ---apiVersion v1 kind Service metadata name wordpress labels app wordpress spec portsport80 targetPort80 nodePort30180 selector app wordpress type NodePort ---apiVersion v1 data db-host mysql-sts # 使用mysql的serviceName作为hostkind ConfigMap metadata name mycm ---apiVersion apps/v1 kind Deployment metadata name wordpress labels app wordpress spec selector matchLabels app wordpress template metadata labels app wordpress spec nodeSelector wordpress mysql containersimage wordpress 4.8-apache name wordpress imagePullPolicy IfNotPresent envname WORDPRESS_DB_HOST valueFrom configMapKeyRef name mycm key db-host name WORDPRESS_DB_USER valueFrom secretKeyRef name mysecret # 使用mysecret配置,参考 mysql-sts.yaml 中定义的 Secret key username name WORDPRESS_DB_PASSWORD valueFrom secretKeyRef name mysecret # # 使用mysecret配置,参考 mysql-sts.yaml 中定义的 Secret key password portscontainerPort80 name wordpress resources requests cpu0.5 volumeMountsname wordpress-local-storage readOnlyfalse mountPath /var/www/html volumesname wordpress-local-storage persistentVolumeClaim claimName pvc-wp
安装 wordpress
kubectl apply -f wp-deploy.yaml -n wp
部署ingress
wp-ingress.yaml 相关配置
apiVersion networking.k8s.io/v1 kind Ingress metadata name ing-wordpress spec ingressClassName nginx ruleshost wp.tuling.com http pathspathType Prefix path"/" backend service name wordpress port number80
注意:
spec.ingressClassName: 需要指定的 部署ingress-nginx 时,创建的ingressClass。
可以通过命令 kubectl get ingressclass , 进行查看。
╰─# kubectl get ingressclass -n ingress-nginx NAME CONTROLLER PARAMETERS AGE nginx k8s.io/ingress-nginx <none> 4d22h ╭─root@k201 ~/k8s/hpa ╰─#
安装 wp-ingress.yaml
kubectl apply -f wp-ingress.yaml -n wp
查看ingress 和 ingress-nginx-controller 是否绑定成功
╰─# kubectl get ingress -n wp NAME CLASS HOSTS ADDRESS PORTS AGE ing-wordpress nginx wp.tuling.com 10.20.143.255 80 102m
查看到 CLASS 和 ADDRESS 绑定了值,说明已创建成功。
使用 `wp.tuling.com` 访问
访问前需要在宿主机上 配置 ip 和 host之间的映射。
如图所示:
访问地址: `http://wp.tuling.com:30180/`
这里的 端口号 `30180` ingress-nginx-controller 的 NodePort 的端口号
通过一下命令查看:
╰─# kubectl get svc -n ingress-nginx NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ingress-nginx-controller NodePort 10.20.143.255 <none> 80:32091/TCP,443:32029/TCP 2d20h ingress-nginx-controller-admission ClusterIP 10.20.129.109 <none> 443/TCP 2d20h ╭─root@k201 ~/k8s/k8s-ingress ╰─#