记录一次在k8s上nacos2.3.1无法注册微服务的问题

**问题回顾: **在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

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的微服务启动脚本,用于将微服务注册Nacos: ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: your-service labels: app: your-service spec: replicas: 1 selector: matchLabels: app: your-service template: metadata: labels: app: your-service spec: containers: - name: your-service image: your-service-image:latest ports: - containerPort: 8080 # 服务端口 env: - name: SPRING_APPLICATION_NAME # 应用名称 value: your-service - name: SPRING_PROFILES_ACTIVE # 激活的配置文件 value: prod - name: SERVER_PORT # 服务端口 value: "8080" - name: NACOS_SERVER_ADDR # Nacos服务地址 value: nacos-server:8848 volumeMounts: - name: config-volume mountPath: /usr/src/app/config # 配置文件目录 volumes: - name: config-volume configMap: name: your-service-config # 配置文件ConfigMap的名称 ``` 注意事项: 1. `your-service` 为服务名称,需要替换成实际的服务名称; 2. `your-service-image` 为服务镜像名称,需要替换成实际的镜像名称; 3. `8080` 为服务端口,需要替换成实际的服务端口; 4. `prod` 为激活的配置文件,需要替换成实际的配置文件; 5. `nacos-server:8848` 为Nacos服务地址,需要替换成实际的Nacos服务地址。 将上述内容保存为一个 YAML 文件,例如 `your-service-deployment.yaml`,然后使用 `kubectl apply -f your-service-deployment.yaml` 命令来部署微服务。部署后,该微服务将自动注册Nacos中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pengkai焱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值