把Spring-boot微服务部署到Kubernetes Ingress
为了方便管理,我们需要把spring-boot微服务部署到K8S中,再通过ingress暴露对外访问。假设我们要封装的工程为base-service-app,端口为9096,下面就一步步来完成部署:
1. 把spring-boot工程打包成docker image
要把spring-boot工程打包成docker镜像,需要在pom.xml中添加如下内容:
<docker.image.prefix>ibm-risk-cloud</docker.image.prefix>
...
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.2.3</version>
<configuration>
<baseImage>openjdk:8-jre-alpine</baseImage>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<exposes>9096</exposes>
<entryPoint>["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
然后执行docker打包:
[johnny@oc3744522240 ~]$ mvn docker:build
打包完成后,用如下命令应该可以看到刚刚生成的image:
[johnny@oc3744522240 ~]$ docker iamge ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ibm-risk-cloud/base-service-app v6.0.0 3ab421e9f3b2 6 days ago 126MB
注意:这步Docker镜像的基础镜像是openjdk:8-jre-alpine,估计有些同学被墙了,不能下载这个镜像,那你可以从阿里镜像服务上找到其它的8-jre-alpine,然后再tag成openjdk:8-jre-alpine就可以了。
2.把Docker Image导出导入到各Node
这一步是要让各Node能找到我们的Image。有两个方法,一是把Image Push到docker仓库中,但这个需要到阿里云上注册相应的服务,比较麻烦点点。今天我们用本地导出,再导入到各个Node上的方法。
在本机上执行导出命令:
[johnny@oc3744522240 ~]$ docker save 3ab421e9f3b2 > ~/base-service-app.tar
再把导出的base-service-app.tar导入到各Node中。
[johnny@k8s-node2 ~]$ docker load < ./base-service-app.tar
[johnny@k8s-node2 ~]$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> v6.0.0 3ab421e9f3b2 6 days ago 126MB
[johnny@k8s-node2 ~]$ docker tag 3ab421e9f3b2 ibm-risk-cloud/base-service-app
3. 部署Pods
编写部署文档:
[johnny@k8s-master ~]$ vi base-service-deploy.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: iisp-base-service-deployment
spec:
replicas: 2 # We want two pods for this deployment
template:
metadata:
labels:
app: iisp-base-service-app
spec:
containers:
- name: iisp-base-service-6-0-0
image: ibm-risk-cloud/base-service-app:v6.0.0
ports:
- containerPort: 9096
部署之:
[johnny@k8s-master ~]$ kubectl appy -f ./base-service-deploy.yaml
4. 把刚才部署的Pods封装成Service
[johnny@k8s-master ~]$ vi base-service.yaml
apiVersion: v1
kind: Service
metadata:
name: iisp-base-service
namespace: default
spec:
type: NodePort
ports:
- port: 9096
nodePort: 30000
selector:
app: iisp-base-service-app
5. 把服务部署到Ingress上
[johnny@k8s-master ~]$ vi ingress-base-service.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: igs-iisp-base-service
namespace: default
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: base.irmp.ibm.com
http:
paths:
- path:
backend:
serviceName: iisp-base-service
servicePort: 9096
6. 在客户机上配置域名
在客户机上配置域名,域名是你ingress-base-service.yaml中配置的域名,ip则是你Ingress那个Node的IP。
如:
[johnny@oc3744522240 ~]$ cat /etc/hosts
192.168.122.12 k8s-node2 base.irmp.ibm.com
7. 访问你的服务
在浏览器中访问:http://base.irmp.ibm.com/…就可以访问你的服务了。
后记:
有人是不是觉得,如果每个服务都要配置一个主机名和IP很不方便,那就请看我另一个文章的解决方案:Bind9泛域名解析配置