项目采用Maven作为项目的编译和构建工具,项目目录结构如下:
├── README.md ├── chart │ ├── Chart.yaml # Chart基本信息 │ ├── charts # 依赖 │ ├── templates # Kubernetes模板 │ │ ├── NOTES.txt │ │ ├── _helpers.tpl │ │ ├── deployment.yaml │ │ ├── ingress.yaml │ │ └── service.yaml │ └── values.yaml # 变量 ├── Dockerfile # Dockerfile定义 ├── entrypoint.sh # 容器的entrypoint.sh文件 ├── mvnw ├── mvnw.cmd ├── pom.xml ├── src # 应用源码 │ └── main │ └── java │ └── hello │ ├── Application.java │ └── HelloController.java
├── Dockerfile # Dockerfile定义 ├── entrypoint.sh # 容器的entrypoint.sh文件
# Build FROM maven:3.5.0-jdk-8-alpine AS builder ADD ./pom.xml pom.xml ADD ./src src/ RUN mvn clean package # Package FROM java:8 COPY --from=builder target/gs-spring-boot-0.1.0.jar gs-spring-boot.jar RUN bash -c 'touch /gs-spring-boot.jar' ADD entrypoint.sh entrypoint.sh RUN chmod +x entrypoint.sh ENTRYPOINT ["./entrypoint.sh"]
mvn clean package
命令实现Java源码的编译打包,产生的jar包会保存到容器的targets目录下。
#!/usr/bin/env bash ACTIVE_PROFILE=${PROFILE:=default} java -Xmx1024m -Djava.security.egd=file:/dev/./urandom -jar gs-spring-boot.jar --spring.profiles.active=${ACTIVE_PROFILE} $@
--spring.profiles.active=prod
。
$ docker build -t yunlzheng/spring-app . # 修改为自己的镜像仓库 Sending build context to Docker daemon 16.38MB Step 1/10 : FROM maven:3.5.0-jdk-8-alpine AS builder ---> 67d11473f554 ...... Successfully built e332622092ce Successfully tagged yunlzheng/spring-app:latest
docker push yunlzheng/spring-app # 修改为自己的镜像仓库
├── chart │ ├── Chart.yaml # Chart基本信息 │ ├── charts # 依赖 │ ├── templates # Kubernetes模板 │ │ ├── NOTES.txt │ │ ├── _helpers.tpl │ │ ├── deployment.yaml │ │ ├── ingress.yaml │ │ └── service.yaml │ └── values.yaml # 变量
$ helm create chart Creating chart
apiVersion: v1 appVersion: "1.0" description: A Spring Boot Application name: chart version: 0.1.0
replicaCount: 1 image: repository: yunlzheng/spring-app #修改为自己的镜像 tag: latest pullPolicy: IfNotPresent service: type: ClusterIP port: 8080 #容器映射的端口 ingress: enabled: true # 打开集群ingress annotations: {} # kubernetes.io/ingress.class: nginx # kubernetes.io/tls-acme: "true" path: / hosts: - spring-example.local tls: []
# deployment.yaml apiVersion: apps/v1beta2 kind: Deployment metadata: name: {{ template "chart.fullname" . }} labels: app: {{ template "chart.name" . }} chart: {{ template "chart.chart" . }} release: {{ .Release.Name }} heritage: {{ .Release.Service }} spec: replicas: {{ .Values.replicaCount }} selector: matchLabels: app: {{ template "chart.name" . }} release: {{ .Release.Name }} template: metadata: labels: app: {{ template "chart.name" . }} release: {{ .Release.Name }} spec: containers: - name: {{ .Chart.Name }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" imagePullPolicy: {{ .Values.image.pullPolicy }} ports: - name: http containerPort: 8080 protocol: TCP
$ cd chart $ helm lint ==> Linting . [INFO] Chart.yaml: icon is recommended 1 chart(s) linted, no failures
$ cd chart $ helm install . # 省略其它输出 ==> v1/Service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE womping-sparrow-spring-app ClusterIP 172.19.11.418080/TCP 1s NOTES: 1. Get the application URL by running these commands: http://spring-example.local/
$ cd .. $ helm package chart Successfully packaged chart and saved it to: /Users/yunlong/workspace/project-samples/containerization-spring-with-helm/chart-0.1.0.tgz
helm package
命令会使用Charts.yaml中文件定义的版本。 而如果在持续集成工具中,如果我们希望每次都能动态生成一个新版本的Chart,那在打包时,可以通过--version,动态修改,从而确保每次持续集成过程都能产生一个新的版本,并且能够对该版本进行独立验证。
$ helm package chart --version 0.0.2 Successfully packaged chart and saved it to: /workspace/tmp/spring-sample/chart-0.0.2.tgz
--username=kHKvnX
和 --password=WsCH7zuHH2
指定用户名和密码:
helm repo add play-helm https://repomanage.rdc.aliyun.com/helm_repositories/26125-play-helm --username=kHKvnX --password=WsCH7zuHH2
$ helm plugin install https://github.com/chartmuseum/helm-push Downloading and installing helm-push v0.7.1 ... https://github.com/chartmuseum/helm-push/releases/download/v0.7.1/helm-push_0.7.1_darwin_amd64.tar.gz Installed plugin: push
$ helm push chart-0.1.0.tgz play-helm Pushing chart-0.1.0.tgz to play-helm... Done.
$ helm update ...Successfully got an update from the "play-helm" chart repository ...Successfully got an update from the "stable" chart repository Update Complete. ⎈ Happy Helming!⎈
$ helm search play-helm NAME CHART VERSION APP VERSION DESCRIPTION play-helm/chart 0.1.0 1.0 A Spring Boot Application $ helm install play-helm/chart
$ helm push chart-0.1.0.tgz play-helm --version=0.2.0 Pushing chart-0.2.0.tgz to play-helm... Done.
$ helm push chart play-helm --version=0.3.0 Pushing chart-0.1.0.tgz to play-helm... Done.
$ helm push chart https://repomanage.rdc.aliyun.com/helm_repositories/26125-play-helm --username=kHKvnX --password=WsCH7zuHH2 Pushing chart-0.1.0.tgz to https://repomanage.rdc.aliyun.com/helm_repositories/26125-play-helm... Done.
到目前为止,我们展示了如何在软件研发的端到端过程中使用Helm,通过基础设施即代码的模式,开发人员可以直接在源码中通过Chart定义管理应用的部署架构,在完成开发工作后开发人员只需要将Chart发布到Helm仓库中,接下来无论是测试,还是运维都可以直接使用Chart快速在Kubernetes集群中对应用进行测试与发布。
本文转自DockOne-使用Helm优化Kubernetes下的研发体验:基础设施即代码