数据库准备

下载nacos二进制包,需要nacos-mysql.sql文件
https://github.com/alibaba/nacos/releases/download/2.1.1/nacos-server-2.1.1.tar.gz

use nacos;
source nacos-mysql.sql

nacos配置文件

application.properties

# spring
server.servlet.contextPath=${SERVER_SERVLET_CONTEXTPATH:/nacos}
server.contextPath=/nacos
server.port=${NACOS_APPLICATION_PORT:8848}
server.tomcat.accesslog.max-days=30
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i %{Request-Source}i
spring.datasource.platform=${SPRING_DATASOURCE_PLATFORM:""}
nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.labelTaskInterval=300
nacos.cmdb.loadDataAtStart=false
db.num=${MYSQL_DATABASE_NUM:1}
db.url.0=jdbc:mysql://${MYSQL_SERVICE_HOST}:${MYSQL_SERVICE_PORT:3306}/${MYSQL_SERVICE_DB_NAME}?${MYSQL_SERVICE_DB_PARAM:characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false}
db.url.1=jdbc:mysql://${MYSQL_SERVICE_HOST}:${MYSQL_SERVICE_PORT:3306}/${MYSQL_SERVICE_DB_NAME}?${MYSQL_SERVICE_DB_PARAM:characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false}
db.user=${MYSQL_SERVICE_USER}
db.password=${MYSQL_SERVICE_PASSWORD}
### The auth system to use, currently only 'nacos' and 'ldap' is supported:
nacos.core.auth.system.type=${NACOS_AUTH_SYSTEM_TYPE:nacos}
nacos.core.auth.enabled=${NACOS_AUTH_ENABLE:true}
### worked when nacos.core.auth.system.type=nacos
### The token expiration in seconds:
nacos.core.auth.plugin.nacos.token.expire.seconds=${NACOS_AUTH_TOKEN_EXPIRE_SECONDS:18000}
### The default token:
nacos.core.auth.plugin.nacos.token.secret.key=${NACOS_AUTH_TOKEN:SecretKey012345678901234567890123456789012345678901234567890123456789}
### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay.
nacos.core.auth.caching.enabled=${NACOS_AUTH_CACHE_ENABLE:false}
nacos.core.auth.enable.userAgentAuthWhite=${NACOS_AUTH_USER_AGENT_AUTH_WHITE_ENABLE:false}
nacos.core.auth.server.identity.key=${NACOS_AUTH_IDENTITY_KEY:serverIdentity}
nacos.core.auth.server.identity.value=${NACOS_AUTH_IDENTITY_VALUE:security}
server.tomcat.accesslog.enabled=${TOMCAT_ACCESSLOG_ENABLED:false}
# default current work dir
server.tomcat.basedir=file:.
## spring security config
### turn off security
nacos.security.ignore.urls=${NACOS_SECURITY_IGNORE_URLS:/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**}
# metrics for elastic search
management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false
nacos.naming.distro.taskDispatchThreadCount=10
nacos.naming.distro.taskDispatchPeriod=200
nacos.naming.distro.batchSyncKeyCount=1000
nacos.naming.distro.initDataRatio=0.9
nacos.naming.distro.syncRetryDelay=5000
nacos.naming.data.warmup=true
# metrics for promethus
management.endpoints.web.exposure.include=*

Dockerfile

FROM nacos/nacos-server:v2.1.1
COPY application.properties /home/nacos/conf/

configmap

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos2-cm
  namespace: release
data:
  mysql.host: "192.168.x.x"
  mysql.db.name: "nacos"
  mysql.port: "3306"
  mysql.user: "nacos"
  mysql.password: "nacos"

nacos-headless

eso-nacos-headless.yaml

apiVersion: v1
kind: Service
metadata:
  name: eso-nacos-headless
  namespace: release
  labels:
    app: eso-nacos
  annotations:
    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
  ports:
    - port: 8848
      name: server
      targetPort: 8848
    - port: 9848
      name: client-rpc
      targetPort: 9848
    - port: 9849
      name: raft-rpc
      targetPort: 9849
    - port: 7848
      name: old-raft-rpc
      targetPort: 7848
  clusterIP: None
  selector:
    app: eso-nacos

nacos-statefulset

eso-nacos-statefulset.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: eso-nacos
  namespace: release
spec:
  serviceName: eso-nacos-headless
  replicas: 3
  selector:
    matchLabels:
      app: eso-nacos
  template:
    metadata:
      labels:
        app: eso-nacos
      annotations:
        pod.alpha.kubernetes.io/initialized: "true"
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values:
                      - eso-nacos
              topologyKey: "kubernetes.io/hostname"
      containers:
        - name: eso-nacos
          image: harbor.com.cn/library/nacos-server:2.1.1
          imagePullPolicy: Always
          resources:
            requests:
              memory: "2Gi"
              cpu: "2000m"
          ports:
            - containerPort: 8848
              name: client
            - containerPort: 9848
              name: client-rpc
            - containerPort: 9849
              name: raft-rpc
            - containerPort: 7848
              name: old-raft-rpc
          env:
            - name: NACOS_REPLICAS
              value: "3"
            - name: MYSQL_SERVICE_HOST
              valueFrom:
                configMapKeyRef:
                  name: nacos2-cm
                  key: mysql.host
            - name: MYSQL_SERVICE_DB_NAME
              valueFrom:
                configMapKeyRef:
                  name: nacos2-cm
                  key: mysql.db.name
            - name: MYSQL_SERVICE_PORT
              valueFrom:
                configMapKeyRef:
                  name: nacos2-cm
                  key: mysql.port
            - name: MYSQL_SERVICE_USER
              valueFrom:
                configMapKeyRef:
                  name: nacos2-cm
                  key: mysql.user
            - name: MYSQL_SERVICE_PASSWORD
              valueFrom:
                configMapKeyRef:
                  name: nacos2-cm
                  key: mysql.password
            - name: MODE
              value: "cluster"
            - name: NACOS_SERVER_PORT
              value: "8848"
            - name: PREFER_HOST_MODE
              value: "hostname"
            - name: SPRING_DATASOURCE_PLATFORM
              value: "mysql"
            - name: NACOS_SERVERS
              value: "eso-nacos-0.eso-nacos-headless.release.svc.cluster.local:8848 eso-nacos-1.eso-nacos-headless.release.svc.cluster.local:8848 eso-nacos-2.eso-nacos-headless.release.svc.cluster.local:8848"

nacos-service

eso-nacos-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: eso-nacos-service
  namespace: release
  annotations:
    nginx.ingress.kubernetes.io/affinity: "true"
    nginx.ingress.kubernetes.io/session-cookie-name: backend
    nginx.ingress.kubernetes.io/load-balancer-method: drr
spec:
  selector:
    app: eso-nacos
  ports:
    - name: web
      port: 8848
      targetPort: 8848
    - name: web-client
      port: 80
      targetPort: 8848
    - name: grpc
      port: 9848
      targetPort: 9848

nacos-service-grpc

eso-nacos-service-grpc.yaml

apiVersion: v1
kind: Service
metadata:
  name: eso-nacos-service-grpc
  namespace: release
  annotations:
    nginx.ingress.kubernetes.io/affinity: "true"
    nginx.ingress.kubernetes.io/session-cookie-name: backend
    nginx.ingress.kubernetes.io/load-balancer-method: drr
spec:
  #type: NodePort
  selector:
    app: eso-nacos
  ports:
    - name: grpc
      port: 9848
      targetPort: 9848
      #nodePort: 31001

nacos-ingress

eso-nacos-ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: "nginx"
  name: eso-nacos
  namespace: release
spec:
  rules:
    - host: eso-nacos.com.cn
      http:
        paths:
          - path: /
            pathType: ImplementationSpecific
            backend:
              service:
                name: eso-nacos-service
                port: 
                  number: 8848
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐