K8s编排部署java应用
创建Dockerfile文件并提前下载jar包
[root@master kubenetres]# ls
Dockerfile halo-1.3.1.jar
[root@master kubenetres]# vim Dockerfile
[root@master kubenetres]# cat Dockerfile
FROM openjdk:18-jdk-alpine3.15
COPY halo-1.3.1.jar /usr/local/halo-1.3.1.jar
ENTRYPOINT ["java","-jar","/usr/local/halo-1.3.1.jar"]
制作镜像
[root@master kubenetres]# docker build -f Dockerfile -t test3:1.0 .
Sending build context to Docker daemon 75.21MB
Step 1/3 : FROM openjdk:18-jdk-alpine3.15
---> c89120dcca4c
Step 2/3 : COPY halo-1.3.1.jar /usr/local/halo-1.3.1.jar
---> adcaca1f84da
Step 3/3 : ENTRYPOINT ["java","-jar","/usr/local/halo-1.3.1.jar"]
---> Running in 735e9129e585
Removing intermediate container 735e9129e585
---> 77fc7bd4fe43
Successfully built 77fc7bd4fe43
Successfully tagged test3:1.0
#更改镜像名称
[root@master kubenetres]# docker tag test3:1.0 1225514226/javatest:v0.1
[root@master kubenetres]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
1225514226/javatest v0.1 77fc7bd4fe43 52 minutes ago 404MB
Deployment,Service的yaml文件
[root@master kubenetres]# vim test.yml
[root@master kubenetres]# cat test.yml
---
apiVersion: v1
kind: Pod
metadata:
name: test01
labels:
app: test01
spec:
containers:
- image: 1225514226/javatest:v0.1
name: java
---
apiVersion: v1
kind: Service
metadata:
name: java
namespace: default
spec:
ports:
- port: 8090
protocol: TCP
targetPort: 8090
selector:
app: test01
type: NodePort
构建并查看
[root@master kubenetres]# kubectl apply -f test.yml
pod/test01 created
service/java created
[root@master kubenetres]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/test01 0/1 ContainerCreating 0 23s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/java NodePort 10.107.92.77 <none> 8090:31839/TCP 23s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6d