k8s下部署nacos叢集
阿新 • • 發佈:2022-05-20
這裡使用的是外部 mysql 資料庫,所以沒有使用官方提供的 mysql 和儲存方案。
1、clone 專案
git clone https://github.com/nacos-group/nacos-k8s.git
2、修改 yaml 檔案,這裡要注意 NACOS_SERVERS 的格式
cat 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: "192.168.2.181" # 新增外部 mysql 訪問地址 mysql.db.name: "nacos" mysql.port: "30006" mysql.user: "root" mysql.password: "mysql@@0627" --- 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.default.svc.cluster.local:8848 nacos-1.nacos-headless.default.svc.cluster.local:8848 nacos-2.nacos-headless.default.svc.cluster.local:8848" # 修改 namespace 名稱 selector: matchLabels: app: nacos
部署 nacos
kubectl apply -f nacos-quick-start.yaml
檢視服務狀態
kubectl get po -n test
部署完成效果圖