在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