root@k8s-master:~# curl http://172.16.194.74 <!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 <ahref="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <ahref="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p> </body> </html>
完美,我们的业务已经可以正常访问了
准备Service
Kubernetes 中的 Pod 是临时性的,也就是说,它们可能会因为扩容、更新或者重启而消失。为了避免这些问题,Kubernetes 通过 Service 来抽象化 Pod,确保即使 Pod 发生变化,外部系统也可以稳定地访问到它们。
root@k8s-master:~# kubectl create -f service.yml service/ingressservice created root@k8s-master:~# kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ingressservice ClusterIP 10.105.179.52 <none> 80/TCP 2s kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 36d
看看service是否能访问到后端的pod,先看看是否有endpoint
1 2 3
root@k8s-master:~# kubectl get endpoints NAME ENDPOINTS AGE ingressservice 172.16.194.74:80,172.16.194.75:80,172.16.194.76:80 75s
root@k8s-master:~# curl http://10.105.179.52 <!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 <ahref="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <ahref="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p> </body> </html>
root@k8s-master:~# kubectl create -f ingress.yml ingress.networking.k8s.io/lixiaohui created root@k8s-master:~# kubectl get ingress NAME CLASS HOSTS ADDRESS PORTS AGE lixiaohui nginx www.lixiaohui.com 192.168.8.4 80 101s
root@k8s-master:~# kubectl get service -n ingress-nginx NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ingress-nginx-controller NodePort 10.110.168.32 <none> 80:30614/TCP,443:30187/TCP 15m ingress-nginx-controller-admission ClusterIP 10.99.2.77 <none> 443/TCP 15m
root@k8s-master:~# curl http://www.lixiaohui.com:30614 <!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>
root@k8s-master:~# curl http://www.lixiaohui.com <!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 <ahref="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <ahref="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p> </body> </html>