本篇内容介绍了“K8s怎么部署发布Golang应用程序”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
目录
创建dockerfile
打包并且推送
创建namespace
创建deployment
创建service
创建ingress
创建hpa
alertGo程序可以参考上篇文章,主要用于alertmanager实现钉钉报警
创建dockerfile
FROM golang:1.14-alpineENV GOPROXY=https://goproxy.cnWORKDIR /buildCOPY . .EXPOSE 8088RUN mkdir /appRUN go mod tidyRUN go build -o /app/alertGo alertGo.goWORKDIR /appCMD ["/app/alertGo"]
打包并且推送
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
apiVersion: v1kind: Namespacemetadata: name: k8s-go
创建deployment
apiVersion: apps/v1kind: Deploymentmetadata: name: alertgo namespace: k8s-gospec: 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
apiVersion: v1kind: Servicemetadata: name: alertgo namespace: k8s-gospec: selector: app: alertgo ports: - port: 80 targetPort: 8088
创建ingress
kind: Ingress # 对象类型apiVersion: networking.k8s.io/v1beta1metadata: name: alertgo namespace: k8s-gospec: rules: - host: alertgo.xxx.com http: paths: - path: / backend: serviceName: alertgo servicePort: 80
创建hpa
kind: HorizontalPodAutoscaler # 对象类型,简称 hpa,水平自动伸缩apiVersion: autoscaling/v2beta2 # autoscaling/v2beta2 与 autoscaling/v1 的 API 有很大的不同,注意识别两者的差异metadata: name: alertgo namespace: frontedspec: 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 0sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/alertgo ClusterIP 10.1.140.126 <none> 80/TCP 65mNAME HOSTS ADDRESS PORTS AGE
ingress.extensions/alertgo alertgo.sentsss.com 80 34mNAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
horizontalpodautoscaler.autoscaling/alertgo Deployment/alertgo 79%/70%, 10%/70% 2 6 2 15s
“K8s怎么部署发布Golang应用程序”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!