我是靠谱客的博主 落寞铅笔,这篇文章主要介绍K8s部署发布Golang应用程序的实现方法,现在分享给大家,希望可以做个参考。

alertGo程序可以参考上篇文章,主要用于alertmanager实现钉钉报警

创建dockerfile

复制代码
1
2
3
4
5
6
7
8
9
10
FROM golang:1.14-alpine ENV GOPROXY=https://goproxy.cn WORKDIR /build COPY . . EXPOSE 8088 RUN mkdir /app RUN go mod tidy RUN go build -o /app/alertGo alertGo.go WORKDIR /app CMD ["/app/alertGo"]

打包并且推送

复制代码
1
2
docker build -t 10.206.16.4/k8s-go/alert.sentsss.com:v2 . docker push 10.206.16.4/k8s-go/alert.sentsss.com:v2

创建namespace

复制代码
1
2
3
4
apiVersion: v1 kind: Namespace metadata: name: k8s-go

创建deployment

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
apiVersion: apps/v1 kind: Deployment metadata: name: alertgo namespace: k8s-go spec: selector: matchLabels: app: alertgo replicas: 2 template: metadata: labels: app: alertgo spec: imagePullSecrets: - name: registry-pull-secret containers: - name: alertgo image: 10.206.16.4/k8s-go/alert.sentsss.com:v2 ports: - containerPort: 8088 livenessProbe: httpGet: path: / port: 8088 initialDelaySeconds: 30 periodSeconds: 10 successThreshold: 1 failureThreshold: 3 timeoutSeconds: 1 readinessProbe: httpGet: path: / port: 8088 initialDelaySeconds: 30 periodSeconds: 10 successThreshold: 1 failureThreshold: 3 timeoutSeconds: 1 lifecycle: preStop: exec: command: ["/bin/bash","-c","sleep 20"] resources: limits: cpu: 20m memory: 20Mi requests: cpu: 10m memory: 10Mi

创建service

复制代码
1
2
3
4
5
6
7
8
9
10
11
apiVersion: v1 kind: Service metadata: name: alertgo namespace: k8s-go spec: selector: app: alertgo ports: - port: 80 targetPort: 8088

创建ingress

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
kind: Ingress # 对象类型 apiVersion: networking.k8s.io/v1beta1 metadata: name: alertgo namespace: k8s-go spec: rules: - host: alertgo.xxx.com http: paths: - path: / backend: serviceName: alertgo servicePort: 80

创建hpa

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
kind: HorizontalPodAutoscaler # 对象类型,简称 hpa,水平自动伸缩 apiVersion: autoscaling/v2beta2 # autoscaling/v2beta2 与 autoscaling/v1 的 API 有很大的不同,注意识别两者的差异 metadata: name: alertgo namespace: fronted spec: scaleTargetRef: # 伸缩的目标对象 apiVersion: apps/v1 # 对象版本 kind: Deployment # 目标对象的类型 name: alertgo # 目标对象的名称 minReplicas: 3 # 最小副本数 maxReplicas: 6 # 最大副本数 metrics: # 指标 - type: Resource # 类型:资源 resource: name: memory # 内存 target: type: Utilization averageUtilization: 70 # 1% 这个值是为了实验,具体值请参考业务方实际情况而定 - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70

结果查看

[root@k8s-master-01 alertGo]# kubectl get pods,svc,ingress,hpa -n k8s-go
NAME                           READY   STATUS              RESTARTS   AGE
pod/alertgo-5bc79ccd65-8thmw   1/1     Running             0          37m
pod/alertgo-5bc79ccd65-dm8ll   1/1     Running             0          38m
pod/alertgo-5bc79ccd65-m9cd4   0/1     ContainerCreating   0          0s

NAME              TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
service/alertgo   ClusterIP   10.1.140.126   <none>        80/TCP    65m

NAME                         HOSTS                 ADDRESS   PORTS   AGE
ingress.extensions/alertgo   alertgo.sentsss.com             80      34m

NAME                                          REFERENCE            TARGETS            MINPODS   MAXPODS   REPLICAS   AGE
horizontalpodautoscaler.autoscaling/alertgo   Deployment/alertgo   79%/70%, 10%/70%   2         6         2          15s

到此这篇关于K8s部署发布Golang应用程序的实现方法的文章就介绍到这了,更多相关K8s部署发布Golang内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是落寞铅笔最近收集整理的关于K8s部署发布Golang应用程序的实现方法的全部内容,更多相关K8s部署发布Golang应用程序内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(197)

评论列表共有 0 条评论

立即
投稿
返回
顶部