linuxea:skywalking9.1基于nacos的动态告警配置一(4)

marksugar
2022-06-30 / 0 评论 / 1,346 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年06月30日,已超过157天没有更新,若内容或图片失效,请留言反馈。

在上一篇中,我们构建了必要的组件,并且打包构建的docker镜像。但这还不够,我们需要添加一些链路追踪的观测,如:skywalking。

此前我使用skywalking9.0进行部署,本次将使用最新的9.1.0进行安装配置

但这次不同,skywalking支持动态配置,于是我们使用动态配置来管理告警,关于skywalking动态配置可以参考官方文档#dynamic-configuration。而动态配置不得不提nacos,作为动态服务发现、配置管理和服务管理平台nacos被大量普及和应用。

image-20220629215351166.png

nacos

要使用nacos,我们需要一个后端数据库,可以放在k8s中,也可以在虚拟机部署

我将在虚拟机部署mysql

1. 准备外部mysql

首先你需要安装docker和docker-compose。因为我将使用docker-compose进行编排

准备yaml文件

version: '3.3'
services:
  nacos-mysql:
    container_name: nacos-mysql
    image: registry.cn-hangzhou.aliyuncs.com/marksugar/mysql:8.0.29-debian
    # docker pull mysql:8.0.29-debian
    # docker pull nacos/nacos-mysql:5.7
    # network_mode: host
    restart: always
# docker exec some-mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /some/path/on/your/host/all-databases.sql
# docker exec -i some-mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /some/path/on/your/host/all-databases.sql
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_ROOT_PASSWORD=PASSWORDABCD
      - MYSQL_DATABASE=nacos_fat
      - MYSQL_USER=nacos
      - MYSQL_PASSWORD=PASSWORDABCD
      #- MYSQL_INITDB_SKIP_TZINFO=
    volumes:
    - /etc/localtime:/etc/localtime:ro  # 时区2
    - /data/mysql/nacos/data:/var/lib/mysql
    - /data/mysql/nacos/file:/var/lib/mysql-files
    - ./my.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
    logging:
      driver: "json-file"
      options:
        max-size: "50M"
    ports:
      - 3306:3307

准备my.cnf

# naocs sql init
# /docker-entrypoint-initdb.d
# /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
port=3307
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
#log-error      = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

启动

docker-compose -f docker-compose.yaml up -d

而后导入nacos.sql

/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = config_info   */
/******************************************/
CREATE TABLE `config_info` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(255) DEFAULT NULL,
  `content` longtext NOT NULL COMMENT 'content',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
  `app_name` varchar(128) DEFAULT NULL,
  `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
  `c_desc` varchar(256) DEFAULT NULL,
  `c_use` varchar(64) DEFAULT NULL,
  `effect` varchar(64) DEFAULT NULL,
  `type` varchar(64) DEFAULT NULL,
  `c_schema` text,
  `encrypted_data_key` text NOT NULL COMMENT '秘钥',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfo_datagrouptenant` (`data_id`,`group_id`,`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info';

/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = config_info_aggr   */
/******************************************/
CREATE TABLE `config_info_aggr` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(255) NOT NULL COMMENT 'group_id',
  `datum_id` varchar(255) NOT NULL COMMENT 'datum_id',
  `content` longtext NOT NULL COMMENT '内容',
  `gmt_modified` datetime NOT NULL COMMENT '修改时间',
  `app_name` varchar(128) DEFAULT NULL,
  `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfoaggr_datagrouptenantdatum` (`data_id`,`group_id`,`tenant_id`,`datum_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='增加租户字段';


/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = config_info_beta   */
/******************************************/
CREATE TABLE `config_info_beta` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `content` longtext NOT NULL COMMENT 'content',
  `beta_ips` varchar(1024) DEFAULT NULL COMMENT 'betaIps',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
  `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
  `encrypted_data_key` text NOT NULL COMMENT '秘钥',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfobeta_datagrouptenant` (`data_id`,`group_id`,`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_beta';

/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = config_info_tag   */
/******************************************/
CREATE TABLE `config_info_tag` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id',
  `tag_id` varchar(128) NOT NULL COMMENT 'tag_id',
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `content` longtext NOT NULL COMMENT 'content',
  `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `src_user` text COMMENT 'source user',
  `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_configinfotag_datagrouptenanttag` (`data_id`,`group_id`,`tenant_id`,`tag_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_tag';

/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = config_tags_relation   */
/******************************************/
CREATE TABLE `config_tags_relation` (
  `id` bigint(20) NOT NULL COMMENT 'id',
  `tag_name` varchar(128) NOT NULL COMMENT 'tag_name',
  `tag_type` varchar(64) DEFAULT NULL COMMENT 'tag_type',
  `data_id` varchar(255) NOT NULL COMMENT 'data_id',
  `group_id` varchar(128) NOT NULL COMMENT 'group_id',
  `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id',
  `nid` bigint(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`nid`),
  UNIQUE KEY `uk_configtagrelation_configidtag` (`id`,`tag_name`,`tag_type`),
  KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_tag_relation';

/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = group_capacity   */
/******************************************/
CREATE TABLE `group_capacity` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `group_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Group ID,空字符表示整个集群',
  `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '配额,0表示使用默认值',
  `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用量',
  `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个配置大小上限,单位为字节,0表示使用默认值',
  `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '聚合子配置最大个数,,0表示使用默认值',
  `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值',
  `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最大变更历史数量',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_group_id` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='集群、各Group容量信息表';

/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = his_config_info   */
/******************************************/
CREATE TABLE `his_config_info` (
  `id` bigint(64) unsigned NOT NULL,
  `nid` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `data_id` varchar(255) NOT NULL,
  `group_id` varchar(128) NOT NULL,
  `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
  `content` longtext NOT NULL,
  `md5` varchar(32) DEFAULT NULL,
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `src_user` text,
  `src_ip` varchar(50) DEFAULT NULL,
  `op_type` char(10) DEFAULT NULL,
  `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
  `encrypted_data_key` text NOT NULL COMMENT '秘钥',
  PRIMARY KEY (`nid`),
  KEY `idx_gmt_create` (`gmt_create`),
  KEY `idx_gmt_modified` (`gmt_modified`),
  KEY `idx_did` (`data_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='多租户改造';


/******************************************/
/*   数据库全名 = nacos_config   */
/*   表名称 = tenant_capacity   */
/******************************************/
CREATE TABLE `tenant_capacity` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `tenant_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Tenant ID',
  `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '配额,0表示使用默认值',
  `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用量',
  `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个配置大小上限,单位为字节,0表示使用默认值',
  `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '聚合子配置最大个数',
  `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值',
  `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最大变更历史数量',
  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='租户容量信息表';


CREATE TABLE `tenant_info` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `kp` varchar(128) NOT NULL COMMENT 'kp',
  `tenant_id` varchar(128) default '' COMMENT 'tenant_id',
  `tenant_name` varchar(128) default '' COMMENT 'tenant_name',
  `tenant_desc` varchar(256) DEFAULT NULL COMMENT 'tenant_desc',
  `create_source` varchar(32) DEFAULT NULL COMMENT 'create_source',
  `gmt_create` bigint(20) NOT NULL COMMENT '创建时间',
  `gmt_modified` bigint(20) NOT NULL COMMENT '修改时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_tenant_info_kptenantid` (`kp`,`tenant_id`),
  KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='tenant_info';

CREATE TABLE `users` (
    `username` varchar(50) NOT NULL PRIMARY KEY,
    `password` varchar(500) NOT NULL,
    `enabled` boolean NOT NULL
);

CREATE TABLE `roles` (
    `username` varchar(50) NOT NULL,
    `role` varchar(50) NOT NULL,
    UNIQUE INDEX `idx_user_role` (`username` ASC, `role` ASC) USING BTREE
);

CREATE TABLE `permissions` (
    `role` varchar(50) NOT NULL,
    `resource` varchar(255) NOT NULL,
    `action` varchar(8) NOT NULL,
    UNIQUE INDEX `uk_role_permission` (`role`,`resource`,`action`) USING BTREE
);

INSERT INTO users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', TRUE);

INSERT INTO roles (username, role) VALUES ('nacos', 'ROLE_ADMIN');

而后使用如下命令导入

docker exec -i nacos-mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" nacos_fat' < ./nacos.sql 

如下

[root@Node-172_16_100_54 /data/mysql]# docker exec -i nacos-mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" nacos_fat' < ./nacos.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.

2. 准备nacos的pvc

我们已经创建了mysql,并且导入了sql,但是对于一些日志我们希望留下来,于是我们创建一个pvc

开始之前,我们创建一个名称空间用作nacos专用的

apiVersion: v1
kind: Namespace
metadata:
  name: nacos

如下

PS J:\k8s-1.23.1-latest\nacos> kubectl.exe apply -f .\namespace.yaml
namespace/nacos created
PS J:\k8s-1.23.1-latest\nacos> kubectl.exe get ns   
NAME              STATUS   AGE  
argocd            Active   2d20h
default           Active   14d  
ingress-nginx     Active   4d19h
kube-node-lease   Active   14d  
kube-public       Active   14d  
kube-system       Active   14d  
marksugar         Active   3d19h
monitoring        Active   13d
nacos             Active   12s

pvc配置如下,我们进行创建

*参数:*

姓名描述默认
onDelete如果存在且有删除值,则删除目录,如果存在且有保留值,则保存目录。将与共享名称一起存档:archived-<volume.Name>
archiveOnDelete如果它存在并且具有 false 值,则删除该目录。如果onDelete存在,archiveOnDelete将被忽略。将与共享名称一起存档:archived-<volume.Name>
pathPattern指定用于通过 PVC 元数据(例如标签、注释、名称或命名空间)创建目录路径的模板。要指定元数据使用${.PVC.<metadata>}. 示例:如果文件夹应命名为<pvc-namespace>-<pvc-name>${.PVC.namespace}-${.PVC.name}则用作 pathPattern。不适用

yaml如下

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-latest
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner
parameters:
  archiveOnDelete: "false"
  pathPattern: "${.PVC.namespace}/${.PVC.name}"
  onDelete: delete 
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nacos-nfs
  namespace: nacos  
spec:
  storageClassName: nfs-latest
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50G

开始创建

PS J:\k8s-1.23.1-latest\nacos> kubectl.exe apply -f .\nfs-pvc.yaml 
storageclass.storage.k8s.io/nfs-latest created
persistentvolumeclaim/nacos-nfs created
  • 须知

而在使用了pathPattern之后,由于我们引用的是${.PVC.namespace}/${.PVC.name},于是我们的nfs目录结构就变成了这样,如下

[root@Node-172_16_100_49 /data/nfs-k8s/1.21.1]# ll
total 0
drwxrwxrwx 2 root root 21 Jun 13 00:02 default-test-claim-pvc-d64f6d7d-3be8-407e-bb3f-59efcd481e3d
drwxr-xr-x 3 root root 23 Jun 26 18:09 nacos
[root@Node-172_16_100_49 /data/nfs-k8s/1.21.1]# ls nacos/nacos-nfs/
data  logs  peer-finder

目录结构变成nacos/nacos-nfs/后,在某些时候将会比较好用的

查看

PS J:\k8s-1.23.1-latest\nacos> kubectl.exe -n nacos get pvc
NAME        STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
nacos-nfs   Bound    pvc-2e3c7f7b-648a-4b22-b2af-ca0ab26b8e8a   50G        RWX            nfs-latest     4m9s
PS J:\k8s-1.23.1-latest\nacos> kubectl.exe -n nacos get sc 
NAME         PROVISIONER                                   RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
nfs-client   k8s-sigs.io/nfs-subdir-external-provisioner   Delete          Immediate           false                  13d
nfs-latest   k8s-sigs.io/nfs-subdir-external-provisioner   Delete          Immediate           false                  4m44s

我们进入数据库验证是否成功创建

[root@linuxea-54 /data/mysql]# docker exec -ti nacos-mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"; '  
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.29 MySQL Community Server - GPL

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| nacos_fat          |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use nacos_fat
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> show tables;
+----------------------+
| Tables_in_nacos_fat  |
+----------------------+
| 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)

mysql> 

授权远程 连接

GRANT ALL PRIVILEGES ON *.* TO 'nacos'@'%' WITH GRANT OPTION;

确保可以连通
image-20220626190458805.png

如果不通。大概率是网络问题,可以修改network模式,如下

version: '3.3'
services:
  nacos-mysql:
    container_name: nacos-mysql
    image: registry.cn-hangzhou.aliyuncs.com/marksugar/mysql:8.0.29-debian
    # docker pull mysql:8.0.29-debian
    # docker pull nacos/nacos-mysql:5.7
    network_mode: host
    restart: always
# docker exec some-mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /some/path/on/your/host/all-databases.sql
# docker exec -i some-mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /some/path/on/your/host/all-databases.sql
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_ROOT_PASSWORD=PASSWORDABCD
      - MYSQL_DATABASE=nacos_fat
      - MYSQL_USER=nacos
      - MYSQL_PASSWORD=PASSWORDABCD
      #- MYSQL_INITDB_SKIP_TZINFO=
    volumes:
    - /etc/localtime:/etc/localtime:ro  # 时区2
    - /data/mysql/nacos/data:/var/lib/mysql
    - /data/mysql/nacos/file:/var/lib/mysql-files
    - ./my.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
    logging:
      driver: "json-file"
      options:
        max-size: "50M"
    #ports:
      #- 3307:3307

3. 安装naocs

nacos yaml清单如下,我们需要修改

---
apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  namespace: skywalking  
  labels:
    app: nacos
  # annotations:
  #   service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
  ports:
    - port: 8848
      name: server
      targetPort: 8848
    - port: 9848
      name: client-rpc
      targetPort: 9848
    - port: 9849
      name: raft-rpc
      targetPort: 9849
    ## 兼容1.4.x版本的选举端口
    - port: 7848
      name: old-raft-rpc
      targetPort: 7848
  clusterIP: None
  type: ClusterIP
  selector:
    app: nacos
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
  namespace: skywalking  
data:
  mysql.host: "172.16.0.158"
  mysql.db.name: "nacos_fat"
  mysql.port: "3307"
  mysql.user: "nacos"
  mysql.password: "PASSWORDABCD"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
  namespace: skywalking  
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"
      # serviceAccountName: nfs-client-provisioner
      initContainers:
        - name: peer-finder-plugin-install
          image: nacos/nacos-peer-finder-plugin:1.1
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - mountPath: /home/nacos/plugins/peer-finder
              name: data
              subPath: peer-finder
      containers:
        - name: nacos
          imagePullPolicy: IfNotPresent
          image: nacos/nacos-server:v2.1.0
          resources:
            requests:
              memory: "2048Mi"
              cpu: "500m"
          ports:
            - containerPort: 8848
              name: client-port
            - containerPort: 9848
              name: client-rpc
            - containerPort: 9849
              name: raft-rpc
            - containerPort: 7848
              name: old-raft-rpc
          env:
            - name: MYSQL_SERVICE_DB_PARAM
              value: characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai
            - name: NACOS_REPLICAS
              value: "3"
            - name: SERVICE_NAME
              value: "nacos-headless"
            - name: DOMAIN_NAME
              value: "cluster.local"
            - name: POD_NAMESPACE            
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
            - 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_HOST
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.host
            - 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_APPLICATION_PORT
              value: "8848"
            - name: NACOS_SERVERS
              value: "nacos-0.nacos-headless.skywalking.svc.cluster.local:8848 nacos-1.nacos-headless.skywalking.svc.cluster.local:8848 nacos-2.nacos-headless.skywalking.svc.cluster.local:8848"
          volumeMounts:
            - name: data
              mountPath: /home/nacos/plugins/peer-finder
              subPath: peer-finder
            - name: data
              mountPath: /home/nacos/data
              subPath: data
            - name: data
              mountPath: /home/nacos/logs
              subPath: logs
            - mountPath: /etc/localtime
              name: nacostime              
      volumes:
      - name: nacostime
        hostPath:
          path: /etc/localtime      
      - name: data
        persistentVolumeClaim:
          claimName: nacos-nfs
  selector:
    matchLabels:
      app: nacos

需要修改如下configmap

1.账号密码

apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
  namespace: skywalking  
data:
  mysql.host: "172.16.0.158"
  mysql.db.name: "nacos_fat"
  mysql.port: "3307"
  mysql.user: "nacos"
  mysql.password: "PASSWORDABCD"

2.nacos变量

            - name: NACOS_SERVERS
              value: "nacos-0.nacos-headless.skywalking.svc.cluster.local:8848 nacos-1.nacos-headless.skywalking.svc.cluster.local:8848 nacos-2.nacos-headless.skywalking.svc.cluster.local:8848"

3.pvc配置

      volumes:
      - name: nacostime
        hostPath:
          path: /etc/localtime      
      - name: data
        persistentVolumeClaim:
          claimName: nas-nacos

最终如下

---
apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  namespace: nacos  
  labels:
    app: nacos
  # annotations:
  #   service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
  ports:
    - port: 8848
      name: server
      targetPort: 8848
    - port: 9848
      name: client-rpc
      targetPort: 9848
    - port: 9849
      name: raft-rpc
      targetPort: 9849
    ## 兼容1.4.x版本的选举端口
    - port: 7848
      name: old-raft-rpc
      targetPort: 7848
  clusterIP: None
  type: ClusterIP
  selector:
    app: nacos
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
  namespace: nacos  
data:
  mysql.host: "172.16.100.54"
  mysql.db.name: "nacos_fat"
  mysql.port: "3306"
  mysql.user: "nacos"
  mysql.password: "PASSWORDABCD"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
  namespace: 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
              topologyKey: "kubernetes.io/hostname"
      initContainers:
        - name: peer-finder-plugin-install
          image: nacos/nacos-peer-finder-plugin:1.1
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - mountPath: /home/nacos/plugins/peer-finder
              name: data
              subPath: peer-finder
      containers:
        - name: nacos
          imagePullPolicy: IfNotPresent
          image: nacos/nacos-server:v2.1.0
          resources:
            requests:
              memory: "2048Mi"
              cpu: "500m"
          ports:
            - containerPort: 8848
              name: client-port
            - containerPort: 9848
              name: client-rpc
            - containerPort: 9849
              name: raft-rpc
            - containerPort: 7848
              name: old-raft-rpc
          env:
            - name: MYSQL_SERVICE_DB_PARAM
              value: characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai
            - name: NACOS_REPLICAS
              value: "3"
            - name: SERVICE_NAME
              value: "nacos-headless"
            - name: DOMAIN_NAME
              value: "cluster.local"
            - name: POD_NAMESPACE            
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
            - 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_HOST
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: mysql.host
            - 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_APPLICATION_PORT
              value: "8848"
            - 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"
          volumeMounts:
            - name: data
              mountPath: /home/nacos/plugins/peer-finder
              subPath: peer-finder
            - name: data
              mountPath: /home/nacos/data
              subPath: data
            - name: data
              mountPath: /home/nacos/logs
              subPath: logs
            - mountPath: /etc/localtime
              name: nacostime              
      volumes:
      - name: nacostime
        hostPath:
          path: /etc/localtime      
      - name: data
        persistentVolumeClaim:
          claimName: nacos-nfs
  selector:
    matchLabels:
      app: nacos

应用

PS J:\k8s-1.23.1-latest\nacos> kubectl.exe apply -f .\nacos-nfs.yaml
service/nacos-headless created
configmap/nacos-cm created
statefulset.apps/nacos created

直到所有的pod runing

PS J:\k8s-1.23.1-latest\nacos> kubectl.exe -n nacos get pod   
NAME      READY   STATUS    RESTARTS   AGE
nacos-0   1/1     Running   0          5m59s
nacos-1   1/1     Running   0          4m37s
nacos-2   1/1     Running   0          3m31s

4. 配置ingress

如下

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nacos-ui
  labels:
    app: nacos 
  namespace: nacos
spec:
  ingressClassName: nginx
  rules:
  - host: nacos.linuxea.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nacos-headless            
            port:
              number: 8848

创建

PS J:\k8s-1.23.1-latest\nacos> kubectl.exe -n nacos get ingress
NAME       CLASS   HOSTS               ADDRESS                       PORTS   AGE
nacos-ui   nginx   nacos.linuxea.com   172.16.100.11,172.16.100.43   80      53s

配置本地Hosts

172.16.100.11 nacos.linuxea.com

访问的域名如下

http://nacos.linuxea.com/nacos/#/login

image-20220626193808334.png

账号密码:nacos/nacos

image-20220626193922473.png

参考: https://github.com/nacos-group/nacos-k8s/tree/master/deploy/nacos

skywalking

skywalking需要一个后端来存储数据,或者MySQL,或者ES,我将在这里使用ES

我们仍然需要一个PVC来存储ES的数据,与nacos不同的是,我这里用k8s来运行ES

1.安装ES

在安装之前,我们需要准备一个PVC

1.1 准备ES的PVC

复制nacos的配置,如法炮制一个,修改下名称即可

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-skywalking-es
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner
parameters:
  archiveOnDelete: "false"
  pathPattern: "${.PVC.namespace}/${.PVC.name}"
  onDelete: delete 
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: es-data
  namespace: skywalking
spec:
  storageClassName: nfs-skywalking-es
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50G

我们创建一个名称空间skwayling,而后创建pvc

PS J:\k8s-1.23.1-latest\nacos\skywalking> kubectl.exe apply -f .\ns.yaml
namespace/skywalking created
PS J:\k8s-1.23.1-latest\nacos\skywalking> kubectl.exe apply -f .\nfs-to-es.yaml
storageclass.storage.k8s.io/nfs-skywalking-es created
persistentvolumeclaim/es-data created

如下

PS J:\k8s-1.23.1-latest\nacos\skywalking> kubectl.exe -n skywalking get pvc
NAME      STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS        AGE
es-data   Bound    pvc-5baafefd-a2da-48f4-b9d4-815c6f3c2fe3   50G        RWX            nfs-skywalking-es   29s

1.2 安装ES

我们仍然要修改一些配置,claimName:的值是我们创建的pvc名称,也就是es-data

     volumes:
      - name: oms-skywalking-to-elasticsearch-data
        persistentVolumeClaim:
          claimName: es-data

最终的yaml如下

# Source: skywalking/charts/elasticsearch/templates/statefulset.yaml
apiVersion: v1
kind: Service
metadata:
  name: elasticsearch
  namespace: skywalking  
  labels:
    app: elasticsearch
spec:
  type: ClusterIP
  ports:
  - name: elasticsearch
    port: 9200
    protocol: TCP
  selector:
    app: elasticsearch 
---    
apiVersion: apps/v1
kind: Deployment
metadata:
  name: elasticsearch
  namespace: skywalking
  labels:
    app: elasticsearch
spec:
  selector:
    matchLabels:
      app:  elasticsearch
  replicas: 1
  template:
    metadata:
      name: elasticsearch
      labels:
        app: elasticsearch
    spec:
      initContainers:
      - name: configure-sysctl
        securityContext:
          runAsUser: 0
          privileged: true
        image: registry.cn-hangzhou.aliyuncs.com/marksugar/elasticsearch:6.8.6
        imagePullPolicy: "IfNotPresent"
        command: ["/bin/sh"]
        args: ["-c", "sysctl -w DefaultLimitNOFILE=65536; sysctl -w DefaultLimitMEMLOCK=infinity; sysctl -w DefaultLimitNPROC=32000; sysctl -w vm.max_map_count=262144"]  
        resources:
          {}
      containers:
      - name: "elasticsearch"
        securityContext:
          capabilities:
            drop:
            - ALL
          runAsNonRoot: true
          runAsUser: 1000
        image: registry.cn-hangzhou.aliyuncs.com/marksugar/elasticsearch:6.8.6
        imagePullPolicy: "IfNotPresent"      
        livenessProbe:
          failureThreshold: 3
          initialDelaySeconds: 30
          periodSeconds: 2
          successThreshold: 1
          tcpSocket:
            port: 9300
          timeoutSeconds: 2
        readinessProbe:
          failureThreshold: 3
          initialDelaySeconds: 30
          periodSeconds: 2
          successThreshold: 2
          tcpSocket:
            port: 9300
          timeoutSeconds: 2                  
        ports:
        - name: http
          containerPort: 9200
        - name: transport
          containerPort: 9300
        resources:
          limits:
            cpu: 1000m
            memory: 2Gi
          requests:
            cpu: 100m
            memory: 2Gi
        env:
          - name: node.name
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: cluster.name
            value: "elasticsearch"
          - name: network.host
            value: "0.0.0.0"
          - name: ES_JAVA_OPTS
            value: "-Xmx1g -Xms1g -Duser.timezone=Asia/Shanghai"
          - name: discovery.type
            value: single-node
        volumeMounts:
        - mountPath: /usr/share/elasticsearch/data
          name: oms-skywalking-to-elasticsearch-data
      restartPolicy: Always
      volumes:
      - name: oms-skywalking-to-elasticsearch-data
        persistentVolumeClaim:
          claimName: es-data

应用清单

PS J:\k8s-1.23.1-latest\nacos\skywalking> kubectl.exe apply -f .\es.yaml
service/elasticsearch created
deployment.apps/elasticsearch created

使用-w来观察状态,直到running

PS J:\k8s-1.23.1-latest\nacos\skywalking> kubectl.exe -n skywalking get pod -w
NAME                             READY   STATUS     RESTARTS   AGE
elasticsearch-64c9d98794-ndktz   0/1     Init:0/1   0          14s
elasticsearch-64c9d98794-ndktz   0/1     PodInitializing   0          55s
elasticsearch-64c9d98794-ndktz   0/1     Running           0          56s
elasticsearch-64c9d98794-ndktz   1/1     Running           0          88s

nfs上面已经创建的es的数据文件

[root@linuxea-49 /data/nfs-k8s/1.21.1]# ls skywalking/es-data/
nodes

es安装完成

1.3 本地es

除此之外,我们可以在vm虚拟机上安装es

version: '3.3'
services:
  elasticsearch:
    image: registry.cn-hangzhou.aliyuncs.com/marksugar/elasticsearch:6.8.6
    container_name: elasticsearch
    sysctls:
      net.core.somaxconn: 10240
      #DefaultLimitNOFILE: 65536
      #DefaultLimitMEMLOCK: infinity
      #DefaultLimitNPROC: 32000
      #vm.max_map_count: 262144
    ulimits:
      memlock:
        soft: -1
        hard: -1
    #network_mode: host
    hostname: elasticsearch
    restart: always
    environment:
    - cluster.name="elasticsearch"
#    - network.host="0.0.0.0"
    - discovery.type=single-node
#    - bootstrap.memory_lock=true
    - "ES_JAVA_OPTS=-Xms2048m -Xmx4096m -XX:-UseConcMarkSweepGC
                                    -XX:-UseCMSInitiatingOccupancyOnly
                                    -XX:+UseG1GC
                                    -XX:InitiatingHeapOccupancyPercent=75
                                    -Duser.timezone=Asia/Shanghai"
    user: root
    ports:
      - 9200:9200
      - 9300:9300
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /data/elasticsearch/data:/usr/share/elasticsearch/data
    logging:
      driver: "json-file"
      options:
        max-size: "50M"
    deploy:
      resources:
        limits:
          memory: 6144m
        reservations:
          memory: 6144m

而后docker-compose up -d即可
未完,因篇幅字数问题,见下一章

0

评论

博主关闭了当前页面的评论