Kubernetes Cloud Native 实践 ( 四 ) 中间件上云

2023-06-13 20:50:16 425

全文目录

Kubernetes Cloud Native 实践 ( 一 ) 安装

Kubernetes Cloud Native 实践 ( 二 ) 简单使用

Kubernetes Cloud Native 实践 ( 三 ) NFS/PV/PVC

Kubernetes Cloud Native 实践 ( 四 ) 中间件上云

Kubernetes Cloud Native 实践 ( 五 ) 应用上云

Kubernetes Cloud Native 实践 ( 六 ) 集成ELK日志平台

Kubernetes Cloud Native 实践 ( 七 ) 应用监控

Kubernetes Cloud Native 实践 ( 八 ) CICD集成

Kubernetes Cloud Native 实践 ( 九 ) 运维管理

Kubernetes Cloud Native 实践 ( 十 ) 相关问题

Kubernetes Cloud Native 实践 ( 十一 ) 运行截图


  1. 项目的全部源码: https://github.com/MQPearth/spring-boot-backend
  2. 中间件上云
kubectl.kubernetes.io/last-applied-configuration: |
 {"apiVersion":"v1","kind":"ServiceAccount","metadata":{"name":"nfs-client-provisioner","namespace":"nacos"}}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nacos-cm
  namespace: nacos
data:
  mysql.host: "10.11.38.190"
  mysql.db.name: "nacos"
  mysql.port: "3307"
  mysql.user: "root"
  mysql.password: "123456"
  nacos.core.auth.server.identity.key: "NzAwN2UwZTMyYWUwNDNiOGFhNTY4NzFhZjI2OTE4YmM="
  nacos.core.auth.server.identity.value: "ZDhjZjNhMThkNjA3NGFkN2JlZTQxMzQyNGJlYzUyOTI="
  nacos.core.auth.plugin.nacos.token.secret.key: "Y2NhMmU0NDZhMDc4NDQ4NGExYTQ2MjQ1YjRlMGYxMWQ="
---
apiVersion: v1
kind: Service
metadata:
  name: nacos-headless
  namespace: nacos
  labels:
    app: nacos
spec:
  publishNotReadyAddresses: true 
  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
  selector:
    app: nacos
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nacos
  namespace: nacos
spec:
  podManagementPolicy: Parallel
  serviceName: nacos-headless
  replicas: 2
  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: 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_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: SPRING_DATASOURCE_PLATFORM
              value: "mysql"
            - name: NACOS_SERVER_PORT
              value: "8848"
            - name: NACOS_APPLICATION_PORT
              value: "8848"
            - name: PREFER_HOST_MODE
              value: "hostname"
            - name: NACOS_AUTH_ENABLE
              value: "true"
            - name: NACOS_CORE_AUTH_ENABLE
              value: "true"
            - name: NACOS_AUTH_TOKEN_EXPIRE_SECONDS
              value: "180000"
            - name: NACOS_CORE_AUTH_SERVER_IDENTITY_KEY
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: nacos.core.auth.server.identity.key
            - name: NACOS_CORE_AUTH_SERVER_IDENTITY_VALUE
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: nacos.core.auth.server.identity.value
            - name: NACOS_CORE_AUTH_PLUGIN_NACOS_TOKEN_SECRET_KEY
              valueFrom:
                configMapKeyRef:
                  name: nacos-cm
                  key: nacos.core.auth.plugin.nacos.token.secret.key
          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
  volumeClaimTemplates:
    - metadata:
        name: data
        annotations:
          volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
      spec:
        accessModes: [ "ReadWriteMany" ]
        resources:
          requests:
            storage: 1Gi
  selector:
    matchLabels:
      app: nacos
  1. redis: 三主三从
apiVersion: v1
kind: Namespace
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{},"labels":{"app.kubernetes.io/instance":"redis","app.kubernetes.io/name":"redis"},"name":"redis"}}
  labels:
    app.kubernetes.io/instance: redis
    app.kubernetes.io/name: redis
  name: redis
---
apiVersion: v1
    databases 16
    always-show-logo yes
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    dir /data
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly no
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-config-file nodes.conf
    cluster-node-timeout 15000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: redis
  name: redis
  namespace: redis
spec:
  ports:
  - port: 6379
    protocol: TCP
    targetPort: 6379
  selector:
    app: redis
  type: ClusterIP
  clusterIP: None
---
apiVersion: apps/v1
  labels:
    app: redis
  name: redis
  namespace: redis
spec:
  selector:
    matchLabels:
      app: redis
  replicas: 6
  serviceName: redis
  template:                     
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:6.0.19
        imagePullPolicy: IfNotPresent
        command: 
        - "redis-server"
        args:
        - "/etc/redis/redis.conf"
        - "--cluster-announce-ip"
        - "$(POD_NAME).$(POD_SERVICE_NAME).$(POD_NAMESPACE).svc.cluster.local"
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: POD_SERVICE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.serviceName
        ports:
        - name: redis-6379
          containerPort: 6379
        volumeMounts:
        - name: "redis-conf"
          mountPath: "/etc/redis"
        - name: "redis-data"
          mountPath: "/data"
        - name: localtime
          mountPath: /etc/localtime
          readOnly: true
      restartPolicy: Always
      volumes:
      - name: "redis-conf"
        configMap:
          name: "redis-cm"
          items:
            - key: "redis.conf"
              path: "redis.conf"
      - name: localtime
        hostPath:
          path: /usr/share/zoneinfo/Asia/Shanghai
          type: File
  volumeClaimTemplates:
    - metadata:
        name: "redis-data"
        annotations:
          volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
      spec:
        accessModes: [ "ReadWriteMany" ]
        resources: 
          requests:
            storage: 100M
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.244.140.248:6379 to 10.244.140.246:6379
Adding replica 10.244.140.89:6379 to 10.244.140.65:6379
Adding replica 10.244.140.87:6379 to 10.244.140.247:6379
M: bd56f4d44933a5631b033f00c83694444e9b2d51 10.244.140.246:6379
   slots:[0-5460] (5461 slots) master
M: f1ca6281b160f5b3dd0ddaa4fe55b62f7a985fa7 10.244.140.65:6379
   slots:[5461-10922] (5462 slots) master
M: 63713ea0264fe6a646a872b423121a435e814888 10.244.140.247:6379
   slots:[10923-16383] (5461 slots) master
S: 883c295d225da18471c2d87b298b19893ce2313a 10.244.140.87:6379
   replicates 63713ea0264fe6a646a872b423121a435e814888
S: c88da2594b499f7f0773d957bd80d196effcfae3 10.244.140.248:6379
   replicates bd56f4d44933a5631b033f00c83694444e9b2d51
S: f0db516991b5db3e65f3de943be57d230af0aa9b 10.244.140.89:6379
   replicates f1ca6281b160f5b3dd0ddaa4fe55b62f7a985fa7
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
..
>>> Performing Cluster Check (using node 10.244.140.246:6379)
M: bd56f4d44933a5631b033f00c83694444e9b2d51 10.244.140.246:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: c88da2594b499f7f0773d957bd80d196effcfae3 10.244.140.248:6379
   slots: (0 slots) slave
   replicates bd56f4d44933a5631b033f00c83694444e9b2d51
M: f1ca6281b160f5b3dd0ddaa4fe55b62f7a985fa7 10.244.140.65:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: f0db516991b5db3e65f3de943be57d230af0aa9b 10.244.140.89:6379
   slots: (0 slots) slave
   replicates f1ca6281b160f5b3dd0ddaa4fe55b62f7a985fa7
M: 63713ea0264fe6a646a872b423121a435e814888 10.244.140.247:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 883c295d225da18471c2d87b298b19893ce2313a 10.244.140.87:6379
   slots: (0 slots) slave
   replicates 63713ea0264fe6a646a872b423121a435e814888
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  1. skywalking:
apiVersion: v1
kind: Namespace
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{},"labels":{"app.kubernetes.io/instance":"skywalking-oap","app.kubernetes.io/name":"skywalking-oap"},"name":"skywalking-oap"}}
  labels:
    app.kubernetes.io/instance: skywalking-oap
    app.kubernetes.io/name: skywalking-oap
  name: skywalking-oap
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: skywalking-oap
  name: skywalking-oap
  namespace: skywalking-oap
spec:
  ports:
  - port: 11800
    name: "11800"
    nodePort: 30081    
    protocol: TCP
    targetPort: 11800
  - port: 12800
    name: "12800"
    nodePort: 30082
    protocol: TCP
    targetPort: 12800
  selector:
    app: skywalking-oap
  type: NodePort
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: skywalking-oap
  namespace: skywalking-oap
  labels: 
    app: skywalking-oap
spec:
  replicas: 2
  selector:
    matchLabels:
      app: skywalking-oap
  template:
    metadata:
      labels: 
        app: skywalking-oap
    spec:
      containers:
      - name: skywalking-oap
        image: apache/skywalking-oap-server:9.4.0-java17
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 11800
          name: "tcp-11800"
        - containerPort: 12800
          name: "tcp-12800"
        env:
        - name: TZ
          value: "Asia/Shanghai"
        - name: SW_STORAGE
          value: "elasticsearch"
        - name: SW_STORAGE_ES_CLUSTER_NODES
          value: "10.11.38.190:9200"
        - name: SW_CLUSTER
          value: "nacos"
        - name: SW_CLUSTER_NACOS_HOST_PORT
          value: "nacos-headless.nacos:8848"
        - name: SW_CLUSTER_NACOS_NAMESPACE
          value: "19a0fa32-ed2e-40f1-a1e1-aae8c81d8cf8"
        - name: SW_CLUSTER_NACOS_USERNAME
          value: "nacos"
        - name: SW_CLUSTER_NACOS_PASSWORD
          value: "nacos"
        - name: SW_CLUSTER_INTERNAL_COM_HOST
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: SW_CLUSTER_INTERNAL_COM_PORT
          value: "11800"
        volumeMounts:
        - name: localtime
          mountPath: /etc/localtime
          readOnly: true
      volumes:
      - name: localtime
        hostPath:
          path: /etc/localtime

apiVersion: v1
kind: Namespace
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{},"labels":{"app.kubernetes.io/instance":"skywalking-ui","app.kubernetes.io/name":"skywalking-ui"},"name":"skywalking-ui"}}
  labels:
    app.kubernetes.io/instance: skywalking-ui
    app.kubernetes.io/name: skywalking-ui
  name: skywalking-ui
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: skywalking-ui
  name: skywalking-ui
  namespace: skywalking-ui
spec:
  ports:
  - port: 8080
    name: "8080"
    nodePort: 31081
    protocol: TCP
    targetPort: 8080
  selector:
    app: skywalking-ui
  type: NodePort
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: skywalking-ui
  namespace: skywalking-ui
  labels: 
    app: skywalking-ui
spec:
  replicas: 1
  selector:
    matchLabels:
      app: skywalking-ui
  template:
    metadata:
      labels: 
        app: skywalking-ui
    spec:
      containers:
      - name: skywalking-ui
        image: apache/skywalking-ui:v9.4.0-java17
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
          name: "tcp-8080"
        env:
        - name: TZ
          value: "Asia/Shanghai"
        - name: SW_OAP_ADDRESS
          value: "http://skywalking-oap.skywalking-oap:12800"
        volumeMounts:
        - name: localtime
          mountPath: /etc/localtime
          readOnly: true
      volumes:
      - name: localtime
        hostPath:
          path: /etc/localtime


Spring RedisTemplate Scan

keys 不能用, 那就只能用scan了public static Set<String> scan(RedisTemplate<String, String> redisTemplate, String pattern) { return redisTemplate.execute((Re
2024-10-30

基于Redis ZSet的滑动窗口限流

private boolean limit() { String ip = ServletUtil.getClientIP(request); String banKey = "limit_ban_" + ip; Boolean b = redisTemplate.hasK
2024-10-29

Kubernetes Cloud Native 实践 ( 四 ) 中间件上云

全文目录Kubernetes Cloud Native 实践 ( 一 ) 安装Kubernetes Cloud Native 实践 ( 二 ) 简单使用Kubernetes Cloud Native 实践 ( 三 ) NFS/PV/PVCKubernetes Cloud Native 实践 ( 四
2023-06-13

更改Nacos配置文件缓存路径

Nacos会对远程拉下来的配置文件做缓存处理默认在%USER_DIR%/nacos/config下Naocs在com.alibaba.nacos.client.config.impl.LocalConfigInfoProcessor这个类里进行路径处理static { LOCAL_F
2021-09-26

SpringCloudAlibaba搭建过程踩坑记录

Nacos如果使用docker部署Nacos, 则需要设置容器环境变量NACOS_AUTH_ENABLE=true, 才能使nacos开启连接密码认证 spring: profiles: active: @profileActive@ application: name: w
2021-09-02

Redis特性

1. GEO可用于存储地理位置添加地理位置的坐标计算两个位置间的直线距离根据指定的经纬度坐标来获取指定范围内的地理位置集合2. Pipinglineing 管道, 一次发送多个命令, 节省网络开销3. Public/Subscribe 发布订阅, 可用于构建轻量级的消息队列, 但如果客户端断连, 消
2021-04-30

Redis缓存更新修改原有值但是不修改失效时间

第一种方式redisTemplate.opsForValue().set("key", "value", 0);第二种方式redisTemplate.opsForValue().increment("key", 1);
2021-04-17

Dubbo连接不到对应服务

尝试删除C:\Users\用户\nacos该目录, 然后重启项目原因根据官方图,dubbo调用者需要通过注册中心(例如:ZK、nacos)注册信息, 获取提供者,但是如果频繁往ZK获取信息,肯定会存在单点故障问题, 所以dubbo提供了将提供者信息缓存在本地的方法。 Dubbo在订阅注册中心的回调
2021-01-29

Java操作Redis的常见误区

不能使用 keys * 命令不能在set中存放大量数据
2021-01-11

SpringBoot连接Redis服务出现Command timed out

docker 部署的redis一直出现超时docker pull redis 网上的都说要设置这个值spring.redis.timeout=50000 然而实际使用过程中仍然超时查看redis 服务端配置-bash# cat redis.conf | grep tcp-k tcp-keepali
2020-12-14

SpringBoot RedisTemplate 操作不同的库

其他开发解决不了redis切换库, 于是通过消息队列, 让其他项目去操作对应的库, 太奇葩了, PM居然不管???@Bean("redisTemplateForDb6") public RedisTemplate<Object, Object> redisTemplate(RedisConnecti
2020-12-10

Spring Boot项目中使用RedisTemplate.delete() 删除指定key失败的解决办法

https://blog.csdn.net/hello_world_qwp/article/details/85763286上面这篇博客扯一大堆, 居然还分析源码实际只是自定义了key的序列化方式导致最终操作redis的时候序列化的key与预期的key不一致而已, 自然就删不掉redis中的数据了
2020-11-26

Redis实现分布式锁

boolean redisRes = stringRedisTemplate.opsForValue().setIfAbsent(key, value) 原子操作当key不存在, 存入value,返回true当key存在, 不进行操作, 返回false当处于redis事务中 , 返回null
2020-11-25

关于redis缓存时间设置可能导致的bug

看到在项目代码中有人先调用redisTemplate的方法判断某个key是否存在,然后再调用redisTemplate的方法拿到这个key里的数据。但是,在设置了key缓存时间的情况下,这个逻辑就会出错。比如第一步这个key存在,但是紧接着key过期了,后面就会拿不到这个key,造成后面的逻辑出错所
2020-06-19

SpringDataRedis 常用操作

//向redis里存入数据和设置缓存时间 stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS); //根据key获取缓存中的val stringRedisTemplate.opsForValue().
2020-03-28

freemarker 时间显示不正常 设置时区

项目在本地开发的时候显示正常,部署上服务器就一直差8个小时,最后发现freemarker官方文档有这样的说明time_zone:时区的名称来显示并格式化时间。 默认情况下,使用JVM的时区。 也可以是 Java 时区 API 接受的值,或者 "JVM default" (从 FreeMarker 2
2020-03-28
IDEA 2019.1 xml 不高亮

IDEA 2019.1 xml 不高亮

前几天更新了idea后,发现xml里的代码都没有了高亮,变得跟记事本一个德性了打开setting ,搜索 File Types,找到xml项, 查看下方的匹配格式,果然没有xml,(idea真是厉害)点击右方的+,输入*.xml,点击ok,解决问题
2020-03-28

npm install 淘宝镜像

npm install --registry=https://registry.npm.taobao.org
2020-03-28
Java中方法的参数传递机制

Java中方法的参数传递机制

来看一段代码 public class Man { private String name; private Integer age; public String getName() { return name; } publi
2020-03-28
基于自定义注解手写权限控制

基于自定义注解手写权限控制

方法一: AOP 方法二: 拦截器项目结构项目依赖<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-w
2020-03-28

Docker 部署 详细全过程 附代码

Docker 部署本站 全过程环境:CentOS7.61. 安装Docker其他版本CentOS可以参考这个https://help.aliyun.com/document_detail/187598.html查看本机内核版本,内核版本需高于 3.10uname -r 确保 yum 包最新yum u
2020-03-28

SpringBoot 启动普通java工程

引入依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.0.9</version> </dependency>
2020-03-28

Vue.js DOM操作

<template> <input type="button" @click="reply($event)" value="回复"> </template> export default { methods: { replyFun(e) {
2020-03-29
CentOS7编译调试OpenJDK12

CentOS7编译调试OpenJDK12

1. 下载源码https://hg.openjdk.java.net/jdk/jdk12点击左侧的browse,再点击zip,就可以下载zip格式的源码压缩包。unzip xxx.zip 解压文件2. 安装jdkyum install java-11-openjdk-devel -y3. 运行con
2020-04-23
编写自己的Spring Boot Starter

编写自己的Spring Boot Starter

1.新建一个maven项目命名规则统一是xxx-spring-boot-starter完整pom.xml<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"
2020-06-29