k8s1.23.5部署nacos持久化集群

参考大神:https://blog.csdn.net/u011936655/article/details/108364176

  • 数据库配置
  1. 创建数据库

mysql5.7

create database nacos;

create user 'nacos'@'%' identified by 'nacoss';

grant all privileges on nacos.* to 'nacos'@'%';

SHOW DATABASES;

  1. 导入数据库

wget https://github.com/alibaba/nacos/blob/master/distribution/conf/nacos-mysql.sql

mysql -h 10.1.18.245 -u nacos -p nacos < nacos-mysql.sql

  • nacos配置

mkdir /data/nacos-cluster

cd /data/nacos-cluster

  1. configmap配置

cat nacos-configmap.yaml

apiVersion: v1

kind: ConfigMap

metadata:

  name: nacos-cm

  namespace: k8s-nacos #namespace

data:

  mysql.host: "10.1.18.245"

  mysql.db.name: "nacos"

  mysql.port: "3306"

  mysql.user: "nacos"

  mysql.password: "nacoss"

加载

kubectl apply -f nacos-configmap.yaml

查看

  1. headless部署

cat headless-server.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 headless-server.yaml

  1. statefulset部署

cat nacos-statefulset.yaml

apiVersion: apps/v1

kind: StatefulSet

metadata:

  name: nacos

  namespace: k8s-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:latest

          resources:

            requests:

              memory: "1Gi" #实际情况定

              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

查看

  1. service+ ingress配置

cat nacos-svc.yaml

apiVersion: v1

kind: Service

metadata:

  name: nacos-headless

  namespace: k8s-nacos

  annotations:

    kubernetes.io/ingress.class: "nginx"

spec:

  selector:

    app: nacos

  type: NodePort

  ports:

    - name: web

      port: 80

      targetPort: 8848

      nodePort: 30848 #这样IP+端口/nacos就可以直接外部访问了

---

apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

  name: nacos-web

#  namespace: k8s-nacos

  annotations:

    kubernetes.io/ingress.class: "nginx"

spec:

  rules:

    - host: nacos-web.nacos-demo.com #集群内部VIP作用

      http:

        paths:

        - pathType: Prefix

          path: /

          backend:

            service:

                name: nacos-headless

                port:

                  number: 8848

注意:重点是k8s集群高版本,配置内容也会变。

加载

kubectl apply -f nacos-svc.yaml

查看

  • 检验

访问nacos页面

http://192.168.0.27:30848/nacos

登录 nacos 密码nacos

添加了一个配置

集群内java pod访问

在node的hosts文件中解析

10.1.237.242   nacos-web.nacos-demo.com

集群内就可以把IP改为域名了。

也相当于是VIP了。

集群读取nacos验证等我把harbor和jenkins部署好再弄。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐