服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟)
k8s-master CentOS7.7 2C/4G/20G 172.16.1.110 10.0.0.110
k8s-node01 CentOS7.7 2C/4G/20G 172.16.1.111 10.0.0.111
k8s-node02 CentOS7.7 2C/4G/20G 172.16.1.112 10.0.0.112
Ingress概述
Ingress 是对集群中服务的外部访问进行管理的 API 对象,典型的访问方式是 HTTP和HTTPS。
Ingress 可以提供负载均衡、SSL 和基于名称的虚拟托管。
必须具有 ingress 控制器【例如 ingress-nginx】才能满足 Ingress 的要求。仅创建 Ingress 资源无效。
Ingress 是什么
Ingress 公开了从集群外部到集群内 services 的 HTTP 和 HTTPS 路由。 流量路由由 Ingress 资源上定义的规则控制。
internet
|
[ Ingress ]
--|-----|--
[ Services ]
可以将 Ingress 配置为提供服务外部可访问的 URL、负载均衡流量、 SSL / TLS,以及提供基于名称的虚拟主机。Ingress 控制器 通常负责通过负载均衡器来实现 Ingress,尽管它也可以配置边缘路由器或其他前端来帮助处理流量。
Ingress 不会公开任意端口或协议。若将 HTTP 和 HTTPS 以外的服务公开到 Internet 时,通常使用 Service.Type=NodePort 或者 Service.Type=LoadBalancer 类型的服务。
以Nginx Ingress为例,图如下
该Nginx是经过改造的,而不是传统的Nginx。
Ingress-Nginx官网地址:https://kubernetes.github.io/ingress-nginx/
Ingress-Nginx GitHub地址:https://github.com/kubernetes/ingress-nginx
本次下载版本:nginx-0.30.0
镜像下载与重命名
ocker pull registry.cn-beijing.aliyuncs.com/google_registry/nginx-ingress-controller:0.30.0
docker tag 89ccad40ce8e quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.30.0
docker rmi registry.cn-beijing.aliyuncs.com/google_registry/nginx-ingress-controller:0.30.0
ingress-nginx的yaml文件修改后并启动
# 当前目录
[root@k8s-master ingress]# pwd
/root/k8s_practice/ingress
# 获取NGINX: 0.30.0
[root@k8s-master ingress]# wget https://github.com/kubernetes/ingress-nginx/archive/nginx-0.30.0.tar.gz
[root@k8s-master ingress]# tar xf nginx-0.30.0.tar.gz
# yaml文件在下载包中的位置:ingress-nginx-nginx-0.30.0/deploy/static/mandatory.yaml
[root@k8s-master ingress]# cp -a ingress-nginx-nginx-0.30.0/deploy/static/mandatory.yaml ./
[root@k8s-master ingress]#
# yaml文件配置修改
[root@k8s-master ingress]# vim mandatory.yaml
………………
apiVersion: apps/v1
kind: DaemonSet # 从Deployment改为DaemonSet
metadata:
name: nginx-ingress-controller
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
spec:
#replicas: 1 # 注释掉
………………
nodeSelector:
kubernetes.io/hostname: k8s-master # 修改处
# 如下几行为新加行 作用【允许在master节点运行】
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
………………
ports:
- name: http
containerPort: 80
hostPort: 80 # 添加处【可在宿主机通过该端口访问Pod】
protocol: TCP
- name: https
containerPort: 443
hostPort: 443 # 添加处【可在宿主机通过该端口访问Pod】
protocol: TCP
………………
[root@k8s-master ingress]#
[root@k8s-master ingress]# 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
clusterrole.rbac.authorization.k8s.io/nginx-ingress-clusterrole created
role.rbac.authorization.k8s.io/nginx-ingress-role created
rolebinding.rbac.authorization.k8s.io/nginx-ingress-role-nisa-binding created
clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress-clusterrole-nisa-binding created
daemonset.apps/nginx-ingress-controller created
limitrange/ingress-nginx created
[root@k8s-master ingress]#
[root@k8s-master ingress]# kubectl get ds -n ingress-nginx -o wide
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE CONTAINERS IMAGES SELECTOR
nginx-ingress-controller 1 1 1 1 1 kubernetes.io/hostname=k8s-master 9m47s nginx-ingress-controller quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.30.0 app.kubernetes.io/name=ingress-nginx,app.kubernetes.io/part-of=ingress-nginx
[root@k8s-master ingress]#
[root@k8s-master ingress]# kubectl get pod -n ingress-nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-ingress-controller-rrbh9 1/1 Running 0 9m55s 10.244.0.46 k8s-master
[root@k8s-master ingress]# pwd
/root/k8s_practice/ingress
[root@k8s-master ingress]# cat deply_service1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy1
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: myapp
release: v1
template:
metadata:
labels:
app: myapp
release: v1
env: test
spec:
containers:
- name: myapp
image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: myapp-clusterip1
namespace: default
spec:
type: ClusterIP # 默认类型
selector:
app: myapp
release: v1
ports:
- name: http
port: 80
targetPort: 80
启动Deployment和Service
[root@k8s-master ingress]# kubectl apply -f deply_service1.yaml
deployment.apps/myapp-deploy1 created
service/myapp-clusterip1 created
查看Deploy状态和信息
[root@k8s-master ingress]# kubectl get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
myapp-deploy1 3/3 3 3 28s myapp registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1 app=myapp,release=v1
[root@k8s-master ingress]#
[root@k8s-master ingress]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp-deploy1-5695bb5658 3 3 3 30s myapp registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1 app=myapp,pod-template-hash=5695bb5658,release=v1
[root@k8s-master ingress]#
[root@k8s-master ingress]# kubectl get pod -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
myapp-deploy1-5695bb5658-n6548 1/1 Running 0 36s 10.244.2.144 k8s-node02 app=myapp,env=test,pod-template-hash=5695bb5658,release=v1
myapp-deploy1-5695bb5658-rqcpb 1/1 Running 0 36s 10.244.2.143 k8s-node02 app=myapp,env=test,pod-template-hash=5695bb5658,release=v1
myapp-deploy1-5695bb5658-vv6gm 1/1 Running 0 36s 10.244.3.200 k8s-node01 app=myapp,env=test,pod-template-hash=5695bb5658,release=v1
curl访问pod
[root@k8s-master ingress]# curl 10.244.2.144
Hello MyApp | Version: v1 | Pod Name
[root@k8s-master ingress]#
[root@k8s-master ingress]# curl 10.244.2.144/hostname.html
myapp-deploy1-5695bb5658-n6548
[root@k8s-master ingress]#
[root@k8s-master ingress]# curl 10.244.2.143/hostname.html
myapp-deploy1-5695bb5658-rqcpb
[root@k8s-master ingress]#
[root@k8s-master ingress]# curl 10.244.3.200/hostname.html
myapp-deploy1-5695bb5658-vv6gm
查看Service状态和信息
[root@k8s-master ingress]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 443/TCP 19d
myapp-clusterip1 ClusterIP 10.104.146.14 80/TCP 5m38s app=myapp,release=v1
curl访问svc
[root@k8s-master ingress]# curl 10.104.146.14
Hello MyApp | Version: v1 | Pod Name
[root@k8s-master ingress]#
[root@k8s-master ingress]# curl 10.104.146.14/hostname.html
myapp-deploy1-5695bb5658-n6548
[root@k8s-master ingress]#
[root@k8s-master ingress]# curl 10.104.146.14/hostname.html
myapp-deploy1-5695bb5658-vv6gm
[root@k8s-master ingress]#
[root@k8s-master ingress]# curl 10.104.146.14/hostname.html
myapp-deploy1-5695bb5658-rqcpb
[root@k8s-master ingress]# pwd
/root/k8s_practice/ingress
[root@k8s-master ingress]# cat deply_service2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy2
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: myapp
release: v2
template:
metadata:
labels:
app: myapp
release: v2
env: test
spec:
containers:
- name: myapp
image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v2
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: myapp-clusterip2
namespace: default
spec:
type: ClusterIP # 默认类型
selector:
app: myapp
release: v2
ports:
- name: http
port: 80
targetPort: 80
启动Deployment和Service
[root@k8s-master ingress]# kubectl apply -f deply_service2.yaml
deployment.apps/myapp-deploy2 created
service/myapp-clusterip2 created
查看Deploy状态和信息
[root@k8s-master ingress]# kubectl get deploy myapp-deploy2 -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
myapp-deploy2 3/3 3 3 9s myapp registry.cn-beijing.aliyuncs.com/google_registry/myapp:v2 app=myapp,release=v2
[root@k8s-master ingress]#
[root@k8s-master ingress]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp-deploy1-5695bb5658 3 3 3 7m23s myapp registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1 app=myapp,pod-template-hash=5695bb5658,release=v1 # 之前创建的
myapp-deploy2-54f48f879b 3 3 3 15s myapp registry.cn-beijing.aliyuncs.com/google_registry/myapp:v2 app=myapp,pod-template-hash=54f48f879b,release=v2 # 当前deploy创建的
[root@k8s-master ingress]#
[root@k8s-master ingress]# kubectl get pod -o wide --show-labels -l "release=v2"
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
myapp-deploy2-54f48f879b-7pxwp 1/1 Running 0 25s 10.244.3.201 k8s-node01 app=myapp,env=test,pod-template-hash=54f48f879b,release=v2
myapp-deploy2-54f48f879b-lqlh2 1/1 Running 0 25s 10.244.2.146 k8s-node02 app=myapp,env=test,pod-template-hash=54f48f879b,release=v2
myapp-deploy2-54f48f879b-pfvnn 1/1 Running 0 25s 10.244.2.145 k8s-node02 app=myapp,env=test,pod-template-hash=54f48f879b,release=v2
查看Service状态和信息
[root@k8s-master ingress]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 443/TCP 19d
myapp-clusterip1 ClusterIP 10.104.146.14 80/TCP 8m9s app=myapp,release=v1
myapp-clusterip2 ClusterIP 10.110.181.62 80/TCP 61s app=myapp,release=v2
curl访问svc
[root@k8s-master ingress]# curl 10.110.181.62
Hello MyApp | Version: v2 | Pod Name
[root@k8s-master ingress]#
[root@k8s-master ingress]# curl 10.110.181.62/hostname.html
myapp-deploy2-54f48f879b-lqlh2
[root@k8s-master ingress]#
[root@k8s-master ingress]# curl 10.110.181.62/hostname.html
myapp-deploy2-54f48f879b-7pxwp
[root@k8s-master ingress]#
[root@k8s-master ingress]# curl 10.110.181.62/hostname.html
myapp-deploy2-54f48f879b-pfvnn
yaml文件【由于自建的service在默认default名称空间,因此这里也是default名称空间】
[root@k8s-master ingress]# pwd
/root/k8s_practice/ingress
[root@k8s-master ingress]# cat ingress-http.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: nginx-http
namespace: default
spec:
rules:
- host: www.zhangtest.com
http:
paths:
- path: /
backend:
serviceName: myapp-clusterip1
servicePort: 80
- host: blog.zhangtest.com
http:
paths:
- path: /
backend:
serviceName: myapp-clusterip2
servicePort: 80
启动ingress http并查看状态
[root@k8s-master ingress]# kubectl apply -f ingress-http.yaml
ingress.networking.k8s.io/nginx-http created
[root@k8s-master ingress]#
[root@k8s-master ingress]# kubectl get ingress -o wide
NAME HOSTS ADDRESS PORTS AGE
nginx-http www.zhangtest.com,blog.zhangtest.com 80 9s
查看nginx配置文件
[root@k8s-master ~]# kubectl get pod -A | grep 'ingre'
ingress-nginx nginx-ingress-controller-rrbh9 1/1 Running 0 27m
[root@k8s-master ~]#
[root@k8s-master ~]# kubectl exec -it -n ingress-nginx nginx-ingress-controller-rrbh9 bash
bash-5.0$ cat /etc/nginx/nginx.conf
…………
##### 可见server www.zhangtest.com 和 server blog.zhangtest.com的配置
hosts文件修改,添加如下信息
文件位置:C:\WINDOWS\System32\drivers\etc\hosts
添加信息如下:
# K8S ingress学习
10.0.0.110 www.zhangtest.com blog.zhangtest.com
浏览器访问
本文参考自: https://blog.csdn.net/woshizhangliang999/article/details/108762012?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-3.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-3.control