云环境可以使用云盘或者RDS做为共享存储

一、配置NFS

1、节点ip

31_NFS-SERVER
集群
KM01    192.168.1.31
KM02    192.168.1.32
node01  192.168.1.71
node02  192.168.1.72

2、创建目录、配置

mkdir -p /data/nfs-share/mysql
mkdir -p /data/nacos/
chmod 777 /data

[root@km01 nfs-share]# vim /etc/exports
/data/nfs-share/ *(rw,sync,no_root_squash)
/data/nacos/ *(rw,sync,no_root_squash)

3、启动

exportfs -r 
systemctl start nfs
systemctl enable nfs

二、配置nacos

1、 下载文件及目录结构

[root@km01 ~]# yum install git -y 
[root@km01 ~]# git clone https://github.com/nacos-group/nacos-k8s.git

[root@km01 deploy]# tree ./
./
├── ceph
│   ├── pvc.yaml
│   └── sc.yaml
├── mysql
│   ├── mysql-ceph.yaml
│   ├── mysql-local.yaml
│   └── mysql-nfs.yaml
├── nacos
│   ├── nacos-nodeport.yaml
│   ├── nacos-no-pvc-ingress.yaml
│   ├── nacos-pvc-ceph.yaml
│   ├── nacos-pvc-nfs.yaml
│   └── nacos-quick-start.yaml
└── nfs
    ├── class.yaml
    ├── deployment.yaml
    └── rbac.yaml

2、配置nfs-provisioner

cd ./nfs
[root@km01 nfs]# ls
class.yaml  deployment.yaml  rbac.yaml

vim deployment.yaml
 env:
            - name: PROVISIONER_NAME
              value: fuseim.pri/ifs
            - name: NFS_SERVER
              value: 192.168.1.31  #NFS-server
            - name: NFS_PATH
              value: /data/nfs-share   #NFS 目录
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.1.31    #NFS-server
            path: /data/nfs-share   #NFS 目录

[root@km01 nfs]# kubectl apply -f .
storageclass.storage.k8s.io/managed-nfs-storage unchanged
serviceaccount/nfs-client-provisioner unchanged
deployment.apps/nfs-client-provisioner unchanged
clusterrole.rbac.authorization.k8s.io/nfs-client-provisioner-runner unchanged
clusterrolebinding.rbac.authorization.k8s.io/run-nfs-client-provisioner unchanged
role.rbac.authorization.k8s.io/leader-locking-nfs-client-provisioner unchanged
rolebinding.rbac.authorization.k8s.io/leader-locking-nfs-client-provisioner unchanged

3、查看 nfs-provisioner

[root@km01 nacos]# kubectl get pod -l app=nfs-client-provisioner
NAME                                     READY   STATUS    RESTARTS   AGE
nfs-client-provisioner-9d8f7f9c5-8d2qh   1/1     Running   0          97m

三、部署mysql

1、部署

kubectl apply -f ./mysql-nfs.yaml

#yaml文件

[root@km01 mysql]# cat mysql-nfs.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
  labels:
    name: mysql
spec:
  replicas: 1
  selector:
    name: mysql
  template:
    metadata:
      labels:
        name: mysql
    spec:
      containers:
      - name: mysql
        image: nacos/nacos-mysql:5.7   #镜像版本
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: mysql-data
          mountPath: /var/lib/mysql
        env: #数据库参数
        - name: MYSQL_ROOT_PASSWORD
          value: "root"
        - name: MYSQL_DATABASE
          value: "nacos_devtest"
        - name: MYSQL_USER
          value: "nacos"
        - name: MYSQL_PASSWORD
          value: "nacos"
      volumes:
      - name: mysql-data
        nfs:
          server: 192.168.1.31
          path: /data/nfs-share/mysql   #挂载至NFS目录
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    name: mysql
spec:
  ports:
  - port: 3306       #svc 端口
    targetPort: 3306 #容器端口  
  selector:
    name: mysql

2、查看mysql状态

[root@km01 mysql]# kubectl get pods -o wide
NAME                                     READY   STATUS    RESTARTS   AGE    IP            NODE     NOMINATED NODE   READINESS GATES
busybox                                  1/1     Running   2          159m   10.244.1.11   km01     <none>           <none>
mysql-x6slw                              1/1     Running   0          83m    10.244.3.7    km02     <none>           <none>

[root@km01 mysql]# kubectl get svc
NAME             TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
kubernetes       ClusterIP   10.0.0.1     <none>        443/TCP          7d9h
mysql            ClusterIP   10.0.0.245   <none>        3306/TCP         89m

四、部署nacos应用

1、修改配置文件

vim nacos-pvc-nfs.yaml

[root@km01 nacos]# cat nacos-pvc-nfs.yaml 
---
apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  labels:
    app: nacos
  annotations:
    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
  ports:
    - port: 8848
      name: server
      targetPort: 8848    #此处注意
    ## 兼容1.4.x版本的选举端口
  clusterIP: None
  selector:
    app: nacos
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
data:  #数据库参数
  mysql.db.name: "nacos_devtest"
  mysql.port: "3306"
  mysql.user: "nacos"
  mysql.password: "nacos"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
spec:
  serviceName: nacos-headless
  replicas: 3
  template:
    metadata:
      labels:
        app: nacos
      annotations:
        pod.alpha.kubernetes.io/initialized: "true"
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values:
                      - nacos
              topologyKey: "kubernetes.io/hostname"
      serviceAccountName: nfs-client-provisioner
      initContainers:
        - name: peer-finder-plugin-install
          image: nacos/nacos-peer-finder-plugin:1.1
          imagePullPolicy: Always
          volumeMounts:
            - mountPath: /home/nacos/plugins/peer-finder
              name: data
              subPath: peer-finder
      containers:
        - name: nacos
          imagePullPolicy: Always
          image: nacos/nacos-server:latest
          resources:
            requests:
              memory: "2Gi"  #硬件配置
              cpu: "500m"
          ports:
            - containerPort: 8848
              name: client-port
          env:
            - name: NACOS_REPLICAS
              value: "3"
            - name: SERVICE_NAME
              value: "nacos-headless"
            - name: DOMAIN_NAME
              value: "cluster.local"
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
            - name: MYSQL_SERVICE_DB_NAME
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.db.name
            - name: MYSQL_SERVICE_PORT
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.port
            - name: MYSQL_SERVICE_USER
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.user
            - name: MYSQL_SERVICE_PASSWORD
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.password
            - name: NACOS_SERVER_PORT
              value: "8848"
            - name: NACOS_APPLICATION_PORT
              value: "8848"
            - name: PREFER_HOST_MODE
              value: "hostname"
          volumeMounts:
            - name: data
              mountPath: /home/nacos/plugins/peer-finder
              subPath: peer-finder
            - name: data
              mountPath: /home/nacos/data
              subPath: data
            - name: data
              mountPath: /home/nacos/logs
              subPath: logs
  volumeClaimTemplates:
    - metadata:
        name: data
        annotations:
          volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
      spec:
        accessModes: [ "ReadWriteMany" ]
        resources:
          requests:
            storage: 20Gi
  selector:
    matchLabels:
      app: nacos

2、nodeport暴露端口,用于外部访问

[root@km01 nacos]# vim nacos-nodeport.yaml 
apiVersion: v1
kind: Service
metadata: 
  name: nacos-nodeport
  labels:
    app: nacos-nodeport
spec:
  type: NodePort
  ports:
    - name: nacos
      port: 8848
      targetPort: 8848
      nodePort: 30000
  selector:
    app: nacos

kubectl apply ./nacos-nodeport.yaml

3、查看所有服务状态

[root@km01 nacos]# kubectl get pods -o wide
NAME                                     READY   STATUS    RESTARTS   AGE    IP            NODE     NOMINATED NODE   READINESS GATES
busybox                                  1/1     Running   2          168m   10.244.1.11   km01     <none>           <none>
mysql-x6slw                              1/1     Running   0          93m    10.244.3.7    km02     <none>           <none>
nacos-0                                  1/1     Running   0          59m    10.244.2.12   node01   <none>           <none>
nacos-1                                  1/1     Running   0          59m    10.244.0.11   node02   <none>           <none>
nacos-2                                  1/1     Running   0          59m    10.244.3.9    km02     <none>           <none>
nfs-client-provisioner-9d8f7f9c5-8d2qh   1/1     Running   0          112m   10.244.0.9    node02   <none>           <none>

登陆nacos
image.png