第二十七章 Kubernetes部署Java项目

一、容器交付流程

二、k8s部署项目流程(细节过程)

三、Java项目打包

准备Java项目,把java进行打包(jar包或者war包)
依赖环境: 
1)java环境 jdk环境
2)maven环境

打包:
mvn clean package

打包完成后会在target目录下生成jar包:

四、构建镜像

1.上传jar包

[root@kubernetes-master-001 ~]# mkdir -p test-java && cd test-java
[root@kubernetes-master-001 ~/test-java]# rz
[root@kubernetes-master-001 ~/test-java]# ll
total 17156
-rw-r--r-- 1 root root 17566601 Nov 26 16:35 demojenkins.jar

2.编写Dockerfile文件

[root@kubernetes-master-001 ~/test-java]# vi Dockerfile 
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD ./demojenkins.jar demojenkins.jar
ENTRYPOINT ["java","-jar","/demojenkins.jar", "&"]

3.构建镜像

[root@kubernetes-master-001 ~/test-java]# docker build -t java-demo:v1.0 .
Sending build context to Docker daemon  17.57MB
Step 1/4 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/4 : VOLUME /tmp
 ---> Using cache
 ---> cf4c76b7202b
Step 3/4 : ADD ./demojenkins.jar demojenkins.jar
 ---> ff2b30af1a30
Step 4/4 : ENTRYPOINT ["java","-jar","/demojenkins.jar", "&"]
 ---> Running in b4acb4c7da0f
Removing intermediate container b4acb4c7da0f
 ---> b00fb1e33c49
Successfully built b00fb1e33c49
Successfully tagged java-demo:v1.0

4.验证镜像

[root@kubernetes-master-001 ~/test-java]# docker images
REPOSITORY                                                           TAG            IMAGE ID       CREATED          SIZE
java-demo                                                            v1.0           b00fb1e33c49   22 seconds ago   122MB

五、推送镜像到镜像仓库

#1.登录阿里云 --> 容器镜像服务 --> 选择个人仓库/企业仓库 --> 然后选择镜像仓库 --> 选择创建镜像仓库

[root@kubernetes-master-001 ~/test-java]# docker login  --username=j**** registry.cn-shanghai.aliyuncs.com
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@kubernetes-master-001 ~/test-java]# docker tag  b00fb1e33c49 registry.cn-shanghai.aliyuncs.com/linux_yuncloud/java-demo:v1.0.0
[root@kubernetes-master-001 ~/test-java]# docker push  registry.cn-shanghai.aliyuncs.com/linux_yuncloud/java-demo:v1.0.0
The push refers to repository [registry.cn-shanghai.aliyuncs.com/linux_yuncloud/java-demo]
37b94cd6b0c0: Pushed 
ceaf9e1ebef5: Pushed 
9b9b7f3d56a0: Pushed 
f1b5933fe4b5: Pushed 
v1.0.0: digest: sha256:96595cf6e6a26c22c44712b0fb777845a859996b15ab7c788351fd1a29ea196c size: 1159

六、部署Java应用

[root@kubernetes-master-001 ~/test-java]# kubectl  create  deployment  javademo1 --image=registry.cn-shanghai.aliyuncs.com/linux_yuncloud/java-demo:v1.0.0 --dry-run=client  -o yaml > javademo1.yaml

[root@kubernetes-master-001 ~/test-java]# kubectl  apply -f javademo1.yaml 
deployment.apps/javademo1 created

[root@kubernetes-master-001 ~/test-java]# kubectl  get  pods -o wide
NAME                                            READY   STATUS    RESTARTS   AGE     IP               NODE                    NOMINATED NODE   READINESS GATES
javademo1-789d495564-4jxmc                      1/1     Running   0          3m22s   10.244.1.129     kubernetes-node-002     <none>           <none>

[root@kubernetes-master-001 ~/test-java]# kubectl  scale  deployment  javademo1 --replicas=3
deployment.apps/javademo1 scaled

[root@kubernetes-master-001 ~/test-java]# kubectl  get  pods 
NAME                                            READY   STATUS    RESTARTS   AGE
javademo1-789d495564-4jxmc                      1/1     Running   0          7m15s
javademo1-789d495564-lnvcc                      1/1     Running   0          2m15s
javademo1-789d495564-qzqrj                      1/1     Running   0          2m15s

七、暴露service

[root@kubernetes-master-001 ~/test-java]# kubectl  expose deployment  javademo1 --port=8111 --target-port=8111 --type=NodePort
service/javademo1 exposed

[root@kubernetes-master-001 ~/test-java]# kubectl  get svc
NAME             TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
javademo1        NodePort    10.98.66.203   <none>        8111:31873/TCP   15s

八、访问测试

posted @ 2021-11-29 14:55  年少纵马且长歌  阅读(561)  评论(0编辑  收藏  举报