YAZONG 我的开源

Kubernetes(七)(7.2/3)springboot的web服务迁移kubernetes

  , , ,
0 评论0 浏览

源码:springboot-web-demo

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
</parent>

    <groupId>com.mooc</groupId>
    <artifactId>springboot-web-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
</dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
package com.mooc.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ServiceApplication {
    public static void main(String args[]) {
        SpringApplication.run(ServiceApplication.class, args);
    }
}
package com.mooc.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    @RequestMapping("/hello")
    public String sayHello(@RequestParam String name) {
        return "Hello "+name+"! I'm springboot-web-demo controller!";
    }
}
applications.properties
server.name=springboot-web-demo
server.port=8080

把服务做到镜像中

#第1步:搞定服务运行的相关文件。核实源文件的合法性。

[root@node-1 mooc-k8s-demo]# pwd
/root/mooc-k8s-demo
[root@node-1 mooc-k8s-demo]# mkdir springboot-web-demo
[root@node-1 mooc-k8s-demo]# cd springboot-web-demo/
#查看jar中的依赖文件
[root@node-1 springboot-web-demo]# jar -tf springboot-web-demo-1.0-SNAPSHOT.jar 
META-INF/
META-INF/MANIFEST.MF
org/
......
org/springframework/boot/loader/jar/StringSequence.class
META-INF/maven/
......
META-INF/maven/com.mooc/springboot-web-demo/pom.properties
BOOT-INF/
BOOT-INF/classes/
......
BOOT-INF/classes/com/mooc/demo/ServiceApplication.class
META-INF/maven/com.mooc/springboot-web-demo/pom.xml
BOOT-INF/lib/
......
BOOT-INF/lib/spring-boot-starter-web-2.0.5.RELEASE.jar
#运行测试
[root@node-1 springboot-web-demo]# java -jar springboot-web-demo-1.0-SNAPSHOT.jar
#浏览器访问"http://10.0.0.21:8080/hello?name=test"
Hello test! I'm springboot-web-demo controller!
#第2步:构建镜像-DockerFile

[root@node-1 springboot-web-demo]# cat Dockerfile
FROM hub.mooc.com/kubernetes/openjdk:8-jre-alpine
COPY springboot-web-demo-1.0-SNAPSHOT.jar /springboot-web.jar
#注意数组里一定有空格!
ENTRYPOINT ["java", "-jar", "/springboot-web.jar"]

#指定一个名字,构建一个镜像。注意这最后有个点。
[root@node-1 springboot-web-demo]# docker build -t springboot-web:v1 .

#测试镜像
[root@node-1 springboot-web-demo]# docker run -it springboot-web:v1
[root@node-1 ~]# docker ps
CONTAINER ID   IMAGE               COMMAND                  CREATED         STATUS         PORTS     NAMES
52c2abb3a005   springboot-web:v1   "java -jar /springbo¡­"   2 minutes ago   Up 2 minutes             mystifying_taussig
[root@node-1 ~]# ps -ef|grep java
root       3641   3621  4 20:12 pts/0    00:00:07 java -jar /springboot-web.jar

#把这个镜像打个tag放到harbor仓库中
#要注意这里的名字后面不带.jar,要注意和K8S的编辑文件保持一致!
[root@node-1 springboot-web-demo]# docker tag springboot-web:v1 hub.mooc.com/kubernetes/springboot-web:v1
[root@node-1 springboot-web-demo]# docker images|grep springboot-web
springboot-web                           v1             e5e494a7ee46   7 minutes ago   101MB
hub.mooc.com/kubernetes/springboot-web   v1             e5e494a7ee46   7 minutes ago   101MB
[root@node-1 springboot-web-demo]# docker push hub.mooc.com/kubernetes/springboot-web:v1

image.png

制作K8S服务并调度

#第1步:服务发现策略

这里没网络,没接口,那么不需要服务发现。
#第2步:编写K8S配置文件
#这里在原文件中设置指定node-2节点部署,否则部署到node-3会和harbor冲突。
[root@node-1 cronjob-demo]# mkdir -p /root/mooc-k8s-demo/configs
[root@node-1 cronjob-demo]# cd ../configs/
[root@node-1 configs]# pwd
[root@node-1 configs]# cat 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:
      nodeName: node-2
      containers:
      - name: springboot-web-demo
        image: hub.mooc.com/kubernetes/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
#第3步:运行K8S服务

#创建cronjob.yaml
[root@node-1 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@node-1 configs]# kubectl get pods -o wide -n default
NAME                                   READY   STATUS    RESTARTS   AGE    IP               NODE     NOMINATED NODE   READINESS GATES
springboot-web-demo-7564d56d5f-hn8lv   1/1     Running   0          3m22s   10.200.247.54    node-2   <none>           <none>
[root@node-2 ~]# crictl ps
CONTAINER           IMAGE               CREATED             STATE               NAME                       ATTEMPT             POD ID
067773e171a8a       e5e494a7ee468       12 seconds ago      Running             springboot-web-demo        0                   0db9c2e22c929
[root@node-2 ~]# crictl logs 067773e171a8a

#本机hosts和服务器hosts都加入
C:\Windows\System32\drivers\etc\hosts
10.0.0.22   springboot.mooc.com
[root@node-1/2/3 ingress-nginx]# cat /etc/hosts
10.0.0.22   springboot.mooc.com

#浏览器访问”http://springboot.mooc.com/hello?name=111”
#输出”Hello 111! I'm springboot-web-demo controller!”

停服务(可选)

#为节约资源,这里把cronjob.yaml停掉
[root@node-1 configs]# kubectl delete -f springboot-web.yaml    
deployment.apps "springboot-web-demo" deleted
service "springboot-web-demo" deleted
Warning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
ingress.extensions "springboot-web-demo" deleted
[root@node-1 configs]# kubectl get pods -o wide -n default
[root@node-2 ~]# crictl ps

解析:springboot-web.yaml

[root@node-1 configs]# cat 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:
      nodeName: node-2
      containers:
      - name: springboot-web-demo
        image: hub.mooc.com/kubernetes/springboot-web:v1
        ports:
        - containerPort: 8080
---	注意这根线是必须有的,而且跟下面的#service标签中间无空格

#Deployment完了之后要有一个service,因为在配置ingress的时候要配置一个service,80端口可自定义。???????????????
#selector/app名字要跟deployment的一样,这样才会找到deployment的后端。

#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标签中间无空格

#上面service定义完成之后,需要定义ingress,配置了域名,springboot.mooc.com,一个路径/,意思是所有的访问都会转发到下面的backend,
#selector/app的名字要与deployment和service一致,servicePort的端口80也要和上面的service的port80保持一致。


#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

标题:Kubernetes(七)(7.2/3)springboot的web服务迁移kubernetes
作者:yazong
地址:https://blog.llyweb.com/articles/2022/11/11/1668175763965.html