SpringBoot k8s部署
工作中需要将应用部署到k8s的集群上,之前是使用docker来部署的;这里使用service的nodePort的方式来访问;1.Deployment的配置apiVersion: apps/v1kind: Deploymentmetadata:name: powerflownamespace: powerflowspec:replicas: 2selector...
工作中需要将应用部署到k8s的集群上,之前是使用docker来部署的;这里使用service的nodePort的方式来访问;
1.Deployment的配置
apiVersion: apps/v1
kind: Deployment
metadata:
name: powerflow
namespace: powerflow
spec:
replicas: 2
selector:
matchLabels:
app: powerflow
version: "1.0"
template:
metadata:
labels:
app: powerflow
version: "1.0"
spec:
containers:
- name: powerflow-app
image: gg-sophon-13:5000/transwarp/powerflow
env:
- name: SPRING_PROFILES_ACTIVE
value: prod
- name: SPRING_DATASOURCE_URL
value: jdbc:mysql://172.26.4.13:3316/powerflow?useUnicode=true&characterEncoding=utf8&useSSL=false
- name: SPRING_DATASOURCE_USERNAME
value: root
# resources:
# requests:
# memory: "256m"
# cpu: "0.1"
# limits:
# memory: "512m"
# cpu: "0.1"
ports:
- name: http
containerPort: 9998
readinessProbe:
httpGet:
path: /management/health
port: http
scheme: HTTPS
initialDelaySeconds: 20
periodSeconds: 15
failureThreshold: 6
livenessProbe:
httpGet:
path: /management/health
port: http
scheme: HTTPS
initialDelaySeconds: 120
nodeSelector:
powerflow-node: app
这里把resource配置的部分注释了,是因为在我的集群上运行的时候莫名的内核异常;注意的几点问题:
1>selector的matchLabel必须和template中的labels匹配;
2>image中的镜像必须推送到集群的镜像仓库上;格式是镜像仓库的路径/镜像的名称:镜像的标签
3>ports里面的containerPort指容器暴露的端口,这个跟prod中配置的端口应该一直;
4>readinessProbe是容器存活状态检查,有几种方式,比如command,tcp,http等方式,sheme这里是HTTPS的,因为我的应用是https的;initialDelaySeconds指容器就绪之后多久开始执行存活检测;periodSeconds这里是检查的时间间隔是15秒;failureThreshold是失败之前尝试的次数,意味着达到这个值,放弃检查,会重新启动pod;livenessProbe是就绪检查;
5>最后的nodeSelector是调度到对应有这个标签的节点上去,所以对应的节点也要打标签;
2.Service的配置
apiVersion: v1
kind: Service
metadata:
name: powerflow
namespace: powerflow
labels:
app: powerflow
spec:
selector:
app: powerflow
type: NodePort
ports:
- name: http
port: 9998
nodePort: 31566
protocol: TCP
这里标识servie的类型是nodePort;NodePort 服务是引导外部流量到你的服务的最原始方式,在所有节点(虚拟机)上开放一个特定端口,任何发送到该端口的流量都被转发到对应服务。其他的方式还有ingress和LoadBalancer;

这里可以看到两个端口,对外的端口是31566,这个是我们设置nodePort的端口;那么就可以通过这个端口进行访问;比如集群有3个节点,172.16.3.13,172.16.3.14,172.16.3.15;无论起了几个pod,那么只有一个pod(假设是在15上面),也可以通过172.16.1.13:31566来访问这个pod;注意点:
1>这里pod的labels要匹配pod中的标签
2>这里的nodePort端口是30000-32767,如果不配置的话是随机生成的;
下面一篇会记录下k8s下的文件挂载方式;
更多推荐



所有评论(0)