K8S 生产实践-07-02-业务系统迁移

Springboot的web服务迁移

1、在服务器端安装maven工具

 yum -y install maven

2、下载工程代码

[root@hombd03 ~]# git clone https://git.imooc.com/corwien/mooc-k8s-demo-docker.git

3、打包

cd /root/mooc-k8s-demo-docker/springboot-web-demo
mvn package

file

可以通过命令查看jar包:

[root@hombd03 target]# jar -tf springboot-web-demo-1.0-SNAPSHOT.jar
META-INF/
META-INF/MANIFEST.MF
org/
org/springframework/
org/springframework/boot/
org/springframework/boot/loader/
org/springframework/boot/loader/data/
org/springframework/boot/loader/data/RandomAccessDataFile$1.class
org/springframework/boot/loader/data/RandomAccessDataFile$DataInputStream.class
org/springframework/boot/loader/jar/
org/springframework/boot/loader/jar/JarFileEntries$1.class
...

然后运行该 jar包:

java -jar springboot-web-demo-1.0-SNAPSHOT.jar

file

下载基础镜像:

[root@hombd03 ~]# docker pull openjdk:8-jre-alpine

将镜像打标签push到私有仓库:

[root@hombd03 ~]# docker tag openjdk:8-jre-alpine hub.mooc.com:5000/k8s/openjdk:8-jre-alpine
[root@hombd03 ~]# docker push hub.mooc.com:5000/k8s/openjdk:8-jre-alpine
The push refers to repository [hub.mooc.com:5000/k8s/openjdk]
edd61588d126: Pushed 
9b9b7f3d56a0: Pushed 
f1b5933fe4b5: Pushed 
8-jre-alpine: digest: sha256:b2ad93b079b1495488cc01375de799c402d45086015a120c105ea00e1be0fd52 size: 947
[root@homaybd03 ~]# 

编写Dockerfile文件:

[root@homaybd03 springboot-web-demo]# cat Dockerfile 
FROM hub.mooc.com:5000/k8s/openjdk:8-jre-alpine

COPY target/springboot-web-demo-1.0-SNAPSHOT.jar /springboot-web.jar

ENTRYPOINT ["java", "-jar", "/springboot-web.jar"]

本地打镜像测试:

[root@homaybd03 springboot-web-demo]# docker build -t springboot-web:v1 .
Sending build context to Docker daemon  16.26MB
Step 1/3 : FROM hub.mooc.com:5000/k8s/openjdk:8-jre-alpine
 ---> f7a292bbb70c
Step 2/3 : COPY target/springboot-web-demo-1.0-SNAPSHOT.jar /springboot-web.jar
 ---> 6dfee1ebf6c6
Step 3/3 : ENTRYPOINT ["java", "-jar", "/springboot-web.jar"]
 ---> Running in 4c241909bc40
Removing intermediate container 4c241909bc40
 ---> 44d63fded15d
Successfully built 44d63fded15d
Successfully tagged springboot-web:v1
[root@homaybd03 springboot-web-demo]# docker images
REPOSITORY                                                       TAG             IMAGE ID       CREATED          SIZE
springboot-web                                                   v1              44d63fded15d   18 seconds ago   101MB
nginx                                                            1.17.9          5a8dfb2ca731   2 years ago      127MB
hub.mooc.com:5000/k8s/nginx                                      1.17.9          5a8dfb2ca731   2 years ago      127MB
quay.io/kubernetes-ingress-controller/nginx-ingress-controller   0.30.0          89ccad40ce8e   2 years ago      323MB
openjdk                                                          8-jre-alpine    f7a292bbb70c   3 years ago      84.9MB
hub.mooc.com:5000/k8s/openjdk                                    8-jre-alpine    f7a292bbb70c   3 years ago      84.9MB
registry.cn-hangzhou.aliyuncs.com/liuyi01/tomcat                 8.0.51-alpine   2304e340e238   4 years ago      145MB
[root@homaybd03 springboot-web-demo]# 

然后启动该镜像进行测试:

[root@hombd03 springboot-web-demo]# docker run -it springboot-web:v1

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.5.RELEASE)

2022-06-30 17:11:08.464  INFO 1 --- [           main] com.mooc.demo.ServiceApplication         : Starting ServiceApplication v1.0-SNAPSHOT on 5563fc883798 with PID 1 (/springboot-web.jar started by root in /)
2022-06-30 17:11:08.476  INFO 1 --- [           main] com.mooc.demo.ServiceApplication   

启动没有问题,然后将该镜像推送到镜像仓库:

[root@hombd03 ~]# docker tag springboot-web:v1 hub.mooc.com:5000/k8s/springboot-web:v1
[root@hombd03 ~]# docker push hub.mooc.com:5000/k8s/springboot-web:v1

然后编写部署yaml 文件,将服务部署上去:

root@hombd03 configs]# vi springboot-web.yaml 
#deploy
apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot-web-demo
spec:
  selector:
    matchLabels:
      app: springboot-web-demo
  replicas: 1
  template:
    metadata:
      labels:
        app: springboot-web-demo
    spec:
      containers:
      - name: springboot-web-demo
        image: hub.mooc.com:5000/k8s/springboot-web:v1
        ports:
        - containerPort: 8080
---
#service
apiVersion: v1
kind: Service
metadata:
  name: springboot-web-demo
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: springboot-web-demo
  type: ClusterIP

---
#ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: springboot-web-demo
spec:
  rules:
  - host: springboot.mooc.com
    http:
      paths:
      - path: /
        backend:
          serviceName: springboot-web-demo
          servicePort: 80

部署服务:

[root@homaybd03 configs]# kubectl apply -f springboot-web.yaml 
deployment.apps/springboot-web-demo created
service/springboot-web-demo created
Warning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
ingress.extensions/springboot-web-demo created
[root@homaybd03 configs]# kubectl get pods
NAME                                   READY   STATUS             RESTARTS   AGE
nginx                                  1/1     Running            1          25d
nginx-ds-87flg                         1/1     Running            1          26d
nginx-ds-j7mqr                         1/1     Running            1          26d
springboot-web-demo-7547bffb79-pvsx4   0/1     ImagePullBackOff   0          24s
tomcat-demo-54cbbcffdb-dgct2           1/1     Running            0          11d

为者常成,行者常至