k8s配置go服务
# 配置Dockerfile
# 第一阶段:编译Go应用
FROM golang:1.20-alpine AS builder
# 设置Go环境变量,使用国内代理
ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.cn,direct
# 设置工作目录
WORKDIR /build
# 复制Go项目文件
COPY . .
# 编译Go应用
RUN go build -o xirang .
# 第二阶段:运行环境
FROM alpine:3.16
# 设置工作目录
WORKDIR /app
# 从builder阶段复制编译后的二进制文件
COPY --from=builder /build/xirang .
# 复制配置文件
COPY config.yml .
COPY rbac_model.conf .
COPY xirang-priv.pem .
COPY xirang-pub.pem .
# 暴露端口
EXPOSE 8888
# 设置启动命令
CMD ["./xirang", "-c", "config.yml"]
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
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
打包镜像
docker build -t docker.cnb.cool/zzppjj/docker-images/xirang:v6 .
docker push docker.cnb.cool/zzppjj/docker-images/xirang:v6
1
2
2
# 配置kubernetes部署文件
创建命名空间
kubectl create ns deploy02
1
configmap.yml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config-files
namespace: apply02
data:
config.yml: |
system:
mode: debug
url-path-prefix: api
port: 8888
init-data: true
rsa-public-key: xirang-pub.pem
rsa-private-key: xirang-priv.pem
logs:
level: -1
path: logs
max-size: 50
max-backups: 100
max-age: 30
compress: false
database:
driver: mysql
source: xirang.db
mysql:
username: root
password: 2025W#@qGhsd
database: xirang
host: 192.168.51.49
port: 13306
query: parseTime=True&loc=Local&timeout=10000ms
log-mode: true
table-prefix: tb
charset: utf8mb4
collation: utf8mb4_general_ci
casbin:
model-path: 'rbac_model.conf'
jwt:
realm: test jwt
key: secret key
timeout: 12000
max-refresh: 12000
rate-limit:
fill-interval: 50
capacity: 200
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
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
挂载日志卷pv、pvc(持久化日志)
# persistent-volume.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: go-logs-pv
namespace: apply02
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: local-storage
local:
path: /data/go/xirang/logs
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- k8s-node2 # 替换为实际的节点名称
---
# persistent-volume-claim.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: go-logs-pvc
namespace: apply02
spec:
accessModes:
- ReadWriteOnce
storageClassName: local-storage
resources:
requests:
storage: 10Gi
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
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
deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: go-deployment
namespace: apply02
labels:
app: go-app
spec:
replicas: 3
selector:
matchLabels:
app: go-app
template:
metadata:
labels:
app: go-app
spec:
containers:
- name: go
image: docker.cnb.cool/zzppjj/docker-images/xirang:v6
ports:
- containerPort: 8888
args:
["sh", "-c", "./xirang -c config.yml > /dev/null 2>&1"]
volumeMounts:
- name: app-config
mountPath: /app/config.yml
subPath: config.yml
- name: logs-volume
mountPath: /app/logs
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "256Mi"
cpu: "200m"
volumes:
- name: app-config
configMap:
name: app-config-files
items:
- key: config.yml
path: config.yml
- name: logs-volume
persistentVolumeClaim:
claimName: go-logs-pvc
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
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
svc.yaml
apiVersion: v1
kind: Service
metadata:
name: go-service
namespace: apply02
spec:
selector:
app: go-app
ports:
- protocol: TCP
port: 8888
targetPort: 8888
type: ClusterIP
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 前端代码部署
打包镜像文件
ROM node:14.18 AS builder
RUN mkdir /app
ADD . /app/
WORKDIR /app
RUN git config --global url."https://".insteadOf git:// \
&& sed -i s#localhost:8888#xirang.xsnd.com#g .env.development \
&& sed -i s#localhost:8888#xirang.xsnd.com#g .env.production \
&& npm install --registry=http://registry.npmmirror.com \
&& yarn build:prod
FROM nginx:alpine
# 安装 netcat 或 telnet(这里以 netcat 为例)
RUN apk update && apk add --no-cache netcat-openbsd
COPY /app/dist /usr/share/nginx/html
CMD nginx -g "daemon off;"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
configmap
apiVersion: v1
kind: ConfigMap
metadata:
name: my.conf
namespace: apply02
data:
my.conf: |
server {
listen 10480;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://go-service:8888; # 设置代理服务器的协议和地址
}
}
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
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
deployment.yml、svc.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: xirang-ui
labels:
app: xirang-ui
namespace: apply02
spec:
replicas: 1
selector:
matchLabels:
app: xirang-ui
template:
metadata:
labels:
app: xirang-ui
spec:
containers:
- name: xirang-ui-container
image: docker.cnb.cool/zzppjj/docker-images/xirang-ui:v3 # 如果使用具体版本请修改 tag
ports:
- containerPort: 10480
resources:
requests:
memory: "256Mi"
cpu: "100m"
limits:
memory: "512Mi"
cpu: "200m"
volumeMounts:
- mountPath: /etc/nginx/conf.d/
name: nginx-config
livenessProbe:
httpGet:
path: /
port: 10480
initialDelaySeconds: 15
periodSeconds: 20
readinessProbe:
httpGet:
path: /
port: 10480
initialDelaySeconds: 5
periodSeconds: 10
volumes:
- name: nginx-config
configMap:
name: my.conf
---
apiVersion: v1
kind: Service
metadata:
name: xirang-ui-service
namespace: apply02
spec:
type: ClusterIP # 如果使用云服务商,会创建外部负载均衡器
selector:
app: xirang-ui
ports:
- protocol: TCP
port: 80 # 外部访问端口
targetPort: 10480 # 容器端口
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
52
53
54
55
56
57
58
59
60
61
62
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
52
53
54
55
56
57
58
59
60
61
62
ingress.yml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: go-ingress
namespace: apply02
annotations:
nginx.ingress.kubernetes.io/rewrite-target: / # Nginx Ingress 专用注解
spec:
ingressClassName: nginx # 指定 Ingress 控制器类型
rules:
- host: xirang.xsnd.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: xirang-ui-service
port:
number: 80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
其它配置示例参考
- https://github.com/flipped-aurora/gin-vue-admin/tree/main/deploy/kubernetes
项目仓库地址
后端:https://github.com/eryajf/xirang.git
前端:https://github.com/eryajf/xirang-ui.git


上次更新: 2025/02/18, 09:32:44