Kubernetes 中使用 Helm 部署应用

Kubernetes 专栏收录该内容
12 篇文章 1 订阅

Kubernetes 中使用 Helm 部署应用

创建应用

创建一个简单的应用,提供一个 REST 接口;使用 Golang 编写,然后将镜像 push 到 Docker Hub

  • go.mod
module github.com/helloworlde/rest

go 1.12
  • main.go
package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/ping", func(writer http.ResponseWriter, request *http.Request) {
		fmt.Println("Pong")
		_, _ = fmt.Fprint(writer, "Pong")
	})

	fmt.Println("Server Started")
	log.Fatal(http.ListenAndServe(":8080", nil))
}
  • Dockerfile
FROM golang AS build-env
WORKDIR /go/src/github.com/hellowrolde/rest
COPY . .

ENV GO111MODULE on
ENV GOPROXY=https://gocenter.io
ENV GOOS linux
ENV GOARCH 386

RUN go mod download
RUN go build -v -o /go/src/github.com/hellowrolde/rest/app

FROM alpine
COPY --from=build-env  /go/src/github.com/hellowrolde/rest/app /usr/local/bin/app
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
EXPOSE 8080
CMD ["app"]
  • 构建并 push 镜像
docker build -t hellowooeds/rest . 
docker push hellowooeds/rest

使用 Helm

添加 Helm

初始化

helm create rest

然后会在项目目录下创建一个 rest 的文件夹,里面包含所需要的 Helm 配置文件

.
├── Chart.yaml
├── charts
├── templates
│   ├── NOTES.txt
│   ├── _helpers.tpl
│   ├── deployment.yaml
│   ├── ingress.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

3 directories, 8 files
  • Chart.yaml: 用于描述 Chart 的属性
  • values.yaml: 存放项目的配置项,如镜像名称或者 tag 等,用于和模板组装
  • charts: 用于存放依赖的 chart 的,当有依赖需要管理时,可以添加 requirements.yaml 文件,可用于管理项目内或者外部的依赖
  • templates: 用于存放需要的配置模板

修改配置文件

  • Chart.yaml
apiVersion: v1
appVersion: "1.0"
description: Rest application with Helm
name: rest
version: 0.1.0
maintainers:
  - name: HelloWood
  • 修改 values.yaml
backend:
  image: hellowoodes/rest
  tag: "latest"
  pullPolicy: IfNotPresent
  replicas: 1

namespace: rest

service:
  type: NodePort
  • backend-service.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: backend
  name: backend
  namespace: {{ .Values.namespace }}
spec:
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
  selector:
    app: backend
  type: {{ .Values.service.type }}
  • backend-deployment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: backend
  name: backend
  namespace: {{ .Values.namespace }}
spec:
  selector:
    matchLabels:
      app: backend
  replicas: 1
  template:
    metadata:
      labels:
        app: backend
    spec:
      containers:
        - name: backend
          image: {{ .Values.backend.image }}:{{ .Values.backend.tag }}
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8080

安装应用

helm install ./rest --name rest

如果不指定名称,会随机生成一个

NAME:   rest
LAST DEPLOYED: Sun Jul 14 18:50:16 2019
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/Pod(related)
NAME                      READY  STATUS             RESTARTS  AGE
backend-5bc9d6cb99-pfwn5  0/1    ContainerCreating  0         0s

==> v1/Service
NAME     TYPE      CLUSTER-IP     EXTERNAL-IP  PORT(S)         AGE
backend  NodePort  10.105.41.183  <none>       8080:31381/TCP  0s

==> v1beta1/Deployment
NAME     READY  UP-TO-DATE  AVAILABLE  AGE
backend  0/1    1           0          0s


NOTES:
1. Get the application URL by running these commands:
  export NODE_PORT=$(kubectl get service -l app=backend --namespace default  -o jsonpath="{.items[0].spec.ports[0].nodePort}")
  export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}")
  echo http://$NODE_IP:$NODE_PORT

查看

  • 查看 Helm 信息
helm list
NAME	REVISION	UPDATED                 	STATUS  	CHART     	APP VERSION	NAMESPACE
rest	1       	Sun Jul 14 18:55:25 2019	DEPLOYED	rest-0.1.0	1.0        	default
  • 查看 Deployment、Service、Pod
kubectl get all -l app=backend
NAME                           READY   STATUS    RESTARTS   AGE
pod/backend-5bc9d6cb99-kmpvx   1/1     Running   0          7m15s

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
service/backend      NodePort    10.104.248.37   <none>        8080:31064/TCP   7m15s

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/backend   1/1     1            1           7m15s

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/backend-5bc9d6cb99   1         1         1       7m15s

测试

访问通过 NOTES 获取到的 IP 和端口

http get http://192.168.0.110:31381/ping
HTTP/1.1 200 OK
Content-Length: 4
Content-Type: text/plain; charset=utf-8
Date: Sun, 14 Jul 2019 10:53:18 GMT

Pong

打包

helm package ./rest
Successfully packaged chart and saved it to: /path/to/project/rest-0.1.0.tgz

更新

当应用发生了更新之后,需要升级部署,这时候可以通过修改 Helm 的配置来实现;如镜像版本从1.0 更新到 1.1

  • 修改 values.yaml
backend:
  image: hellowoodes/rest
  tag: "1.1"
  pullPolicy: IfNotPresent
  replicas: 1

namespace: default

service:
  type: NodePort
  • 执行更新
 helm upgrade rest ./rest --repo local
Release "rest" has been upgraded. Happy Helming!
LAST DEPLOYED: Sun Jul 14 21:03:57 2019
NAMESPACE: default
STATUS: DEPLOYED

RESOURCES:
==> v1/Pod(related)
NAME                     READY  STATUS   RESTARTS  AGE
backend-56876f876-vvbzz  1/1    Running  0         85s

==> v1/Service
NAME     TYPE      CLUSTER-IP   EXTERNAL-IP  PORT(S)         AGE
backend  NodePort  10.97.22.28  <none>       8080:31496/TCP  7m10s

==> v1beta1/Deployment
NAME     READY  UP-TO-DATE  AVAILABLE  AGE
backend  1/1    1           1          7m10s


NOTES:
1. Get the application URL by running these commands:
  export NODE_PORT=$(kubectl get service -l app=backend --namespace default -o jsonpath="{.items[0].spec.ports[0].nodePort}")
  export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}")
  echo http://$NODE_IP:$NODE_PORT

等待更新完成后,查看 Pod 的描述信息

kubectl describe pod backend-56876f876-vvbzz

可以看到 pod 的镜像已经从 1.0 更新到了 1.1

Normal  Pulled     29s   kubelet, ubuntu-server  Container image "hellowoodes/rest:1.1" already present on machine

删除应用

删除 Helm 应用后,所有相关的 Service,Deployment,Pod 等都会被删除

helm delete --purge rest

查看 Repo 应用

helm serve
Regenerating index. This may take a moment.
Now serving you on 127.0.0.1:8879

helm-app-list.png


项目地址

https://github.com/helloworlde/helm-app-rest

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

打赏
文章很值,打赏犒劳作者一下
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页

打赏

呜呜呜啦啦啦

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值