14、Ingress

检查是否有安装
[root@k8s-master-node1 ~/yaml/test]# kubectl get pod,svc -n ingress-nginx
NAME READY STATUS RESTARTS AGE
pod/ingress-nginx-admission-create--1-74mtg 0/1 Completed 0 172m
pod/ingress-nginx-admission-patch--1-5qrct 0/1 Completed 0 172m
pod/ingress-nginx-controller-f97bd58b5-vr8c2 1/1 Running 0 172m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/ingress-nginx-controller NodePort 10.96.109.80 <none> 80:30127/TCP,443:36903/TCP 172m
service/ingress-nginx-controller-admission ClusterIP 10.96.215.201 <none> 443/TCP 172m
[root@k8s-master-node1 ~/yaml/test]#

若未安装可以查看官网文档:https://kubernetes.github.io/ingress-nginx/deploy/

创建环境:
[root@k8s-master-node1 ~/yaml/test]# vim ingress.yaml
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]# cat ingress.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-server
spec:
replicas: 2
selector:
matchLabels:
app: hello-server
template:
metadata:
labels:
app: hello-server
spec:
containers:
- name: hello-server
image: registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/hello-server
ports:
- containerPort: 9000
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx-demo
name: nginx-demo
spec:
replicas: 2
selector:
matchLabels:
app: nginx-demo
template:
metadata:
labels:
app: nginx-demo
spec:
containers:
- image: nginx
name: nginx
---
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx-demo
name: nginx-demo
spec:
selector:
app: nginx-demo
ports:
- port: 8000
protocol: TCP
targetPort: 80
---
apiVersion: v1
kind: Service
metadata:
labels:
app: hello-server
name: hello-server
spec:
selector:
app: hello-server
ports:
- port: 8000
protocol: TCP
targetPort: 9000
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]# kubectl apply -f ingress.yaml
deployment.apps/hello-server created
deployment.apps/nginx-demo created
service/nginx-demo created
service/hello-server created
[root@k8s-master-node1 ~/yaml/test]#
查看四层负载并测试
[root@k8s-master-node1 ~/yaml/test]# kubectl  get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-server ClusterIP 10.96.246.46 <none> 8000/TCP 21s
ingress-demo-app ClusterIP 10.96.145.40 <none> 80/TCP 3h1m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3h3m
my-dep NodePort 10.96.241.162 <none> 8000:32306/TCP 23m
nginx ClusterIP None <none> 80/TCP 115m
nginx-demo ClusterIP 10.96.162.193 <none> 8000/TCP 21s
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]# curl -I 10.96.162.193:8000
HTTP/1.1 200 OK
Server: nginx/1.21.4
Date: Wed, 17 Nov 2021 09:49:40 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 02 Nov 2021 14:49:22 GMT
Connection: keep-alive
ETag: "61814ff2-267"
Accept-Ranges: bytes [root@k8s-master-node1 ~/yaml/test]# curl -I 10.96.246.46:8000
HTTP/1.1 200 OK
Date: Wed, 17 Nov 2021 09:49:52 GMT
Content-Length: 12
Content-Type: text/plain; charset=utf-8 [root@k8s-master-node1 ~/yaml/test]#
创建七层
[root@k8s-master-node1 ~/yaml/test]# vim ingress-7.yaml
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]# cat ingress-7.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-host-bar
spec:
ingressClassName: nginx
rules:
- host: "hello.chenby.cn"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: hello-server
port:
number: 8000
- host: "demo.chenby.cn"
http:
paths:
- pathType: Prefix
path: "/nginx" # 把请求会转给下面的服务,下面的服务一定要能处理这个路径,不能处理就是404
backend:
service:
name: nginx-demo ## java,比如使用路径重写,去掉前缀nginx
port:
number: 8000
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]# kubectl apply -f ingress-7.yaml
ingress.networking.k8s.io/ingress-host-bar created
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress-demo-app <none> app.demo.com 192.168.1.62 80 3h14m
ingress-host-bar nginx hello.chenby.cn,demo.chenby.cn 192.168.1.62 80 9m50s
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]# kubectl describe ingress ingress-host-bar
Name: ingress-host-bar
Namespace: default
Address: 192.168.1.62
Default backend: default-http-backend:80 (10.244.2.7:8080)
Rules:
Host Path Backends
---- ---- --------
hello.chenby.cn
/ hello-server:8000 (10.244.2.47:9000,10.244.2.48:9000)
demo.chenby.cn
/nginx nginx-demo:8000 (10.244.0.13:80,10.244.1.34:80)
Annotations: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Sync 6m26s (x2 over 6m50s) nginx-ingress-controller Scheduled for sync
[root@k8s-master-node1 ~/yaml/test]#
电脑上写死hosts
[root@k8s-master-node1 ~/yaml/test]# kubectl  get service ingress-demo-app
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-demo-app ClusterIP 10.96.145.40 <none> 80/TCP 3h15m
[root@k8s-master-node1 ~/yaml/test]# 10.96.145.40 hello.chenby.cn
10.96.145.40 demo.chenby.cn
测试访问
[root@k8s-master-node1 ~/yaml/test]# curl hello.chenby.cn
Hostname: ingress-demo-app-694bf5d965-8rh7f
IP: 127.0.0.1
IP: 10.244.1.6
RemoteAddr: 192.168.1.61:49809
GET / HTTP/1.1
Host: hello.chenby.cn
User-Agent: curl/7.68.0
Accept: */* [root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]# curl demo.chenby.cn
Hostname: ingress-demo-app-694bf5d965-swkpb
IP: 127.0.0.1
IP: 10.244.2.4
RemoteAddr: 192.168.1.61:57797
GET / HTTP/1.1
Host: demo.chenby.cn
User-Agent: curl/7.68.0
Accept: */* [root@k8s-master-node1 ~/yaml/test]#
url 重写
[root@k8s-master-node1 ~/yaml/test]# vim ingress-url
[root@k8s-master-node1 ~/yaml/test]# cat ingress-url.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
name: ingress-host-bar
spec:
ingressClassName: nginx
rules:
- host: "hello.chenby.cn"
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: hello-server
port:
number: 8000
- host: "demo.chenby.cn"
http:
paths:
- pathType: Prefix
path: "/nginx(/|$)(.*)" # 把请求会转给下面的服务,下面的服务一定要能处理这个路径,不能处理就是404
backend:
service:
name: nginx-demo ## java,比如使用路径重写,去掉前缀nginx
port:
number: 8000
[root@k8s-master-node1 ~/yaml/test]# kubectl apply -f ingress-url.yaml
ingress.networking.k8s.io/ingress-host-bar created
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]# curl hello.chenby.cn
Hostname: ingress-demo-app-694bf5d965-8rh7f
IP: 127.0.0.1
IP: 10.244.1.6
RemoteAddr: 192.168.1.61:42303
GET / HTTP/1.1
Host: hello.chenby.cn
User-Agent: curl/7.68.0
Accept: */* [root@k8s-master-node1 ~/yaml/test]# curl demo.chenby.cn
Hostname: ingress-demo-app-694bf5d965-swkpb
IP: 127.0.0.1
IP: 10.244.2.4
RemoteAddr: 192.168.1.61:1108
GET / HTTP/1.1
Host: demo.chenby.cn
User-Agent: curl/7.68.0
Accept: */* [root@k8s-master-node1 ~/yaml/test]#
流量限制
[root@k8s-master-node1 ~/yaml/test]# vim ingress-limit
[root@k8s-master-node1 ~/yaml/test]# cat ingress-limit.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-limit-rate
annotations:
nginx.ingress.kubernetes.io/limit-rps: "1"
spec:
ingressClassName: nginx
rules:
- host: "haha.chenby.cn"
http:
paths:
- pathType: Exact
path: "/"
backend:
service:
name: nginx-demo
port:
number: 8000
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]# kubectl apply -f ingress-limit.yaml
ingress.networking.k8s.io/ingress-limit-rate created
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]# vim /etc/hosts
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]# curl haha.chenby.cn
Hostname: ingress-demo-app-694bf5d965-8rh7f
IP: 127.0.0.1
IP: 10.244.1.6
RemoteAddr: 192.168.1.61:1676
GET / HTTP/1.1
Host: haha.chenby.cn
User-Agent: curl/7.68.0
Accept: */* [root@k8s-master-node1 ~/yaml/test]#

注:可以将server 改为nodeport 即可在外部访问,将 type 改为 NodePort

[root@k8s-master-node1 ~/yaml/test]# kubectl  get service ingress-demo-app
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-demo-app ClusterIP 10.96.145.40 <none> 80/TCP 6h27m
[root@k8s-master-node1 ~/yaml/test]# kubectl edit service ingress-demo-app
service/ingress-demo-app edited
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]#
[root@k8s-master-node1 ~/yaml/test]# kubectl get service ingress-demo-app
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-demo-app NodePort 10.96.145.40 <none> 80:30975/TCP 6h28m
[root@k8s-master-node1 ~/yaml/test]#

https://blog.csdn.net/qq_33921750

https://my.oschina.net/u/3981543

https://www.zhihu.com/people/chen-bu-yun-2

https://segmentfault.com/u/hppyvyv6/articles

https://juejin.cn/user/3315782802482007

https://space.bilibili.com/352476552/article

https://cloud.tencent.com/developer/column/93230

知乎、CSDN、开源中国、思否、掘金、哔哩哔哩、腾讯云

kubernetes核心实战(九)的更多相关文章

  1. Kubernetes 核心概念

    什么是Kubernetes? Kubernetes(k8s)是自动化容器操作的开源平台,这些操作包括部署,调度和节点集群间扩展.如果你曾经用过Docker容器技术部署容器,那么可以将Docker看成K ...

  2. 十分钟带你理解Kubernetes核心概念

    什么是Kubernetes? Kubernetes(k8s)是自动化容器操作的开源平台,这些操作包括部署,调度和节点集群间扩展.如果你曾经用过Docker容器技术部署容器,那么可以将Docker看成K ...

  3. Kubernetes核心概念简介

    本文将会简单介绍Kubernetes的核心概念.因为这些定义可以在Kubernetes的文档中找到,所以文章也会避免用大段的枯燥的文字介绍.相反,我们会使用一些图表(其中一些是动画)和示例来解释这些概 ...

  4. k8s kubernetes 核心笔记 镜像仓库 项目k8s改造(含最新k8s v1.16.2版本)

    k8s kubernetes 核心笔记 镜像仓库 项目k8s改造 2019/10/24 Chenxin 一 基本资料 一 参考: https://kubernetes.io/ 官网 https://k ...

  5. 后端技术杂谈11:十分钟理解Kubernetes核心概念

    本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 本文转自 https://github.com/h2pl/Java-Tutorial 喜欢的 ...

  6. 01 . 容器编排简介及Kubernetes核心概念

    Kubernetes简介 Kubernetes是谷歌严格保密十几年的秘密武器-Borg的一个开源版本,是Docker分布式系统解决方案.2014年由Google公司启动. Kubernetes提供了面 ...

  7. [转]十分钟带你理解Kubernetes核心概念

    本文将会简单介绍 Kubernetes的核心概念.因为这些定义可以在Kubernetes的文档中找到,所以文章也会避免用大段的枯燥的文字介绍.相反,我们会使用一些图表(其中一些是动画)和示例来解释这些 ...

  8. SpringSecurity权限管理系统实战—九、数据权限的配置

    目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战-三 ...

  9. 第3 章 : Kubernetes 核心概念

    Kubernetes 核心概念 本文整理自 CNCF 和阿里巴巴联合举办的云原生技术公开课的课时 3:Kubernetes 核心概念.本次课程中,阿里巴巴资深技术专家.CNCF 9个 TCO 之一 李 ...

  10. kubernetes实战(九):k8s集群动态存储管理GlusterFS及使用Heketi扩容GlusterFS集群

    1.准备工作 所有节点安装GFS客户端 yum install glusterfs glusterfs-fuse -y 如果不是所有节点要部署GFS管理服务,就在需要部署的节点上打上标签 [root@ ...

随机推荐

  1. 狂神day3

    放个假,有几天没有学习了,也好久没有更新博客了 IDEA使用 在上一次的时候,我们直接建立一个工程写了一个"helloworld"的java文件,今天首先说的是先建立一个空的工程, ...

  2. percona mongo热备

    https://www.percona.com/doc/percona-server-for-mongodb/LATEST/hot-backup.html#hot-backup Hot Backup ...

  3. Linux系列---【设置ssh连接超时时间】

    设置ssh连接超时时间 shell工具总是不断的超时,上个厕所就断开了,很不方便,这里根据自己习惯设置一下. echo "export TMOUT=600" >> /e ...

  4. 2、HTTP的消息格式

    概念 HTTP协议 Hyper Text Transfer Protocol 超文本传输协议 传输协议 传输协议定义了客户端和服务器端通信时,发送数据的格式. 特点 基于TCP/IP的高级协议 默认端 ...

  5. WV电影网站的设计与实现-可行性研究分析报告

    引言 WV(Wonderful View)电影网站--奇景电影网. 1.1编写目的 1.2背景 在信息发展的时代,地球人口越来越多,人们相比去拥挤的电影院,更喜欢待在舒适的家中,通过互联网访问本站,实 ...

  6. [Vue warn]: Duplicate keys detected: ''. This may cause an update error. found in

    原因: 使用element-ui 导致 使用路由模式之后  index 没写 导致 解决办法: 删掉  或者天添加路由

  7. C/C++ 数据结构循环队列的实现

    #include <iostream> #include <Windows.h> using namespace std; #define MAXSIZE 6 typedef ...

  8. EF 操作实例

    一.Linq语法 List操作 1.1          有参 public List<GoodsInfo> ShowInfo(string GName,int?Gid) { //true ...

  9. stm32 USART

    stm32 USART 1 USART基础知识 三种模式:轮询模式.中断模式.DMA模式 轮询模式属于阻塞模式 中断模式和DMA模式属于非阻塞模式 发送数据 接收数据 1.1 Polling mode ...

  10. Java--接口和抽象类有什么区别

    他们都不能实例化对象,都可以包含抽象方法,而且抽象方法必须被继承的类全部实现. 区别: 1.抽象类和接口都不能直接实例化,如果要实例化,抽象类变量必须指向实现所有抽象方法的子类对象,接口变量必须指向实 ...