1. 通过Jar包,生成镜像,推送到镜像仓库

此blog 采用1.7.2版本

1.1 通过 Sentinel 官方,下载Jar包

https://github.com/alibaba/Sentinel/releases

1.2 编写Dockerfile

  1. FROM java:8
  2. VOLUME /tmp
  3. ADD sentinel-dashboard-1.7.2.jar sentinel-dashboard.jar
  4. CMD java ${JAVA_OPTS} -jar sentinel-dashboard.jar
  5. EXPOSE 8080

1.3 根据Dockerfile 生成镜像

  1. [root@k8s-master ~]# docker build -t easycamp/sentinel-dashboard .
  2. Sending build context to Docker daemon 193MB
  3. Step 1/5 : FROM java:8
  4. ---> d23bdf5b1b1b
  5. Step 2/5 : VOLUME /tmp
  6. ---> Running in ee8e93f9996f
  7. Removing intermediate container ee8e93f9996f
  8. ---> 6677c980f695
  9. Step 3/5 : ADD sentinel-dashboard-1.7.2.jar sentinel-dashboard.jar
  10. ---> 36fc452961eb
  11. Step 4/5 : CMD java ${JAVA_OPTS} -jar sentinel-dashboard.jar
  12. ---> Running in c2739ade26e0
  13. Removing intermediate container c2739ade26e0
  14. ---> 1f936874a139
  15. Step 5/5 : EXPOSE 8080
  16. ---> Running in c633881219e8
  17. Removing intermediate container c633881219e8
  18. ---> dc3e65fd52eb
  19. Successfully built dc3e65fd52eb
  20. Successfully tagged easycamp/sentinel-dashboard:latest

1.4 将本地镜像推送到阿里云镜像仓库中

  1. sudo docker login --username=your username registry.cn-hangzhou.aliyuncs.com
  2. sudo docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/xxx:[镜像版本号]
  3. sudo docker push registry.cn-hangzhou.aliyuncs.com/xxx:[镜像版本号]

1.5 查看阿里云镜像仓库是否包含这个镜像

image.png

2. 编写 sentinel.yaml 及 sentinel-statefulset.yaml

2.1 编写sentinel.yaml

因为采用的是官方jar包,且在Dockerfile中没有额外指定其他的端口,所以,此yaml文件中的port均为8080,如在Dockerfile中额外指定了端口,则需将port相关的修改为对应的端口

  1. ---
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: sentinel
  6. labels:
  7. app: sentinel
  8. annotations:
  9. service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
  10. spec:
  11. ports:
  12. - port: 8080
  13. name: server
  14. targetPort: 8080
  15. clusterIP: None
  16. selector:
  17. app: sentinel
  18. ---
  19. # 外部访问服务
  20. apiVersion: v1
  21. kind: Service
  22. metadata:
  23. name: sentinel-svc
  24. labels:
  25. app: sentinel
  26. spec:
  27. ports:
  28. - name: http
  29. protocol: "TCP"
  30. port: 8080
  31. targetPort: 8080
  32. nodePort: 31808
  33. type: NodePort
  34. selector:
  35. app: sentinel

2.2 编写 sentinel-statefulset.yaml

注意 containerPort 以及 image,修改成自己的 image地址

  1. ---
  2. apiVersion: apps/v1
  3. kind: StatefulSet
  4. metadata:
  5. name: sentinel
  6. spec:
  7. serviceName: sentinel
  8. replicas: 1
  9. template:
  10. metadata:
  11. labels:
  12. app: sentinel
  13. annotations:
  14. pod.alpha.kubernetes.io/initialized: "true"
  15. spec:
  16. containers:
  17. - name: sentinel
  18. imagePullPolicy: IfNotPresent
  19. image: cike/sentinel-dashboard-docker:latest
  20. resources:
  21. requests:
  22. memory: "512Mi"
  23. cpu: "200m"
  24. ports:
  25. - containerPort: 8080
  26. name: client
  27. env:
  28. - name: TZ
  29. value: Asia/Shanghai
  30. - name: JAVA_OPTS
  31. value: "-Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -Djava.security.egd=file:/dev/./urandom -Dcsp.sentinel.api.port=8719"
  32. selector:
  33. matchLabels:
  34. app: sentinel

2.3 启动测试

  1. [root@k8s-master ~]# kubectl apply -f sentinel.yaml
  2. service/sentinel created
  3. service/sentinel-svc created
  4. [root@k8s-master ~]# kubectl apply -f sentinel-statefulset.yaml
  5. statefulset.apps/sentinel created

运行发现,可能会报如下的错误,会说,需要登录才能从私有仓库中拉取镜像

  1. Failed to pull image "registry.cn-hangzhou.aliyuncs.com/xxx:1.7.2":
  2. rpc error: code = Unknown desc = Error response from daemon:
  3. pull access denied for registry.cn-hangzhou.aliyuncs.com/xxx,
  4. repository does not exist or may require 'docker login'

根据如上的问题,解决方法如下:

1. 创建一个密钥

  1. kubectl create secret docker-registry registry-key \
  2. --docker-server=registry.cn-hangzhou.aliyuncs.com \
  3. --docker-username=your user name \
  4. --docker-password=<your-password> \
  5. --docker-email=xxx@qq.com

2. 修改 sentinel-statefulset.yaml,添加拉取时所使用的密钥

添加 imagePullSecrets

  1. apiVersion: apps/v1
  2. kind: StatefulSet
  3. metadata:
  4. name: sentinel
  5. spec:
  6. serviceName: sentinel
  7. replicas: 1
  8. template:
  9. metadata:
  10. labels:
  11. app: sentinel
  12. annotations:
  13. pod.alpha.kubernetes.io/initialized: "true"
  14. spec:
  15. imagePullSecrets:
  16. - name: registry-key

添加密钥之后,delete之后,重新启动

  1. [root@k8s-master ~]# kubectl delete -f sentinel.yaml
  2. service "sentinel" deleted
  3. service "sentinel-svc" deleted
  4. [root@k8s-master ~]# kubectl delete -f sentinel-statefulset.yaml
  5. statefulset.apps "sentinel" deleted
  6. [root@k8s-master ~]# kubectl apply -f sentinel.yaml
  7. service/sentinel created
  8. service/sentinel-svc created
  9. [root@k8s-master ~]# kubectl apply -f sentinel-statefulset.yaml
  10. statefulset.apps/sentinel created

发现,已经成功启动

  1. [root@k8s-master ~]# kubectl get pods;
  2. NAME READY STATUS RESTARTS AGE
  3. sentinel-0 1/1 Running 0 9m37s

2.4 浏览器访问Sentinel-dashboard

http://your-ip:31808/(**31808 为 sentinel.yaml 中配置的nodePort**) 输入用户名密码 sentinel,点击登录

image.png

2.5 Sentinel-Dashboard 安装成功