乐趣区

关于helm3:helm3安装nacos

前言

之前写过应用 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 就能够拜访了,如下所示:

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(一)

退出移动版