深入学习Kubernetes(六):在kubernetes中搭建LNMP环境,并安装Discuz

##登录harbor,并push新的镜像
[root@marster1 dz_web_dockerfile]# docker login harbor.wangjianhua.xyz
//输入正确的用户名和密码
[root@marster1 dz_web_dockerfile]# docker tag mysql:5.7 harbor.wangjianhua.xyz/wjh/mysql:5.7 
[root@marster1 dz_web_dockerfile]# docker tag nginx-php  harbor.wangjianhua.xyz/wjh/nginx-php
//给镜像打标签
[root@marster1 dz_web_dockerfile]# docker push harbor.wangjianhua.xyz/wjh/nginx-php  
[root@marster1 dz_web_dockerfile]# docker push  harbor.wangjianhua.xyz/wjh/mysql:5.7
//往 harbor推镜像

  

一、准备工作(把需要的镜像上传到harbor)

1.1下载镜像

[root@marster1 ~]# docker pull mysql:5.7  
[root@marster1 ~]# docker pull richarvey/nginx-php-fpm

1.2.用dockerfile重建nginx-php-fpm镜像

[root@marster1 ~]# git clone https://git.coding.net/aminglinux/k8s_discuz.git
[root@marster1 dz_web_dockerfile]# ls
Dockerfile  localtime  nginx.conf  php-fpm-www.conf
[root@marster1 dz_web_dockerfile]# docker build -t nginx-php .

1.3.将镜像push到harbor

##登录harbor,并push新的镜像
[root@marster1 dz_web_dockerfile]# docker login harbor.wangjianhua.xyz     
//输入正确的用户名和密码
[root@marster1 dz_web_dockerfile]# docker tag nginx-php  harbor.wangjianhua.xyz/wjh/nginx-php
[root@marster1 dz_web_dockerfile]# docker tag mysql:5.7 harbor.wangjianhua.xyz/wjh/mysql:5.7
//给镜像打标签
[root@marster1 dz_web_dockerfile]# docker push  harbor.wangjianhua.xyz/wjh/mysql:5.7
[root@marster1 dz_web_dockerfile]# docker push harbor.wangjianhua.xyz/wjh/nginx-php  
//往harbor,push镜像

1.4.harbor查看已上传的镜像

二、搭建nfs

[root@master2 ~]# yum install nfs-utils rpcbing
[root@master2 ~]# vim /etc/exports 
/data/k8s/ 192.168.0.0/24(sync,rw,no_root_squash)
[root@master2 ~]# mkdir -p /data/k8s/discuz/{db,web}
[root@master2 ~]# chmod 777 -R /data
[root@master2 ~]# systemctl start nfs
[root@master2 ~]# systemctl enable nfs  

三、搭建mysql服务

3.1创建secret (设定mysql的root密码)

[root@marster1 dz_web_dockerfile]# kubectl create secret generic mysql-pass --from-literal=password=111111
secret/mysql-pass created

3.2 创建PV 

[root@marster1 k8s_discuz]# cd mysql
[root@marster1 mysql]# ls
mysql-dp.yaml  mysql-pvc.yaml  mysql-pv.yaml  mysql-svc.yaml
//mysql目录已有做好各种资源的yaml文件
[root@marster1 mysql]# vi mysql-pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: /data/k8s/discuz/db
    ##这里的IP,是NFS server的IP
    server: 192.168.0.113
[root@marster1 mysql]# kubectl create -f mysql-pv.yaml
persistentvolume/mysql-pv created
//创建mysql-pv
[root@marster1 mysql]# kubectl get pv
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM             STORAGECLASS   REASON    AGE
mysql-pv   10Gi       RWX            Retain           Available                                              9s
//查看创建的pv

3.2 创建PVC

[root@marster1 mysql]# vi mysql-pvc.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-claim
  labels:
    app: discuz
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
[root@marster1 mysql]# kubectl create -f mysql-pvc.yaml 
persistentvolumeclaim/mysql-claim created
[root@marster1 mysql]# kubectl get pvc mysql-claim 
NAME          STATUS    VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-claim   Bound     mysql-pv   10Gi       RWX                           31s
[root@marster1 mysql]# kubectl get pv
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM                 STORAGECLASS   REASON    AGE
mysql-pv   10Gi       RWX            Retain           Bound     default/mysql-claim                            3m
//查看pv,已经和pvc绑定

3.3 创建deployment 

[root@marster1 mysql]# vi mysql-dp.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dz-mysql
  labels:
    app: discuz
spec:
  selector:
    matchLabels:
      app: discuz
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: discuz
        tier: mysql
    spec:
      imagePullSecrets:
       - name: my-secret
      containers:
      - image: harbor.wangjianhua.xyz/wjh/mysql:5.7
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        ports:
        - containerPort: 3306
          name: dz-mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-claim
[root@marster1 mysql]# kubectl create -f mysql-dp.yaml 
deployment.apps/dz-mysql created
[root@marster1 mysql]# kubectl get pod
NAME                        READY     STATUS             RESTARTS   AGE
dz-mysql-77c6c66578-2x9cf   1/1       Running            0          1m
[root@marster1 mysql]# kubectl get deployments
NAME       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
dz-mysql   1         1         1            1           4m

3.4创建service 

[root@marster1 mysql]# vi mysql-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: dz-mysql
  labels:
    app: discuz
spec:
  ports:
    - port: 3306
  selector:
    app: discuz
    tier: mysql
[root@marster1 mysql]# kubectl create -f mysql-svc.yaml 
service/dz-mysql created
[root@marster1 mysql]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
dz-mysql     ClusterIP   10.68.5.237     <none>        3306/TCP   7s

3.5验证mysql

[root@marster1 mysql]# mysql -uroot -p111111 -h10.68.5.237
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.24 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)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.04 sec)

MySQL [(none)]> quit
Bye

四、搭建Nginx+php-fpm服务

4.1搭建pv

[root@marster1 ~]# cd k8s_discuz/nginx_php/
[root@marster1 nginx_php]# ls
web-dp.yaml  web-pvc.yaml  web-pv.yaml  web-svc.yaml
[root@marster1 nginx_php]# vi web-pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: web-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: /data/k8s/discuz/web
    server: 192.168.0.113
[root@marster1 nginx_php]# kubectl create -f web-pv.yaml 
persistentvolume/web-pv created
[root@marster1 nginx_php]# kubectl get pv
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                 STORAGECLASS   REASON    AGE
mysql-pv   10Gi       RWX            Retain           Bound       default/mysql-claim                            10h
pv001      10Gi       RWX            Retain           Bound       default/myclaim                                1d
web-pv     10Gi       RWX            Retain           Available                                                  8s

4.2创建PVC

[root@marster1 nginx_php]# vi web-pvc.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: web-claim
  labels:
    app: discuz
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
[root@marster1 nginx_php]# kubectl create -f web-pvc.yaml 
persistentvolumeclaim/web-claim created
[root@marster1 nginx_php]# kubectl get pvc web-claim 
NAME        STATUS    VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
web-claim   Bound     web-pv    10Gi       RWX                           56s
[root@marster1 nginx_php]# kubectl get pv web-pv 
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM               STORAGECLASS   REASON    AGE
web-pv    10Gi       RWX            Retain           Bound     default/web-claim                            2m

4.3创建deployment

[root@marster1 nginx_php]# vi web-dp.yaml                
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dz-web
  labels:
    app: discuz
spec:
  replicas: 2
  selector:
    matchLabels:
      app: discuz
      tier: nginx-php
  template:
    metadata:
      labels:
        app: discuz
        tier: nginx-php
    spec:
      imagePullSecrets:
      - name: my-secret
      containers:
      - image: harbor.wangjianhua.xyz/wjh/nginx-php
        name: dz-web
        ports:
        - containerPort: 9000
        - containerPort: 80
[root@marster1 nginx_php]# kubectl get deployments. 
NAME       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
dz-mysql   1         1         1            1           10h
dz-web     2         2         2            0           2m
[root@marster1 nginx_php]# kubectl get pods
NAME                        READY     STATUS              RESTARTS   AGE
dz-mysql-77c6c66578-2x9cf   1/1       Running             1          11h
dz-web-648dcfbfc9-brwb7     1/1       Running             0          3m
dz-web-648dcfbfc9-v7pfl     0/1       ContainerCreating   0          3m

4.4 创建service

[root@marster1 nginx_php]# vi web-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: dz-web
  labels:
    app: discuz
spec:
  ports:
    - port: 80
  selector:
    app: discuz
    tier: nginx-php
[root@marster1 nginx_php]# kubectl create -f web-svc.yaml 
service/dz-web created
[root@marster1 nginx_php]# kubectl get svc dz-web
NAME      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
dz-web    ClusterIP   10.68.122.234   <none>        80/TCP    20s

五、安装discuz

5.1下载dz代码 (到NFS服务器上)

[root@master2 ~]# cd /tmp
[root@master2 tmp]# git clone https://gitee.com/ComsenzDiscuz/DiscuzX.git
[root@master2 tmp]# cd /data/k8s/discuz/web/
[root@master2 web]# mv /tmp/DiscuzX/upload/* .
[root@master2 web]# chown -R 100 data uc_server/data/ uc_client/data/ config/
[root@master2 web]# ls
admin.php  archiver     crossdomain.xml  forum.php  index.php  member.php  portal.php  source    uc_client
api        config       data             group.php  install    misc.php    robots.txt  static    uc_server
api.php    connect.php  favicon.ico      home.php   m          plugin.php  search.php  template

5.2 设置MySQL普通用户

[root@master2 web]# kubectl get svc dz-mysql 
NAME       TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
dz-mysql   ClusterIP   10.68.5.237   <none>        3306/TCP   11h
//查看service的cluster-ip

[root@marster1 nginx_php]# mysql -uroot -h10.68.5.237 -p111111
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.24 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)]> create database dz;
Query OK, 1 row affected (0.04 sec)

MySQL [(none)]> grant all on dz.* to 'dz'@'%' identified by '111111';       
Query OK, 0 rows affected, 1 warning (0.01 sec)

MySQL [(none)]> quit

5.3 设置nginx代理

注意:目前nginx服务是运行在kubernetes集群里,node节点以及master节点上是可以通过cluster-ip访问到,但是外部的客户端就不能访问了。所以,可以在任意一台node或者master上建一个nginx反向代理即可访问到集群内的nginx。
[root@marster1 nginx_php]# kubectl get svc dz-web 
NAME      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
dz-web    ClusterIP   10.68.122.234   <none>        80/TCP    16m
//查看cluster-ip
[root@marster1 ~]# yum install -y nginx
//把marster1做为nginx反向代理
[root@marster1 ~]# vi /etc/nginx/nginx.conf

server {
listen 80;
server_name www.discuz.com;

location / {
proxy_pass http://10.68.133.120:80;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

[root@marster1 ~]# systemctl start nginx

5.4 安装Discuz

##域名绑定
打开windows的host文件增加:
192.168.0.110 www.discuz.com

在浏览器输入www.discuz.com/install

 

  

 

  

5.5 也可以通过node的IP来访问discuz了

[root@master2 web]# kubectl get pod dz-web-648dcfbfc9-68wwf -o wide
NAME                      READY     STATUS    RESTARTS   AGE       IP            NODE            NOMINATED NODE
dz-web-648dcfbfc9-68wwf   1/1       Running   1          11h       172.20.2.35   192.168.0.112   <none>
//查询node的IP
[root@marster1 nginx_php]# vi web-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: dz-web
  labels:
    app: discuz
spec:
  type: NodePort                //指定类型是NodePort 
  ports:
    - port: 80
      nodePort: 30001         //添加nodePort端口
  selector:
    app: discuz
    tier: nginx-php
[root@marster1 nginx_php]# kubectl create -f web-svc.yaml
[root@marster1 nginx_php]# kubectl get svc dz-web 
NAME      TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
dz-web    NodePort   10.68.133.120   <none>        80:30001/TCP   23m
//已有30001端口

 

  

  

  

  

  

  

  

  

  

 

 

  

  

 

 

  

  

posted @ 2019-01-18 21:48  学习记事本  阅读(708)  评论(0编辑  收藏  举报