k8s部署nacos集群

k8s部署nacos集群参考过很多文档,大部分写得都很复杂;无意间看到一位大佬文档,参考了他的文档但是没注意保存文档,寻找不到。
nacos安装和直接宿主机安装差不多,只是访问实列的IP需要通过svc暴露。集群内部使用Headless Service分别分配固定的访问地址,再建一个普通的svc通过ingress转发通过web界面操作即可。

1、建立连接MySQL配置
vi 1-nacos-db-config.yaml

# nacos连接mysql配置
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-db-config
data:
  mysql.host: "1.2.32.4"
  mysql.db.name: "nacos_config"
  mysql.port: "3306"
  mysql.user: "nacos_config"
  mysql.password: "nacos_config_db@yangdouya"

执行部署
kubectl apply -f 1-nacos-db-config.yaml

2、新建一个集群内部数量服务nacos-headless
vi 2-nacos-headless.yaml

# Headless Service为每个pod(nacos实例)生成一个DNS地址,用作NACOS_SERVERS配置
apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  labels:
    app: nacos
  annotations:
    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
  ports:
    - port: 8848
      name: server
      targetPort: 8848
  clusterIP: None
  selector:
    app: nacos

执行部署
kubectl apply -f 2-nacos-headless.yaml

3、使用statefulSet部署nacos集群
vi 3-naocs-statefulset.yaml

# StatefulSet部署方式为每个POD生成固定的名称,如nacos-0、nacos-1、nacos-2等
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: 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: 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_HOST
              valueFrom:
                configMapKeyRef:
                  name: nacos-db-config
                  key: mysql.host
            - name: MYSQL_SERVICE_DB_NAME
              valueFrom:
                configMapKeyRef:
                  name: nacos-db-config
                  key: mysql.db.name
            - name: MYSQL_SERVICE_PORT
              valueFrom:
                configMapKeyRef:
                  name: nacos-db-config
                  key: mysql.port
            - name: MYSQL_SERVICE_USER
              valueFrom:
                configMapKeyRef:
                  name: nacos-db-config
                  key: mysql.user
            - name: MYSQL_SERVICE_PASSWORD
              valueFrom:
                configMapKeyRef:
                  name: nacos-db-config
                  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.default.svc.cluster.local:8848 nacos-1.nacos-headless.default.svc.cluster.local:8848 nacos-2.nacos-headless.default.svc.cluster.local:8848"
  selector:
    matchLabels:
      app: nacos

执行部署
kubectl apply -f 3-naocs-statefulset.yaml

4、配置一个普通svc服务
vi 4-nacos-service.yaml

#  部署普通Service(Nginx使用)
apiVersion: v1
kind: Service
metadata:
  name: nacos-service
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/affinity: "true"
    nginx.ingress.kubernetes.io/session-cookie-name: backend
    nginx.ingress.kubernetes.io/load-balancer-method: drr


spec:
  selector:
    app: nacos
  ports:
    - name: web
      port: 80
      targetPort: 8848

执行部署
kubectl apply -f 4-nacos-service.yaml

5、配置ingress提供外网web访问
vi 5-nacos-service-ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-nacos-http
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: nacos-web.yunzaixian.top
    http:
      paths:
      - path: /
        pathType: Prefix    # 前缀匹配
        backend:
          service:
            name: nacos-service
            port:
              number: 80

执行部署
kubectl apply -f 5-nacos-service-ingress.yaml

等待安装完成执行web访问:
k8s部署nacos集群_第1张图片
直接下载脚本文件
完成!

你可能感兴趣的:(k8s,kubernetes,容器,云原生,spring,cloud,微服务)