Kubernetes部署Java项目

使用Docker和Kubernetes部署Java应用
本文详细介绍了如何将Java项目打包成jar,创建Docker镜像并上传到仓库,最后通过Kubernetes的资源清单进行部署。重点讨论了在Kubernetes环境中运行Java应用程序的关键步骤。
[root@master ~]# tree javaweb/
javaweb/
├── Dockerfile
└── files
    └── halo-1.4.16.jar

制作镜像

[root@master javaweb]# vim Dockerfile 

FROM openjdk:18-jdk-alpine
  
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 javaweb]# docker build -t harry1004/halo:1.0 .
Sending build context to Docker daemon  79.63MB
Step 1/3 : FROM openjdk:18-jdk-alpine
18-jdk-alpine: Pulling from library/openjdk
59bf1c3509f3: Pull complete 
20ae5de21b22: Pull complete 
3048de22201a: Pull complete 
Digest: sha256:e5c5b35b831a4f655074a25604130ce53e33567b82c8a7204f0e5641b66d477e
Status: Downloaded newer image for openjdk:18-jdk-alpine
 ---> c89120dcca4c
Step 2/3 : COPY files/halo-1.4.16.jar /usr/local/halo-1.4.16.jar
 ---> 2134d5f8cacf
Step 3/3 : ENTRYPOINT ["java","-jar","/usr/local/halo-1.4.16.jar"]
 ---> Running in c173a74eeed4
Removing intermediate container c173a74eeed4
 ---> eaa8c9ae6a21
Successfully built eaa8c9ae6a21
Successfully tagged harry1004/halo:1.0
[root@master javaweb]# docker images |grep halo
harry1004/halo                                                    1.0             eaa8c9ae6a21   15 seconds ago   408MB

上传镜像到docker仓库

[root@master javaweb]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: harry1004
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@master javaweb]# docker push harry1004/halo:1.0
The push refers to repository [docker.io/harry1004/halo]
75586d88cfa3: Pushed 
f64df31005fa: Mounted from library/openjdk 
4935b8115970: Mounted from library/openjdk 
8d3ac3489996: Mounted from library/openjdk 
1.0: digest: sha256:1bd73a2178e42cf71bd3f26e6e7abeeb3e7ff1697bb68d928071b21c4b58e114 size: 1163

k8s的资源清单

[root@master manifest]# vim 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: harry1004/halo:1.0
        imagePullPolicy: IfNotPresent
        name: halo

---
apiVersion: v1
kind: Service
metadata:
  name: halo
spec:
  ports:
  - port: 8090
    targetPort: 8090
    nodePort: 30101
  selector:
    app: halo
  type: NodePort
[root@master manifest]# kubectl apply -f halo.yml
deployment.apps/halo created
service/halo created

[root@master manifest]# kubectl get deploy,pod,svc
NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/halo   0/1     1            0           18m

NAME                        READY   STATUS              RESTARTS   AGE
pod/halo-6c78b67985-bjk84   1/1     Running             0          18m

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
service/halo         NodePort    10.108.115.47   <none>        8090:30101/TCP   18m
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          26h
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值