1. 制作java镜像

[root@master]# yum -y install maven git

[root@master]# git clone https://gitee.com/forgotten/tomcat-java-demo.git

[root@master]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.54/bin/apache-tomcat-9.0.54.tar.gz

[root@master]# cat Dockerfile
FROM centos

ADD apache-tomcat-9.0.54.tar.gz /usr/local

ADD tomcat-java-demo/target/myapp.war /usr/local/apache-tomcat-9.0.54/webapps/myapp.war

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 ~]# docker build -t dockerimage123/java-deamon:v1/root/java

2. 将镜像推入docker hub仓库中

root@master ~]# docker push dockerimage/java-deamon:v1

3. 创建Deployment,Service的yaml文件

[root@master test]# cat java.yaml 

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: java-deamon
  name: java
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: java-deamon
  template:
    metadata:
      labels:
        app: java-deamon
    spec:
      containers:
      - image: dockerimage123/java-deamon:v1
        name: java
        imagePullPolicy: IfNotPresent

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: java-deamon
  name: java
  namespace: default
spec:
  ports:
  - port: 80
    protocol: TCP
    nodePort: 30000
    targetPort: 8080
  selector:
    app: java-deamon
  type: NodePort


[root@master test]# kubectl apply -f java.yaml 
deployment.apps/java created
service/java created

[root@master test]# kubectl get pods
NAME                   READY   STATUS            RESTARTS     AGE
java-ok7ge0acd3-y7hs5   1/1     Running             0         11m

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐