云原生之kubernetes实战在k8s集群下部署wordpress

Posted 江湖有缘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了云原生之kubernetes实战在k8s集群下部署wordpress相关的知识,希望对你有一定的参考价值。

【云原生之kubernetes实战】在k8s集群下部署wordpress

一、wordpress介绍

1.wordpress简介

WordPress是一款免费开源的内容管理系统(CMS),它是使用php语言和mysql数据库开发的,用户可以在支持 PHP 和 MySQL数据库的服务器上使用自己的博客。

2.wordpress的优点

(1)WordPress如今已经占据全球web的35%以上了。
(2)WordPress适合海外买家的使用习惯。
(3)WordPress容易获取谷歌排名。
(4)WordPress针对移动用户进行了优化。
(5)WordPress安全可靠。
(6)安装简单、操作方便。

3.wordpress使用场景

商业网站
电子商务商店
博客
作品集网站
简历
论坛
社交网络
会员网站
……

二、检查本地kubernetes集群状态

[root@k8s-master ~]# kubectl get nodes -owide
NAME         STATUS   ROLES                  AGE     VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION          CONTAINER-RUNTIME
k8s-master   Ready    control-plane,master   3d16h   v1.23.1   192.168.3.201   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   containerd://1.6.6
k8s-node01   Ready    <none>                 3d16h   v1.23.1   192.168.3.202   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   containerd://1.6.6
k8s-node02   Ready    <none>                 3d16h   v1.23.1   192.168.3.203   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   containerd://1.6.6

三、配置nfs共享存储

1.安装nfs

 yum install -y nfs-utils

2.创建共享目录

mkdir -p /nfs/data

3.配置共享目录

echo "/nfs/data/ *(insecure,rw,sync,no_root_squash)" > /etc/exports

4.使配置生效

exportfs -r

5.重启nfs相关服务

①设置nfs服务开机启动

 systemctl enable --now rpcbind
 systemctl enable --now  nfs-server

②重启nfs服务

service rpcbind stop
service nfs stop
service rpcbind start
service nfs start

6.其他节点检查nfs共享

[root@k8s-node01 ~]#  showmount -e 192.168.3.201
Export list for 192.168.3.201:
/nfs/data *

四、安装wordpress数据库

1.创建命名空间

[root@k8s-master ~]# kubectl create namespace  wordpress
namespace/wordpress created

2.创建数据库存储目录

mkdir -p /nfs/data/wordpressdb

3.编写wordpressdb.yaml文件

[root@k8s-master wordpress]# cat wordpressdb.yaml 
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: wordpressdb-pv
spec:
  capacity:   #创建存储的大小
    storage: 10Gi
  volumeMode: Filesystem  #存储的类型
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: /nfs/data/wordpressdb
    server: 192.168.3.201
  mountOptions:
    - hard
    - nfsvers=4.1
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wordpressdb-pvc
  namespace: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  volumeMode: Filesystem   #存储类型 ,块存储Block
  resources:
    requests:
      storage: 8Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: wordpressdb
  name: wordpressdb
  namespace: wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpressdb
  strategy: 
  template:
    metadata:
      labels:
        app: wordpressdb
    spec:
      volumes:
      - name: wordpressdb-data
        persistentVolumeClaim:
          claimName: wordpressdb-pvc
      containers:
      - image: mysql:5.7
        name: mysql
        ports:
        - name: mysql
          containerPort: 3306
          protocol: TCP
        volumeMounts:
        - name: wordpressdb-data
          mountPath: /var/lib/mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: wordpress
        - name: MYSQL_DATABASE
          value: wordpress
        - name:  MYSQL_USER
          value: wordpress
        - name:  MYSQL_PASSWORD
          value: wordpress
        resources:         
          requests:
             cpu: 0.1
             memory: 128Mi
          limits:
             cpu: 0.3
             memory:  512Mi

---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: wordpressdb
  name: wordpressdb
  namespace: wordpress
spec:
  clusterIP: 10.110.221.111
  ports:
  - name: 3306-3306
    port: 3306
    protocol: TCP
    targetPort: 3306
  selector:
    app: wordpressdb
  type: ClusterIP
status:
  loadBalancer: 


4.创建数据库

[root@k8s-master wordpress]# kubectl apply -f wordpressdb.yaml 
persistentvolume/wordpressdb-pv created
persistentvolumeclaim/wordpressdb-pvc created
deployment.apps/wordpressdb created
service/wordpressdb created

5.查看wordpressdb的pod

[root@k8s-master wordpress]# kubectl get pods -n wordpress 
NAME                          READY   STATUS    RESTARTS   AGE
wordpressdb-54745c765-gtv79   1/1     Running   0          2m8s

五、测试登录数据库

1.查看数据库的svc

[root@k8s-master ~]# kubectl get svc -n wordpress 
NAME          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
wordpressdb   ClusterIP   10.110.221.111   <none>        3306/TCP   38s

2.登录数据库

[root@k8s-master wordpress]# mysql -uwordpress  -pwordpress -h10.110.221.111
Welcome to the MariaDB monitor.  Commands end with ; or \\g.
Your MySQL connection id is 5
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

MySQL [(none)]> 

3.查看数据库列表

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| wordpress          |
+--------------------+
2 rows in set (0.01 sec)


六、安装wordpress

1.编写wordpress.yaml文件

[root@k8s-master wordpress]# cat wordpress.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: wordpress
  name: wordpress
  namespace: wordpress
spec:
  replicas: 2
  selector:
    matchLabels:
      app: wordpress
  strategy: 
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - image: wordpress:php8.0-apache
        name: wordpress
        env:
        - name: WORDPRESS_DB_HOST
          value: 10.110.221.111
        - name: WORDPRESS_DB_USER
          value: root
        - name: WORDPRESS_DB_PASSWORD
          value: wordpress
        - name: WORDPRESS_DB_NAME
          value: wordpress
        resources: 
~                     
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: wordress
  name: wordress
  namespace: wordpress   
spec:
  ports:
  - name: 80-80
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30088
  selector:
    app: wordpress
  type: NodePort
status:
  loadBalancer: 


2.安装wordpress

[root@k8s-master wordpress]# kubectl apply -f wordpress.yaml 

3.检查pod状态

[root@k8s-master wordpress]# kubectl get pods -n wordpress 
NAME                          READY   STATUS    RESTARTS   AGE
wordpress-7db67c5bd8-7nblw    1/1     Running   0          9s
wordpress-7db67c5bd8-hfc86    1/1     Running   0          9s
wordpressdb-54745c765-gtv79   1/1     Running   0          4m33s


七、访问wordpress的web

1.查看svc

[root@k8s-master ~]# kubectl get svc -n wordpress 
NAME          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
wordpressdb   ClusterIP   10.110.221.111   <none>        3306/TCP       4m53s
wordress      NodePort    10.109.18.248    <none>        80:30088/TCP   29s



2.设置wordpress登录账号

http://192.168.3.202:30088

3.登录web


以上是关于云原生之kubernetes实战在k8s集群下部署wordpress的主要内容,如果未能解决你的问题,请参考以下文章

云原生之kubernetes实战在k8s下部署Redis集群

云原生之kubernetes实战在k8s集群下部署portainer-k8s平台

云原生之kubernetes实战在k8s集群下部署Weave Scope监控平台

云原生之kubernetes实战在k8s集群下部署ingress对外访问服务

云原生之kubernetes实战在k8s环境下部署Heimdall导航页

云原生之kubernetes实战使用helm在k8s集群下部署DataEase可视化分析平台