k8s平台部署Java网站项目

k8s平台部署Java网站项目,第1张

k8s平台部署Java网站项目 k8s平台部署Java网站项目
  • 制作镜像
//安装依赖包
[root@master ~]# yum -y install java-17-openjdk maven git

//拉取代码
[root@master java]# pwd
/opt/java
[root@master java]# git clone https://gitee.com/forgotten/tomcat-java-demo.git
Cloning into 'tomcat-java-demo'...
remote: Enumerating objects: 558, done.
remote: Total 558 (delta 0), reused 0 (delta 0), pack-reused 558
Receiving objects: 100% (558/558), 5.08 MiB | 757.00 KiB/s, done.
Resolving deltas: 100% (217/217), done.

[root@master java]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.54/bin/apache-tomcat-9.0.54.tar.gz
--2021-12-28 11:09:42--  https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.54/bin/apache-tomcat-9.0.54.tar.gz
Resolving archive.apache.org (archive.apache.org)... 138.201.131.134, 2a01:4f8:172:2ec5::2
Connecting to archive.apache.org (archive.apache.org)|138.201.131.134|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11576317 (11M) [application/x-gzip]
Saving to: ‘apache-tomcat-9.0.54.tar.gz’

apache-tomcat-9. 100%[=======>]  11.04M  1.90MB/s    in 7.0s    

2021-12-28 11:09:50 (1.57 MB/s) - ‘apache-tomcat-9.0.54.tar.gz’ saved [11576317/11576317]

[root@master java]# ls 
apache-tomcat-9.0.54.tar.gz  tomcat-java-demo

//代码编译构建
[root@master tomcat-java-demo]# mvn clean package
[root@master tomcat-java-demo]# cd target/
[root@master target]# ls 
classes
generated-sources
ly-simple-tomcat-0.0.1-SNAPSHOT
ly-simple-tomcat-0.0.1-SNAPSHOT.war    //打的war包
maven-archiver
maven-status
//若感觉打的包名字太长,可以修改
[root@master target]# mv ly-simple-tomcat-0.0.1-SNAPSHOT.war myapp.war
[root@master target]# ls 
appdemo.war  generated-sources                maven-archiver
classes      ly-simple-tomcat-0.0.1-SNAPSHOT  maven-status

//编写Dockerfile
[root@master java]# cat Dockerfile 
FROM centos

LABEL MANTAINER "wawa 1@2.com"

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 java]# docker build -t soumnswa/demo:v3 .[root@master java]# docker images|grep demo
soumnswa/demo                                                     v3       0cce89d69965   45 seconds ago   266MB

//上传到仓库,这里上传到公有仓库,在实际生产环境中应该上传到私有仓库
[root@master java]# docker login
Authenticating with existing credentials...
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 soumnswa/demo:v3
The push refers to repository [docker.io/soumnswa/demo]
33a48ed75ce0: Pushed 
53ab8a9d621d: Pushed 
74ddd0ec08fa: Mounted from library/centos 
v3: digest: sha256:0199738c106e22130dc4608473e18193f5f5ce5cf6a9c8c654b8dd5743d9fc3a size: 953
  • 清单文件
[root@master manifest]# cat test.yml 
---
apiVersion: apps/v1
kind: Deployment 
metadata:
  name: web
  namespace: default 
spec:
  replicas: 1
  selector: 
    matchLabels:
      app: java-demo
  template:
    metadata:
      labels:
        app: java-demo
    spec:
      containers:
      - name: java
        image: soumnswa/demo:v3
        imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
  name: web
  namespace: default
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
    nodePort: 30000
  selector:
    app: java-demo
  type: NodePort

[root@master manifest]# kubectl apply -f test.yml 
deployment.apps/web created
service/web created

[root@master manifest]# kubectl get pods
NAME                   READY   STATUS              RESTARTS   AGE
web-6f6bcdcbf6-fxfg2   0/1     ContainerCreating   0          2m25s

[root@master manifest]# kubectl get pods -o wide
NAME                   READY   STATUS    RESTARTS   AGE   IP            NODE                NOMINATED NODE   READINESS GATES
web-6f6bcdcbf6-fxfg2   1/1     Running   0          11m   10.244.1.36   node1.example.com              
  • 网页 测试访问

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5680893.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存