前言

之前写过使用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: nacos
nacos:
  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=false

persistence:
  enabled: false
  data:
    accessModes:
      - ReadWriteOnce

    # 使用alibabacloud-cnfs-nas存储类
    storageClassName: alibabacloud-cnfs-nas
    resources:
      requests:
        storage: 5Gi


service:
  #type: ClusterIP
  type: NodePort
  port: 8848
  nodePort: 30000


ingress:
  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.com

resources:
  # 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: 2Gi
annotations: { }

nodeSelector: { }

tolerations: [ ]

affinity: { }

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

接着我们来安装一下nacos,如下所示:

helm install nacos ./ -f values.yaml --namespace nacos
NAME: nacos
LAST DEPLOYED: Sat Jun 17 17:07:10 2023
NAMESPACE: nacos
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
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/nacos
2. 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 -nnacos
NAME          READY   STATUS    RESTARTS   AGE
pod/nacos-0   1/1     Running   0          21h

NAME               TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)                                                       AGE
service/nacos-cs   NodePort   192.168.58.227   <none>        8848:31798/TCP,9848:32226/TCP,9849:30232/TCP,7848:30000/TCP   21h

NAME                     READY   AGE
statefulset.apps/nacos   1/1     21h

很显然安装成功了!然后我们获取一下ip和port

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

这里说明一下,因为nacos ui是通过http协议访问的,而且8848端口是提供web访问的,所以这里的8848对外的端口就是31798,然后我们在浏览器中输入:xxx.xxx.xxx.xxx:31798就可以访问了,如下所示:
image.png

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
@RefreshScope
public 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.nacos

Server:        192.168.0.10
Address:    192.168.0.10#53

Name:    nacos-cs.nacos.svc.cluster.local
Address: 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(一)


Awbeci
3.1k 声望213 粉丝

Awbeci