目錄
- 創(chuàng)建dockerfile
- 打包并且推送
- 創(chuàng)建namespace
- 創(chuàng)建deployment
- 創(chuàng)建service
- 創(chuàng)建ingress
- 創(chuàng)建hpa
alertGo程序可以參考上篇文章,主要用于alertmanager實(shí)現(xiàn)釘釘報(bào)警
創(chuàng)建dockerfile
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"]
打包并且推送
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
創(chuàng)建namespace
apiVersion: v1
kind: Namespace
metadata:
name: k8s-go
創(chuàng)建deployment
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
創(chuàng)建service
apiVersion: v1
kind: Service
metadata:
name: alertgo
namespace: k8s-go
spec:
selector:
app: alertgo
ports:
- port: 80
targetPort: 8088
創(chuàng)建ingress
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
創(chuàng)建hpa
kind: HorizontalPodAutoscaler # 對象類型,簡稱 hpa,水平自動伸縮
apiVersion: autoscaling/v2beta2 # autoscaling/v2beta2 與 autoscaling/v1 的 API 有很大的不同,注意識別兩者的差異
metadata:
name: alertgo
namespace: fronted
spec:
scaleTargetRef: # 伸縮的目標(biāo)對象
apiVersion: apps/v1 # 對象版本
kind: Deployment # 目標(biāo)對象的類型
name: alertgo # 目標(biāo)對象的名稱
minReplicas: 3 # 最小副本數(shù)
maxReplicas: 6 # 最大副本數(shù)
metrics: # 指標(biāo)
- type: Resource # 類型:資源
resource:
name: memory # 內(nèi)存
target:
type: Utilization
averageUtilization: 70 # 1% 這個值是為了實(shí)驗(yàn),具體值請參考業(yè)務(wù)方實(shí)際情況而定
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
結(jié)果查看
[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
到此這篇關(guān)于K8s部署發(fā)布Golang應(yīng)用程序的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)K8s部署發(fā)布Golang內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 使用k8s部署Django項(xiàng)目的方法步驟
- k8s部署redis cluster集群的實(shí)現(xiàn)
- K8S二進(jìn)制部署的K8S(1.15)部署hpa功能
- 在K8s上部署Redis集群的方法步驟
- SpringBoot應(yīng)用快速部署到K8S的詳細(xì)教程
- 使用Rancher在K8S上部署高性能PHP應(yīng)用程序的教程
- k8s部署docker容器的實(shí)現(xiàn)
- Docker學(xué)習(xí)筆記之k8s部署方法