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截图

相关推荐
昌sit!3 小时前
K8S node节点没有相应的pod镜像运行故障处理办法
云原生·容器·kubernetes
A ?Charis6 小时前
Gitlab-runner running on Kubernetes - hostAliases
容器·kubernetes·gitlab
wclass-zhengge7 小时前
Docker篇(Docker Compose)
运维·docker·容器
茶馆大橘7 小时前
微服务系列五:避免雪崩问题的限流、隔离、熔断措施
java·jmeter·spring cloud·微服务·云原生·架构·sentinel
北漂IT民工_程序员_ZG7 小时前
k8s集群安装(minikube)
云原生·容器·kubernetes
coding侠客7 小时前
揭秘!微服务架构下,Apollo 配置中心凭啥扮演关键角色?
微服务·云原生·架构
梦魇梦狸º10 小时前
腾讯轻量云服务器docker拉取不到镜像的问题:拉取超时
docker·容器·github
南猿北者13 小时前
docker镜像仓库常用命令
运维·docker·容器
2301_8061313614 小时前
Kubernetes的基本构建块和最小可调度单元pod-0
云原生·容器·kubernetes
SilentCodeY14 小时前
containerd配置私有仓库registry
容器·kubernetes·containerd·镜像·crictl