1.ingress定义
前面已经装了ingress controller控制器,现在可以定义ingress资源来实现七层负载均衡了,ingress支持三种使用方式:
- 基于虚拟主机转发
- 基于虚拟主机URI转发
- TLS加密转发
创建一个nginx的Deployment应用,包含两个副本
kubectl run ingress-demo --image=nginx:1.7.9 --port=80 --replicas=2
kubectl get deployments
kubectl expose deployment ingress-demo --port=80 --protocol=TCP --target-port=80
kubectl get services
上面创建了一个service,定义一个ingress对象转发至ingress-demo这个service,通过ingress.class指定控制器的类型为nginx
cat nginx-ingress-demo.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: nginx-ingress-demo
labels:
ingres-controller: nginx
annotations:
kubernets.io/ingress.class: nginx
spec:
rules:
- host: www.happylau.cn
http:
paths:
- path: /
backend:
serviceName: ingress-demo
servicePort: 80
创建ingress对象
kubectl apply -f nginx-ingress-demo.yaml
查看ingress资源列表
kubectl get ingresses
查看ingress详情,可以在rules规则中看到后端pod列表,自动发现和关联pod
Name: nginx-ingress-demo
Namespace: default
Address:
Default backend: default-http-backend:80 (<none>)
Rules:
Host Path Backends
www.tengwang.com
/ ingress-demo:80 (10.244.1.167:80,10.244.2.197:80)
Annotations:
kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"kubernetes.io/ingress.class":"nginx"},"labels":{"ingress-controller":"nginx"},"name":"nginx-ingress-demo","namespace":"default"},"spec":{"rules":[{"host":"www.tengwang.com","http":{"paths":[{"backend":{"serviceName":"ingress-demo","servicePort":80},"path":"/"}]}}]}}
kubernetes.io/ingress.class: nginx
Events: <none>
测试验证
ingress规则的配置信息已经注入到ingress controller中,环境中ingress controller是以DaemonSets方式部署到集群中的,如果有外部负载均衡,则将www.tengwang.com 解析到负载均衡VIP,这款测试环境没有搭建负载均衡,所以直接在hosts解析, echo "172.19.159.8 www.tengwang.com" >> /etc/hosts
curl www.tengwang.com
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
注意因为我们部署的ingress controller只部署在了noode-2和node-3上,所以解析的时候只解析到这两个节点上,前面已经测试解析到node-2上,现在解析到node-3上
echo "172.19.159.9 www.tengwang.com" >> /erc/hosts
kubectl describe ingress nginx-ingress-demo
查看pod的endpoint方便后面测试
echo "172.19.159.9 www.tengwang.com" >> /erc/hosts
curl -I http://www.tengwang.com --resolve www.tengwang.com:80:10.244.2.197
返回的是前面建立的2个副本的nginx的deployments,但是如果直接访问节点端口返回的是nginx controller的错误页面