应用服务配置
前置条件
- 已部署安装Choerodon 猪齿鱼,并使用默认的 admin 用户名和密码登录了 Choerodon 系统。
默认的 admin 用户进入 Choerodon 系统后,默认拥有一个组织并为该组织的组织管理员角色。关于Choerodon角色的详情,请移步角色管理。
已完成项目创建及团队成员建设。
已在Choerodon项目下创建应用服务,并配置了 Git,包括下载安装、设置等。
Choerodon猪齿鱼秉承云原生的理念,基于平台的应用需要进行容器化改造才能够使用Choerodon进行开发和部署。在本节中将给大家介绍Choerodon容器化的一些概念, 以及为原代码库增加相关的配置使其满足Choerodon容器化要求。
容器化的第一步是编写合适的, Dockerfile
定义了如何将一个可执行程序打包成镜像. 例如: 一个SpringBoot
项目, 在maven构建之后会生成一个可执行的jar
包, 基于这个jar
包可以打包成一个镜像, 最简单的Dockerfile
如下:
应用Helm配置
所以, 在Choerodon的标准应用代码结构中一定要包含charts文件夹,如下截图,这是一个后端项目的标准结构。
Chart包结构:
├── charts
│ └── choerodon-todo-servie
│ ├── Chart.yaml 包含关于chart的的信息的YAML文件
│ ├── README.md 可选:chart的README文件, 简单介绍Chart(例如: Chart的用法用途, 环境变量)
│ ├── templates 这个目录下包含了多个模板文件以结合配置值生成有效的Kubernetes manifest文件
│ │ ├── deployment.yaml 创建 Kubernetes 部署的基本清单。
│ │ ├── _helpers.tpl 可选: 放置模板助手的地方,您可以在整个 chart 中重复使用
│ │ ├── ingress.yaml 可选: 为部署配置Ingress, 以通过域名访问服务
│ │ ├── pre-config-config.yaml 可选: 为部署配置前置job, 用于初始化配置中心
│ │ ├── pre-config-db.yaml 可选: 为部署配置前置job, 用于初始化数据库
│ │ └── service.yaml 可选: 为您的部署创建服务端点的基本清单。
│ └── values.yaml 为模板的预定义变量。
第一步: 创建目录 在项目根目录下创建如下目录结构,首先创建一个名为charts
的文件夹,再创建一个与应用名相同的文件夹,在此处示例为choerodon-todo-servie
,在其下创建如上文Chart
包结构所示的文件
第二步: 编写_helpers.tpl文件 在 templates文件夹下将一些公共的lable或值定义到 _helpers.tpl文件中:
apiVersion: {{ include "app.deployment.apiVersion" . }}
kind: Deployment
metadata:
name: {{ .Release.Name }}
labels:
{{ include "service.labels.standard" . | indent 4 }}
{{ include "service.logging.deployment.label" . | indent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
{{ include "service.labels.standard" . | indent 6 }}
template:
metadata:
labels:
{{ include "service.labels.standard" . | indent 8 }}
{{ include "service.microservice.labels" . | indent 8 }}
annotations:
{{ include "service.monitoring.pod.annotations" . | indent 8 }}
spec:
containers:
- name: {{ .Release.Name }}
image: "{{ .Values.image.repository }}:{{ .Chart.Version }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
env:
{{- range $name, $value := .Values.env.open }}
{{- if not (empty $value) }}
- name: {{ $name | quote }}
value: {{ $value | quote }}
{{- end }}
{{- end }}
ports:
- name: http
containerPort: {{ .Values.service.port }}
readinessProbe:
exec:
failureThreshold: 3
initialDelaySeconds: 60
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 10
resources:
{{ toYaml .Values.resources | indent 12 }}
第四步:编写Chart.yaml文件 在halm-dev文件夹中编写 Chart.yaml文件,这个文件中写明应用的的相关信息。
第五步:编写文件 在charts/choerodon-todo-service
文件夹中编写 values.yaml文件,这个文件中编写 templates文件夹中 deployment.yml文件(以及其它清单文件)会用到的变量及默认值。
# Default values for manager-service.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
# pod运行数量
replicaCount: 1
image:
# 镜像库地址
repository: registry.cn-hangzhou.aliyuncs.com/feifei-feifei-05/choerodon-todo-servie
# 镜像拉取策略
pullPolicy: IfNotPresent
preJob:
# job超时时间
timeout: 300
# job镜像库地址
image: registry.cn-hangzhou.aliyuncs.com/choerodon-tools/dbtool:0.6.7
preConfig:
# 是否初始化manager_service数据库
enabled: true
# 初始化到配置中心文件名
configFile: application.yml
# 初始化到配置中心存储方式
configType: k8s
# 注册中心地址
registerHost: http://register-server:8000
datasource:
# manager_service数据库连接地址
url: jdbc:mysql://localhost:3306/manager_service?useUnicode=true&characterEncoding=utf-8&useSSL=false&useInformationSchema=true&remarks=true
# manager_service数据库用户名
username: choerodon
# manager_service数据库密码
password: 123456
preInitDB:
# 是否初始化demo_service数据库
enabled: true
datasource:
# demo_service数据库连接地址
url: jdbc:mysql://localhost:3306/demo_service?useUnicode=true&characterEncoding=utf-8&useSSL=false&useInformationSchema=true&remarks=true
# demo_service数据库用户名
username: choerodon
# demo_service数据库密码
password: 123456
deployment:
managementPort: 18081
open:
# 注册服务地址
EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://register-server.io-choerodon:8000/eureka/
# 是否启用配置中心
SPRING_CLOUD_CONFIG_ENABLED: true
# 配置中心地址
SPRING_CLOUD_CONFIG_URI: http://config-server.framework:8010/
# 数据库连接地址
SPRING_DATASOURCE_URL: jdbc:mysql://localhost::3306/demo_service?useUnicode=true&characterEncoding=utf-8&useSSL=false&useInformationSchema=true&remarks=true
# 数据库用户名
SPRING_DATASOURCE_USERNAME: choerodon
# 数据库密码
SPRING_DATASOURCE_PASSWORD: 123456
metrics:
# 收集应用的指标数据路径
path: /prometheus
# 性能指标应用分组
group: spring-boot
logs:
# 日志收集格式
parser: spring-boot
persistence:
# 是否启用持久化存储
enabled: false
# 绑定的pvc名称
# existingClaim:
# 持久化路径
# subPath:
service:
# 是否创建k8s service
enabled: false
# service类型
type: ClusterIP
# service端口
port: 18080
# service名称
name: choerodon-todo-servie
ingress:
# 是否创建k8s ingress
enabled: false
resources:
# k8s中容器能使用资源的资源最大值
limits:
# cpu: 100m
memory: 2Gi
# k8s中容器使用的最小资源需求
requests:
memory: 1.5Gi
Choerodon使用Gitlab-CI作为CI工具,需要在应用源代码中加上.gitlab-ci.yml文件。
.gitlab-ci.yml
是用于指导gitlab
进行自动化的持续集成步骤的
在CI中主要的工作就是进行镜像构建, 并且生成Chart包,最后将Chart包上传至Choerodon,与Choerodon进行集成。