大家好,又见面了,我是你们的朋友全栈君。
部署一个简单的springboot服务,有个restful接口,如下:
List-1
mjduan@mjduan-host:/opt/software/tool/virtualbox-vagrant/master$ curl http://192.168.33.10:30002/person/1
{"id":1,"name":"name=1","address":"address"}
mjduan@mjduan-host:/opt/software/tool/virtualbox-vagrant/master$
1、NodePort方式暴露端口
我们将这个springboot部署到k8s上,制作镜像就不描述了,k8s的yaml文件如下,之后执行”kubectl apply -f k8s-demo.yaml”,就可以了。
List-1.1 k8s-demo.yaml
apiVersion: v1
kind: Service
metadata:
name: k8s-learn-service
namespace: default
labels:
app: k8s-learn-service
spec:
type: NodePort
ports:
- port: 8080
nodePort: 30002
selector:
app: k8s-learn-service
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: k8s-learn-deployment
labels:
app: k8s-learn-deployment
spec:
replicas: 1
selector:
matchLabels:
app: k8s-learn-service
template:
metadata:
labels:
app: k8s-learn-service
spec:
containers:
- name: k8s-learn-container1
image: k8s-learn:1.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
volumeMounts:
- mountPath : /opt/applog
name: my-volume
volumes:
- name: my-volume
emptyDir: {}
2、ClusterIP方式,用Ingress暴露端口
NodePort方式,是直接暴露端口,这种方式不是很好。ClusterIP方式,集群外是访问不了的,我们用Ingress方式来暴露端口,如下:
List-2.1 k8s-demo.yaml
apiVersion: v1
kind: Service
metadata:
name: k8s-learn-service
namespace: default
labels:
app: k8s-learn-service
spec:
type: ClusterIP
ports:
- port: 8080
selector:
app: k8s-learn-service
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: k8s-learn-deployment
labels:
app: k8s-learn-deployment
spec:
replicas: 1
selector:
matchLabels:
app: k8s-learn-service
template:
metadata:
labels:
app: k8s-learn-service
spec:
containers:
- name: k8s-learn-container1
image: k8s-learn:1.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
volumeMounts:
- mountPath : /opt/applog
name: my-volume
volumes:
- name: my-volume
emptyDir: {}
之后执行”kubectl apply -f k8s-demo.yaml”,之后查看该服务:
List-2.2
[root@master springboot]# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
...
k8s-learn-service ClusterIP 10.107.55.103 <none> 8080/TCP 3m
...
之后创建用于ingress,如下
List-2.3 springboot-ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: apps-springboot
spec:
rules:
- host: springboot-ui
http:
paths:
- path: /
backend:
serviceName: k8s-learn-service
servicePort: 8080
执行”kubectl apply -f springboot-ingress.yaml “,查看ingress状态:
List-2.4
[root@master springboot]# kubectl get ingress
NAME HOSTS ADDRESS PORTS AGE
apps-springboot springboot-ui 80 17m
之后在你本地/etc/hosts中加入”k8s的任一节点IP springboot-ui”,在浏览器中访问”http://springboot-ui/person/12″
转载于:https://my.oschina.net/u/2518341/blog/3041545
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106860.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...