Sentinel云原生K8S部署实战

939 阅读3分钟

部署的流程图

部署的详细过程

编译Docker镜像

Sentinel源码下载

https://gitee.com/pingfanrenbiji/Sentinel.git

Sentinel-dashboard编译打包

编写Dockerfile文件

FROM openjdk:8-jdk

# 环境变量
ENV LC_ALL=zh_CN.utf8
ENV LANG=zh_CN.utf8
ENV LANGUAGE=zh_CN.utf8

# 开放端口
EXPOSE 8080

# 指定工作目录
WORKDIR /app

# 将当前目录下的jar包复制道镜像中的/app目录下
COPY sentinel-dashboard.jar .

# 时间同步设置
# 设置权限 chown 用户:所在组 文件目录 0表示root用户所在的组标识;1001是指定了一个用户标识
# chmod 读取权限 r = 4,写入权限 w = 2,执行权限 x = 1
# 3个数字分别代表 拥有者,组用户,其他用户的权限
# 775中的第一个7表示4+2+1 表示拥有者拥有读写执行权限
# 第二个7当前用户所在组中的用户拥有者拥有读写执行权限
# 第三个5表示其他用户有读和执行的权限 没有写的权限

RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone \ 
 && chown 1001:0 -R /app \
 && chmod 775 -R /app

# java启动jar包
ENTRYPOINT ["java","-Dfile.encoding=UTF8","-Dsun.jnu.encoding=UTF8","-jar","sentinel-dashboard.jar"]

编译docker镜像

docker build -t sentinel-dashboard:1.8.2 .

启动下确认Dockerfile没问题

docker run -p 8080:8080 -d sentinel-dashboard:1.8.2 sentinel

http://localhost:8080/#/login

说明Dockerfile没有问题

K8S部署

pod.yml

apiVersion: apps/v1
kind: StatefulSet
metadata:

  name: sentinel
  labels:
    app: sentinel
spec:
  serviceName: sentinel
  replicas: 1
  selector:
    matchLabels:
      app: sentinel
  template:
    metadata:
      labels:
        app: sentinel
    spec:
      containers:
        - name: sentinel
          image: sentinel-dashboard:1.8.2
          imagePullPolicy: IfNotPresent
          resources:
            limits:
              cpu: 450m
              memory: 1024Mi
            requests:
              cpu: 400m
              memory: 1024Mi
          env:
            - name: TZ
              value: Asia/Shanghai
            - name: JAVA_OPT_EXT
              value: -Dserver.servlet.session.timeout=7200
            - name: SERVER_HOST
              valueFrom:
                configMapKeyRef:
                  name: sentinel-cm
                  key: sentinel.server.host
            - name: SERVER_PORT
              valueFrom:
                configMapKeyRef:
                  name: sentinel-cm
                  key: sentinel.server.port
            - name: USERNAME
              valueFrom:
                  configMapKeyRef:
                    name: sentinel-cm
                    key: sentinel.auth.username
            - name: PASSWORD
              valueFrom:
                  configMapKeyRef:
                    name: sentinel-cm
                    key: sentinel.auth.password
          ports:  
            - containerPort: 8280 #Dashboard服务的端口 客户端向控制台发送心跳包的控制台地址,指定控制台后客户端会自动向该地址发送心跳包
            - containerPort: 8719 #客户端的端口 提供给Dashboard访问
          volumeMounts:
            - name: vol-log
              mountPath: /opt/logs
      volumes:
        - name: vol-log
          hostPath:
            path: /opt/docker/k8s/sentinel/logs
            type: Directory

service.yml

apiVersion: v1
kind: Service
metadata:

  name: sentinel
  labels:
    app: sentinel
spec:
  type: NodePort
  ports:
    - port: 8280
      targetPort: 8280
      nodePort: 30280
      name: web
    - port: 8719
      targetPort: 8719
      nodePort: 30719
      name: api
  selector:
    app: sentinel

cm.yml

apiVersion: v1
kind: ConfigMap
metadata:
  name: sentinel-cm
data:
  sentinel.server.host: "192.168.43.22" #服务器IP地址
  sentinel.server.port: "8280"
  sentinel.auth.username: "sentinel"
  sentinel.auth.password: "sentinel"

create.s.sentinel.start.sh

#!/usr/bin/env bash

DIR=$(cd $(dirname $0); pwd)

mkdir -p        ${DIR}/sentinel/logs
chmod -R 777    ${DIR}/sentinel/logs

kubectl create -f  ${DIR}/sentinel/cm.yml
kubectl create -f  ${DIR}/sentinel/service.yml
kubectl create -f  ${DIR}/sentinel/pod.yml

kubectl get pod

echo -e "\n\n\n"

echo " success"

k8s部署

启动成功

kubectl logs -f sentinel-0

可以看到sentinel-dashboard默认的日志目录是${user.home}/logs/csp/目录下

用户标识是1001 和 chown配置的一致

k8s部署sentinel-dashboard成功

http://localhost:30280/#/login

在网关应用中配置限流规则

引入依赖

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
<version>1.8.2</version>
</dependency>
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-transport-simple-http</artifactId>
</dependency>

在配置文件中配置

spring:
  cloud:
    sentinel:
      transport:
        dashboard: 192.168.43.22:30280
        port: 192.168.43.22:30719

定一个资源

服务起来之后就可以在控制台看到

在sentinel中给指定资源配置限流规则

为了测试设置1秒只允许1个QPS 1秒内超过1个的请求被拦截

访问测试接口即可以看到限流的情况

该docker镜像没有问题 生成tar包上传镜像库或到其他地方部署

docker image save sentinel-dashboard:1.8.2 -o sentinel-dashboard.1.8.2.tar