Kubernetes 之 Ingress
定义
Ingress 可以把外部需要进入到集群内部的请求转发到集群中的一些服务上,从而实现把服务映射到集群外部的需要。Ingress 能把集群内 Service 配置成外网能够访问的 URL,流量负载均衡,提供基于域名访问的虚拟主机等,相当于一个流量入口。它的基础构件叫做 Ingress Controller。
部署
构建 Ingress Controller
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.11.1/deploy/static/provider/baremetal/deploy.yaml
检查 Ingress Controller 是否创建完毕
root@k8s-master1:~# kubectl get pods --namespace=ingress-nginx
NAME READY STATUS RESTARTS AGE
ingress-nginx-admission-create-29fkf 0/1 Completed 0 47s
ingress-nginx-admission-patch-6fn6j 0/1 Completed 0 47s
ingress-nginx-controller-784997fdc7-rxkwb 0/1 Running 0 47s
编写 service 规则
apiVersion: v1
kind: Service
metadata:name: service-static-web2namespace: defaultlabels:app: service-static-web2
spec:type: ClusterIPports:- port: 8080protocol: TCPtargetPort: 80selector:app: pod-web-static
编写 Ingress 规则
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: ingress-static-webnamespace: default
spec:ingressClassName: nginxrules:- host: web.test1.comhttp:paths:- backend:service:name: service-static-web2port:number: 8080path: /pathType: Prefix
在 /etc/hosts
中加入127.0.0.1 web.test1.com
,并进行测试
root@k8s-master1:~# kubectl get service -ningress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.109.159.254 <none> 80:31284/TCP,443:30204/TCP 124m
ingress-nginx-controller-admission ClusterIP 10.100.3.205 <none> 443/TCP 124mroot@k8s-master1:~# curl http://web.test1.com:31284
<html><body><h1>This is a test</h1></body></html>