部署一个简单的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: v1kind: Servicemetadata: name: k8s-learn-service namespace: default labels: app: k8s-learn-servicespec: type: NodePort ports: - port: 8080 nodePort: 30002 selector: app: k8s-learn-service---apiVersion: apps/v1kind: Deploymentmetadata: name: k8s-learn-deployment labels: app: k8s-learn-deploymentspec: 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: v1kind: Servicemetadata: name: k8s-learn-service namespace: default labels: app: k8s-learn-servicespec: type: ClusterIP ports: - port: 8080 selector: app: k8s-learn-service---apiVersion: apps/v1kind: Deploymentmetadata: name: k8s-learn-deployment labels: app: k8s-learn-deploymentspec: 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 servicesNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE...k8s-learn-service ClusterIP 10.107.55.1038080/TCP 3m...
之后创建用于ingress,如下
List-2.3 springboot-ingress.yaml
apiVersion: extensions/v1beta1kind: Ingressmetadata: name: apps-springbootspec: 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 ingressNAME HOSTS ADDRESS PORTS AGEapps-springboot springboot-ui 80 17m
之后在你本地/etc/hosts中加入"k8s的任一节点IP springboot-ui",在浏览器中访问"http://springboot-ui/person/12"