前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Kubernetes(TKE集群)部署Nacos

Kubernetes(TKE集群)部署Nacos

原创
作者头像
chen1900s
修改2023-02-17 13:26:03
3.7K0
修改2023-02-17 13:26:03
举报
文章被收录于专栏:TKE学习

官网文档:https://nacos.io/zh-cn/docs/use-nacos-with-kubernetes.html

部署数据库

1,数据库是NFS做数据化存储(或者CBS都可以)

注意点: 镜像要使用nacos提供的数据库nacos/nacos-mysql:5.7,自带的数据库创建完成后相关库和数据都已经导入

代码语言:javascript
复制
apiVersion: apps/v1
kind: Deployment
metadata:
  generation: 1
  labels:
    k8s-app: nacos-mysql
    qcloud-app: nacos-mysql
  name: nacos-mysql
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: nacos-mysql
      qcloud-app: nacos-mysql
  template:
    metadata:
      labels:
        k8s-app: nacos-mysql
        qcloud-app: nacos-mysql
    spec:
      containers:
      - env:
        - name: MYSQL_ROOT_PASSWORD
          value: root
        - name: MYSQL_DATABASE
          value: nacos
        - name: MYSQL_USER
          value: nacos
        - name: MYSQL_PASSWORD
          value: nacos
        image: nacos/nacos-mysql:5.7
        imagePullPolicy: IfNotPresent
        name: mysql
        resources:
          limits:
            cpu: 500m
            memory: 1Gi
          requests:
            cpu: 250m
            memory: 256Mi
        securityContext:
          privileged: false
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: mysql-data
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: qcloudregistrykey
      restartPolicy: Always
      volumes:
      - name: mysql-data
        nfs:
          path: /nacos
          server: 192.168.1.6

---
apiVersion: v1
kind: Service
metadata:
  name: nacos-mysql
  namespace: default
  labels:
    name: nacos-mysql
spec:
  ports:
  - port: 3306
    targetPort: 3306
  selector:
    k8s-app: nacos-mysql
    qcloud-app: nacos-mysql

验证数据库可用性:

代码语言:javascript
复制
[root@VM-0-17-tlinux ~/nacos]# mysql -h172.18.250.54 -unacos -pnacos
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| nacos              |
+--------------------+
2 rows in set (0.01 sec)

MySQL [(none)]> use nacos
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MySQL [nacos]> show tables;
+----------------------+
| Tables_in_nacos      |
+----------------------+
| config_info          |
| config_info_aggr     |
| config_info_beta     |
| config_info_tag      |
| config_tags_relation |
| group_capacity       |
| his_config_info      |
| permissions          |
| roles                |
| tenant_capacity      |
| tenant_info          |
| users                |
+----------------------+
12 rows in set (0.00 sec)

测试数据库是否正常
测试数据库是否正常

部署nacos

1,创建链接mysql的配置文件

代码语言:javascript
复制
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
  namespace: default
data:
  mysql.host: "172.18.253.36"  #如果是K8S集群内数据库,可以使用服务名称
  mysql.db.name: "nacos"      #上面创建数据库是指的的库名称
  mysql.port: "3306"           #端口
  mysql.user: "nacos"          #用户
  mysql.password: "nacos"      #用户密码

2,创建nacos-headless 用于集群之间的链接

代码语言:javascript
复制
apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  namespace: default
  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
    
   

3,部署nacos服务

代码语言:javascript
复制
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
  namespace: default
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:
            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.nacos.svc.cluster.local:8848 nacos-1.nacos-headless.nacos.svc.cluster.local:8848 nacos-2.nacos-headless.nacos.svc.cluster.local:8848"
            - name: SPRING_DATASOURCE_PLATFORM
              value: mysql
  selector:
    matchLabels:
      app: nacos
      
      

4,创建公网类型CLB的service(可选)

代码语言:javascript
复制
apiVersion: v1
kind: Service
metadata:
  annotations:
    service.kubernetes.io/service.extensiveParameters: '{"AddressIPVersion":"IPV4","ZoneId":"ap-chongqing-1"}'
  name: nacos
  namespace: default
spec:
  externalTrafficPolicy: Cluster
  ports:
  - name: 8848-8848-tcp
    port: 8848
    protocol: TCP
    targetPort: 8848
  selector:
    app: nacos
  sessionAffinity: None
  type: LoadBalancer

5,创建公网访问的ingress(可选)

代码语言:javascript
复制
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    ingress.cloud.tencent.com/direct-access: "false"
  name: nacos-ingress
  namespace: default
spec:
  rules:
  - host: nacos.dev.cc
    http:
      paths:
      - backend:
          serviceName: nacos
          servicePort: 8848
        path: /nacos

nacos使用

1,使用service访问 或者ingress进行访问

2.0版本的
2.0版本的

以上安装仅供参考,具体可以参考nacos官方文档介绍https://nacos.io/zh-cn/docs/use-nacos-with-kubernetes.html

【附录事项】

附1,如果对应数据库配置没有问题,但是nacos启动一直报如下错,找不到数据源

代码语言:javascript
复制
nacos server did not start because dumpservice bean construction failure. errMsg:102, dataSource or tableName is null

报错如上
报错如上

导致的原因是,需要额外加一下这个环境变量

SPRING_DATASOURCE_PLATFORM

值为:mysql

代码语言:javascript
复制
- name: SPRING_DATASOURCE_PLATFORM  #最新的版本一定要这个,nacos官方的yaml漏了这个(2.2版本,2.0版本是不需要加这个)
  value: "mysql"

2.2版本的
2.2版本的

附2,使用CBS做数据库的持久化存储,实例yaml: 仅参考

代码语言:javascript
复制
apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    k8s-app: nacos-mysql
    qcloud-app: nacos-mysql
  name: nacos-mysql
  namespace: default
spec:
  podManagementPolicy: OrderedReady
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: nacos-mysql
      qcloud-app: nacos-mysql
  serviceName: ""
  template:
    metadata:
      annotations:
        eks.tke.cloud.tencent.com/retain-ip: "true" #EKS集群需要,TKE集群不需要这个
        eks.tke.cloud.tencent.com/root-cbs-size: "20"  #EKS集群需要,TKE集群不需要这个
      labels:
        k8s-app: nacos-mysql
        qcloud-app: nacos-mysql
    spec:
      containers:
      - env:
        - name: MYSQL_ROOT_PASSWORD
          value: root
        - name: MYSQL_DATABASE
          value: nacos
        - name: MYSQL_USER
          value: nacos
        - name: MYSQL_PASSWORD
          value: nacos
        image: nacos/nacos-mysql:5.7
        imagePullPolicy: IfNotPresent
        name: nacos-mysql
        resources:
          limits:
            cpu: 500m
            memory: 1Gi
          requests:
            cpu: 250m
            memory: 256Mi
        securityContext:
          privileged: false
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: nacos-mysql
          subPath: mysql
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: qcloudregistrykey
      restartPolicy: Always
  updateStrategy:
    rollingUpdate:
      partition: 0
    type: RollingUpdate
  volumeClaimTemplates:
  - apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: nacos-mysql
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 10Gi
      storageClassName: cbs
      volumeMode: Filesystem

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 部署数据库
  • 部署nacos
  • nacos使用
  • 【附录事项】:
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档