本篇文章给大家分享的是有关kubernates中怎么部署springboot,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
1、创建一个Springboot应用
List-1.1
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping(value = "/")
public String hello(){
System.out.println("hello方法");
return "Hello, k8s!";
}
}
之后mvn clean package,打包,得到app.jar,和Dockerfile放在同一个目录下
List-1.2
mjduan@mjduan-ubuntu:/opt/software/docker/springboot-k8s$ ll
total 676796
drwxr-xr-x 2 mjduan mjduan 4096 2月 15 15:43 ./
drwxr-xr-x 4 mjduan mjduan 4096 2月 15 15:30 ../
-rw-r--r-- 1 mjduan mjduan 16674428 2月 15 15:31 app.jar
-rw-r--r-- 1 mjduan mjduan 362 2月 15 15:37 Dockerfile
2、制作镜像
Dockerfile的内容为
List-2.1
FROM openjdk:8
ADD app.jar /opt/app/app.jar
RUN bash -c 'touch /opt/app/app.jar'
# 开放8080端口
EXPOSE 8080
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# 配置容器启动后执行的命令
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/opt/app/app.jar"]
之后执行命令来得到镜像
List-2.2
mjduan@mjduan-ubuntu:/opt/software/docker/springboot-k8s$ docker build -t hello:1.0 .
3、将该镜像放到所有的节点上
之后将这个镜像放到k8s的master和俩个node上,这里我是先用
docker save hello:1.0>hello.tar
命令得到文件hello.tar,之后将hello.tar用scp命令传到k8s的master和俩个node上。
之后在k8s的master和俩个node上,执行如下命令得到镜像hello:1.0
docker load -i hello.tar
4、kubernates上部署
好了,上述步骤完成后,在k8s的master上创建yaml文件
[root@localhost mjduan]# vi /data/mjduan/hello.yaml
hello.yaml的内容如下
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: example-demo3
labels:
app: example-demo3
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: example-demo3
template:
metadata:
labels:
app: example-demo3
spec:
containers:
- name: example-demo3
#image名称是我们的hello:1.0
image: hello:1.0
ports:
- containerPort: 8080
protocol: TCP
livenessProbe:
httpGet:
path: /
port: 8080
initialDelaySeconds: 30
timeoutSeconds: 30
imagePullPolicy: Never
# Comment the following tolerations if Dashboard must not be deployed on master
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
---
apiVersion: v1
kind: Service
metadata:
name: example-demo3
labels:
app: example-demo3
spec:
ports:
- port: 8080
targetPort: 8080
selector:
app: example-demo3
type: NodePort
部署Docker image,命令如下
[root@localhost mjduan]# kubectl create -f hello.yaml
deployment.apps/example-demo3 created
service/example-demo3 created
查看pod信息,如下
[root@localhost mjduan]# kubectl get pods
NAME READY STATUS RESTARTS AGE
example-demo3-746fc684df-h5dpq 1/1 Running 0 10s
查看service,如下,NAME为example-demo3的那个就是我们刚刚部署的,端口映射是宿主机的32300对应到容器的8080
[root@localhost mjduan]# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
example-demo3 NodePort 10.107.189.127 <none> 8080:32300/TCP 1m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2h
访问服务
[root@localhost mjduan]# curl http://k8s.master:32300
Hello, k8s!
以上就是kubernates中怎么部署springboot,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注天达云行业资讯频道。