生产持久化部署。必须要使用持久卷,否则会出现数据丢失的情况
Nacos的自动扩容缩容需要依赖持久卷,以及数据持久化也是一样,本例中使用的是NFS来使用PVC.
1. 部署nfs-csi (storageclass) 这里采用的是Qist大佬的官方yaml
nfs-StorageClass 部署
1 |
部署好nfs持久化存储后,下来部署nacos |
2. 设置nacos数据库,本人用的本地数据库(必须)
创建数据库nacos-test,执行下面建表语句
1 2 |
#官方建表sql https://github.com/alibaba/nacos/blob/master/distribution/conf/nacos-mysql.sql |
3.部署nacos
1 2 |
#下载官方yaml git clone https://github.com/nacos-group/nacos-k8s.git |
3.1本人下载官方yaml后,自己有所改动,主要修改 StorageClass 对应的nfs-csi环境
1 |
vim deploy/nacos/nacos-pvc-nfs.yaml |
3.2 修改configmap(修改成你的数据库配置)
1 2 3 4 5 6 7 8 9 10 |
apiVersion: v1 kind: ConfigMap metadata: name: nacos-cm data: mysql.host: "192.168.0.20" mysql.db.name: "nacos_test" mysql.port: "3306" mysql.user: "shooter" mysql.password: "123456" |
1 2 3 4 |
注意这里sts是三个副本,且不相互容忍,如果你节点不够会造成无法启动某个pod 解决办法:或者删除亲和性,或者开放主节点为工作节点 #去除主节点污点,设置master也可以被调度 kubectl taint nodes --all node-role.kubernetes.io/master- |
3.3本人使用yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
apiVersion: v1 kind: ServiceAccount metadata: name: nfs-client-provisioner --- apiVersion: v1 kind: Service metadata: name: nacos-headless labels: app: nacos-headless spec: type: ClusterIP clusterIP: None ports: - port: 8848 name: server targetPort: 8848 - port: 9848 name: client-rpc targetPort: 9848 - port: 9849 name: raft-rpc targetPort: 9849 ## 兼容1.4.x版本的选举端口 - port: 7848 name: old-raft-rpc targetPort: 7848 selector: app: nacos --- apiVersion: v1 kind: ConfigMap metadata: name: nacos-cm data: mysql.host: "192.168.0.20" mysql.db.name: "nacos_test" mysql.port: "3306" mysql.user: "shooter" mysql.password: "123456" --- 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: "1Gi" cpu: "500m" ports: - containerPort: 8848 name: client-port - containerPort: 9848 name: client-rpc - containerPort: 9849 name: raft-rpc - containerPort: 7848 name: old-raft-rpc 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_HOST valueFrom: configMapKeyRef: name: nacos-cm key: mysql.host - 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: SPRING_DATASOURCE_PLATFORM value: "mysql" - name: NACOS_SERVER_PORT value: "8848" - name: NACOS_APPLICATION_PORT value: "8848" - name: PREFER_HOST_MODE value: "hostname" - name: MODE value: "cluster" #value: "standalone" #单节点模式切这个即可 - name: NACOS_SERVERS value: "nacos-0.nacos-headless.default.svc.cluster.local:8848 nacos-1.nacos-headless.default.svc.cluster.local:8848 nacos-2.nacos-headless.default.svc.cluster.local:8848" 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: "nfs-csi" spec: accessModes: [ "ReadWriteMany" ] storageClassName: nfs-csi resources: requests: storage: 2Gi selector: matchLabels: app: nacos |
3.4查看日志看nacos是否启动
1 |
StatefulSet控制器根据其序数索引为每个Pod提供唯一的主机名。 主机名采用 - 的形式。 因为nacos StatefulSet的副本字段设置为3,所以当前集群文件中只有3个Nacos节点地址 |
1 |
for i in 0 1; do echo nacos-$i; kubectl exec nacos-$i cat conf/cluster.conf; done |
1 2 3 4 |
#使用kubectl scale 对Nacos动态扩容 kubectl scale sts nacos --replicas=5 #使用 kubectl exec执行Nacos API 在每台节点上获取当前Leader是否一致 for i in 0 1 2; do echo nacos-$i; kubectl exec nacos-$i curl -X GET "http://localhost:8848/nacos/v1/ns/raft/state"; done |
4. 为nacos配置对外访问(ingress)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: nacos-headless namespace: default spec: rules: - host: nacos.shooter.com http: paths: - path: / backend: serviceName: nacos-headless servicePort: server |
(成功)
1 2 |
nacos集群关闭双写模式 https://blog.csdn.net/h13710501070/article/details/122373962 |
- 本文固定链接: https://www.yoyoask.com/?p=6736
- 转载请注明: shooter 于 SHOOTER 发表