k8s+springcloud+nacos部署配置

1 k8s 部署nacos-2.1.2配置k8s-nacos-statefulSet.yaml文件

apiVersion: v1
kind: Service
metadata:
   name: nacos-headless
   namespace: rz-dt
   labels:
     app: nacos
   annotations:
     service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
 # 3个端口打开,否则外网应用连接不上k8s里面的nacos
   ports:
     - protocol: TCP
       port: 8848
       name: server
       targetPort: 8848
     - protocol: TCP
       port: 9848
       name: client-rpc
       targetPort: 9848
     - protocol: TCP
       port: 9849
       name: server-rpc
       targetPort: 9849
   clusterIP: None
   selector:
     app: nacos
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
   name: nacos
   namespace: rz-dt
spec:
   serviceName: nacos-headless
   replicas: 3
   template:
     metadata:
       labels:
         app: nacos
       annotations:
         pod.alpha.kubernetes.io/initialized: "true"
     spec:
       affinity:
         podAntiAffinity:
           requiredDuringSchedulingIgnoredDuringExecution:
             - labelSelector:
                 matchExpressions:
                   - key: "app"
                     operator: In
                     values:
                       - nacos-headless
               topologyKey: "kubernetes.io/hostname"
       nodeSelector:
           rz_dt_nacos_node: rz-dt-nacos
       containers:
         - name: k8snacos
           imagePullPolicy: Always
           #v2.1.2可以重启nacos服务自动注册 2.0.3 不会自动注册服务 对应库nacos_config
           image: nacos/nacos-server:v2.1.2
           resources:
             limits:
               cpu: 900m
               memory: 2Gi
             requests:
               cpu: 10m
               memory: 50Mi
           ports:
             - containerPort: 8848
               name: client-port
             - containerPort: 9848
               name: client-grpc
             - containerPort: 9849
               name: server-grpc
           #环境变量配置
           env:
             - name: NACOS_REPLICAS
               value: "3"
             - name: MYSQL_SERVICE_HOST
               valueFrom:
                 configMapKeyRef:
                   name: nacos-cm
                   key: mysql.host
             - name: MYSQL_SERVICE_DB_NAME
               valueFrom:
                 configMapKeyRef:
                   name: nacos-cm
                   key: mysql.db.name
             - name: MYSQL_SERVICE_PORT
               valueFrom:
                 configMapKeyRef:
                   name: nacos-cm
                   key: mysql.port
             - name: MYSQL_SERVICE_USER
               valueFrom:
                 configMapKeyRef:
                   name: nacos-cm
                   key: mysql.user
             - name: MYSQL_SERVICE_PASSWORD
               valueFrom:
                 configMapKeyRef:
                   name: nacos-cm
                   key: mysql.password
             - name: MODE
               value: "cluster"
             - name: NACOS_SERVER_PORT
               value: "8848"
             - name: NACOS_APPLICATION_PORT
               value: "9848"
             - name: NACOS_APPLICATION_PORT
               value: "9849"
             - name: PREFER_HOST_MODE
               value: "hostname"
             - name: NACOS_SERVERS
               value: "nacos-0.nacos-headless.rz-dt.svc.cluster.local:8848 nacos-1.nacos-headless.rz-dt.svc.cluster.local:8848 nacos-2.nacos-headless.rz-dt.svc.cluster.local:8848"
   selector:
     matchLabels:
       app: nacos
---
apiVersion: v1
kind: Service
metadata:
   name: nacos-service
   namespace: rz-dt
   annotations:
     nginx.ingress.kubernetes.io/affinity: "true"
     nginx.ingress.kubernetes.io/session-cookie-name: backend
     nginx.ingress.kubernetes.io/load-balancer-method: drr

spec:
   selector:
     app: nacos
   # 3个端口打开,否则外网应用连接不上k8s里面的nacos
   ports:
     - name: nacos-headless
       protocol: TCP
       port: 8848
       targetPort: 8848
       nodePort: 30048
     - name: nacos-rpc
       protocol: TCP
       port: 9848
       targetPort: 9848
       nodePort: 31048
     - name: nacos-grpc
       protocol: TCP
       port: 9849
       targetPort: 9849
       nodePort: 31049
   type: NodePort
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
   name: nacos-web
   namespace: rz-dt
spec:
   rules:
     - host: nacos.real.com.cn
       http:
         paths:
           - path: /nacos
             backend:
               serviceName: nacos-service
               servicePort: 8848
---
apiVersion: v1
kind: ConfigMap
metadata:
   name: nacos-cm
   namespace: rz-dt
data:
   mysql.host: "rm-uf6c2o6h7hg.mysql.rds.aliyuncs.com"
   mysql.db.name: "nacos_config"
   mysql.port: "3306"
   mysql.user: "real"
   mysql.password: "1234"

2 k8s 一键部署脚本 test.sh

echo "开始制作镜像..."
image_name=k8s-nacos-statefulSet

echo "k8s一键部署"
export IMG_NAME=${image_name}
envsubst < ${image_name}.yaml | kubectl --kubeconfig ~/.kube-rz/config apply -f -

3 springcloud 微服务中配置nacos地址properties

server.port=8090
logging.level.org.springframework.cloud.sleuth=info
spring.application.name=k8s-springboot-demo
#spring.profiles.active=dev
spring.sleuth.enabled=true
spring.sleuth.reactor.instrumentation-type=decorate_on_each
#spring.config.activate.on-profile=dev
#spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
#spring.cloud.nacos.config.server-addr=127.0.0.1:8848
#spring.cloud.nacos.config.file-extension=yml
#spring.cloud.nacos.config.shared-configs[0]=realize-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
#spring.cloud.sentinel.eager=true
#spring.cloud.sentinel.transport.dashboard=127.0.0.1:8718
#spring.cloud.sentinel.datasource.ds1.nacos.server-addr=127.0.0.1:8848
#spring.cloud.sentinel.datasource.ds1.nacos.dataId=sentinel-application-gateway
#spring.cloud.sentinel.datasource.ds1.nacos.groupId=DEFAULT_GROUP
#spring.cloud.sentinel.datasource.ds1.nacos.data-type=json
#spring.cloud.sentinel.datasource.ds1.nacos.rule-type=flow

#spring.config.activate.on-profile=server 
# nacos访问地址 服务名.命名空间:服务端口号
spring.cloud.nacos.discovery.server-addr=nacos-service.rz-dt:8848
#spring.cloud.nacos.discovery.server-addr=nacos-0.nacos-headless.rz-dt.svc.cluster.local:8848,nacos-1.nacos-headless.rz-dt.svc.cluster.local:8848,nacos-2.nacos-headless.rz-dt.svc.cluster.local:8848
spring.cloud.nacos.discovery.metadata.preserved.heart.beat.interval=1000
spring.cloud.nacos.discovery.metadata.preserved.heart.beat.timeout=3000
spring.cloud.nacos.discovery.metadata.preserved.ip.delete.timeout=3000
# nacos访问地址 服务名.命名空间:服务端口号
spring.cloud.nacos.config.server-addr=nacos-service.rz-dt:8848
#spring.cloud.nacos.config.server-addr=nacos-0.nacos-headless.rz-dt.svc.cluster.local:8848,nacos-1.nacos-headless.rz-dt.svc.cluster.local:8848,nacos-2.nacos-headless.rz-dt.svc.cluster.local:8848
spring.cloud.nacos.config.file-extension=yml
spring.cloud.nacos.config.shared-configs[0]=realize-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
spring.cloud.sentinel.eager=true
spring.cloud.sentinel.transport.dashboard=127.0.0.1:8718
spring.cloud.sentinel.datasource.ds1.nacos.server-addr=nacos-service.rz-dt:8848
#spring.cloud.sentinel.datasource.ds1.nacos.server-addr=nacos-0.nacos-headless.rz-dt.svc.cluster.local:8848,nacos-1.nacos-headless.rz-dt.svc.cluster.local:8848,nacos-2.nacos-headless.rz-dt.svc.cluster.local:8848
spring.cloud.sentinel.datasource.ds1.nacos.dataId=sentinel-application-gateway
spring.cloud.sentinel.datasource.ds1.nacos.groupId=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds1.nacos.data-type=json
spring.cloud.sentinel.datasource.ds1.nacos.rule-type=flow

springcloud 微服务中配置nacos地址yaml

logging:
  level:
    org:
      springframework:
        cloud:
          sleuth: info
server:
  port: 8090
spring:
  application:
    name: k8s-springboot-demo
  cloud:
    nacos:
      config:
        file-extension: yml
        # nacos访问地址 服务名.命名空间:服务端口号
        #spring.cloud.nacos.discovery.server-addr=nacos-0.nacos-headless.rz-dt.svc.cluster.local:8848,nacos-1.nacos-headless.rz-dt.svc.cluster.local:8848,nacos-2.nacos-headless.rz-dt.svc.cluster.local:8848
        server-addr: nacos-service.rz-dt:8848
        shared-configs:
        - realize-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
      discovery:
        metadata:
          preserved:
            heart:
              beat:
                interval: 1000
                timeout: 3000
            ip:
              delete:
                timeout: 3000
        server-addr: nacos-service.rz-dt:8848
    sentinel:
      datasource:
        ds1:
          nacos:
            data-type: json
            dataId: sentinel-application-gateway
            groupId: DEFAULT_GROUP
            rule-type: flow
            server-addr: nacos-service.rz-dt:8848
      eager: true
      transport:
        dashboard: 127.0.0.1:8718
  sleuth:
    enabled: true
    reactor:
      instrumentation-type: decorate_on_each

4 k8s 里面nacos截图

相关推荐
Python私教33 分钟前
ubuntu搭建k8s环境详细教程
linux·ubuntu·kubernetes
运维&陈同学1 小时前
【zookeeper01】消息队列与微服务之zookeeper工作原理
运维·分布式·微服务·zookeeper·云原生·架构·消息队列
O&REO2 小时前
单机部署kubernetes环境下Overleaf-基于MicroK8s的Overleaf应用部署指南
云原生·容器·kubernetes
politeboy2 小时前
k8s启动springboot容器的时候,显示找不到application.yml文件
java·spring boot·kubernetes
运维小文3 小时前
K8S资源限制之LimitRange
云原生·容器·kubernetes·k8s资源限制
登云时刻3 小时前
Kubernetes集群外连接redis集群和使用redis-shake工具迁移数据(二)
redis·容器·kubernetes
wuxingge12 小时前
k8s1.30.0高可用集群部署
云原生·容器·kubernetes
志凌海纳SmartX13 小时前
趋势洞察|AI 能否带动裸金属 K8s 强势崛起?
云原生·容器·kubernetes
锅总13 小时前
nacos与k8s service健康检查详解
云原生·容器·kubernetes
BUG弄潮儿13 小时前
k8s 集群安装
云原生·容器·kubernetes