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           8090:32767/TCP   32s
service/kubernetes   ClusterIP   10.96.0.1              443/TCP          46h

使用本机IP:32767访问

k8s部署java项目的实现_第1张图片

k8s部署java项目的实现_第2张图片

k8s部署java项目的实现_第3张图片

到此这篇关于k8s部署java项目的实现的文章就介绍到这了,更多相关k8s部署java项目内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(k8s部署java项目的实现)