部署在kubernetes中,以NFS作为数据存储卷

环境介绍:

名称 版本
K8S v1.17.2
Docker 19.03.5
nacos 1.3.0

一、拉取代码

  1. git clone https://github.com/nacos-group/nacos-k8s.git

二、安装NFS服务

1、安装服务

  1. yum install nfs-utils rpcbind -y

2、创建共享目录

  1. mkdir /data/k8s -p

3、配置NFS配置文件

  1. [root@master ~]# vim /etc/exports
  2. /data/k8s *(rw,sync,no_root_squash)

配置详解:

  1. ro 只读访问
  2. rw 读写访问
  3. sync 所有数据在请求时写入共享
  4. async NFS在写入数据前可以相应请求
  5. secure NFS通过1024以下的安全TCP/IP端口发送
  6. insecure NFS通过1024以上的端口发送
  7. wdelay 如果多个用户要写入NFS目录,则归组写入(默认)
  8. no_wdelay 如果多个用户要写入NFS目录,则立即写入,当使用async时,无需此设置。
  9. Hide NFS共享目录中不共享其子目录
  10. no_hide 共享NFS目录的子目录
  11. subtree_check 如果共享/usr/bin之类的子目录时,强制NFS检查父目录的权限(默认)
  12. no_subtree_check 和上面相对,不检查父目录权限
  13. all_squash 共享文件的UIDGID映射匿名用户anonymous,适合公用目录。
  14. no_all_squash 保留共享文件的UIDGID(默认)
  15. root_squash root用户的所有请求映射成如anonymous用户一样的权限(默认)
  16. no_root_squas root用户具有根目录的完全管理访问权限
  17. anonuid=xxx 指定NFS服务器/etc/passwd文件中匿名用户的UID

4、启动服务

先启动rpcbind,再启动nfs

  1. systemctl start rpcbind && systemctl enable rpcbind
  2. systemctl start nfs && systemctl enable nfs

5、客户端安装nfs和rpcbind测试

安装见第一步。

  1. [root@node01 ~]# showmount -e 172.16.1.128
  2. Export list for 172.16.1.128:
  3. /data/k8s *

自此,NFS创建完成。

三、部署NFS provisioner

上面拉取完代码后,进入nacos-k8s/deploy/nfs,然后修改deployment.yaml中的NFS配置,如下:

  1. apiVersion: v1
  2. kind: ServiceAccount
  3. metadata:
  4. name: nfs-client-provisioner
  5. ---
  6. kind: Deployment
  7. apiVersion: apps/v1
  8. metadata:
  9. name: nfs-client-provisioner
  10. spec:
  11. replicas: 1
  12. selector:
  13. matchLabels:
  14. app: nfs-client-provisioner
  15. strategy:
  16. type: Recreate
  17. template:
  18. metadata:
  19. labels:
  20. app: nfs-client-provisioner
  21. spec:
  22. serviceAccount: nfs-client-provisioner
  23. containers:
  24. - name: nfs-client-provisioner
  25. image: quay.io/external_storage/nfs-client-provisioner:latest
  26. volumeMounts:
  27. - name: nfs-client-root
  28. mountPath: /persistentvolumes
  29. env:
  30. - name: PROVISIONER_NAME
  31. value: fuseim.pri/ifs
  32. - name: NFS_SERVER
  33. value: 172.17.100.50
  34. - name: NFS_PATH
  35. value: /home/middleware/nacos/cluster_nacos
  36. volumes:
  37. - name: nfs-client-root
  38. nfs:
  39. server: 172.17.100.50
  40. path: /home/middleware/nacos/cluster_nacos

其中需要修改的地方: 1、NFS_SERVER:NFS Server地址 2、NFS_PATH:NFS地址 3、PROVISIONER_NAME:可选泽修改,默认也可以,如果修改,后面使用的时候就用修改后的名字。 另外,由于我的集群版本是v1.17.2,所以Deployment的版本还有一些语法需要修改,如上。

然后创建即可:

  1. kubectl apply -f .

四、部署数据库

数据库依然部署在集群中,这里只部署单节点模式。进入数据库部署目录nacos-k8s/deploy/mysql
先创建namespace

  1. kubectl create ns nacos

(1)、部署数据库,依然以NFS作为后端存储,修改配置文件

mysql-nfs.yaml的配置如下:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: mysql
  5. namespace: nacos
  6. labels:
  7. name: mysql
  8. spec:
  9. replicas: 1
  10. selector:
  11. matchLabels:
  12. name: mysql
  13. template:
  14. metadata:
  15. labels:
  16. name: mysql
  17. spec:
  18. containers:
  19. - name: mysql
  20. image: nacos/nacos-mysql:5.7
  21. ports:
  22. - containerPort: 3306
  23. volumeMounts:
  24. - name: mysql-data
  25. mountPath: /var/lib/mysql
  26. env:
  27. - name: MYSQL_ROOT_PASSWORD
  28. value: "root"
  29. - name: MYSQL_DATABASE
  30. value: "nacos"
  31. - name: MYSQL_USER
  32. value: "nacos"
  33. - name: MYSQL_PASSWORD
  34. value: "nacos"
  35. volumes:
  36. - name: mysql-data
  37. nfs:
  38. server: 10.1.10.130
  39. path: /data/k8s/nacos/mysql
  40. ---
  41. apiVersion: v1
  42. kind: Service
  43. metadata:
  44. name: mysql
  45. namespace: nacos
  46. labels:
  47. name: mysql
  48. spec:
  49. ports:
  50. - port: 3306
  51. targetPort: 3306
  52. selector:
  53. name: mysql

然后执行即可。

  1. kubectl apply -f mysql-nfs.yaml

如果是自有数据库,则需要自己创建数据库,然后导入数据表。表所在位置:https://github.com/alibaba/nacos/blob/master/distribution/conf/nacos-mysql.sql

五、部署nacos

进入nacos-k8s/deploy/nacos,修改nacos-pvc-nfs.yaml,主要修改DB连接这块,修改成自己的。
1.0.0版本:

  1. apiVersion: v1
  2. kind: ServiceAccount
  3. metadata:
  4. name: nfs-client-provisioner
  5. namespace: nacos
  6. ---
  7. apiVersion: v1
  8. kind: Service
  9. metadata:
  10. name: nacos-headless
  11. namespace: nacos
  12. labels:
  13. app: nacos
  14. annotations:
  15. service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
  16. spec:
  17. ports:
  18. - port: 8848
  19. name: server
  20. targetPort: 8848
  21. clusterIP: None
  22. selector:
  23. app: nacos
  24. ---
  25. apiVersion: v1
  26. kind: ConfigMap
  27. metadata:
  28. name: nacos-cm
  29. namespace: nacos
  30. data:
  31. mysql.master.db.name: "nacos"
  32. mysql.master.db.host: "mysql.nacos.svc.cluster.local"
  33. mysql.master.port: "3306"
  34. mysql.master.user: "nacos"
  35. mysql.master.password: "nacos"
  36. mysql.slave.db.host: "mysql.nacos.svc.cluster.local"
  37. mysql.slave.db.port: "3306"
  38. ---
  39. apiVersion: apps/v1
  40. kind: StatefulSet
  41. metadata:
  42. name: nacos
  43. namespace: nacos
  44. spec:
  45. serviceName: nacos
  46. replicas: 3
  47. template:
  48. metadata:
  49. labels:
  50. app: nacos
  51. annotations:
  52. pod.alpha.kubernetes.io/initialized: "true"
  53. spec:
  54. # affinity:
  55. # podAntiAffinity:
  56. # requiredDuringSchedulingIgnoredDuringExecution:
  57. # - labelSelector:
  58. # matchExpressions:
  59. # - key: "app"
  60. # operator: In
  61. # values:
  62. # - nacos
  63. # topologyKey: "kubernetes.io/hostname"
  64. serviceAccountName: nfs-client-provisioner
  65. initContainers:
  66. - name: peer-finder-plugin-install
  67. image: nacos/nacos-peer-finder-plugin:1.0
  68. imagePullPolicy: IfNotPresent
  69. volumeMounts:
  70. - mountPath: "/home/nacos/plugins/peer-finder"
  71. name: plugindir
  72. containers:
  73. - name: nacos
  74. imagePullPolicy: IfNotPresent
  75. image: swr.cn-north-1.myhuaweicloud.com/cartechfin/nacos:latest
  76. resources:
  77. requests:
  78. memory: "2Gi"
  79. cpu: "500m"
  80. ports:
  81. - containerPort: 8848
  82. name: client-port
  83. env:
  84. - name: NACOS_REPLICAS
  85. value: "3"
  86. - name: SERVICE_NAME
  87. value: "nacos"
  88. - name: DOMAIN_NAME
  89. value: "cluster.local"
  90. - name: MYSQL_SLAVE_SERVICE_HOST
  91. valueFrom:
  92. configMapKeyRef:
  93. name: nacos-cm
  94. key: mysql.slave.db.host
  95. - name: MYSQL_SLAVE_SERVICE_PORT
  96. valueFrom:
  97. configMapKeyRef:
  98. name: nacos-cm
  99. key: mysql.slave.db.port
  100. - name: POD_NAMESPACE
  101. valueFrom:
  102. fieldRef:
  103. apiVersion: v1
  104. fieldPath: metadata.namespace
  105. - name: MYSQL_MASTER_SERVICE_DB_NAME
  106. valueFrom:
  107. configMapKeyRef:
  108. name: nacos-cm
  109. key: mysql.master.db.name
  110. - name: MYSQL_MASTER_SERVICE_PORT
  111. valueFrom:
  112. configMapKeyRef:
  113. name: nacos-cm
  114. key: mysql.master.port
  115. - name: MYSQL_MASTER_SERVICE_USER
  116. valueFrom:
  117. configMapKeyRef:
  118. name: nacos-cm
  119. key: mysql.master.user
  120. - name: MYSQL_MASTER_SERVICE_PASSWORD
  121. valueFrom:
  122. configMapKeyRef:
  123. name: nacos-cm
  124. key: mysql.master.password
  125. - name: MYSQL_MASTER_SERVICE_HOST
  126. valueFrom:
  127. configMapKeyRef:
  128. name: nacos-cm
  129. key: mysql.master.db.host
  130. - name: NACOS_SERVER_PORT
  131. value: "8848"
  132. - name: PREFER_HOST_MODE
  133. value: "hostname"
  134. volumeMounts:
  135. - name: plugindir
  136. mountPath: /home/nacos/plugins/peer-finder
  137. - name: datadir
  138. mountPath: /home/nacos/data
  139. - name: logdir
  140. mountPath: /home/nacos/logs
  141. volumeClaimTemplates:
  142. - metadata:
  143. name: plugindir
  144. annotations:
  145. volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
  146. spec:
  147. accessModes: [ "ReadWriteMany" ]
  148. resources:
  149. requests:
  150. storage: 5Gi
  151. - metadata:
  152. name: datadir
  153. annotations:
  154. volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
  155. spec:
  156. accessModes: [ "ReadWriteMany" ]
  157. resources:
  158. requests:
  159. storage: 5Gi
  160. - metadata:
  161. name: logdir
  162. annotations:
  163. volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
  164. spec:
  165. accessModes: [ "ReadWriteMany" ]
  166. resources:
  167. requests:
  168. storage: 5Gi
  169. selector:
  170. matchLabels:
  171. app: nacos

1.3.0版本:
(1)、把配置文件挂载到configMap中,方便修改参数
nacos-conf.yaml

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: nacos-application-conf
  5. namespace: nacos
  6. data:
  7. application.properties: |
  8. # spring
  9. server.servlet.contextPath=${SERVER_SERVLET_CONTEXTPATH:/nacos}
  10. server.contextPath=/nacos
  11. server.port=${NACOS_APPLICATION_PORT:8848}
  12. spring.datasource.platform=${SPRING_DATASOURCE_PLATFORM:""}
  13. nacos.cmdb.dumpTaskInterval=3600
  14. nacos.cmdb.eventTaskInterval=10
  15. nacos.cmdb.labelTaskInterval=300
  16. nacos.cmdb.loadDataAtStart=false
  17. db.num=${MYSQL_DATABASE_NUM:1}
  18. db.url.0=jdbc:mysql://${MYSQL_SERVICE_HOST}:${MYSQL_SERVICE_PORT:3306}/${MYSQL_SERVICE_DB_NAME}?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
  19. db.url.1=jdbc:mysql://${MYSQL_SERVICE_HOST}:${MYSQL_SERVICE_PORT:3306}/${MYSQL_SERVICE_DB_NAME}?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
  20. db.user=${MYSQL_SERVICE_USER}
  21. db.password=${MYSQL_SERVICE_PASSWORD}
  22. ### The auth system to use, currently only 'nacos' is supported:
  23. nacos.core.auth.system.type=${NACOS_AUTH_SYSTEM_TYPE:nacos}
  24. ### The token expiration in seconds:
  25. nacos.core.auth.default.token.expire.seconds=${NACOS_AUTH_TOKEN_EXPIRE_SECONDS:18000}
  26. ### The default token:
  27. nacos.core.auth.default.token.secret.key=${NACOS_AUTH_TOKEN:SecretKey012345678901234567890123456789012345678901234567890123456789}
  28. ### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay.
  29. nacos.core.auth.caching.enabled=${NACOS_AUTH_CACHE_ENABLE:false}
  30. server.tomcat.accesslog.enabled=${TOMCAT_ACCESSLOG_ENABLED:false}
  31. server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D
  32. # default current work dir
  33. server.tomcat.basedir=
  34. ## spring security config
  35. ### turn off security
  36. nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**
  37. # metrics for elastic search
  38. management.metrics.export.elastic.enabled=false
  39. management.metrics.export.influx.enabled=false
  40. nacos.naming.distro.taskDispatchThreadCount=10
  41. nacos.naming.distro.taskDispatchPeriod=200
  42. nacos.naming.distro.batchSyncKeyCount=1000
  43. nacos.naming.distro.initDataRatio=0.9
  44. nacos.naming.distro.syncRetryDelay=5000
  45. nacos.naming.data.warmup=true

修改deploy.yaml配置文件如下

  1. apiVersion: v1
  2. kind: ServiceAccount
  3. metadata:
  4. name: nfs-client-provisioner
  5. namespace: nacos
  6. ---
  7. apiVersion: v1
  8. kind: Service
  9. metadata:
  10. name: nacos
  11. namespace: nacos
  12. labels:
  13. app: nacos
  14. annotations:
  15. service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
  16. spec:
  17. ports:
  18. - port: 8848
  19. name: server
  20. targetPort: 8848
  21. clusterIP: None
  22. selector:
  23. app: nacos
  24. ---
  25. apiVersion: v1
  26. kind: ConfigMap
  27. metadata:
  28. name: nacos-cm
  29. namespace: nacos
  30. data:
  31. mysql.db.name: "nacos"
  32. mysql.db.host: "mysql.nacos.svc.cluster.local"
  33. mysql.port: "3306"
  34. mysql.user: "nacos"
  35. mysql.password: "nacos"
  36. ---
  37. apiVersion: apps/v1
  38. kind: StatefulSet
  39. metadata:
  40. name: nacos
  41. namespace: nacos
  42. spec:
  43. serviceName: nacos
  44. replicas: 3
  45. template:
  46. metadata:
  47. labels:
  48. app: nacos
  49. annotations:
  50. pod.alpha.kubernetes.io/initialized: "true"
  51. spec:
  52. # affinity:
  53. # podAntiAffinity:
  54. # requiredDuringSchedulingIgnoredDuringExecution:
  55. # - labelSelector:
  56. # matchExpressions:
  57. # - key: "app"
  58. # operator: In
  59. # values:
  60. # - nacos
  61. # topologyKey: "kubernetes.io/hostname"
  62. serviceAccountName: nfs-client-provisioner
  63. initContainers:
  64. - name: peer-finder-plugin-install
  65. image: nacos/nacos-peer-finder-plugin:1.0
  66. imagePullPolicy: IfNotPresent
  67. volumeMounts:
  68. - mountPath: "/home/nacos/plugins/peer-finder"
  69. name: plugindir
  70. volumes:
  71. - name: application-conf
  72. configMap:
  73. name: nacos-application-conf
  74. containers:
  75. - name: nacos
  76. imagePullPolicy: IfNotPresent
  77. image: nacos/nacos-server:1.3.0
  78. resources:
  79. requests:
  80. memory: "2Gi"
  81. cpu: "500m"
  82. ports:
  83. - containerPort: 8848
  84. name: client-port
  85. env:
  86. - name: NACOS_REPLICAS
  87. value: "3"
  88. - name: SERVICE_NAME
  89. value: "nacos"
  90. - name: DOMAIN_NAME
  91. value: "cluster.local"
  92. - name: POD_NAMESPACE
  93. valueFrom:
  94. fieldRef:
  95. apiVersion: v1
  96. fieldPath: metadata.namespace
  97. - name: MYSQL_SERVICE_DB_NAME
  98. valueFrom:
  99. configMapKeyRef:
  100. name: nacos-cm
  101. key: mysql.db.name
  102. - name: MYSQL_SERVICE_PORT
  103. valueFrom:
  104. configMapKeyRef:
  105. name: nacos-cm
  106. key: mysql.port
  107. - name: MYSQL_SERVICE_USER
  108. valueFrom:
  109. configMapKeyRef:
  110. name: nacos-cm
  111. key: mysql.user
  112. - name: MYSQL_SERVICE_PASSWORD
  113. valueFrom:
  114. configMapKeyRef:
  115. name: nacos-cm
  116. key: mysql.password
  117. - name: MYSQL_SERVICE_HOST
  118. valueFrom:
  119. configMapKeyRef:
  120. name: nacos-cm
  121. key: mysql.db.host
  122. - name: NACOS_SERVER_PORT
  123. value: "8848"
  124. - name: PREFER_HOST_MODE
  125. value: "hostname"
  126. volumeMounts:
  127. - name: plugindir
  128. mountPath: /home/nacos/plugins/peer-finder
  129. - name: datadir
  130. mountPath: /home/nacos/data
  131. - name: logdir
  132. mountPath: /home/nacos/logs
  133. - name: application-conf
  134. mountPath: /home/nacos/conf/application.properties
  135. subPath: application.properties
  136. volumeClaimTemplates:
  137. - metadata:
  138. name: plugindir
  139. annotations:
  140. volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
  141. spec:
  142. accessModes: [ "ReadWriteMany" ]
  143. resources:
  144. requests:
  145. storage: 5Gi
  146. - metadata:
  147. name: datadir
  148. annotations:
  149. volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
  150. spec:
  151. accessModes: [ "ReadWriteMany" ]
  152. resources:
  153. requests:
  154. storage: 5Gi
  155. - metadata:
  156. name: logdir
  157. annotations:
  158. volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
  159. spec:
  160. accessModes: [ "ReadWriteMany" ]
  161. resources:
  162. requests:
  163. storage: 5Gi
  164. selector:
  165. matchLabels:
  166. app: nacos

浏览器访问

  1. apiVersion: extensions/v1beta1
  2. kind: Ingress
  3. metadata:
  4. name: nacos
  5. namespace: nacos
  6. spec:
  7. rules:
  8. - host: nacos.coolops.cn
  9. http:
  10. paths:
  11. - backend:
  12. serviceName: nacos
  13. servicePort: 8848

六、参数调优

(1)、JVM调优
集群模式默认的配置如下:

  1. if [[ "${EMBEDDED_STORAGE}" == "embedded" ]]; then
  2. JAVA_OPT="${JAVA_OPT} -DembeddedStorage=true"
  3. fi
  4. JAVA_OPT="${JAVA_OPT} -server -Xms${JVM_XMS} -Xmx${JVM_XMX} -Xmn${JVM_XMN} -XX:MetaspaceSize=${JVM_MS} -XX:MaxMetaspaceSize=${JVM_MMS}"
  5. if [[ "${NACOS_DEBUG}" == "y" ]]; then
  6. JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n"
  7. fi
  8. JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof"
  9. JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages"
  10. print_servers

如果要更改内存大小,直接更新deployment的yaml文件,将上面的参数通过env的方式传递进去。

七、监控

使用Prometheus进行监控,首先打开server端metrics。如下修改configMap的配置文件:

  1. nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/v1/console/server/**
  2. # metrics for elastic search
  3. management.metrics.export.elastic.enabled=false
  4. management.metrics.export.influx.enabled=false
  5. management.endpoints.web.exposure.include=*

然后访问{ip}:8848/nacos/actuator/prometheus 查看是否能正确收集到指标:

需要监控的指标主要有:

指标 含义
system_cpu_usage cpu的使用率
jvm_memory_used_bytes 内存使用率
system_load_average_1m 系统负载
nacos_monitor{name=’failedPush’} Nacos naming推送失败数
nacos_exception_total{name=’db’} 数据库异常检查
nacos_exception_total{name=’configNotify’} Nacos config水平通知失败
nacos_exception_total{name=’unhealth’} 集群健康检查
nacos_exception_total{name=’disk’} 读写磁盘异常检查
nacos_exception_total{name=’leaderSendBeatFailed’} Nacos naming leader发送心跳异常
nacos_exception_total{name=’illegalArgument’} 请求参数不合法
nacos_exception_total{name=’nacos’} Nacos请求响应内部错误异常(读写失败,没权限,参数错误)
nacosSync_sync_task_error 所有同步执行时的异常检查

以上指标如果异常需要及时告警处理。