在Kubernetes上部署Nacos


这里使用的是外部 mysql 数据库,所以没有使用官方提供的 mysql 和存储方案。

1、clone 项目

git clone https://github.com/nacos-group/nacos-k8s.git

2、修改 yaml 文件,这里要注意 NACOS_SERVERS 的格式

cd nacos-k8s/deploy/nacos/
vim nacos-quick-start.yaml
---
# 添加一个 namespace
apiVersion: v1
kind: Namespace
metadata:
  name: yunjing-test
---
apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  namespace: yunjing-test
  labels:
    app: nacos-headless
spec:
  ports:
    - port: 8848
      name: server
      targetPort: 8848
      nodePort: 30112 # 添加 nodePort 曝光控制台
  selector:
    app: nacos
  type: NodePort
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
  namespace: yunjing-test
data:
  mysql.db.host: "172.20.72.200" # 添加外部 mysql 访问地址
  mysql.db.name: "nacos_k8s"
  mysql.port: "3306"
  mysql.user: "nacos_rw"
  mysql.password: "nacos_rw@unicom"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
  namespace: yunjing-test
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: "2Gi"
              cpu: "500m"
          ports:
            - containerPort: 8848
              name: client
          env:
            - name: NACOS_REPLICAS
              value: "3"
            - name: MYSQL_SERVICE_DB_NAME
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.db.name
            - name: MYSQL_SERVICE_HOST # 添加 mysql 访问地址的环境变量
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.db.host
            - 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: PREFER_HOST_MODE
              value: "hostname"
            - name: NACOS_SERVERS
              value: "nacos-0.nacos-headless.yunjing-test.svc.cluster.local:8848 nacos-1.nacos-headless.yunjing-test.svc.cluster.local:8848 nacos-2.nacos-headless.yunjing-test.svc.cluster.local:8848" # 修改 namespace 名称
  selector:
    matchLabels:
      app: nacos

3、部署 nacos

kubectl apply -f nacos-quick-start.yaml

4、查看服务状态

kubectl get po -n yunjing-test

正常情况效果如下:

参考:
https://nacos.io/zh-cn/docs/use-nacos-with-kubernetes.html
https://hub.docker.com/r/nacos/nacos-server


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM