前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nginx-ingress使用(下)

nginx-ingress使用(下)

原创
作者头像
虚心学习
修改2020-01-16 14:51:33
1.3K0
修改2020-01-16 14:51:33
举报
文章被收录于专栏:PAAS

上文已对nginx-ingress进行相关组件安装部署,可以根据实际需求进行定义Ingress资源来实现七层负载转发了。

这里需要先部署一个用来被访问的容器资源,我们这里就使用最简单的nginx容器。

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
[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

代码语言:javascript
复制
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 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档