首先在K8S中部署MYSQL,部署方式

创建MYSQL数据库

创建 nacos_config 数据库

CREATE DATABASE IF NOT EXISTS `nacos_config` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; 

创建表结构

脚本原文

配置 nacos ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
  namespace: spring-cloud-demo
data:
  platform: "mysql"
  mysql.db.host: "mysql-db-svc"
  mysql.db.name: "nacos_config"
  mysql.port: "3306"
  mysql.user: "root"
  mysql.password: "123456"

StatefulSet 脚本

apiVersion: apps/v1
kind: StatefulSet
metadata:
  namesapce: spring-cloud-demo
  name: nacos
spec:
  serviceName: nacos-headless
  replicas: 1  # 根据你的kubernetes集群node的数量决定,如果只有两个node,可以设置成2
  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: k8s-nacos
          imagePullPolicy: Always
          image: nacos/nacos-server:2.0.1
          resources:
            requests:
              memory: "2Gi"
              cpu: "500m"
          ports:
            - containerPort: 8848
              name: client
          env:
            - name: MODE
              value: standalone
            - name: SPRING_DATASOURCE_PLATFORM
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: platform
            - name: MYSQL_SERVICE_HOST
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.db.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: NACOS_SERVER_PORT
              value: "8848"
            - name: NACOS_APPLICATION_PORT
              value: "8848"
            - name: PREFER_HOST_MODE
              value: "ip"
  selector:
    matchLabels:
      app: nacos

在这里插入图片描述

部署 nacos service与ingresses

kind: Service
apiVersion: v1
metadata:
  labels:
    app: nacos-svc
  name: nacos-svc
  namespace: spring-cloud-demo
spec:
  selector:
    app: nacos
  ports:
  - name: port-8848
    port: 8848
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nacos-ingress
  namespace: spring-cloud-demo
spec:
  rules:
  - host: "nacos.benchan.com"
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: nacos-svc
            port:
              number: 8848

在这里插入图片描述
默认登录账号密码:
账号:nacos
密码:nacos

在这里插入图片描述
在这里插入图片描述

Logo

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

更多推荐