Java业务上kubernetes的注意点

java业务上k8s的话,看到一篇比较好的文章,要注意的地方:

 1apiVersion: apps/v1
 2kind: Deployment
 3metadata:
 4  name: java-web
 5  namespace: ops
 6  labels:
 7    app: java-web
 8spec:
 9  selector:
10    matchLabels:
11      app: java-web
12  replicas: 2
13  strategy:
14    type: RollingUpdate
15  template:
16    metadata:
17      name: java-web
18      annotations:   # 第一点:新增注解
19        loki.io/scrape: 'true' # 抓取直接输出的日志到loki里
20        prometheus.io/path: /metrics #采集的监控地址
21        prometheus.io/port: '2112'  #采集暴露的监控端口
22        prometheus.io/scrape: 'true'  #打开采集开关
23      labels:
24        app: java-web
25    spec:
26      imagePullSecrets:  # 第二点:添加权限
27        - name: my-docker #只有该用户才有pull镜像的权限
28      containers:
29        - name: java-web
30          image: hb.ops.top/ops/java-web:5917f92c
31          imagePullPolicy: IfNotPresent
32          readinessProbe:  # 第三点:新增存活检测,当前是针对监控端口进行存活检测,也就是说只有当监控端口起来了,程序才算完全启动
33            httpGet:
34              port: 2112
35              path: /metrics/prometheus
36            initialDelaySeconds: 60
37            successThreshold: 1
38            failureThreshold: 3
39            timeoutSeconds: 5
40          startupProbe:
41            httpGet:
42              port: 2112
43              path: /metrics/prometheus
44            initialDelaySeconds: 60
45            successThreshold: 1
46            failureThreshold: 3
47            timeoutSeconds: 5
48          livenessProbe:
49            httpGet:
50              port: 2112
51              path: /metrics/prometheus
52            initialDelaySeconds: 60
53            successThreshold: 1
54            failureThreshold: 3
55            timeoutSeconds: 5
56          resources: # 第四点:设置资源限制,如果是java程序,建议limit比request大,这样可以合理分配堆内存和非堆内存
57            requests:
58              memory: 1024Mi
59            limits:
60              memory: 2048Mi
61          ports:
62            - name: web-port
63              containerPort: 8889
64          env: # 第五点:设置环境变量,可以设置时区,指定EVN环境,添加JAVA参数等
65            - name: JVM_OPTS
66              value: -javaagent:/opt/skywalking/skywalking-agent.jar -Xmx1G -Xms1G
67            - name: TZ
68              value: Asia/Shanghai
69            - name: APOLLO_LABEL
70              value: gray
71            - name: SW_AGENT_NAME
72              value: 'k8s-java-web'
73            - name: SW_AGENT_COLLECTOR_BACKEND_SERVICES
74              value: '10.0.0.123:11800'
75            - name: ENV
76              value: prod
77
78---
79
80apiVersion: v1
81kind: Service
82metadata:
83  labels:
84    app: java-web
85  name: java-web
86  namespace: ops
87spec:
88  ports:
89  - name: java-web-port
90    port: 8889
91    protocol: TCP
92    targetPort: 8889
93    nodePort: 30009
94  selector:
95    app: java-web
96  sessionAffinity: None
97  type: NodePort
98status:
99  loadBalancer: {}

上面忽略了启动命令,其实容器内的启动命令也是很有学问的。


Kubernetes中Pod资源清单和解释
禁掉Rsyslog的dns解析
comments powered by Disqus