​​​​​​

目录

概览

简单快速安装方式

使用外部数据库安装方式

修改配置

创建Nacos

部署ingress

测试


概览

Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。

Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。

简单快速安装方式

# clone项目
git clone https://github.com/nacos-group/nacos-k8s.git
# 快速开始,无Pvc,仅作示例
cd nacos-k8s
chmod +x quick-startup.sh
./quick-startup.sh

使用外部数据库安装方式

修改配置

  • 部署文件为:修改 deploy/nacos/nacos-no-pvc.yaml
  • 修改对应的命名空间及数据库配置
###使用自建数据库;使用Ingress发布配置后台###
---
apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  namespace: tools
  labels:
    app: nacos-headless
spec:
  type: ClusterIP
  clusterIP: None
  ports:
    - port: 8848
      name: server
      targetPort: 8848
    - port: 9848
      name: client-rpc
      targetPort: 9848
    - port: 9849
      name: raft-rpc
      targetPort: 9849
      ## 兼容1.4.x版本的选举端口
    - port: 7848
      name: old-raft-rpc
      targetPort: 7848
  selector:
    app: nacos
---
# 修改以下数据库配置
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
  namespace: tools
data:
  mysql.host: "192.168.1.4"
  mysql.db.name: "nacos"
  mysql.port: "3306"
  mysql.user: "nacos"
  mysql.password: "abc123456"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
  namespace: tools
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
              topologyKey: "kubernetes.io/hostname"
      containers:
        - name: k8snacos
          imagePullPolicy: Always
          image: nacos/nacos-server:latest
          resources:
            limits:
              cpu: "2000m"
              memory: "4096Mi"
            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_HOST
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.host
            - name: MYSQL_SERVICE_DB_NAME
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.db.name
            - 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: MODE
              value: "cluster"
            - name: NACOS_SERVER_PORT
              value: "8848"
            - name: PREFER_HOST_MODE
              value: "hostname"
            - name: NACOS_SERVERS
              value: "nacos-0.nacos-headless.tools.svc.cluster.local:8848 nacos-1.nacos-headless.tools.svc.cluster.local:8848 nacos-2.nacos-headless.tools.svc.cluster.local:8848"
  selector:
    matchLabels:
      app: nacos

创建Nacos

kubectl create -f nacos-k8s/deploy/nacos/nacos-no-pvc.yaml

部署ingress

  • ingress及对应service文件:nacos-service.yaml
  • 初始默认帐号密码:nacos/nacos
apiVersion: v1
kind: Service
metadata:
  name: nacos
  namespace: tools
spec:
  ports:
    - port: 8848
      name: server
      targetPort: 8848
    - port: 9848
      name: client-rpc
      targetPort: 9848
    - port: 9849
      name: raft-rpc
      targetPort: 9849
    - port: 7848
      name: old-raft-rpc
      targetPort: 7848
  selector:
    app: nacos
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nacos
  namespace: tools
  annotations:
    kubernetes.io/ingress.class: nginx-ingress
spec:
  rules:
  - host:nacosbeta.abc.tech
    http:
      paths:
      - path: /
        backend:
          serviceName: nacos
          servicePort: 8848
  tls:
  - secretName: abc-tech

测试

# 服务注册
curl -X PUT 'http://172.27.254.153:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'

# 服务发现
curl -X GET 'http://172.27.254.153:8848/nacos/v1/ns/instance/list?serviceName=nacos.naming.serviceName'

# 发布配置
curl -X POST "http://172.27.254.153:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=helloWorld"

# 获取配置
curl -X GET "http://172.27.254.153:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐