1.nacos数据库使用mysq,自行安装mysql,创建数据库nacos_config,导入nacos初始化sql
2.k8s里部署Headless Service
Headless Service为每个pod(nacos实例)生成一个DNS地址,用作NACOS_SERVERS配置
3.通过StatefulSet部署nacos,StatefulSet部署方式为每个POD生成固定的名称,如nacos-0、nacos-1、nacos-2
4.部署Ingress,访问应该统一访问域名可以自己起,到时直接访问
我们将部署3个实例的nacos,StatefulSet部署方式为每个POD生成固定的名称:nacos-0、nacos-1、nacos-2。
cat nacos_cluster.yaml
---apiVersion: v1kind: Servicemetadata: namespace: lishanbinnacos name: nacos-headless labels: app: nacos-headlessspec: ports: - port: 8848 name: server targetPort: 8848 clusterIP: None selector: app: nacos---apiVersion: v1kind: ConfigMapmetadata: namespace: lishanbinnacos name: nacos-cmdata: mysql.host: "192.168.0.249" mysql.db.name: "nacos_config" mysql.port: "3306" mysql.user: "root" mysql.password: "xxxxx"---apiVersion: apps/v1kind: StatefulSetmetadata: namespace: lishanbinnacos name: nacosspec: 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-headless topologyKey: "kubernetes.io/hostname" containers: - name: k8snacos imagePullPolicy: IfNotPresent image: nacos/nacos-server:2.0.4 resources: requests: memory: "2Gi" cpu: "500m" ports: - containerPort: 8848 name: client env: - name: NACOS_REPLICAS value: "3" - 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: MODE value: "cluster" - name: NACOS_SERVER_PORT value: "8848" - name: PREFER_HOST_MODE value: "hostname" - name: NACOS_SERVERS value: "nacos-0.nacos-headless.lishanbinnacos.svc.cluster.local:8848 nacos-1.nacos-headless.lishanbinnacos.svc.cluster.local:8848 nacos-2.nacos-headless.lishanbinnacos.svc.cluster.local:8848" selector: matchLabels: app: nacos
使用ingress方式对外暴露:
apiVersion: extensions/v1beta1kind: Ingressmetadata: namespace: lishanbinnacos name: nacos-webspec: rules: - host: lishanbin.nacos.com http: paths: - path: / backend: serviceName: nacos-headless servicePort: 8848
使用node port方式对外暴露:
apiVersion: v1kind: Servicemetadata: name: nacos-nodeport namespace: lishanbinnacosspec: type: NodePort ports: - name: http port: 8848 targetPort: 8848 nodePort: 30088 selector: app: nacos