这篇文章给大家分享的是有关k8s如何部署java项目的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
下载jar的包
[root@master test]# wget https://dl.halo.run/release/halo-1.4.16.jar [root@master ~]# ls anaconda-ks.cfg flannel.yml halo-1.4.16.jar init [root@master ~]#
编写Dockerfile并制作镜像
[root@master ~]# mkdir -p test/files [root@master ~]# mv halo-1.4.16.jar test/files/ [root@master ~]# cd test/ [root@master test]# touch Dockerfile [root@master test]# ls Dockerfile files [root@master test]# [root@master test]# cat Dockerfile FROM centos COPY files/halo-1.4.16.jar /usr/local/halo-1.4.16.jar ENTRYPOINT ["java","-jar","/usr/local/halo-1.4.16.jar"] [root@master test]#
[root@master test]# docker build -t 3199560936/halo:v1 . Sending build context to Docker daemon 79.63MB Step 1/3 : FROM centos ---> 5d0da3dc9764 Step 2/3 : COPY files/halo-1.4.16.jar /usr/local/halo-1.4.16.jar ---> 099645e57a21 Step 3/3 : ENTRYPOINT ["java","-jar","/usr/local/halo-1.4.16.jar"] ---> Running in d1e44734578c Removing intermediate container d1e44734578c ---> a38d07129607 Successfully built a38d07129607 Successfully tagged 3199560936/halo:v1 [root@master test]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 3199560936/halo v1 a38d07129607 3 seconds ago 311MB [root@master test]#
上传镜像到docker仓库
[root@master ~]# docker login [root@master ~]# docker push 3199560936/halo:v1 The push refers to repository [docker.io/3199560936/halo] 5906a49ee250: Pushed 74ddd0ec08fa: Mounted from library/centos v1: digest: sha256:fb8cb538526b4d73e1a16b9b1b8a3e4ab4b171f0dbaadfbf6673def9620367f2 size: 741 [root@master ~]#
编写k8s的资源清单
[root@master test]# cat halo.yml apiVersion: apps/v1 kind: Deployment metadata: name: halo labels: app: halo spec: replicas: 1 selector: matchLabels: app: halo template: metadata: name: halo labels: app: halo spec: containers: - image: 3199560936/halo:v1 imagePullPolicy: IfNotPresent name: halo --- apiVersion: v1 kind: Service metadata: name: halo spec: ports: - port: 8090 targetPort: 8090 nodePort: 32767 selector: app: halo type: NodePort [root@master test]#
创建
[root@master test]# kubectl apply -f halo.yml deployment.apps/halo created service/halo created
查看
[root@master test]# kubectl get deploy,pod,svc NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/halo 1/1 1 1 32s NAME READY STATUS RESTARTS AGE pod/halo-6d4cb5dc7b-nzdpw 1/1 Running 0 32s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/halo NodePort 10.106.13.87 <none> 8090:32767/TCP 32s service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 46h
使用本机IP:32767访问
感谢各位的阅读!关于“k8s如何部署java项目”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。