SpringBoot服务迁移至kubernetes
Posted 潇湘海棠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot服务迁移至kubernetes相关的知识,希望对你有一定的参考价值。
1.准备Docker编译工作
1.1基础准备之安装java环境
1.安装java环境
[root@kn-server-master01-13 springboot]# yum install java -y
1.2安装maven
2.安装Maven,因为要使用Maven来进行编译安装。
maven官方下载地址: https://maven.apache.org/download.cgi
3.下载.gz压缩包到本地然后解压。
[root@kn-server-master01-13 springboot]# tar -xvf apache-maven-3.8.6-bin.tar.gz
4.修改配置文件指定包的路径即可。
vim /etc/profile
export M2_HOME=/usr/local/apache-maven-3.8.6
export PATH=$M2_HOME/bin:$PATH
5.重载配置
# source /etc/profile
6.修改maven的源获取地址为aliyun
https://maven.aliyun.com/repository/public
7.centos可以直接执行以下命令即可
[root@kn-server-master01-13 springboot]# yum install maven -y
1.3对Springboot应用进行编译并运行
1.解压应用的tar包
[root@kn-server-master01-13 springboot]# tar -xvf springboot-helloworld-jar.tar.gz
2.执行编译操作
[root@kn-server-master01-13 springboot-helloworld]# mvn clean package
[INFO] Scanning for projects...
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/com/google/guava/guava/11.0.2/guava-11.0.2.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/vafer/jdependency/0.7/jdependency-0.7.jar (12 kB at 4.4 kB/s)
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/commons-io/commons-io/1.3.2/commons-io-1.3.2.jar (88 kB at 32 kB/s)
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/asm/asm-util/3.2/asm-util-3.2.jar (37 kB at 13 kB/s)
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar (33 kB at 11 kB/s)
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/asm/asm-analysis/3.2/asm-analysis-3.2.jar (18 kB at 5.8 kB/s)
Downloaded from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/com/google/guava/guava/11.0.2/guava-11.0.2.jar (1.6 MB at 495 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:26 min
[INFO] Finished at: 2022-09-20T14:28:33+08:00
[INFO] ------------------------------------------------------------------------
3.编译执行完成后当前目录下会有一个jar包
root@ks-master01-10:/cloud-Native/springboot/springboot-helloworld/target# ll
total 12460
drwxr-xr-x 6 root root 4096 Sep 20 14:28 ./
drwxr-xr-x 4 root root 4096 Sep 20 14:27 ../
drwxr-xr-x 3 root root 4096 Sep 20 14:27 classes/
-rw-r--r-- 1 root root 12726619 Sep 20 14:28 demo-service-1.0.jar
-rw-r--r-- 1 root root 3556 Sep 20 14:28 demo-service-1.0.jar.original
drwxr-xr-x 3 root root 4096 Sep 20 14:27 generated-sources/
drwxr-xr-x 2 root root 4096 Sep 20 14:28 maven-archiver/
drwxr-xr-x 3 root root 4096 Sep 20 14:27 maven-status/
4.运行jar包
[root@kn-server-master01-13 target]# java -jar demo-service-1.0.jar
seconds (JVM running for 2.974)
2022-09-20 15:32:30.399 INFO 2344 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet dispatcherServlet
2022-09-20 15:32:30.399 INFO 2344 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet dispatcherServlet: initialization started
2022-09-20 15:32:30.413 INFO 2344 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet dispatcherServlet: initialization completed in 14 ms
1.4访问测试;
1.查看8080端口是否启动
root@kn-server-master01-13 ~]# netstat -tnlp | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 13518/java
2.curl访问测试
[root@kn-server-master01-13 ~]# curl 10.0.0.13:8080/
Hello Knative
2.准备Spring应用迁移至kubernetes
2.1准备Deployment配置片段;
[root@kn-server-master01-13 ~]# cat springboot-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: springboot
spec:
replicas: 3
selector:
matchLabels:
app: spring
template:
metadata:
labels:
app: spring
spec:
containers:
- name: springboot
image: registry.cn-hangzhou.aliyuncs.com/lengyuye/springboot:v1.0
ports:
- name: http
containerPort: 8080
env: # 传递初始堆内存和最大堆内存占用
- name: XMS_OPTS
valueFrom:
resourceFieldRef:
resource: requests.memory
- name: XMX_OPTS
valueFrom:
resourceFieldRef:
resource: limits.memory
resources:
requests:
memory: 150Mi
limits:
memory: 300Mi
readinessProbe: # 就绪探针;如果端口不存活,则从负载均衡中移除
tcpSocket:
port: http # http是一个名字;它会获取这个名字对应的端口;
initialDelaySeconds: 10
failureThreshold: 3
livenessProbe: # 存活探针;获取url,状态码不对那么则触发重启操作
httpGet:
path: /
port: http
initialDelaySeconds: 10
failureThreshold: 3
[root@kn-server-master01-13 ~]# kubectl apply -f springboot-deploy.yaml
deployment.apps/springboot created
查看Pod是否运行
[root@kn-server-master01-13 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
springboot-bd6f67ff-c6dtn 1/1 Running 0 107s
springboot-bd6f67ff-jcjst 1/1 Running 0 107s
springboot-bd6f67ff-pk8gz 1/1 Running 0 107s
2.2准备Service配置文件
[root@kn-server-master01-13 ~]# cat springboot-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: spring-svc
spec:
type: NodePort
clusterIP:
selector:
app: spring
ports:
- name: http
protocol: TCP
port: 8080
targetPort: 8080 # 后端Pod监听什么端口就写什么端口。要不然到达Service的请求转发给Pod,Pod没有那个端口也没用。一定真正转发到后端程序监听的端口。如果没有特殊情况的话,ServicePort和TargetPort保持一致。NodePort可以不用指定。
nodePort: # 正常情况下应由系统自己分配,除非事先能够明确知道它不会与某个现存的Service资源产生冲突,没有特别需求,留给系统自动配置总是好的选择。
[root@kn-server-master01-13 ~]# kubectl apply -f springboot-svc.yaml
service/spring-svc created
查看Serivces资源是否关联Pod;
SVC类型为NodePort
[root@kn-server-master01-13 ~]# kubectl get svc
spring-svc NodePort 10.96.14.100 <none> 8080:32629/TCP 100m
已经关联到后端Pod;
[root@kn-server-master01-13 ~]# kubectl describe svc spring-svc
Name: spring-svc
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=spring
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.96.14.100
IPs: 10.96.14.100
Port: http 8080/TCP
TargetPort: 8080/TCP
NodePort: http 32629/TCP
Endpoints: 192.168.2.46:8080,192.168.2.47:8080,192.168.2.48:8080
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
类型为NodePort,在集群内外都可以访问;
[root@kn-server-master01-13 springboot-helloworld]# curl 10.96.14.100:8080/
Hello Knative
2.3集群外测试访问;
以上是关于SpringBoot服务迁移至kubernetes的主要内容,如果未能解决你的问题,请参考以下文章
实战应用:借助Kubernetes不停机从Heroku迁移至AWS
SpringCloud微服务电商系统在Kubernetes集群中上线详细教程
Cloud Native Weekly | KubeCon首登中国,华为云亮相KubeCon 2018,微软云服务又罢工