目录
前言
ingress就是为进入集群的请求提供路由规则的集合,可以给service提供集群外部访问的url、负载均衡、ssl终止、http路由等,为了配置这些ingress规则。集群管理员需要部署一个ingress controller,它监听ingress和service的变化,并根据规则配置负载均衡并提供访问入口。
部署
1、安装ingress-controller
[root@k8s-master ingress]# kubectl apply -f deploy.yaml
namespace/ingress-nginx created
serviceaccount/ingress-nginx created
configmap/ingress-nginx-controller created
clusterrole.rbac.authorization.k8s.io/ingress-nginx created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx created
role.rbac.authorization.k8s.io/ingress-nginx created
rolebinding.rbac.authorization.k8s.io/ingress-nginx created
service/ingress-nginx-controller-admission created
service/ingress-nginx-controller created
deployment.apps/ingress-nginx-controller created
ingressclass.networking.k8s.io/nginx created
validatingwebhookconfiguration.admissionregistration.k8s.io/ingress-nginx-admission created
serviceaccount/ingress-nginx-admission created
clusterrole.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
role.rbac.authorization.k8s.io/ingress-nginx-admission created
rolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
[root@k8s-master ingress]# kubectl apply -f backend.yaml
deployment.apps/default-http-backend unchanged
service/default-http-backend unchanged
2、查看svc、deploy、pod
[root@k8s-master ingress]# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.96.242.173 <none> 80:32389/TCP,443:32145/TCP 82m
ingress-nginx-controller-admission ClusterIP 10.96.89.172 <none> 443/TCP 82m
[root@k8s-master ingress]# kubectl get deploy -n ingress-nginx
NAME READY UP-TO-DATE AVAILABLE AGE
ingress-nginx-controller 1/1 1 1 82m
[root@k8s-master ingress]# kubectl get pods -n ingress-nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
ingress-nginx-admission-create-4g5nt 0/1 Completed 0 82m 172.26.139.56 k8s-node <none> <none>
ingress-nginx-admission-patch-vbpkj 0/1 Completed 1 82m 172.28.82.199 k8s-master <none> <none>
ingress-nginx-controller-6587b84448-9znkt 1/1 Running 0 82m 172.28.82.198 k8s-master <none> <none>
3、创建ingress规则
[root@k8s-master ingress]# kubectl apply -f ingress-demo-app.yaml
deployment.apps/hello-server created
deployment.apps/nginx-demo created
service/nginx-demo created
service/hello-server created
ingress.networking.k8s.io/ingress-host-bar created
[root@k8s-master ingress]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
hello-server-69684cb794-q7k6t 1/1 Running 0 9m29s 172.26.139.61 k8s-node <none> <none>
nginx-demo-5c4fd7fd55-h7w4c 1/1 Running 0 9m29s 172.26.139.62 k8s-node <none> <none>
[root@k8s-master ingress]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-server ClusterIP 10.96.92.51 <none> 8000/TCP 9m35s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d
nginx-demo ClusterIP 10.101.182.112 <none> 8000/TCP 9m35s
[root@k8s-master ingress]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress-host-bar nginx hello.yunqi.com,demo.yunqi.com 10.103.3.89 80 9m47s
4、查看规则
[root@k8s-master ingress]# kubectl describe ingress ingress-host-bar
Name: ingress-host-bar
Labels: <none>
Namespace: default
Address: 10.103.3.89
Ingress Class: nginx
Default backend: <default>
Rules:
Host Path Backends
---- ---- --------
hello.yunqi.com
/ hello-server:8000 (172.26.139.61:9000)
demo.yunqi.com
/ nginx-demo:8000 (172.26.139.62:80)
Annotations: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Sync 51s (x2 over 111s) nginx-ingress-controller Scheduled for sync
5、通过svc访问
[root@k8s-master ingress]# curl 10.96.92.51:8000
Hello World!
[root@k8s-master ingress]# curl 10.101.182.112:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>