Bootstrap

k8s部署nacos高可用

1.nacos数据库使用mysq,自行安装mysql,创建数据库nacos_config,导入nacos初始化sql

2.k8s里部署Headless Service

Headless Service为每个pod(nacos实例)生成一个DNS地址,用作NACOS_SERVERS配置

3.通过StatefulSet部署nacos,StatefulSet部署方式为每个POD生成固定的名称,如nacos-0、nacos-1、nacos-2

4.部署Ingress,访问应该统一访问域名可以自己起,到时直接访问

我们将部署3个实例的nacos,StatefulSet部署方式为每个POD生成固定的名称:nacos-0、nacos-1、nacos-2。

cat nacos_cluster.yaml

---
apiVersion: v1
kind: Service
metadata:
  namespace: lishanbinnacos
  name: nacos-headless
  labels:
    app: nacos-headless
spec:
  ports:
    - port: 8848
      name: server
      targetPort: 8848
  clusterIP: None
  selector:
    app: nacos
---
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: lishanbinnacos
  name: nacos-cm
data:
  mysql.host: "192.168.0.249"
  mysql.db.name: "nacos_config"
  mysql.port: "3306"
  mysql.user: "root"
  mysql.password: "xxxxx"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: lishanbinnacos
  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: IfNotPresent
          image: registry.cn-hangzhou.aliyuncs.com/lishanbin/library:nacos-server_1.4.2
          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-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.lishanbinnacos.svc.cluster.local:8848 nacos-1.nacos-headless.lishanbinnacos.svc.cluster.local:8848 nacos-2.nacos-headless.lishanbinnacos.svc.cluster.local:8848"
  selector:
    matchLabels:
      app: nacos

使用ingress方式对外暴露:

apiVersion: v1
kind: Service
metadata:
  name: nacos-service
  namespace: lishanbinnacos
  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

使用node port方式对外暴露:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  namespace: lishanbinnacos
  name: nacos-web

spec:
  rules:
  - host: prelishanbinnacosnacos.lishanbinnacos.com.cn
    http:
      paths:
      - path: /
        backend:
          serviceName: nacos-service
          servicePort: web

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;