在Kubernetes上部署Nacos

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

1、clone 项目

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

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

vim nacos-k8s/deploy/nacos/nacos-quick-start.yaml
---
# 添加一个 namespace
apiVersion: v1
kind: Namespace
metadata:
  name: nacos
---
apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  namespace: nacos
  labels:
    app: nacos-headless
spec:
  type: NodePort
  ports:
    - port: 8848
      name: server
      targetPort: 8848
      nodePort: 30112 # 添加 nodePort 曝光控制台
    - port: 9848
      name: client-grpc
      targetPort: 9848
      nodePort: 31112 # 客户端gRPC请求服务端端口,用于客户端向服务端发起连接和请求
    - port: 9849
      name: raft-grpc
      targetPort: 9849
      nodePort: 31113 # 服务端gRPC请求服务端端口,用于服务间同步等
    - port: 7848
      name: old-raft-rpc
      targetPort: 7848 # 兼容1.4.x版本的选举端口
  selector:
    app: nacos
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
  namespace: nacos
data:
  mysql.db.host: "192.168.10.4" # 添加外部 mysql 访问地址
  mysql.db.name: "nacos_config"
  mysql.port: "3306"
  mysql.user: "nacos"
  mysql.password: "china801"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
  namespace: nacos
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: nacos
          imagePullPolicy: Always
          image: nacos/nacos-server:latest
          resources:
            requests:
              memory: "2Gi"
              cpu: "500m"
          ports:
            - containerPort: 8848
              name: client
            - containerPort: 9848
              name: client-rpc
            - containerPort: 9849
              name: raft-rpc
            - containerPort: 7848
              name: old-raft-rpc
          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.nacos.svc.cluster.local:8848 nacos-1.nacos-headless.nacos.svc.cluster.local:8848 nacos-2.nacos-headless.nacos.svc.cluster.local:8848" # 修改 namespace 名称
  selector:
    matchLabels:
      app: nacos

3、部署 nacos

kubectl apply -f nacos-quick-start.yaml

4、查看服务状态

kubectl get po -n nacos

正常情况效果如下:

参考文档

posted @ 2020-06-12 15:54  KeithTt  阅读(3927)  评论(2编辑  收藏  举报