

k8s部署springboot - tiandizhiguai
source link: https://www.cnblogs.com/tiandizhiguai/p/17526820.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

k8s部署springboot
首先介绍发布步骤。
1.从代码仓库下载代码,比如GitLab;
2.接着是进行打包,比如使用Maven;
3.编写Dockerfile文件,把步骤2产生的包制作成镜像;
4.上传步骤3的镜像到远程仓库,比如Harhor;
5.编写Deployment文件;
6.提交Deployment文件到k8s集群;
从以上步骤可以看出,发布需要的工具和环境至少包括:代码仓库(GitLab)、打包环境(Maven)、镜像制作(Docker)、镜像仓库(Harbor)、k8s集群等。
当前,也可以借助一些开源的工具或平台来发布应用,比如:Jenkins、https://github.com/512team/dhorse等。
假如有一个名为springboot的SpringBoot应用,服务端口是8080,并且有一个/hello接口,下面详细介绍发布过程。
这里直接从GitLab下载到本地,执行maven打包命令,这里打为Jar包:
mvn clean package
生成的包为:springboot-1.0.0.jar
以下步骤在具有Docker环境的Linux机器上操作。
- 把springboot-1.0.0.jar放到/usr/local/springboot目录下,并在该目录下创建Dockerfile文件,内容为:
FROM openjdk:8-jdk-alpine
ADD springboot-1.0.0.jar /usr/local/springboot.jar
ENTRYPOINT ["java", "-jar", "/usr/local/springboot.jar"]
则/usr/local/springboot目录的文件为:
-rw-r--r-- 1 root root 119 Feb 22 17:50 Dockerfile
-rw-r--r-- 1 root root 21863457 Feb 22 17:07 springboot-1.0.0.jar
- 制作镜像,在/usr/local/springboot目录下执行命令:
docker build -t 192.0.10.22:20080/dhorse/springboot:1.0.0 .
其中,192.0.10.22:20080是Harbor镜像仓库地址,dhorse是项目名。
- 登录仓库并上传镜像
docker login 192.0.10.22:20080 -u admin -p Harbor12345
docker push 192.0.10.22:20080/dhorse/springboot:1.0.0
编写Deployment文件
创建springboot-k8s.yml文件,内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: springboot
labels:
app: springboot
spec:
replicas: 1
selector:
matchLabels:
app: springboot
template:
metadata:
labels:
app: springboot
spec:
containers:
- name: springboot
image: 192.0.10.22:20080/dhorse/springboot:1.0.0
imagePullPolicy: Always
以下操作在k8s集群的mater机器上执行。
把springboot-k8s.yml文件复制/user/local目录下,并在当前目录执行如下命令:
kubectl apply -f springboot-k8s.yml
稍后,再执行如下命令:
kubectl get pods -o wide
输出结果如下:
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
springboot-79d56dc985-7dz6q 1/1 Running 0 8s 10.32.1.180 centos06 <none> <none>
然后访问/hello接口:
curl http://10.32.1.180:8080/hello
推荐一个不错的部署应用的工具,https://gitee.com/i512team/dhorse
至此,发布结束。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK