k8s安裝配置nacos集群


1 在k8s中部署的難點

在k8s中部署nacos集群和在裸機器上直接部署nacos機器其實差別不大。
最主要的區別是k8s中部署的服務沒有固定的ip地址,而nacos集群部署需要配置所有實例的ip

2 解決

在k8s中通過StatefulSet和Headless Service為每個nacos實例生成一個唯一的dns地址,
創建一個普通Service給可客戶端使用

版本: nacos1.4.0

前提前提條件:

mysql 

Ingress Controller (用於ingress暴露方式)


具體實現如下。

3 實現方式

3.1 創建數據庫配置

vim nacos-mysql-config.yaml

apiVersion: v1
kind: ConfigMap
metadata:
   name: nacos-cm
data:
   mysql.host: "192.168.1.211"
   mysql.db.name: "nacos_config"
   mysql.port: "12345"
   mysql.user: "root"
   mysql.password: "root"

kubectl apply   -f  nacos-mysql-config.yaml

 

注意:我是直接現有mysql的nacos庫,如果要新建數據庫必須先在數據庫新建名稱為nacos_config的庫並導入對應nacos1.4.0下config/nacos-mysql.sql腳本導入到nacos_config庫

image

3.2 部署Headless Service

Headless Service為每個pod(nacos實例)生成一個DNS地址,用作NACOS_SERVERS配置

vim nacos-headless.yaml

apiVersion: v1
kind: Service
metadata:
   name: nacos-headless
   labels:
     app: nacos
   annotations:
     service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
   ports:
     - port: 8848
       name: server
       targetPort: 8848
   clusterIP: None
   selector:
     app: nacos

kubectl apply   -f  nacos-headless.yaml

3.3 通過StatefulSet部署nacos

StatefulSet部署方式為每個POD生成固定的名稱,如nacos-0、nacos-1、nacos-2等。

vim nacos-StatefulSet.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
   name: nacos
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"
       containers:
         - name: k8snacos
           imagePullPolicy: Always
           image: nacos/nacos-server:1.4.0
           resources:
             requests:
               memory: "2Gi"
               cpu: "500m"
           ports:
             - containerPort: 8848
               name: client
           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: PREFER_HOST_MODE
               value: "hostname"
             - name: NACOS_SERVERS
               value: "nacos-0.nacos-headless.default.svc.cluster.local:8848 nacos-1.nacos-headless.default.svc.cluster.local:8848 nacos-2.nacos-headless.default.svc.cluster.local:8848"
   selector:
     matchLabels:
       app: nacos

kubectl apply   -f  nacos-StatefulSet.yaml

3.4 部署普通Service

vim  nacos-service.yaml

 

apiVersion: v1
kind: Service
metadata:
   name: nacos-service
   namespace: default
   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
   ports:
     - name: web
       port: 80
       targetPort: 8848
       nodePort:  8848
   type: NodePort

 

 

kubectl apply   -f  nacos-service.yaml

或者使用下面的ingress,也可以一起使用---------------------------------------------------------------------------------------------------

3.5 配置Ingress

vim ingress-nacos.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
   name: nacos-web
   namespace: default

spec:
   rules:
     - host: nacos-web.nacos-demo.com
       http:
         paths:
           - path: /
             backend:
               serviceName: nacos-service
               servicePort: web

kubectl apply   -f  ingress-nacos.yaml

結果圖

image

最后,想通過域名 nacos-web.nacos-demo.com 可以訪問,需要在dns服務器解析域名或者本地wind的hosts添加本地解析

image

訪問

nacos-web.nacos-demo.com/nacos

image

或者

192.168.1.16:8848/nacos/

 

 

 

 

 

參考:https://blog.csdn.net/u011936655/article/details/108364176


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM