架构图:

在这里插入图片描述

镜像

首先我们将nginx和php镜像准备好,并且将其推送到镜像仓库中:
自己没有Dockerfile文件得可以下载我的:
https://download.csdn.net/download/qq_25611295/10672202

docker login 192.168.1.40:5000
nginx:
docker build -t  192.168.1.40:5000/lnmp/nginx .
docker push 192.168.1.40:5000/lnmp/nginx

php:
docker bulid -t 192.168.1.40:5000/lnmp/php .
docker push 192.168.1.40:5000/lnmp/php

mysql:
docker pull mysql:5.6
docker tag 1f47fade220d 192.168.1.40/lnmp/mysql
docker push 192.168.1.40:5000/lnmp/mysql

检测仓库:
仓库配置:https://blog.csdn.net/qq_25611295/article/details/80967135

[root@k8s-g1 ~]# curl -u liaochao:123456 192.168.1.40:5000/v2/_catalog
{"repositories":["lnmp/mysql","lnmp/nginx","lnmp/php","nginx","php"]}

认证:

镜像准备好以后我们需要配置一些认证文件,比如,mysql的账户密码,仓库的账户密码等等,我们需要将这些放入k8s的认证中
mysql:
创建mysql密码的认证

kubectl create secret generic mysql-pass --from-literal=password=123456

检测:

[root@master LNMP]# kubectl get secret
NAME                  TYPE                                  DATA      AGE
default-token-5bjhk   kubernetes.io/service-account-token   3         24d
liaochao-https        kubernetes.io/tls                     2         6d
mysql-pass            Opaque                                1         19s

创建仓库认证:
查看认证信息:(bash64 -wo 代表以64位转码展示并且不换行)

cat .docker/config.json |base64 -w0

然后我们根据得到的数据创建认证:

[root@master LNMP]# cat docker-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: registrypullsecret
data:
  .dockerconfigjson: ewoJImF1dGhzIjogewoJCSIxOTIuMTY4LjEuNDA6NTAwMCI6IHsKCQkJImF1dGgiOiAiYkdsaGIyTm9ZVzg2TVRJek5EVTIiCgkJfQoJfSwKCSJIdHRwSGVhZGVycyI6IHsKCQkiVXNlci1BZ2VudCI6ICJEb2NrZXItQ2xpZW50LzE4LjA2LjAtY2UgKGxpbnV4KSIKCX0KfQ==
type: kubernetes.io/dockerconfigjson

创建

kubectl create -f docker-secret.yaml

检测:

[root@master LNMP]# kubectl get secret
NAME                  TYPE                                  DATA      AGE
default-token-5bjhk   kubernetes.io/service-account-token   3         24d
liaochao-https        kubernetes.io/tls                     2         7d
mysql-pass            Opaque                                1         23m
registrypullsecret    kubernetes.io/dockerconfigjson        1         12s

配置lnmp环境

pv 数据存储:

[root@master LNMP]# cat pv.yaml
apiVersion: v1
#使用glusterfs做存储,不懂glusterfs的请参考其他文章
kind: PersistentVolume
metadata:
  name: mysql-pv
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteMany
  glusterfs:
    endpoints: "glusterfs-cluster"
    path: "gv1"
---
#使用nfs做存储
apiVersion: v1
kind: PersistentVolume
metadata:
  name: wp-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: /opt/container_data/wp-wwwroot
    server: 192.168.1.39

php配置:

[root@master LNMP]# cat php-deployment.yaml
apiVersion: v1
kind: Service
metadata:
  name: wordpress-php
  labels:
    app: wordpress
spec:
  ports:
    - port: 9000
  selector:
    app: wordpress-php
    tier: frontend
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp-pvc
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
---
apiVersion: apps/v1beta2 # for versions before 1.8.0 use apps/v1beta1
kind: Deployment
metadata:
  name: wordpress-php
  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:
      containers:
      - name: php
        image: 192.168.1.40:5000/lnmp/php
        ports:
        - containerPort: 9000
          name: wordpress
        volumeMounts:
#用于php解析的目录
        - name: wordpress-persistent-storage
          mountPath: /var/www/html
##拉取镜像时的用户认证,没有的时候注释掉这2行
      imagePullSecrets:
        - name: registrypullsecret
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: wp-pvc

nginx配置:
此处nginx和php共用一个pv,pvc

[root@master LNMP]# cat nginx-deployment.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-wp-config
data:
  site.conf: |-
    server {
        listen 80;
        server_name localhost;
        root html;
        index index.html index.php;

        location ~ \.php$ {
            root html;
            fastcgi_pass wordpress-php:9000;
            fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_connect_timeout 60s;
            fastcgi_read_timeout 300s;
            fastcgi_send_timeout 300s;
        }
    }
---
apiVersion: v1
kind: Service
metadata:
  name: wordpress-nginx
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress-nginx
    tier: frontend
  type: NodePort
  sessionAffinity: ClientIP
---
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: wordpress-nginx
  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:
      containers:
      - name: nginx
        image: 192.168.1.40:5000/lnmp/nginx
        ports:
        - containerPort: 80
          name: wordpress
        volumeMounts:
        - name: wordpress-persistent-storage
          mountPath: /usr/local/nginx/html
        - name: config
          mountPath: /usr/local/nginx/conf/vhost/site.conf
          subPath: site.conf
      imagePullSecrets:
        - name: registrypullsecret
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: wp-pvc
      - name: config
        configMap:
          name: nginx-wp-config

yaml文件准备好以后,创建:

kubectl create -f mysql-deployment.yaml
kubectl create -f php-deployment.yaml
kubectl create -f nginx-deployment.yaml

检测存储:

kubectl get pv,pvc
[root@k8s-g1 lnmp]# kubectl get pv,pvc
NAME                        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM                    STORAGECLASS   REASON    AGE
persistentvolume/mysql-pv   20Gi       RWX            Retain           Bound     default/mysql-pv-claim                            17m
persistentvolume/wp-pv      5Gi        RWX            Retain           Bound     default/wp-pvc                                    2h

NAME                                   STATUS    VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/mysql-pv-claim   Bound     mysql-pv   20Gi       RWX                           17m
persistentvolumeclaim/wp-pvc           Bound     wp-pv      5Gi        RWX                           2h

检测节点:(保证所有节点都是running)

kubectl get pod -o wide

检测nginx servers暴露出来的端口:

kubectl get svc

然后根据nginxserver暴露出来的端口测试nginx页面是否能访问
http://192.168.1.40:49665/
利用phpinfo测试php解析

[root@master wp-wwwroot]# cat test.php
<?php phpinfo(); ?>

部署wordpress

此处我们将nfs挂载的共享路径为:/opt/container_data/wp-wwwroot/
我们将wordpress放在此处:
数据库选择:wordpress ,主机名为:wordpress-mysql,账户密码选择:root 123456

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐