**问题回顾: **在k8s平台上部署nacos2.3.1,若依微服务无法注册到nacos上。
镜像为:nacos/nacos-server:v2.3.1
image.png

01:57:24.818 [main] WARN  o.s.c.l.c.LoadBalancerCacheAutoConfiguration$LoadBalancerCaffeineWarnLogger - [logWarning,83] - Spring Cloud LoadBalancer is currently working with the default cache. While this cache implementation is useful for development and tests, it's recommended to use Caffeine cache in production.You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.
01:57:24.869 [main] INFO  c.a.n.p.a.s.c.ClientAuthPluginManager - [init,56] - [ClientAuthPluginManager] Load ClientAuthService com.alibaba.nacos.client.auth.impl.NacosClientAuthServiceImpl success.
01:57:24.869 [main] INFO  c.a.n.p.a.s.c.ClientAuthPluginManager - [init,56] - [ClientAuthPluginManager] Load ClientAuthService com.alibaba.nacos.client.auth.ram.RamClientAuthServiceImpl success.
01:57:31.332 [main] INFO  c.a.c.s.d.c.SentinelConverter - [convert,79] - converter can not convert rules because source is empty
01:57:31.642 [main] INFO  c.a.n.p.a.s.c.ClientAuthPluginManager - [init,56] - [ClientAuthPluginManager] Load ClientAuthService com.alibaba.nacos.client.auth.impl.NacosClientAuthServiceImpl success.
01:57:31.643 [main] INFO  c.a.n.p.a.s.c.ClientAuthPluginManager - [init,56] - [ClientAuthPluginManager] Load ClientAuthService com.alibaba.nacos.client.auth.ram.RamClientAuthServiceImpl success.
01:57:38.254 [main] ERROR c.a.c.n.r.NacosServiceRegistry - [register,81] - nacos registry, ruoyi-gateway register failed...NacosRegistration{nacosDiscoveryProperties=NacosDiscoveryProperties{serverAddr='10.10.10.201:30081', username='nacos', password='nacos', endpoint='', namespace='', watchDelay=30000, logName='', service='ruoyi-gateway', weight=1.0, clusterName='DEFAULT', group='DEFAULT_GROUP', namingLoadCacheAtStart='false', metadata={IPv6=null, preserved.register.source=SPRING_CLOUD}, registerEnabled=true, ip='10.10.10.3', networkInterface='', port=8080, secure=false, accessKey='', secretKey='', heartBeatInterval=null, heartBeatTimeout=null, ipDeleteTimeout=null, instanceEnabled=true, ephemeral=true, failureToleranceEnabled=false}, ipDeleteTimeout=null, failFast=true}},
com.alibaba.nacos.api.exception.NacosException: Client not connected, current status:STARTING
	at com.alibaba.nacos.common.remote.client.RpcClient.request(RpcClient.java:639)
	at com.alibaba.nacos.common.remote.client.RpcClient.request(RpcClient.java:619)
	at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.requestToServer(NamingGrpcClientProxy.java:356)
	at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.doRegisterService(NamingGrpcClientProxy.java:209)
	at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.registerService(NamingGrpcClientProxy.java:123)
	at com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate.registerService(NamingClientProxyDelegate.java:98)
	at com.alibaba.nacos.client.naming.NacosNamingService.registerInstance(NacosNamingService.java:152)
	at com.alibaba.cloud.nacos.registry.NacosServiceRegistry.register(NacosServiceRegistry.java:75)
	at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.register(AbstractAutoServiceRegistration.java:232)
	at com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration.register(NacosAutoServiceRegistration.java:78)
	at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.start(AbstractAutoServiceRegistration.java:133)
	at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.bind(AbstractAutoServiceRegistration.java:98)
	at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.onApplicationEvent(AbstractAutoServiceRegistration.java:86)
	at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.onApplicationEvent(AbstractAutoServiceRegistration.java:47)

nacos后台界面也没有看到微服务的注册信息

一、开启鉴权功能

首先检查配置文件,因为nacos2.2.2版本之前的Nacos默认控制台,无论服务端是否开启鉴权,都会存在一个登录页;这导致很多用户被误导认为Nacos默认是存在鉴权的。在社区安全工程师的建议下,Nacos自2.2.2版本开始,在未开启鉴权时,默认控制台将不需要登录即可访问,同时在控制台中给予提示,提醒用户当前集群未开启鉴权。
检查k8s中的nacos的配置文件,开启鉴权功能:
image.png
具体的配置项为:

nacos.core.auth.enabled=true

参考官方文档地址:https://nacos.io/zh-cn/docs/v2/guide/user/auth.html
k8s中nacos配置文件的挂载位置为:/home/nacos/conf/application.properties
image.png
必须指定子路径。

二、开放9848、9849端口

根据nacos官方文档说明,我们需要开放9848,9849端口,7848端口视情况而定选择开放。
Nacos2.0版本相比1.X新增了gRPC的通信方式,因此需要增加2个端口。新增端口是在配置的主端口(server.port)基础上,进行一定偏移量自动生成。
而且最好是设置为TCP模式的,尽量别设置成HTTP协议的。
官方文档:https://nacos.io/zh-cn/docs/v2/upgrading/2.0.0-compatibility.html
image.png
所以在k8s中的容器部署的时候,要多加2个端口,如下图
image.png
然后对应的svc服务也是一样的。
image.png如果要暴露给外网访问(NodePort模式),则应该注意端口的偏移量,官方文档有说明偏移量是1000
image.png
我们可以手动调整对应的yaml文件
image.png

kind: Service
apiVersion: v1
metadata:
  name: nacos-svc
  namespace: rou-yi
  labels:
    app: nacos-svc
  annotations:
    kubesphere.io/creator: cto-he
    kubesphere.io/description: nacos的访问服务
spec:
  ports:
    - name: tcp-8848
      protocol: TCP
      port: 8848
      targetPort: 8848
      nodePort: 30081
    - name: tcp-9848
      protocol: TCP
      port: 9848
      targetPort: 9848
      nodePort: 31081
    - name: tcp-9849
      protocol: TCP
      port: 9849
      targetPort: 9849
      nodePort: 31082
  selector:
    app: nacos-ruoyi
  clusterIP: 172.30.199.133
  clusterIPs:
    - 172.30.199.133
  type: NodePort
  sessionAffinity: None
  externalTrafficPolicy: Cluster
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack
  internalTrafficPolicy: Cluster

注意端口对应的偏移量是1000

完成以上2步设置,则可以成功启动若依项目了。

image.png
nacos中也能注册成功了:
image.png
参考:https://blog.csdn.net/twosvip/article/details/125467664
参考:https://nacos.io/docs/v2/what-is-nacos/

附录

附上整个部署的yaml

 kind: Deployment
apiVersion: apps/v1
metadata:
  name: nacos-ruoyi
  namespace: rou-yi
  labels:
    app: nacos-ruoyi
  annotations:
    deployment.kubernetes.io/revision: '8'
    kubesphere.io/creator: cto-he
    kubesphere.io/description: '若依项目的nacos,单节点部署'
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nacos-ruoyi
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nacos-ruoyi
      annotations:
        kubesphere.io/creator: cto-he
        kubesphere.io/imagepullsecrets: '{}'
        kubesphere.io/restartedAt: '2024-03-24T14:56:32.551Z'
        logging.kubesphere.io/logsidecar-config: '{}'
    spec:
      volumes:
        - name: host-time
          hostPath:
            path: /etc/localtime
            type: ''
        - name: volume-3jqgyp
          configMap:
            name: nacos-conf
            defaultMode: 420
      containers:
        - name: nacos-ruoyi
          image: 'nacos/nacos-server:v2.3.1'
          ports:
            - name: tcp-8848
              containerPort: 8848
              protocol: TCP
            - name: tcp-9848
              containerPort: 9848
              protocol: TCP
            - name: tcp-9849
              containerPort: 9849
              protocol: TCP
          env:
            - name: MODE
              value: standalone
            - name: JVM_XMS
              value: 512m
            - name: JVM_XMX
              value: 512m
          resources:
            limits:
              cpu: 500m
              memory: 1Gi
          volumeMounts:
            - name: host-time
              readOnly: true
              mountPath: /etc/localtime
            - name: volume-3jqgyp
              readOnly: true
              mountPath: /home/nacos/conf/application.properties
              subPath: application.properties
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      serviceAccountName: default
      serviceAccount: default
      securityContext: {}
      schedulerName: default-scheduler
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  revisionHistoryLimit: 10
  progressDeadlineSeconds: 600

如果我的文章对您有帮助,还请您多多支持我。支付宝帮忙扫一下吧
在这里插入图片描述

GitHub 加速计划 / na / nacos
29.83 K
12.75 K
下载
Nacos是由阿里巴巴开源的服务治理中间件,集成了动态服务发现、配置管理和服务元数据管理功能,广泛应用于微服务架构中,简化服务治理过程。
最近提交(Master分支:3 个月前 )
4334cd16 * Support custom client configuration timeout.(#12748) * Add UT.(#12748) 12 天前
b04d2266 16 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐