上文已对nginx-ingress进行相关组件安装部署,可以根据实际需求进行定义Ingress资源来实现七层负载转发了。
这里需要先部署一个用来被访问的容器资源,我们这里就使用最简单的nginx容器。
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.12
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx
namespace: default
spec:
clusterIP: None
ports:
- name: tcp-80-80
port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
这里nginx的service 使用clusterIP Headless 的暴露方式,用于nginx-ingress 的访问方式。
具体Headless 的使用说明:不创建用于集群内访问的ClusterIP,访问Service名称时返回后端Pods IP地址,用于适配自有的服务发现机制。
TKE 集群中创建,所以要规避qcloud 类型的ingress ,可以参考官网文档:
https://cloud.tencent.com/document/product/457/31711
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx ## 可选值:qcloud(CLB类型ingress), nginx(nginx-ingress)
## kubernetes.io/ingress.subnetId: subnet-xxxxxxxx ##若是创建CLB类型内网ingress需指定该条annotation
name: my-ingress
namespace: default
spec:
rules:
- host: moo.****.club
http:
paths:
- backend:
serviceName: nginx
servicePort: 80
path: /
创建ingress对象
查看ingress详情,可以在Rules规则中看到后端Pod的列表,自动发现和关联相关Pod
[root@VM_16_11_centos ~]# kubectl describe ingress my-ingress
Name: my-ingress
Namespace: default
Address:
Default backend: default-http-backend:80 (<none>)
Rules:
Host Path Backends
---- ---- --------
moo.****.club
/ nginx:80 (<none>)
Annotations:
kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"kubernetes.io/ingress
.class":"nginx"},"name":"my-ingress","namespace":"default"},"spec":{"rules":[{"host":"moo.****.club","http":{"paths":[{"backend":{"serviceName":"nginx","servicePort":80},"path":"/"}]}}]}}
kubernetes.io/ingress.class: nginx
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal AddedOrUpdated 10m nginx-ingress-controller Configuration for default/my-ingress was added or updated
Normal AddedOrUpdated 10m nginx-ingress-controller Configuration for default/my-ingress was added or updated
进行检查ingress是否生效:
查看nginx Controller控制器的配置文件,在nginx-ingress pod中存储着ingress的配置文件
#每个ingress都会生成一个配置文件,文件名为:命名空间-ingres.conf
root@VM_16_11_centos ~]# kubectl exec -it nginx-ingress-66b9d5b9c6-kr267 -n nginx-ingress -- ls -l /etc/nginx/conf.d
total 4
-rw-r--r-- 1 nginx nginx 941 Jan 15 07:35 default-my-ingress.conf
[root@VM_16_11_centos ~]# kubectl exec -it nginx-ingress-66b9d5b9c6-kr267 -n nginx-ingress -- cat /etc/nginx/conf.d/default-my-ingress.conf
# configuration for default/my-ingress
upstream default-my-ingress-moo.****.club-nginx-80 {
zone default-my-ingress-moo.****.club-nginx-80 256k;
random two least_conn;
server 172.16.0.72:80 max_fails=1 fail_timeout=10s max_conns=0;
server 172.16.0.9:80 max_fails=1 fail_timeout=10s max_conns=0;
}
server {
listen 80;
server_tokens on;
server_name moo.****.club;
location / {
proxy_http_version 1.1;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
client_max_body_size 1m;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering on;
proxy_pass http://default-my-ingress-moo.****.club-nginx-80;
}
}
查看配置文件可得知,Nginx Ingress Controller实际是根据ingress规则生成对应的nginx配置文件,以实现代理转发的功能,配置多个ingress即生成多个nginx配置文件
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有