提示:如上圖所示快集,ingress就是ingress 控制器pod的代理規(guī)則降盹;用戶請(qǐng)求某個(gè)后端pod所提供的服務(wù)時(shí),首先會(huì)通過ingress controller pod把流量引入到集群內(nèi)部,然后ingress controller pod根據(jù)ingress定義的規(guī)則,把對(duì)應(yīng)ingress規(guī)則轉(zhuǎn)化為對(duì)應(yīng)ingress controller pod實(shí)現(xiàn)的對(duì)應(yīng)應(yīng)用的配置(ingress controller 可以由任何具有七層反向代理功能的服務(wù)實(shí)現(xiàn)缅糟,比如nginx,haproxy等等)然后再適配用戶請(qǐng)求,把對(duì)應(yīng)請(qǐng)求反代到對(duì)應(yīng)service上祷愉;而對(duì)于pod的選擇上窗宦,ingress控制器可以基于對(duì)應(yīng)service中的標(biāo)簽選擇器,直接同pod直接通信二鳄,無須通過service對(duì)象api的再次轉(zhuǎn)發(fā)赴涵,從而省去了用戶請(qǐng)求到kube-proxy實(shí)現(xiàn)的代理開銷(本質(zhì)上ingress controller 也是運(yùn)行為一個(gè)pod,和其他pod在同一網(wǎng)段中)泥从;
ingress controller部署
在k8s上ingress controller的實(shí)現(xiàn)有很多,比如基于nginx的沪摄,基于haproxy的等等躯嫉,這里以nginx為例;
下載ingress-nginx包
1
wget https://github.com/kubernetes/ingress-nginx/archive/nginx-0.28.0.tar.gz
解壓包杨拐,找到對(duì)應(yīng)的部署清單
[root@master01 ~]# ll
total 92144
-rw------- 1 root root 65586688 Dec? 8 15:16 flannel-v0.13.1-rc1.tar
drwxr-xr-x 2 root root? ? 4096 Dec 21 21:04 manifests
-rw-r--r-- 1 root root 28760559 Dec 21 21:02 nginx-0.28.0.tar.gz
[root@master01 ~]# tar xf nginx-0.28.0.tar.gz
[root@master01 ~]# ls
flannel-v0.13.1-rc1.tar? ingress-nginx-nginx-0.28.0? manifests? nginx-0.28.0.tar.gz
[root@master01 ~]# cd ingress-nginx-nginx-0.28.0/
[root@master01 ingress-nginx-nginx-0.28.0]# ls
build? ? ? ? code-of-conduct.md? docs? ? hack? ? ? labels.yaml? mkdocs.yml? ? ? README.md? ? ? ? ? ? ? SECURITY_CONTACTS? version
Changelog.md? CONTRIBUTING.md? ? go.mod? images? ? LICENSE? ? ? OWNERS? ? ? ? ? requirements-docs.txt? test
cmd? ? ? ? ? deploy? ? ? ? ? ? ? go.sum? internal? Makefile? ? OWNERS_ALIASES? rootfs? ? ? ? ? ? ? ? vendor
[root@master01 ingress-nginx-nginx-0.28.0]# cd deploy/
[root@master01 deploy]# ls
aws? ? ? ? cloud-generic? grafana? prometheus? static? ? ? ? ? ? ? ? ? ? ? with-validating-webhook.yaml.tpl
baremetal? cluster-wide? minikube? README.md? validating-webhook.yaml.tpl
[root@master01 deploy]# cd static/
[root@master01 static]# ls
configmap.yaml? mandatory.yaml? namespace.yaml? provider? rbac.yaml? with-rbac.yaml
[root@master01 static]# pwd
/root/ingress-nginx-nginx-0.28.0/deploy/static
[root@master01 static]#
提示:資源配置清單在ingress-nginx-nginx-0.28.0/deploy/static下祈餐,名為mandatory.yaml;
資源配置清單內(nèi)容
View Code
提示:以上清單主要定義了一個(gè)名稱ingress-nginx的名稱空間哄陶,在其名稱空間下創(chuàng)建了幾個(gè)configmap帆阳,最重要的是用deployment創(chuàng)建了一個(gè)ingress-nginx pod;
這里說一下屋吨,對(duì)于ingress-nginx控制器蜒谤,它本質(zhì)還是運(yùn)行為一個(gè)pod,對(duì)于pod來說要想接入外部訪問流量到集群內(nèi)部來至扰,有三種方式鳍徽,一種是使用NodePort類型的service;第二種是使用ds或deploy控制器敢课,在定義pod模板時(shí)使用hostPort把pod端口映射到宿主機(jī)方式阶祭;第三種是定義pod模板時(shí)使用hostNetwork,直接共享宿主機(jī)網(wǎng)絡(luò)名稱空間直秆;如下所示
使用專有NodePort service來引入外部流量
提示:這種使用deploy控制管理ingress controller pod濒募,如果在pod模板中沒有暴露端口,則需要?jiǎng)?chuàng)建一個(gè)service資源來暴露ingress controller pod的端口來引入外部流量到集群內(nèi)部圾结;
使用ds控制器管理ingress controller pod在pod模板中使用hostPort方式暴露端口
提示:使用ds控制器能夠保證每個(gè)節(jié)點(diǎn)上只運(yùn)行一個(gè)ingress controller,所以我們可以把對(duì)應(yīng)ingress controller pod端端口通過端口映射的方式映射到宿主機(jī)上的某一固定端口瑰剃;
使用ds控制器在pod模板中使用hostNetwork方式共享宿主機(jī)網(wǎng)絡(luò)名稱空間
提示:共享宿主機(jī)網(wǎng)絡(luò)名稱空間,也必須使用ds控制器來確保對(duì)應(yīng)每個(gè)節(jié)點(diǎn)上只能運(yùn)行一個(gè)ingress controller pod筝野,這樣才能確保每個(gè)ingress controller pod能夠正常把端口暴露出去培他,以供集群外部客戶端訪問鹃两;
選擇上述其中一種方式暴露ingress controller pod的端口即可;如果選擇使用ds控制器來暴露端口舀凛,我們就需要修改其對(duì)應(yīng)資源配置清單中的pod模板俊扳,如下所示
使用ds控制器來管理ingress controller pod在pod模板中使用hostPort方式暴露端口
View Code
提示:只需把對(duì)應(yīng)控制器類型更改為DaemonSet,在pod模板中spec字段下把replicas去掉猛遍;在spec.template.spec.containers.ports字段中加上nodePort字段指定要把容器的端口映射到宿主機(jī)上某個(gè)端口馋记;如果暴露的端口是非標(biāo)準(zhǔn)端口,在對(duì)應(yīng)k8s集群外部我們還需要部署反代懊烤,比如使用nginx梯醒,haproxy,lvs;
使用ds控制器管理ingress controller pod在ds控制器資源配置中使用hostNetwork
View Code
提示:把對(duì)應(yīng)控制器類型更改外DaemonSet腌紧,在pod模板中spec字段下的replicas字段去掉茸习;在spec.template.spec字段下加上hostNetwork: true即可;以上兩種使用ds控制器管理ingress controller pod也可以使用node選擇器壁肋,來篩選在某個(gè)節(jié)點(diǎn)上創(chuàng)建ingress controller pod号胚;
使用deploy控制器管理ingress controller pod,就直接應(yīng)用mandatory.yaml即可
[root@master01 ~]# kubectl apply -f mandatory.yaml
namespace/ingress-nginx created
configmap/nginx-configuration created
configmap/tcp-services created
configmap/udp-services created
serviceaccount/nginx-ingress-serviceaccount created
Warning: rbac.authorization.k8s.io/v1beta1 ClusterRole is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRole
clusterrole.rbac.authorization.k8s.io/nginx-ingress-clusterrole created
Warning: rbac.authorization.k8s.io/v1beta1 Role is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 Role
role.rbac.authorization.k8s.io/nginx-ingress-role created
Warning: rbac.authorization.k8s.io/v1beta1 RoleBinding is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 RoleBinding
rolebinding.rbac.authorization.k8s.io/nginx-ingress-role-nisa-binding created
Warning: rbac.authorization.k8s.io/v1beta1 ClusterRoleBinding is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRoleBinding
clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress-clusterrole-nisa-binding created
deployment.apps/nginx-ingress-controller created
limitrange/ingress-nginx created
[root@master01 ~]#
查看應(yīng)用資源清單創(chuàng)建的資源對(duì)象
[root@master01 ~]# kubectl get all -n ingress-nginx
NAME? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? READY? STATUS? ? RESTARTS? AGE
pod/nginx-ingress-controller-5466cb8999-4lsjc? 1/1? ? Running? 0? ? ? ? ? 80s
NAME? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? READY? UP-TO-DATE? AVAILABLE? AGE
deployment.apps/nginx-ingress-controller? 1/1? ? 1? ? ? ? ? ? 1? ? ? ? ? 80s
NAME? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? DESIRED? CURRENT? READY? AGE
replicaset.apps/nginx-ingress-controller-5466cb8999? 1? ? ? ? 1? ? ? ? 1? ? ? 80s
[root@master01 ~]#
提示:可以看到在ingress-nginx名稱空間下創(chuàng)建了一個(gè)deploy控制器浸遗,對(duì)應(yīng)控制器創(chuàng)建了一個(gè)nginx-ingress-controller控制器pod猫胁;但是此pod現(xiàn)在不能被外部客戶端訪問到,我們需要?jiǎng)?chuàng)建一個(gè)service來引入外部流量到此pod上跛锌;
查看pod標(biāo)簽
[root@master01 ~]# kubectl get pod -n ingress-nginx --show-labels
NAME? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? READY? STATUS? ? RESTARTS? AGE? ? LABELS
nginx-ingress-controller-5466cb8999-4lsjc? 1/1? ? Running? 0? ? ? ? ? 4m38s? app.kubernetes.io/name=ingress-nginx,app.kubernetes.io/part-of=ingress-nginx,pod-template-hash=5466cb8999
[root@master01 ~]#
根據(jù)上述標(biāo)簽來寫一個(gè)創(chuàng)建ingress-service資源的配置清單
[root@master01 ~]# cat ingress-nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
? name: ingress-nginx-svc
? namespace: ingress-nginx
spec:
? type: NodePort
? ports:
? ? - port: 80
? ? ? name: http
? ? ? nodePort: 30080
? ? - port: 443
? ? ? name: https
? ? ? nodePort: 30443
? selector:
? ? app.kubernetes.io/name: ingress-nginx
? ? app.kubernetes.io/part-of: ingress-nginx
[root@master01 ~]#
提示:以上配置清單主要把滿足對(duì)應(yīng)標(biāo)簽選擇器的pod關(guān)聯(lián)起來弃秆;并把對(duì)應(yīng)pod的80和443端口分別映射到對(duì)應(yīng)主機(jī)上的30080和30443端口;
應(yīng)用配置清單
[root@master01 ~]# kubectl apply -f ingress-nginx-service.yaml
service/ingress-nginx-svc created
[root@master01 ~]# kubectl get svc -n ingress-nginx
NAME? ? ? ? ? ? ? ? TYPE? ? ? CLUSTER-IP? ? EXTERNAL-IP? PORT(S)? ? ? ? ? ? ? ? ? ? ? AGE
ingress-nginx-svc? NodePort? 10.98.4.208? <none>? ? ? ? 80:30080/TCP,443:30443/TCP? 13s
[root@master01 ~]#
訪問集群任意節(jié)點(diǎn)ip的30080和30443端口髓帽,看看是否訪問到對(duì)應(yīng)pod菠赚?
龍華大道1號(hào) http://www.kinghill.cn/Dynamics/2106.html