前言

之前写过应用k8s装置nacos,然而当初有一种更简略的办法来装置,那就是通过helm3来装置,上面是我装置的以及问题点总结,分享进去。

操作

首先咱们先从官网把nacos helm相干的代码down下来,操作如下:

git clone https://github.com/nacos-group/nacos-k8s.git

而后进入到helm目录,批改values.yaml文件,上面是我的:

# Default values for nacos.# This is a YAML-formatted file.# Declare variables to be passed into your templates.global:  mode: standalone#  mode: cluster############################nacos###########################namespace: nacosnacos:  image:    repository: nacos/nacos-server    tag: latest    pullPolicy: IfNotPresent  plugin:    enable: true    image:      repository: nacos/nacos-peer-finder-plugin      tag: 1.1      pullPolicy: IfNotPresent  replicaCount: 1  podManagementPolicy: Parallel  domainName: cluster.local  preferhostmode: hostname  serverPort: 8848  health:    enabled: false  storage:#    type: embedded    type: mysql    db:      host: host      name: name      port: 3306      username: username      password: password      param: characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=falsepersistence:  enabled: false  data:    accessModes:      - ReadWriteOnce    # 应用alibabacloud-cnfs-nas存储类    storageClassName: alibabacloud-cnfs-nas    resources:      requests:        storage: 5Giservice:  #type: ClusterIP  type: NodePort  port: 8848  nodePort: 30000ingress:  enabled: false  apiVersion: networking.k8s.io/v1#  apiVersion: extensions/v1beta1  annotations:    kubernetes.io/ingress.class: nginx    cert-manager.io/cluster-issuer: "letsencrypt-prod-http01"    kubernetes.io/tls-acme: "true"  hosts:    - host: nacos.seaurl.com      #paths: [ ]  tls:    - secretName: nacos-nginx-tls      hosts:        - nacos.seaurl.comresources:  # We usually recommend not to specify default resources and to leave this as a conscious  # choice for the user. This also increases chances charts run on environments with little  # resources, such as Minikube. If you do want to specify resources, uncomment the following  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.  # limits:  #   cpu: 100m  #   memory: 128Mi  requests:    cpu: 500m    memory: 2Giannotations: { }nodeSelector: { }tolerations: [ ]affinity: { }

我设置的nacos是mode: standalone单例模式,并且应用了mysql存储配置文件,并且不应用ingress,而是应用service.type: NodePort通过ip:port的模式裸露进去。

接着咱们来装置一下nacos,如下所示:

helm install nacos ./ -f values.yaml --namespace nacosNAME: nacosLAST DEPLOYED: Sat Jun 17 17:07:10 2023NAMESPACE: nacosSTATUS: deployedREVISION: 1TEST SUITE: NoneNOTES:1. Get the application URL by running these commands:  export NODE_PORT=$(kubectl get --namespace nacos -o jsonpath="{.spec.ports[0].nodePort}" services  nacos-cs)  export NODE_IP=$(kubectl get nodes --namespace nacos -o jsonpath="{.items[0].status.addresses[0].address}")  echo http://$NODE_IP:$NODE_PORT/nacos2. MODE:   standalone: you need to modify replicaCount in the values.yaml, .Values.replicaCount=1   cluster: kubectl scale sts nacos-nacos --replicas=3

装置好了之后,咱们查看控制台显示是不是装置好了,如下所示:

☁ ⚡  kubectl get all -nnacosNAME          READY   STATUS    RESTARTS   AGEpod/nacos-0   1/1     Running   0          21hNAME               TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)                                                       AGEservice/nacos-cs   NodePort   192.168.58.227   <none>        8848:31798/TCP,9848:32226/TCP,9849:30232/TCP,7848:30000/TCP   21hNAME                     READY   AGEstatefulset.apps/nacos   1/1     21h

很显然装置胜利了!而后咱们获取一下ip和port

# 获取ipkubectl get nodes --namespace nacos -o jsonpath="{.items[0].status.addresses[0].address}"# 获取portkubectl get --namespace nacos -o jsonpath="{.spec.ports[0].nodePort}" services  nacos-cs

这里阐明一下,因为nacos ui是通过http协定拜访的,而且8848端口是提供web拜访的,所以这里的8848对外的端口就是31798,而后咱们在浏览器中输出:xxx.xxx.xxx.xxx:31798就能够拜访了,如下所示:

spring cloud配置nacos

nacos装置胜利之后,咱们能够在springcloud中配置并应用,上面是我的配置步骤:
1、引入依赖包

<dependency>    <groupId>com.alibaba.cloud</groupId>    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency>    <groupId>com.alibaba.cloud</groupId>    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency>

2、配置启动类

@EnableDiscoveryClient@SpringBootApplication// 服务发现@EnableDiscoveryClient// 启用feign@EnableFeignClients@RefreshScopepublic class AccountServiceApplication{}

3、配置bootstrap.yaml

留神,nacos在springcloud中对应的端口是9848,从下面装置后返回的信息可知,9848对外的端口是32226,所以咱们这里应用ip:32226的模式

spring:  cloud:    nacos:      discovery:        server-addr: xxx.xxx.xxx.xxx:32226        namespace: ns        service: server.name      config:        server-addr: xxx.xxx.xxx.xxx:32226        namespace: ns        prefix: server.name        file-extension: yaml

设置好了之后,咱们启动java程序,发现报错,如下所示:

2023-06-18T15:13:06.355+08:00 ERROR 70286 --- [t.remote.worker] c.a.n.c.remote.client.grpc.GrpcClient    : Server check fail, please check server xxx.xxx.xxx.xxx ,port 33226 is available , error ={}java.util.concurrent.ExecutionException: com.alibaba.nacos.shaded.io.grpc.StatusRuntimeException: UNAVAILABLE: io exception

下面的错误信息说,你的ip:xxx.xxx.xxx.xxx和port:33226有问题,问题是咱们配置的端口是32226,为什么在程序中变成了33226了,起初才晓得这是nacos的实现机制,它会在8848端口下面主动+1000,所以咱们配置的32226就变成了33226了,解决的方法就是-1000,所以咱们改成31226就没问题了。

spring:  cloud:    nacos:      discovery:        server-addr: xxx.xxx.xxx.xxx:31226        namespace: ns        service: server.name      config:        server-addr: xxx.xxx.xxx.xxx:31226        namespace: ns        prefix: server.name        file-extension: yaml

改好之后,启动就没问题了。

下面演示的是测试环境,如果是实在k8s集群下的springcloud和nacos,能够间接应用域名拜访,域名能够从上面命令获取:

kubectl exec -i -t dnsutils -- nslookup nacos-cs.nacosServer:        192.168.0.10Address:    192.168.0.10#53Name:    nacos-cs.nacos.svc.cluster.localAddress: 192.168.58.227

有了域名,咱们改一下下面的配置文件,如下所示:

spring:  cloud:    nacos:      discovery:        server-addr: http://nacos-cs.nacos.svc.cluster.local:8848        namespace: ns        service: server.name      config:        server-addr: http://nacos-cs.nacos.svc.cluster.local:8848        namespace: ns        prefix: server.name        file-extension: yaml

留神:同理,nacos会主动+1000,所以实在的port应该是9848!

总结

1、最近在降级java17,于是着手重新安装nacos,发现了下面的port问题,心愿对大家有所帮忙

援用

nacos helm
springboot3.0集成nacos2.2.1(一)