美文网首页
kubernetes 部署 SpringBoot

kubernetes 部署 SpringBoot

作者: 不同而大同 | 来源:发表于2022-01-23 05:00 被阅读0次

修改 service 端口(未安装Tomcat 8080端口跳过)

把 Tomcat 端口 8080 修改为 8081

kubectl expose  deployment  tomcat  --port=8081  --target-port=8081 --type=NodePort --dry-run -o yaml > tomcat_service.yaml

再运行 kubectl apply -f tomcat_service.yaml

K8s 部署 SpringBoot

构建 Docker jdk 镜像

创建文件夹 mkdir -p /root/jdk_dockerfile/
cd /root/jdk_dockerfile/
上传 jdk-8u281-linux-x64.tar.gz
创建 vi Dockerfile 添加下面内容

FROM centos:latest
MAINTAINER cat
ADD jdk-8u281-linux-x64.tar.gz /usr/local/java
ENV JAVA_HOME /usr/local/java/jdk1.8.0_281
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH $PATH:$JAVA_HOME/bin
CMD java -version

构建 docker build -t jdk1.8.0_281 .

Successfully built 52e1777fe157
Successfully tagged jdk1.8.0_281:latest

提示构建成功

查看镜像ID

[root@k8smaster jdk_dockerfile]# docker images
REPOSITORY                                                        TAG       IMAGE ID       CREATED         SIZE
jdk1.8.0_281                                                      latest    52e1777fe157   4 minutes ago   588MB

运行镜像:docker run -d 52e1777fe157

构建 SpringBoot 镜像

创建文件夹mkdir -p /root/springbook_dockerfile
进入文件夹 cd /root/springbook_dockerfile
上传 hello.jar
创建 vi Dockerfile

FROM jdk1.8.0_281
MAINTAINER cat
ADD hello.jar /opt
RUN chmod +x /opt/hello.jar
CMD java -jar /opt/hello.jar

构建镜像 docker build -t springboot-helloworld-1.0.0 .

#前面代码忽略,查看最后两行成功即可
Successfully built 05a58e911290
Successfully tagged springboot-helloworld-1.0.0:latest

空运行测试

# 直接空运行yaml格式
kubectl create deployment springboot-helloworld --image=springboot-helloworld-1.0.0 --dry-run -o yaml
# 直接空运行json格式
kubectl create deployment springboot-helloworld --image=springboot-helloworld-1.0.0 --dry-run -o json
# 空运行导出springboot-helloworld.yaml
kubectl create deployment springboot-helloworld --image=springboot-helloworld-1.0.0 --dry-run -o yaml > springboot-helloworld.yaml
#空运行导出springboot-helloworld.json
kubectl create deployment springboot-helloworld --image=springboot-helloworld-1.0.0 --dry-run -o json> springboot-helloworld.json

空运行导出yaml文件

[root@k8smaster springbook_dockerfile]# kubectl create deployment springboot-helloworld --image=springboot-helloworld-1.0.0 --dry-run -o yaml > springboot-helloworld.yaml
W0121 05:12:22.447185   28189 helpers.go:598] --dry-run is deprecated and can be replaced with --dry-run=client.

打开文件 vi springboot-helloworld.yaml 添加 imagePullPolicy: Never

      containers:
      - image: springboot-helloworld-1.0.0
        name: springboot-helloworld-1-0-0-wkvcm
#加入下面一行,表示不拉取远程镜像,用本地镜像
        imagePullPolicy: Never

yaml部署命令 kubectl apply -f springboot-helloworld.yaml
或者 kubectl create deploy springboot-helloworld --image=springboot-helloworld-1.0.0

[root@k8smaster springbook_dockerfile]# kubectl apply -f springboot-helloworld.yaml
deployment.apps/springboot-helloworld created
[root@k8smaster springbook_dockerfile]# kubectl get pod
NAME                                     READY   STATUS              RESTARTS      AGE
nginx-85b98978db-w87hr                   1/1     Running             2 (20h ago)   28h
springboot-helloworld-75685ddb9d-jvbz4   0/1     ErrImageNeverPull   0             26s

出现状态错误 ErrImageNeverPull 要把node机器也要安装两个镜像 jdk1.8.0_281springboot-helloworld-1.0.0
稍等再执行 kubectl get pod

[root@k8smaster ~]# kubectl get pod
NAME                                     READY   STATUS    RESTARTS      AGE
nginx-85b98978db-w87hr                   1/1     Running   2 (21h ago)   28h
springboot-helloworld-75685ddb9d-jvbz4   1/1     Running   0             19m

暴露端口 helloworld端口是10086
kubectl expose springboot-helloworld --port=10086 --type=NodePort
再查询service

[root@k8smaster ~]# kubectl get svc
NAME                    TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)           AGE
kubernetes              ClusterIP   10.66.0.1     <none>        443/TCP           3d3h
nginx                   NodePort    10.66.32.86   <none>        80:31617/TCP      28h
springboot-helloworld   NodePort    10.66.3.59    <none>        10086:30061/TCP   12s

访问:http://192.168.116.104:30061/


最终效果

相关文章

网友评论

      本文标题:kubernetes 部署 SpringBoot

      本文链接:https://www.haomeiwen.com/subject/wkrshrtx.html