利用Java应用制作一个docker镜像
[root@master ~]# yum -y install maven git
[root@master java]# yum -y install java-11-openjdk*
[root@master ~]# mkdir -p java
[root@master ~]# cd java/
[root@master java]# git clone https://gitee.com/forgotten/tomcat-java-demo.git
[root@master java]# ls
apache-tomcat-9.0.54.tar.gz tomcat-java-demo
[root@master java]# cd tomcat-java-demo/
[root@master tomcat-java-demo]# mvn clean package
[root@master tomcat-java-demo]# mv target/ly-simple-tomcat-0.0.1-SNAPSHOT.war target/myapp.war
[root@master java]# cat Dockerfile
FROM centos
ADD tomcat-java-demo/target/myapp.war /usr/local/apache-tomcat-9.0.54/webapps/myapp.war
ADD apache-tomcat-9.0.54.tar.gz /usr/local
RUN yum -y install java-11-openjdk*
EXPOSE 8080
WORKDIR /usr/local/apache-tomcat-9.0.54
ENTRYPOINT /usr/local/apache-tomcat-9.0.54/bin/startup.sh && tail -f /usr/local/apache-tomcat-9.0.54/logs/catalina.out
CMD ["/usr/local/apache-tomcat-9.0.54/bin/catalina.sh","start"]
[root@master java]# docker build -t dengzichen/java:v1 .
将镜像上传至docker hub仓库中
[root@master java]# 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: dengzichen
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 java]# docker push dengzichen/java:v1
编写service文件
[root@master java]# cat java.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: java
spec:
replicas: 2
selector:
matchLabels:
app: java
template:
metadata:
labels:
app: java
spec:
containers:
- name: java
image: dengzichen/java:v1
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
name: java
spec:
type: NodePort
ports:
- port: 80
targetPort: 8080
protocol: TCP
nodePort: 31000
selector:
app: java
[root@master test]# kubectl apply -f java.yaml
deployment.apps/java created
service/java created
[root@master java]# kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/java-75f796867c-jsndk 0/1 ContainerCreating 0 35s
pod/java-75f796867c-tpfnn 0/1 ContainerCreating 0 35s
pod/web-6cfcd88d4-4rh75 1/1 Running 1 12h
pod/web-6cfcd88d4-ql4k6 1/1 Running 1 12h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/java NodePort 10.101.248.9 <none> 80:31000/TCP 15s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d14h
service/web NodePort 10.110.225.36 <none> 80:30000/TCP 12h
浏览器上访问:
评论区