1 k8s 部署nacos-2.1.2配置k8s-nacos-statefulSet.yaml文件
apiVersion: v1
kind: Service
metadata:name: nacos-headlessnamespace: rz-dtlabels:app: nacosannotations:service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:# 3个端口打开,否则外网应用连接不上k8s里面的nacosports:- protocol: TCPport: 8848name: servertargetPort: 8848- protocol: TCPport: 9848name: client-rpctargetPort: 9848- protocol: TCPport: 9849name: server-rpctargetPort: 9849clusterIP: Noneselector:app: nacos
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: nacosnamespace: rz-dt
spec:serviceName: nacos-headlessreplicas: 3template:metadata:labels:app: nacosannotations:pod.alpha.kubernetes.io/initialized: "true"spec:affinity:podAntiAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: "app"operator: Invalues:- nacos-headlesstopologyKey: "kubernetes.io/hostname"nodeSelector:rz_dt_nacos_node: rz-dt-nacoscontainers:- name: k8snacosimagePullPolicy: Always#v2.1.2可以重启nacos服务自动注册 2.0.3 不会自动注册服务 对应库nacos_configimage: nacos/nacos-server:v2.1.2resources:limits:cpu: 900mmemory: 2Girequests:cpu: 10mmemory: 50Miports:- containerPort: 8848name: client-port- containerPort: 9848name: client-grpc- containerPort: 9849name: server-grpc#环境变量配置env:- name: NACOS_REPLICASvalue: "3"- name: MYSQL_SERVICE_HOSTvalueFrom:configMapKeyRef:name: nacos-cmkey: mysql.host- name: MYSQL_SERVICE_DB_NAMEvalueFrom:configMapKeyRef:name: nacos-cmkey: mysql.db.name- name: MYSQL_SERVICE_PORTvalueFrom:configMapKeyRef:name: nacos-cmkey: mysql.port- name: MYSQL_SERVICE_USERvalueFrom:configMapKeyRef:name: nacos-cmkey: mysql.user- name: MYSQL_SERVICE_PASSWORDvalueFrom:configMapKeyRef:name: nacos-cmkey: mysql.password- name: MODEvalue: "cluster"- name: NACOS_SERVER_PORTvalue: "8848"- name: NACOS_APPLICATION_PORTvalue: "9848"- name: NACOS_APPLICATION_PORTvalue: "9849"- name: PREFER_HOST_MODEvalue: "hostname"- name: NACOS_SERVERSvalue: "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-servicenamespace: rz-dtannotations:nginx.ingress.kubernetes.io/affinity: "true"nginx.ingress.kubernetes.io/session-cookie-name: backendnginx.ingress.kubernetes.io/load-balancer-method: drrspec:selector:app: nacos# 3个端口打开,否则外网应用连接不上k8s里面的nacosports:- name: nacos-headlessprotocol: TCPport: 8848targetPort: 8848nodePort: 30048- name: nacos-rpcprotocol: TCPport: 9848targetPort: 9848nodePort: 31048- name: nacos-grpcprotocol: TCPport: 9849targetPort: 9849nodePort: 31049type: NodePort
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:name: nacos-webnamespace: rz-dt
spec:rules:- host: nacos.real.com.cnhttp:paths:- path: /nacosbackend:serviceName: nacos-serviceservicePort: 8848
---
apiVersion: v1
kind: ConfigMap
metadata:name: nacos-cmnamespace: 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-statefulSetecho "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-democloud: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:8848server-addr: nacos-service.rz-dt:8848shared-configs:- realize-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}discovery:metadata:preserved:heart:beat:interval: 1000timeout: 3000ip:delete:timeout: 3000server-addr: nacos-service.rz-dt:8848sentinel:datasource:ds1:nacos:data-type: jsondataId: sentinel-application-gatewaygroupId: DEFAULT_GROUPrule-type: flowserver-addr: nacos-service.rz-dt:8848eager: truetransport:dashboard: 127.0.0.1:8718sleuth:enabled: truereactor:instrumentation-type: decorate_on_each
4 k8s 里面nacos截图