文章转自微信公众号:https://mp.weixin.qq.com/s/6S7SErPpotFsF1TyWzexpA
环境信息
软件 | 版本 | 备注 |
---|---|---|
OS | CentOS Linux release 7.7.1908 (Core) | |
Kubernetes | v1.17.3 | |
Nginx Ingress Controller | v1.6.3 | Nginx 维护的Ingress Controller |
Network Plugin | weave 2.6.1 | |
kubeadmin | v1.17.3 |
部署
部署Nginx Ingress Controller我们参考 nginx官网。就是因为网络上很多的文章都没有标注版本,导致参考的时候会遇到一些问题,对新手来说还很难排查,所以才有了这篇文章。请部署时参照官网,并且注意自己部署的版本和文档的一致性。
-
下载yaml文件
$ git clone https://github.com/nginxinc/kubernetes-ingress/
$ cd kubernetes-ingress/deployments
$ git checkout v1.6.3
NOTE:这里注意 获取对应版本的nginx ingress controller 部署文件的操作。
-
配置相应的RBAC
$ kubectl apply -f common/ns-and-sa.yaml
$ kubectl apply -f rbac/rbac.yaml
-
创建Secret、ConfigMap及CDR(Custom Resource Definitions)
$ kubectl apply -f common/default-server-secret.yaml
$ kubectl apply -f common/nginx-config.yaml
$ kubectl apply -f common/custom-resource-definitions.yaml
-
选择daemonset的方式来部署nginx ingress controller pod。(也可以选择deployment方式)
$ kubectl apply -f daemon-set/nginx-ingress.yaml
由于我们选择的daemon-set的方式来部署,所以我们的Ingress Controller监听的80和443端口会在每个Node节点上都存在。不需要再创建service。
-
检查Pod状态
$ kubectl get pods --namespace=nginx-ingress
如果Pod正常执行了,则我们的Ingress Controller部署完成。具体的配置项可以自行查看YAML文件。这里注意,不同的版本
*NOTE:遇到问题,请自行查阅官方的troubleshooting文档
部署测试应用
在部署应用的时候注意自己的Nginx Ingress Controller的版本,不同的版本之间在使用的过程中会有差别,或者不一致的表现。官方示例
我们本次的部署也是以官方示例complete-example为例(在我们部署的时候clone的repo里面的example目录中)
-
部署Pod
$ kubectl create -f cafe.yaml
-
创建ingress 和secret
$ kubectl create -f cafe-secret.yaml
$ kubectl create -f cafe-ingress.yaml
-
将 yaml文件中涉及的域名“cafe.example.com”添加到您的/etc/hosts中,或者修改为您自己的域名。
echo "cafe.example.com 192.168.1.100" >> /etc/hosts
-
CURL访问链接测试
$ curl https://cafe.example.com/coffee -k
Server address: 10.12.0.20:80
Server name: coffee-7586895968-r16zn
$ curl https://cafe.example.com/tea -k
Server address: 10.12.0.22:80
Server name: tea-7cd44fcb4d-xfw99
后续
经过上面的操作基本上了解了如何在自己的K8S集群中部署一个Nginx Ingress Controller,并部署一个简单的hello-world 例子来体验下Ingress的使用。但是在使用的过程中还是会有很多的细节需要考虑和处理。例如,在上面的例子中, https://cafe.example.com/teaxxxxxx , https://cafe.example.com/tea/xxxxx 和 https://cafe.example.com/tea 一样会得被正常转发到后段服务。
会在接下来的文章中陆续整理。现在先列个大纲:
-
Nginx Ingress Controller的工作原理。
-
troubleshooting
-
基于Nginx Ingress Controller的Ingress对象的设置以应对不同的场景(多个)。