容器化之-helm(K8S包管理器)一键部署nacos集群连外部mysql

前言:

1.helm新的chart包制作helm create myapp( metallb和nacos)

2.自建mysql导入nacos初始数据(略)

3.helm启动LoadBalancer服务(之前用NodePort暴漏3000+端口压测100个微服务就挂了,所以更换了LoadBalancer模式很稳定)

4.持久化分布存储longhorn安装(longhorn安装略,rancher商店有现成的可以一键安装,网上也有很多longhorn.yaml模板k8s启动就可以了)

1.#metallb目录结构

[root@test10 metallb]# tree

.

├── Chart.yaml

├── README.md

├── templates

│   ├── config.yaml

│   ├── controller.yaml

│   ├── _helpers.tpl

│   ├── NOTES.txt

│   ├── prometheusrules.yaml

│   ├── psp.yaml

│   ├── rbac.yaml

│   ├── service-accounts.yaml

│   ├── servicemonitor.yaml

│   ├── service.yaml

│   └── speaker.yaml

└── values.yaml

1.1# values.yaml 更改成自己IP同一网段(切记别冲突自己内外IP)

1.2#启动服务metallb   (每台都会启动speaker服务)

helm install  metallb .

2.安装nacos

#nacos目录结构

 [root@test10 nacos]# tree

.

├── Chart.yaml

├── images

│   ├── cluster1.png

│   ├── cluster2.png

│   ├── cluster3.png

│   ├── nacos.png

│   ├── quickstart.png

│   └── standalone.png

├── README.md

├── templates

│   ├── configmap.yaml

│   ├── deployment.yaml

│   ├── _helpers.tpl

│   ├── ingress.yaml

│   ├── NOTES.txt

│   ├── service.yaml

│   └── storageclass.yaml

└── values.yaml

2.1#更改配置configmap.yaml

cat  templates/configmap.yaml

{{- if ne .Values.global.mode "quickstart" }}

apiVersion: v1

kind: ConfigMap

metadata:

  name: nacos-cc

data:

  mysql.master.service.host: "192.168.78.13"

  mysql.slave.service.host: "182.168.78.14"

  mysql.master.db.name: "nacos"

  mysql.master.port: "3306"

  mysql.slave.port: "3306"

  mysql.master.user: "root"

  mysql.master.password: "123456"

{{- end }}

2.2.#deployment.yaml 配置

cat templates/deployment.yaml

apiVersion: apps/v1

kind: StatefulSet

metadata:

  name: {{ include "nacos.fullname" . }}

  annotations:

  {{- toYaml .Values.annotations | indent 4 }}

spec:

{{- if eq .Values.global.mode "cluster" }}

  serviceName: nacos-hs

{{- else }}

  serviceName: nacos-cs

{{- end }} 

  replicas: {{ .Values.replicaCount }}

  selector:

    matchLabels:

      app.kubernetes.io/name: {{ include "nacos.name" . }}

      app.kubernetes.io/instance: {{ .Release.Name }}

  template:

    metadata:

      labels:

        app.kubernetes.io/name: {{ include "nacos.name" . }}

        app.kubernetes.io/instance: {{ .Release.Name }}

    spec:

{{- if eq .Values.global.mode "cluster" }}

      initContainers:

        - name: peer-finder-plugin-install

          image: nacos/nacos-peer-finder-plugin:1.0

          imagePullPolicy: Always

          volumeMounts:

            - mountPath: /home/nacos/plugins/peer-finder

              name: plugindir

{{- end }}

      containers:

        - name: {{ .Chart.Name }}

          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"

          imagePullPolicy: {{ .Values.image.pullPolicy }}

          ports:

            - name: http

              containerPort: {{ .Values.env.serverPort }}

              protocol: TCP

          resources:

            {{- toYaml .Values.resources | nindent 12 }}

{{- if eq .Values.global.mode "quickstart" }}

          env:

          - name: PREFER_HOST_MODE

            value: {{ .Values.env.preferhostmode | quote }}

          - name: MODE

            value: "standalone"

{{- else if eq .Values.global.mode "standalone" }}

          env:

          - name: PREFER_HOST_MODE

            value: {{ .Values.env.preferhostmode | quote }}

          - name: MODE

            value: "standalone"

          - name: SPRING_DATASOURCE_PLATFORM

            value: "mysql"

          - name: MYSQL_MASTER_SERVICE_HOST

            valueFrom:

              configMapKeyRef:

                name: nacos-cc

                key: mysql.master.service.host

          - name: MYSQL_SLAVE_SERVICE_HOST

            valueFrom:

              configMapKeyRef:

                name: nacos-cc

                key: mysql.slave.service.host

          - name: MYSQL_MASTER_SERVICE_DB_NAME

            valueFrom:

               configMapKeyRef:

                 name: nacos-cc

                 key: mysql.master.db.name

          - name:  MYSQL_MASTER_SERVICE_PORT

            valueFrom:

              configMapKeyRef:

                 name: nacos-cc

                 key: mysql.master.port

          - name:  MYSQL_SLAVE_SERVICE_PORT

            valueFrom:

              configMapKeyRef:

                 name: nacos-cc

                 key: mysql.slave.port

          - name: MYSQL_MASTER_SERVICE_USER

            valueFrom:

              configMapKeyRef:

                 name: nacos-cc

                 key: mysql.master.user

          - name:  MYSQL_MASTER_SERVICE_PASSWORD

            valueFrom:

              configMapKeyRef:

                 name: nacos-cc

                 key: mysql.master.password

          - name: NACOS_SERVER_PORT

            value: {{ .Values.env.serverPort | quote }}

          - name: NACOS_APPLICATION_PORT

            value: {{ .Values.env.serverPort | quote }}

          volumeMounts:

          - name: datadir

            mountPath: /home/nacos/data

{{- if not .Values.persistence.enabled  }}

      volumes:

      - name: datadir

        emptyDir: {}

{{- end }}  

{{- else if eq .Values.global.mode "cluster" }}

          env:

          - name: NACOS_REPLICAS

            value: "3"

          - name: SERVICE_NAME

            value: "nacos-hs"

          - name: POD_NAMESPACE

            valueFrom:

              fieldRef:

                apiVersion: v1

                fieldPath: metadata.namespace

          - name: PREFER_HOST_MODE

            value: {{ .Values.env.preferhostmode | quote }}

          - name: MYSQL_MASTER_SERVICE_HOST

            valueFrom:

              configMapKeyRef:

                name: nacos-cc

                key: mysql.master.service.host

          - name: MYSQL_SLAVE_SERVICE_HOST

            valueFrom:

              configMapKeyRef:

                name: nacos-cc

                key: mysql.slave.service.host

          - name: MYSQL_MASTER_SERVICE_DB_NAME

            valueFrom:

               configMapKeyRef:

                 name: nacos-cc

                 key: mysql.master.db.name

          - name:  MYSQL_MASTER_SERVICE_PORT

            valueFrom:

              configMapKeyRef:

                 name: nacos-cc

                 key: mysql.master.port

          - name:  MYSQL_SLAVE_SERVICE_PORT

            valueFrom:

              configMapKeyRef:

                 name: nacos-cc

                 key: mysql.slave.port

          - name: MYSQL_MASTER_SERVICE_USER

            valueFrom:

              configMapKeyRef:

                 name: nacos-cc

                 key: mysql.master.user

          - name:  MYSQL_MASTER_SERVICE_PASSWORD

            valueFrom:

              configMapKeyRef:

                 name: nacos-cc

                 key: mysql.master.password

          - name: NACOS_SERVER_PORT

            value: {{ .Values.env.serverPort | quote }}

          volumeMounts:

            - name: plugindir

              mountPath: /home/nacos/plugins/peer-finder

            - name: datadir

              mountPath: /home/nacos/data

            - name: logdir

              mountPath: /home/nacos/logs

{{- if not .Values.persistence.enabled  }}

      volumes:

      - name: plugindir

        emptyDir: {}

      - name: datadir

        emptyDir: {}

      - name: logdir

        emptyDir: {}

      

{{- end }}  

{{- end }}  

{{- if and .Values.persistence.enabled (eq .Values.global.mode "standalone") }}

  volumeClaimTemplates:

  - metadata:

      name: datadir

    spec:

    {{- toYaml .Values.persistence.data | nindent 6 }}

{{- end }}

{{- if and .Values.persistence.enabled (eq .Values.global.mode "cluster") }}

  volumeClaimTemplates:

  - metadata:

      name: datadir

    spec:

    {{- toYaml .Values.persistence.data | nindent 6 }}

  - metadata:

      name: plugindir

    spec:

    {{- toYaml .Values.persistence.plugin | nindent 6 }}

  - metadata:

      name: logdir

    spec:

    {{- toYaml .Values.persistence.log| nindent 6 }}

{{- end }}

    {{- with .Values.nodeSelector }}

      nodeSelector:

        {{- toYaml . | nindent 8 }}

    {{- end }}

    {{- with .Values.affinity }}

      affinity:

        {{- toYaml . | nindent 8 }}

    {{- end }}

    {{- with .Values.tolerations }}

      tolerations:

        {{- toYaml . | nindent 8 }}

    {{- end }}

2.3.#service.yaml 配置

cat templates/service.yaml

{{- if and (eq .Values.global.mode "cluster") }}

apiVersion: v1

kind: Service

metadata:

  name: nacos-hs

spec:

  type: ClusterIP

  clusterIP: None

  ports:

    - port: {{ .Values.service.port }}

      targetPort: http

      protocol: TCP

      name: http

  selector:

    app.kubernetes.io/name: {{ include "nacos.name" . }}

    app.kubernetes.io/instance: {{ .Release.Name }}

{{- end }}

---

apiVersion: v1

kind: Service

metadata:

  name: nacos-cs

  labels:

{{- toYaml .Values.service.labels | nindent 4 }}

  annotations:

{{- toYaml .Values.service.annotations | nindent 4 }}

spec:

  type: {{ .Values.service.type }}

  ports:

    - port: {{ .Values.service.port }}

      targetPort: http

      protocol: TCP

      name: http

      {{- if eq .Values.service.type "NodePort" }}

      nodePort: {{ .Values.service.nodePort }}

      {{- end }}

  selector:

    app.kubernetes.io/name: {{ include "nacos.name" . }}

    app.kubernetes.io/instance: {{ .Release.Name }}

2.4.#values.yaml 配置

 cat values.yaml

# Default values for nacos.

# This is a YAML-formatted file.

# Declare variables to be passed into your templates.

global:

 #mode: quickstart

 #mode: standalone

 mode: cluster

 storageClass:

   provisioner:

############################nacos###########################

replicaCount: 3

image:

  repository: nacos/nacos-server

  tag: 1.1.3

  pullPolicy: IfNotPresent

health:

  enabled: false

env:

  preferhostmode: hostname

  serverPort: 8848

persistence:

  enabled: true

  storageClassName: longhorn

  classParameters: {}

  data:

    accessModes:

    - ReadWriteOnce

    storageClassName: longhorn

    resources:

      requests:

        storage: 10Gi

  plugin:

    accessModes:

    - ReadWriteOnce

    storageClassName: longhorn

    resources:

      requests:

        storage: 10Gi

  log:

    accessModes:

    - ReadWriteOnce

    storageClassName: longhorn

    resources:

      requests:

        storage: 10Gi

service:

  type: LoadBalancer

  port: 8848

ingress:

  enabled: false

  annotations: {}

    # kubernetes.io/ingress.class: nginx

    # kubernetes.io/tls-acme: "true"

  hosts:

    - host: nacos.example.com

      paths: []

  tls: []

  #  - secretName: chart-example-tls

  #    hosts:

  #      - chart-example.local

resources:

  # We usually recommend not to specify default resources and to leave this as a conscious

  # choice for the user. This also increases chances charts run on environments with little

  # resources, such as Minikube. If you do want to specify resources, uncomment the following

  # lines, adjust them as necessary, and remove the curly braces after 'resources:'.

  # limits:

  #   cpu: 100m

  #   memory: 128Mi

  requests:

     cpu: 1000m

     memory: 4Gi

annotations: {}

nodeSelector: {}

tolerations: []

affinity: {}

2.5.#启动 nacos

 helm  install nacos .

#查看日志启动正常

#登录http://192.168.78.100:8848/nacos/ (这个IP100就是LoadBalancer分配的对外服务地址)用户名密码默认是nacos(记得更改)

#查看集群节点(我这里部署的是集群模式#mode: quickstart #mode: standalone

mode: cluster )

#这里就可以正常使用了,导入或创建配置(我这里之前有了的就直接导入了)

#接下来我们再看看mysql里面的数据(已经存入自建的数据库)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值