首页 > 技术文章 > Kubernetes之Ingress组件部署

sunnyyangwang 2019-05-24 10:34 原文

Kubernetes之Ingress组件部署

 

这里不做kubernetes暴露服务的三种方式,只解说Ingress方式。

Service后端分类,手机后端分类,通过ingress动态注入,

Ingress基于分类,注入服务,自行监控配置文件变化。

 

一、原理说明

如下,ingress包含两大组件:ingress controller和ingress。

Ingress:即为nginx配置,在pod内部实现

Ingress-Controller:暴露服务,生成模板,读取ingress走向。

 

Ingress也是标准的kubernetes资源。

 

二、部署ingress

官网下载4个包:

 

先部署命名空间,

新版本的configmap包含了tcp、udp配置。

[root@master ing]# pwd

/root/yaml/ingress-nginx/ing

[root@master ing]# ls

configmap.yaml  namespace.yaml  rbac.yaml  with-rbac.yaml

 

完成ingress服务配置。

老版本有依赖,不需要这个default-http pod。

 

三、部署Ingress-Controller

手动部署,确保ingress-controller可以接入外部流量。

[root@master ing]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/provider/baremetal/service-nodeport.yaml

[root@master ing]# vim service-nodeport.yaml

修改默认配置,将ingress-controller固定暴露端口。

 

[root@master ing]# kubectl apply -f service-nodeport.yaml

service/ingress-nginx created

[root@master ~]# kubectl get svc -n ingress-nginx                            

NAME    TYPE    CLUSTER-IP      EXTERNAL-IP   PORT(S)               AGE

ingress-nginx  NodePort  10.104.234.98   <none>   80:30080/TCP,443:30443/TCP   16m

[root@master ~]# curl 10.104.234.98

[root@master ~]# curl node1:30080

如下截图,正常显示。

 

测试对外服务ingress-controller是否运行成功,如上,查看错误信息为404,出现nginx提示,表示成功部署,运行正常。

 

四、发布测试服务

准备后端服务。

[root@master ~]# kubectl run httpd --image=httpd

[root@master ~]# kubectl expose deployment httpd --port=80

 [root@master ~]# kubectl get pods |grep httpd

httpd-5d8cbbcd67-ct56t                  1/1     Running   1          108m

[root@master ~]# kubectl get svc/httpd

NAME    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE

httpd   ClusterIP   10.105.142.50   <none>        80/TCP    21h

[root@master ~]#

如下,后端通过pod、svc服务都可以正常访问。

 

后端准备好,现在可以发布。

[root@master ingress-nginx]# vim ingress_httpd.yaml

 

[root@master ingress-nginx]# vim ingress_httpd.yaml

[root@master ~]# kubectl get ing

NAME         HOSTS         ADDRESS   PORTS   AGE

httpd-test   foo.bar.com             80      19h

[root@master ~]# kubectl get ing -n ingress-nginx

No resources found.

[root@master ~]# kubectl get ing

[root@master ~]# kubectl describe ing/httpd-test

 

 

五、测试服务

1、命令行测试

方法一:

curl -v http://nodeip:80/foo -H 'host: foo.bar.com'

2、配置域名hosts文件

[root@node1 ~]# vim /etc/hosts 

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.19.133 node1 foo.bar.com

192.168.19.134 master

192.168.19.135 node2 foo.bar.com

如下,访问正常。

 

 

整个过程:

部署ingress—>部署ingress controller

部署应用deploy(pod)—>部署应用svc—>暴露域名服务

 

六、多个服务

增加一个配置后端服务,在同一个域名下访问。

[root@master ingress-nginx]# cat ingress_deploy-demo.yaml

 

测试后端服务,

 

同样的方法测试,不同服务不同的域名,正常显示如下,

 

如上,正常。

进入容器查看相关配置信息。

[root@mar ~]# kubectl exec -it nginx-ingress-controller-689498bc7c-c76gt  -n ingress-nginx – sh

$ pwd

/etc/nging

$ more nginx.conf

 

 

自此,完成ingress配置,相关yaml文件:https://github.com/Wangwang12345/k8s

遗留问题:这里测试了一下同一个域名下多个服务,发现只能存在一个/根路径访问?

 

推荐阅读