搭建及使用K8s集群 [使用ingress 暴露springcloud服务]
使用ingress 暴露springcloud服务k8s 集群搭建成功了,也能部署应用了,但是不可能每次去访问服务的时候都要去查一下运行的node和nodePort吧,即使查好node和nodePort ,避免不了重启k8s或者ReplicationController,node 和 nodeport 又会改变,K8s 提供了 Ingress 来解决这个问题。1.生成一个默认的后端生成一个默认的
#使用ingress 暴露springcloud服务
k8s 集群搭建成功了,也能部署应用了,但是不可能每次去访问服务的时候都要去查一下运行的node和nodePort吧,即使查好node和nodePort ,避免不了重启k8s或者 ReplicationController,node 和 nodeport 又会改变,K8s 提供了 Ingress 来解决这个问题。
1.生成一个默认的后端
生成一个默认的后端,如果遇到解析不到的URL就转发到默认后端页面
[root@master ingress]# cat default-backend.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: default-http-backend
labels:
k8s-app: default-http-backend
namespace: default
spec:
replicas: 1
template:
metadata:
labels:
k8s-app: default-http-backend
spec:
terminationGracePeriodSeconds: 60
containers:
- name: default-http-backend
# Any image is permissable as long as:
# 1. It serves a 404 page at /
# 2. It serves 200 on a /healthz endpoint
image: docker.io/cdchen/defaultbackend:1.0
livenessProbe:
httpGet:
path: /healthz
port: 8080
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 5
ports:
- containerPort: 8080
resources:
limits:
cpu: 10m
memory: 20Mi
requests:
cpu: 10m
memory: 20Mi
---
apiVersion: v1
kind: Service
metadata:
name: default-http-backend
namespace: default
labels:
k8s-app: default-http-backend
spec:
ports:
- port: 80
targetPort: 8080
selector:
k8s-app: default-http-backend
[root@master ingress]# kubectl create -f default-backend.yaml
deployment "default-http-backend" created
service "default-http-backend" created
2.部署Ingress Controller
[root@master ingress]# cat nginx-ingress-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx-ingress-lb
labels:
name: nginx-ingress-lb
namespace: default
spec:
replicas: 1
template:
metadata:
labels:
name: nginx-ingress-lb
annotations:
prometheus.io/port: '10254'
prometheus.io/scrape: 'true'
spec:
terminationGracePeriodSeconds: 60
hostNetwork: true
containers:
- image: docker.io/cdchen/nginx-ingress-controller:0.9.0-beta.12
name: nginx-ingress-lb
readinessProbe:
httpGet:
path: /healthz
port: 10254
scheme: HTTP
livenessProbe:
httpGet:
path: /healthz
port: 10254
scheme: HTTP
initialDelaySeconds: 10
timeoutSeconds: 1
ports:
- containerPort: 80
hostPort: 80
- containerPort: 443
hostPort: 443
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: KUBERNETES_MASTER
value: http://192.168.6.150:8080
args:
- /nginx-ingress-controller
- --default-backend-service=$(POD_NAMESPACE)/default-http-backend
- --apiserver-host=http://192.168.6.45:8080
记得修改 - --apiserver-host=http://192.168.6.45:8080
[root@master ingress]# kubectl create -f nginx-ingress-controller.yaml
replicationcontroller "nginx-ingress-lb" created
3.配置ingress
贴一个k8s dashboard 的 ingress配置
[root@master ingress]# cat k8s-dashboard.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: k8s-dashboard-ingress
namespace: kube-system
spec:
rules:
- host: k8s.webui
http:
paths:
- path: /
backend:
serviceName: kubernetes-dashboard
servicePort: 80
[root@master ingress]# kubectl create -f k8s-dashboard.yaml
ingress "k8s-dashboard-ingress" created
理解如下:
- host指虚拟出来的域名,具体地址(我理解应该是Ingress-controller那台Pod所在的主机的地址)应该加入/etc/hosts中,这样所有去k8s.webui的请求都会发到nginx
- path:/console匹配后面的应用路径
- servicePort主要是定义服务的时候的端口,不是NodePort.
- path:/ 匹配后面dashboard应用的路径
在 node 节点中docker ps 查看是否存在ingress Controller,然后把对应的ip添加进本机host,
在浏览器内访问 http://k8s.webui/
如下图:
贴一个前一篇博客介绍的多应用的部署的ingress 配置文件
[root@master ingress]# cat cloud-vue.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: cloud-vue-api
namespace: default
spec:
rules:
- host: cloud.vue.api
http:
paths:
- path: /console
backend:
serviceName: helloworldsvc
servicePort: 7001
- path: /
backend:
serviceName: cloud-simple-server
servicePort: 8081
- host: cloud.vue.eureka
http:
paths:
- path: /
backend:
serviceName: cloud-eureka-server
servicePort: 8888
- host: cloud.vue.config
http:
paths:
- path: /
backend:
serviceName: cloud-config-server
servicePort: 1111
- host: cloud.vue.zipkin
http:
paths:
- path: /
backend:
serviceName: cloud-zipkin-server
servicePort: 9012
注:已经使用ingress暴露服务了,再部署的时候service的类型可以选择internal了 ,不再需要external。还有https的配置,请参考 http://www.cnblogs.com/ericnie/p/6965091.html
最近自己写了一个小程序电商,卖洗化用品,家里人是立白代理,进货价格低,售价自然低,大家觉着文章对自己有帮助的,正好也需要购买洗化用品的,可以去电商看看,买一些,不胜感激。
附上一张小程序二维码
更多推荐
所有评论(0)