k8s下部署nacos集群

这里使用的是外部 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

部署完成效果图

 

 




posted @ 2022-05-20 16:36  四毛二(ت)  阅读(1133)  评论(1编辑  收藏  举报
/* 鼠标点击求赞文字特效 */