k8s-wordpress 部署

一、环境准备

1、安装NFS,部署存储用户持久化存储(动态存储PVC)

2、私有仓库harbor

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

# 构建镜像
docker build -t php:v1 -f Dockerfile-php .
docker build -t nginx:v1 -f Dockerfile-nginx .

# 镜像打标签
docker tag nginx:v1 192.168.0.212/test/nginx:v1
docker tag php:v1 192.168.0.212/test/php:v1

# 上传镜像到harbor
docker push 192.168.0.212/test/nginx:v1
docker push 192.168.0.212/test/php:v1

4、创建secret 保持访问 harbor仓库认证信息

kubectl create secret docker-registry registry-pull-secret --docker-username=admin --docker-password=123456 --docker-email=123@qq.com --docker-server=192.168.0.212

二、部署MySQL

1、创建wordpress-mysql

# mysql-deployment.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: wordpress-mysql
  labels:
    app: wp-mysql
spec:
  ports:
    - port: 3306
  selector:
    app: wp-mysql

---
apiVersion: v1
kind: Secret
metadata:
  name: mysql-pass
data:
  password: MTIzNDU2
type: Opaque

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc-mysql
  labels:
    app: wp-mysql
spec:
  storageClassName: "managed-nfs-storage"
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 10Gi

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-mysql
  labels:
    app: wp-mysql
spec:
  selector:
    matchLabels:
      app: wp-mysql
  template:
    metadata:
      labels:
        app: wp-mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.6
        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: my-pvc-mysql

2、创建mysql容器

kubectl create -f mysql-deployment.yaml

3、进入mysql pod 创建wordpress数据库

kubectl exec -it wordpress-mysql-797c87559c-h2cp6 sh

mysql -uroot -p
Enter password:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

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

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

三、部署PHP

1、创建php-deployment

# php-deployment.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: php
  labels:
    app: wp-php
spec:
  ports:
  - port: 9000
  selector:
    app: wp-php

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php
  labels:
    app: wp-php
spec:
  replicas: 3
  selector:
    matchLabels:
      app: wp-php
  template:
    metadata:
      labels:
        app: wp-php
    spec:
      imagePullSecrets:
      - name: registry-pull-secret
      containers:
      - name: php
        image: 192.168.0.212/test/php:v1
        ports:
        - containerPort: 9000
          name: wp-php
        volumeMounts:
        - name: nginx-data
          mountPath: /usr/local/nginx/html
      volumes:
      - name: nginx-data
        persistentVolumeClaim:
          claimName: my-pvc-nginx  

2、创建php容器

kubectl create -f php-deployment.yaml

四、部署nginx

1、创建nginx-deployment

# nginx-deployment.yaml

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  site.conf: |-
    server {
        listen 80;
        server_name localhost;
        root html;
        index index.php index.html;

        location ~ \.php$ {
            root html;
            fastcgi_pass php:9000;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-wp-service
  labels:
    app: wp-nginx
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: wp-nginx
  type:
    NodePort

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc-nginx
  labels:
    app: wp-nginx
spec:
  storageClassName: "managed-nfs-storage"
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 5Gi

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: wp-nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: wp-nginx
  template:
    metadata:
      labels:
        app: wp-nginx
    spec:
      imagePullSecrets:
      - name: registry-pull-secret
      containers:
      - name: nginx
        image: 192.168.0.212/test/nginx:v1
        ports:
        - containerPort: 80
          name: wp-nginx
        volumeMounts:
        - name: nginx-data
          mountPath: /usr/local/nginx/html
        - name: config
          mountPath: /usr/local/nginx/conf/vhost/site.conf
          subPath: site.conf
      volumes:
      - name: nginx-data
        persistentVolumeClaim:
          claimName: my-pvc-nginx
      - name: config
        configMap:
          name: nginx-config

2、创建nginx容器

kubectl create -f nginx-deployment.yaml

五、部署wordpress

1、下载wordpress

# 在nfs宿主机找到挂载卷,下载wordpress
cd /opt/k8s/default-my-pvc-nginx-pvc-af09752d-6226-492d-b6d8-fecb745937bd

wget https://cn.wp.xz.cn/latest-zh_CN.tar.gz

# 解压
tar zxvf latest-zh_CN.tar.gz

2、修改wp-config.php

cp wp-config-sample.php wp-config.php

# 修改数据库名称、用户名、密码、主机
sed -i 's#database_name_here#wordpress#g' wp-config.php
sed -i 's#username_here#root#g' wp-config.php
sed -i 's#password_here#123456#g' wp-config.php
sed -i 's#localhost#wordpress-mysql#g' wp-config.php

3、安装wordpress

  • 3.1 打开url: http://ip:30319 进行安装即可

版权声明:如无特殊说明,文章均为本站原创,转载请注明出处

本文链接:http://jiazone.cn:8848/article/2/