创建MySQL密码(Secret )

Create the Secret object from the following command. You will need to replace YOUR_PASSWORD with the password you want to use.

# vim password.txt # kubectl create secret generic nc-mysql-pass --from-file=password.txt 
[root@ec-b nextcloud]# kubectl create secret generic nc-mysql-pass --from-literal=password=YOUR_PASSWORD
[root@ec-b ~]# kubectl get secrets
NAME                 TYPE              DATA   AGE
......
nc-mysql-pass        Opaque            1      7m59s

 

持久化存储卷 Persistent Volume

[root@ec-b nextcloud]# vi 00-nextcloud-storageClass.yaml 
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: nextcloud-sc
provisioner: ceph.com/cephfs
parameters:
    monitors: 172.16.1.61:6789,172.16.1.62:6789,172.16.0.63:6789
    adminId: admin
    adminSecretName: ceph-secret-admin
    adminSecretNamespace: "kube-system"
    #adminSecretNamespace: "default"
    claimRoot: /volumes/kubernetes/nextcloud
reclaimPolicy: Retain # pvc移除后,保留数据

[root@ec-b nextcloud]# kubectl create -f nextcloud-storageClass.yaml
storageclass.storage.k8s.io/nextcloud-sc created

[root@ec-b nextcloud]# kubectl get storageclasses
NAME           PROVISIONER       AGE
nextcloud-sc   ceph.com/cephfs   2m12s
[root@ec-b nextcloud]# vi 01-nc-mysql-pvc.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nc-mysql-pv-claim
  annotations:
    # 动态PVC,绑定,StorageClass
    volume.beta.kubernetes.io/storage-class: "nextcloud-sc"
  labels:
    app: nextcloud
spec:
  accessModes:
    # - ReadWriteOnce
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
[root@ec-b nextcloud]# vi 02-nextcloud-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nc-pv-claim
  annotations:
    # 动态PVC,绑定,StorageClass
    volume.beta.kubernetes.io/storage-class: "nextcloud-sc"
  labels:
    app: nextcloud
spec:
  accessModes:
    #- ReadWriteOnce
    - ReadWriteMany
  resources:
    requests:
      storage: 512Gi
[root@ec-b nextcloud]# kubectl create -f 01-nc-mysql-pvc.yaml -f  02-nextcloud-pvc.yaml
persistentvolumeclaim/nc-mysql-pv-claim created
persistentvolumeclaim/nc-pv-claim created
[root@ec-b nextcloud]# kubectl get pvc
NAME                STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
nc-mysql-pv-claim   Bound    pvc-6e19b34c-140f-11e9-967d-080027e83152   10Gi       RWX            nextcloud-sc   3m
nc-pv-claim         Bound    pvc-bf2a114c-0f06-11e9-ac2c-080027dce69c   512Gi      RWX            nextcloud-sc   3m

部署 MySQL (Deploy MySQL )

mysql-deployment

[root@ec-b nextcloud]# vi 10-nc-mysql-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: nextcloud-mysql
  labels:
    app: nextcloud-mysql
    component: nextcloud
spec:
  ports:
    - port: 3306
  selector:
    app: nextcloud-mysql
    component: nextcloud
  clusterIP: None
[root@ec-b nextcloud]# vi 11-nc-mysql-deployment.yaml
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: nextcloud-mysql
  labels:
    app: nextcloud-mysql
    component: nextcloud
spec:
  selector:
    matchLabels:
      app: nextcloud-mysql
      component: nextcloud
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nextcloud-mysql
        component: nextcloud
    spec:
      containers:
      - image: mysql:5.7
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: nc-mysql-pass
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: nc-mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: nc-mysql-persistent-storage
        persistentVolumeClaim:
          claimName: nc-mysql-pv-claim
[root@ec-b nextcloud]# kubectl create -f 02-nc-mysql-deployment.yaml -f 11-nc-mysql-deployment.yaml
service/nextcloud-mysql created
deployment.apps/nextcloud-mysql created
[root@ec-b nextcloud]# vi 12-mysql-client-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: mysql-client
spec:
  containers:
    - name: mysql-client
      image: mysql:5.7
      command: ["/bin/sleep"]
      args: [ "3600" ]
[root@ec-b nextcloud]# kubectl create -f 12-mysql-client-pod.yaml
deployment.apps/nextcloud-mysql created
[root@ec-b nextcloud]# kubectl get pods -o wide
NAME                              READY   STATUS    RESTARTS   AGE    IP               NODE   NOMINATED NODE   READINESS GATES
mysql-client                      1/1     Running   0          12m    172.58.247.90    ec-d   <none>           <none>
nextcloud-mysql-f6bf67f4f-p8z9h   1/1     Running   0          18m    172.58.89.224    ec-g   <none>           <none>

[root@ec-b nextcloud]# kubectl get svc
NAME              TYPE           CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
nextcloud-mysql   ClusterIP      None          <none>        3306/TCP       19m
[root@ec-b nextcloud]# kubectl exec -it mysql-client /bin/bash
root@mysql-client:/# 
# 创建数据库、用户、权限等
# create database nextcloud_db;              
# create user ecuser@'%' identified by 'nextcloud@ec';
# grant all privileges on nextcloud_db.* to ecuser@'%' identified by 'nextcloud@ec';
# flush privileges;

root@mysql-client:/# mysql -h nextcloud-mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 
mysql> create database nextcloud_db;              
Query OK, 1 row affected (0.00 sec)

mysql> create user ecuser@'%' identified by 'nextcloud@ec';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on nextcloud_db.* to ecuser@'%' identified by 'nextcloud@ec';
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

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

mysql> 
mysql> exit;
Bye

 

 

3. 部署 NextCloud

[root@ec-b nextcloud]# vi 20-nextcloud-svc.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: nextcloud-svc
  labels:
    component: nextcloud
spec:
  ports:
    - port: 80
      targetPort: 80
  selector:
    component: nextcloud
    app: nextcloud
  type: LoadBalancer #NodePort #ClusterIP
[root@ec-b nextcloud]# vi 21-nextcloud-deployment.yaml
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: nextcloud
  labels:
    component: nextcloud
    app: nextcloud
spec:
  selector:
    matchLabels:
      component: nextcloud
      app: nextcloud
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        component: nextcloud
        app: nextcloud
    spec:
      containers:
      - image: nextcloud:13.0.3-apache
        imagePullPolicy: IfNotPresent
        name: nextcloud
        # MySQL 数据库主机名、数据库名、用户名、密码
        env:
        - name: MYSQL_HOST
          value: nextcloud-mysql:3306
        - name: MYSQL_DATABASE
          value: nextcloud_db
        - name: MYSQL_USER
          value: ecuser
        - name: MYSQL_PASSWORD
          value: nextcloud@ec
        ports:
        - containerPort: 80
          name: nextcloud
        volumeMounts:
        - name: nextcloud-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: nextcloud-persistent-storage
        persistentVolumeClaim:
          claimName: nc-pv-claim
[root@ec-b nextcloud]# kubectl create -f 20-nextcloud-svc.yaml -f 21-nextcloud-deployment.yaml
[root@ec-b nextcloud]# kubectl get svc,pods -o wide
NAME                      TYPE           CLUSTER-IP    EXTERNAL-IP   PORT(S)      
service/nextcloud-mysql   ClusterIP      None          <none>        3306/TCP     
service/nextcloud-svc     LoadBalancer   10.86.22.18   10.35.1.238   80:32594/TCP 

NAME                                  READY   STATUS    RESTARTS   AGE   IP              NODE  
pod/nextcloud-7cd5795c6b-7lxrt        1/1     Running   0          2h   172.58.89.226   ec-g   
pod/nextcloud-mysql-f6bf67f4f-p8z9h   1/1     Running   0          2h   172.58.89.224   ec-g   

在浏览器中打开 EXTERNAL-IP,10.35.1.238

Install Page
Installing

 

 

自动调整 NextCloud pods 数量。

[root@ec-b nextcloud]# kubectl autoscale deployment nextcloud --min=2 --max=6
horizontalpodautoscaler.autoscaling/nextcloud autoscaled
[root@ec-b nextcloud]# 
[root@ec-b nextcloud]# kubectl get deployment
NAME              READY   UP-TO-DATE   AVAILABLE   AGE
nextcloud         1/1     1            1           46m
nextcloud-mysql   1/1     1            1           18h


[root@ec-b nextcloud]# kubectl get deployment
NAME              READY   UP-TO-DATE   AVAILABLE   AGE
nextcloud         1/1     1            1           46m
nextcloud-mysql   1/1     1            1           18h

[root@ec-b nextcloud]# kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
mysql-client                      1/1     Running   18         18h
nextcloud-7cd5795c6b-7lxrt        1/1     Running   0          46m
nextcloud-7cd5795c6b-cfzrk        1/1     Running   0          9s
nextcloud-mysql-f6bf67f4f-p8z9h   1/1     Running   0          18h
# 手动调整
[root@ec-k8s-m1 nextcloud]# kubectl scale --replicas=2 deployment/nextcloud
deployment.extensions/nextcloudscaled



参考资料:

https://kubernetes.io/docs/

https://hub.docker.com/_/nextcloud/