mysql搭建

使用mysql存储nacos数据
mysql搭建可以参考:https://blog.csdn.net/yorao4565/article/details/128445946

nacos搭建

部署nacos配置

在k8s中部署nacos配置nacos-cm

apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
  namespace: test-ns
data:
  mysql.host: "mysql-master.test-ns.svc.cluster.local"
  mysql.db.name: "nacos-config"
  mysql.port: "3306"
  mysql.user: "root"
  mysql.password: "78eOWYPrtWgbmdPn"

部署nacos集群headless-service

在k8s中部署nacos的headless-service,用于集群间访问

apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  namespace: test-ns
  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
  selector:
    app: nacos

部署nacos集群statefulSets

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
  namespace: test-ns
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:v2.1.1
          resources:
            requests:
              memory: "200Mi"
              cpu: "100m"
          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.test-ns.svc.cluster.local:8848 nacos-1.nacos-headless.test-ns.svc.cluster.local:8848 nacos-2.nacos-headless.test-ns.svc.cluster.local:8848"
  selector:
    matchLabels:
      app: nacos

部署nacos集群nacos-svc

用于服务注册

apiVersion: v1
kind: Service
metadata:
  name: nacos-svc
  labels:
    app: nacos-svc
spec:
  type: ClusterIP
  ports:
    - port: 8848
      name: "8088"
      targetPort: 8848
    - name: client-rpc
      port: 9848
      protocol: TCP
      targetPort: 9848
    - name: raft-rpc
      port: 9849
      protocol: TCP
      targetPort: 9849
  selector:
    app: nacos

部署nacos集群nodeport

用于外部访问管理页面
在这里插入图片描述
在这里插入图片描述
访问nacos界面:http://10.10.10.10:30004
在这里插入图片描述

服务注册

新建springboot服务

创建一个简单的服务,能启动就行,增加nacos相关依赖,新建DockerFile,生成服务镜像,并上传到harbor

FROM openjdk:8-jre
COPY *.jar /app.jar
CMD ["---server.port=8800"]
EXPOSE 8800
ENTRYPOINT ["java","-jar","/app.jar"]

在k8s中新建服务配置文件

在这里插入图片描述

部署服务,使用configmap中的配置文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8s-test-springboot
  namespace: test-ns
  labels:
    app: k8s-test-springboot
spec:
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: k8s-test-springboot
  template:
    metadata:
      labels:
        app: k8s-test-springboot
    spec:
	  imagePullSecrets:
      - name: harbor-secret
      containers:
      - name: k8s-test-springboot
        image: 10.10.10.10:8020/test/k8s_test:1.0
        args: ["--spring.config.location=application.yaml"]
        ports:
        - containerPort: 8800
        volumeMounts:
        - name: k8s-test-cm
          mountPath: application.yaml
          subPath: application.yaml
      volumes:
      - name: k8s-test-cm
        configMap:
          name: k8s-test-cm
          items:
            - key: application.yaml
              path: application.yaml

查看服务注册

在这里插入图片描述

Logo

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

更多推荐