k8s基础实战之部署wordpress博客系统

1 创建wordpress使用的pv

[root@k8s-master01 wordpress]# cat worldpress_pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
spec:
  capacity:
    storage: 6Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: /data/k8s-volume/wordpress/mysql
    server: 192.168.10.72
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: wp-pv01
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: /data/k8s-volume/wordpress/nginx
    server: 192.168.10.72
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: wp-pv02
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: /data/k8s-volume/wordpress/nginx
    server: 192.168.10.72

执行

[root@k8s-master01 wordpress]# kubectl apply -f worldpress_pv.yaml 
persistentvolume/mysql-pv created
persistentvolume/wp-pv01 created
persistentvolume/wp-pv02 created

查看创建的pv
[root@k8s-master01 wordpress]# kubectl get pv |egrep "mysql|wp"
mysql-pv                                   6Gi       RWX            Retain           Available                                                                              100s
wp-pv01                                    5Gi        RWX            Retain           Available                                                                              100s
wp-pv02                                    5Gi        RWX            Retain           Available                                                                              100s

2 部署wordpress使用的mysql

cat mysql_wordpress.yaml 
apiVersion: v1
kind: Service
metadata:
  name: wordpress-mysql
  namespace: blog
  labels:
    app: wordpress
spec:
  ports:
    - port: 3306
  selector:
    app: wordpress
    tier: mysql
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  namespace: blog
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 6Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-mysql
  namespace: blog
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: mysql
    spec:
      containers:
      - name: mysql
    spec:
      containers:
      - name: mysql
        image: daocloud.io/library/mysql:5.7
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - mountPath: "/var/lib/mysql"
          name: mysql-data
      volumes:
      - name: mysql-data
        persistentVolumeClaim:
          claimName: mysql-pv-claim
创建mysql的secret
kubectl create secret generic mysql-pass --from-literal=password=123456 -n blog

创建mysql的pod
[root@k8s-master01 wordpress]# kubectl apply -f mysql_wordpress.yaml 

查看创建的pod
[root@k8s-master01 wordpress]# kubectl get pods -n blog
NAME                              READY   STATUS    RESTARTS   AGE
wordpress-mysql-d44c49788-xgvj4   1/1     Running   0          25m
查看services
 kubectl get svc -n blog
NAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
wordpress-mysql   ClusterIP   10.100.16.241   <none>        3306/TCP   25m

3 构建nginx,php镜像并上传至harbor

关于搭建harbor的步骤如https://devopstack.cn/k8s/1301.html

3.1 构建nginx镜像上传harbor

[root@k8s-master01 nginx]# ll
总用量 1048
-rw-r--r-- 1 root root     399 6月   9 14:46 Dockerfile
-rw-r--r-- 1 root root 1061461 5月  25 23:34 nginx-1.20.1.tar.gz
-rw-r--r-- 1 root root     863 6月   9 14:46 nginx.conf

nginx dockerfile如下

[root@k8s-master01 nginx]# cat Dockerfile 
FROM centos:7
MAINTAINER devopstack.cn
RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel && \
    yum clean all
ADD nginx-1.20.1.tar.gz /tmp
RUN cd /tmp/nginx-1.20.1 && \
    ./configure --prefix=/usr/local/nginx && \
    make -j 2 && make install && \
    rm -rf /tmp/nginx-1.20.1
COPY nginx.conf /usr/local/nginx/conf

EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]

构建镜像
[root@k8s-master01 nginx]# docker build -t 192.168.10.20:8081/lnmp/nginx:latest .

如下图,已上传至harbor仓库
k8s基础实战之部署wordpress博客系统

3.2 构建php镜像上传harbor

wget https://www.php.net/distributions/php-7.2.34.tar.gz
cat Dockerfile 
FROM centos:7
MAINTAINER  devopstack.cn
RUN yum install -y gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel
ENV VERSION=7.2.34
ADD php-${VERSION}.tar.gz /tmp/

RUN cd /tmp/php-${VERSION} && \
    ./configure --prefix=/usr/local/php \
    --with-config-file-path=/usr/local/php/etc \
    --with-mysql --with-mysqli \
    --with-openssl --with-zlib --with-curl --with-gd \
    --with-jpeg-dir --with-png-dir --with-iconv \
    --enable-fpm --enable-zip --enable-mbstring && \
    make -j 4 && \
    make install && \
    cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && \
    cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf && \
    sed -i "s/127.0.0.1/0.0.0.0/" /usr/local/php/etc/php-fpm.d/www.conf && \
    sed -i "21a \daemonize = no" /usr/local/php/etc/php-fpm.conf && \
    cp  php.ini-production /usr/local/php/etc/php.ini

RUN rm -rf /tmp/php-${VERSION}* && yum clean all

WORKDIR /usr/local/php
EXPOSE 9000
CMD ["./sbin/php-fpm", "-c", "/usr/local/php/etc/php-fpm.conf"]

构建镜像
 docker build -t 192.168.10.20:8081/lnmp/php:latest .

上传harbor仓库
[root@gitlab php]# docker push 192.168.10.20:8081/lnmp/php:latest
 如下图 已上传至harbor
k8s基础实战之部署wordpress博客系统

4 部署php环境

创建k8s访问harbor的认证信息
 kubectl create secret docker-registry  dockerpullauth --docker-username=admin --docker-password=Harbor12345 --docker-server=192.168.10.20:8081

 cat worldpress_php.yaml 
apiVersion: v1
kind: Service
metadata:
  name: wordpress-php
  namespace: blog
  labels:
    app: wordpress
spec:
  ports:
    - port: 9000
  selector:
    app: wordpress-php
    tier: frontend
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp-pvc01
  namespace: blog
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-php
  namespace: blog
  labels:
    app: wordpress
spec:
  replicas: 3
  selector:
    matchLabels:
      app: wordpress-php
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress-php
        tier: frontend
    spec:
      imagePullSecrets:
      - name: dockerpullauth
      containers:
      - name: php
        image: 192.168.10.20:8081/lnmp/php:latest
        ports:
        - containerPort: 9000
          name: wordpress
        volumeMounts:
        - name: php-data
          mountPath: "/usr/local/nginx/html"
      volumes:
      - name: php-data
        persistentVolumeClaim:
          claimName: wp-pvc01
[root@k8s-master01 wordpress]# kubectl apply -f worldpress_php.yaml 
service/wordpress-php created
persistentvolumeclaim/wp-pvc01 created
deployment.apps/wordpress-php created

查看创建的pod和svc
[root@k8s-master01 wordpress]# kubectl get pods -n blog
NAME                              READY   STATUS    RESTARTS   AGE
wordpress-mysql-d44c49788-xgvj4   1/1     Running   0          30h
wordpress-php-78f9c97bcf-bmwfx    1/1     Running   0          64s
wordpress-php-78f9c97bcf-mpnxs    1/1     Running   0          64s
wordpress-php-78f9c97bcf-v59w9    1/1     Running   0          64s
[root@k8s-master01 wordpress]# kubectl get svc -n blog
NAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
wordpress-mysql   ClusterIP   10.100.16.241   <none>        3306/TCP   30h
wordpress-php     ClusterIP   10.105.30.204   <none>        9000/TCP   83s

查看pv绑定 
[root@k8s-master01 wordpress]# kubectl get pv |grep blog
pv00003                                    6Gi        RWX            Recycle          Bound       blog/mysql-pv-claim                                                        57d
wp-pv01                                    5Gi        RWX            Retain           Bound       blog/wp-pvc01                                                              30h

5 部署nginx环境

 cat worldpress_nginx.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-wp-config
  namespace: blog
data:
  site.conf: |-
    server {
        listen 80;
        server_name localhost;
        root html;
        index index.php index.html;
        location ~ \.php$ {
            root html;
            fastcgi_pass wordpress-php:9000;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
            include fastcgi_params;
        }
    }
---
apiVersion: v1
kind: Service
metadata:
  name: wordpress-nginx
  namespace: blog
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress-nginx
    tier: frontend
  type: NodePort
  sessionAffinity: ClientIP
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp-pvc03
  namespace: blog
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-nginx
  namespace: blog
  labels:
    app: wordpress
spec:
  replicas: 3
  selector:
    matchLabels:
      app: wordpress-nginx
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress-nginx
        tier: frontend
    spec:
      imagePullSecrets:
      - name: dockerpullauth 
      containers:
      - name: nginx
        image: 192.168.10.20:8081/lnmp/nginx:latest 
        ports:
        - containerPort: 80
          name: wordpress
        volumeMounts:
        - mountPath: "/usr/local/nginx/html"
          name: nginx-data
        - mountPath: "/usr/local/nginx/conf/vhost/site.conf"
          name: config
          subPath: site.conf
      volumes:
      - name: nginx-data
        persistentVolumeClaim:
          claimName: wp-pvc03
      - name: config
        configMap:
          name: nginx-wp-config

启动nginx pod
[root@k8s-master01 wordpress]# kubectl apply -f worldpress_nginx.yaml 
configmap/nginx-wp-config created
service/wordpress-nginx created
persistentvolumeclaim/wp-pvc02 created
deployment.apps/wordpress-nginx created

查看nginx pod 
[root@k8s-master01 wordpress]# kubectl get services  -n blog 
NAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
wordpress-mysql   ClusterIP   10.100.16.241   <none>        3306/TCP       33h
wordpress-nginx   NodePort    10.99.98.133    <none>        80:30048/TCP   15s
wordpress-php     ClusterIP   10.105.30.204   <none>        9000/TCP       3h18m

查看nginx  pv
[root@k8s-master01 wordpress]# kubectl get persistentvolumes |grep blog
pv00003                                    6Gi        RWX            Recycle          Bound      blog/mysql-pv-claim                                                        57d
wp-pv01                                    5Gi        RWX            Retain           Bound      blog/wp-pvc01                                                              33h
wp-pv03                                    5Gi        RWX            Retain           Bound      blog/wp-pvc03                                                              104s

查看nginx pvc

[root@k8s-master01 wordpress]# kubectl get pvc -n blog
NAME             STATUS   VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-pv-claim   Bound    pv00003   6Gi        RWX                           33h
wp-pvc01         Bound    wp-pv01   5Gi        RWX                           3h25m
wp-pvc03         Bound    wp-pv03   5Gi        RWX                           101s

查看nginx pods
[root@k8s-master01 wordpress]# kubectl get pods -n blog
NAME                              READY   STATUS    RESTARTS   AGE
wordpress-mysql-d44c49788-xgvj4   1/1     Running   0          33h
wordpress-nginx-98fc79d7b-hqzzg   1/1     Running   0          8s
wordpress-nginx-98fc79d7b-hzsjk   1/1     Running   0          8s
wordpress-nginx-98fc79d7b-rtvbz   1/1     Running   0          8s
wordpress-php-78f9c97bcf-bmwfx    1/1     Running   0          3h30m
wordpress-php-78f9c97bcf-mpnxs    1/1     Running   0          3h30m
wordpress-php-78f9c97bcf-v59w9    1/1     Running   0          3h30m

6 创建wordpress对应的mysql数据库

[root@k8s-master01 wordpress]# kubectl get pods -n blog |grep mysql
NAME                              READY   STATUS    RESTARTS   AGE
wordpress-mysql-d44c49788-xgvj4   1/1     Running   0          33h

[root@k8s-master01 wordpress]# kubectl exec -it wordpress-mysql-d44c49788-xgvj4 bash -n blog
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
root@wordpress-mysql-d44c49788-xgvj4:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> create database wordpress;
Query OK, 1 row affected (0.01 sec)

7 部署wordpress代码

在 nfs服务器对应目录上 
cd /data/k8s-volume/wordpress/nginx
wget https://wordpress.org/wordpress-5.7.2.tar.gz
tar -xf wordpress-5.7.2.tar.gz 

访问如下地址
http://192.168.10.72:30493/wordpress/wp-admin/setup-config.php
k8s基础实战之部署wordpress博客系统

配置相关信息如下

k8s基础实战之部署wordpress博客系统

下一步如下,然后就配置好一个wordpress博客了

k8s基础实战之部署wordpress博客系统

如图 博客已搭建好

k8s基础实战之部署wordpress博客系统
  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: