• 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

    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

  • 相关阅读:
    编译和和运行amor
    用好C语言的中库,系统错误的处理
    C语言中的宏
    时隔多年,再次实现的链表
    脚本更改桌面背景
    python爬虫 一个security的RSA加密爬虫
    mysql 8.0版本
    mysql5.7的并行复制
    kinshard中间件问题
    Springboot2-@Transactional 事务不生效
  • 原文地址:https://www.cnblogs.com/pengrj/p/15711687.html
Copyright © 2020-2023  润新知