92、K8s之ingress下集

embedded/2024/9/24 11:35:35/

一、ingress

1.1、两种部署方式

1、ingress------------deployment + nodeport

​ daemonset + hostnetwork----每台设备只能有一个pod,因为直接使用宿主机的端口,所以只能开启一个pod。

2、ingress------svc------deployment里面的pod,这种可以有多个pod。

1.2、ingess的权限控制:

访问页面的时候,输入账号密码才可以访问页面。

basicAuth:可以创建访问密码

traefik ingress controller

专门为了部署k8s微服务开发的http方向代理和负载均衡工具。

自动发现匹配的后端pod的变化,同时有可视化的页面

自动感知变化,实现服务的自动发现

daemonset + hostnetwork 适用于大集群

deployment + nodeport 适用内部访问,性能较低

1.3、ingress-traefik和ingress-nginx之间的区别。

igress-nginx 使用nginx作为前端的负载均衡,ingress-controller和k8s的api交互来实现后端服务器的发现,pod的ip地址的变化。

动态实现nginx的配置修改。

ingress-traefik:

本身就能和k8s的api的交互,感知后端的service以及pod的变化。

traefik更简单,更方便。

go语言写的,和k8s的兼容性更好。并发能力只有ingress-nginx的6成。

二、试验操作

1、访问页面的时候,输入账号密码才可以访问页面。

basicAuth:可以创建访问密码

[root@master01 opt]# cd ingress/
[root@master01 ingress]# htpasswd -c auth zhailiming
New password: 
Re-type new password: 
Adding password for user zhailiming
[root@master01 ingress]# ls
auth   ingress-nginx1.yaml  service-nodeport.yaml
https  mandatory.yaml
[root@master01 ingress]# kubectl create secret generic basic-auth --from-file=auth 
secret/basic-auth created[root@master01 ingress]# vim ingress-nginx1.yaml annotations:
#设置认证的类型::nginx.ingress.kubernetes.io/auth-type: basic
#设置认证的secret的名称nginx.ingress.kubernetes.io/auth-secret: basic-auth
#设置认证窗口的提示信息nginx.ingress.kubernetes.io/auth-realm: 'wo ai zhailiming'[root@master01 ingress]# kubectl apply -f ingress-nginx1.yaml [root@master01 ingress]# kubectl get pod -o wide -n ingress-nginx 
NAME                             READY   STATUS    RESTARTS   AGE   IP               NODE       NOMINATED NODE   READINESS GATES
nginx-ingress-controller-44ktd   1/1     Running   0          18h   192.168.168.83   node02     <none>           <none>
nginx-ingress-controller-ksjkr   1/1     Running   0          18h   192.168.168.81   master01   <none>           <none>
nginx-ingress-controller-z4lrr   1/1     Running   0          18h   192.168.168.82   node01     <none>           <none>##进入虚拟机终端浏览器

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

2、重定向-----rewrite-target:

实现从www.zlm.com跳转www.xy102.com

[root@master01 ingress]# vim ingress-nginx1.yamlannotations:
#设置认证的类型::
#    nginx.ingress.kubernetes.io/auth-type: basic
#设置认证的secret的名称
#    nginx.ingress.kubernetes.io/auth-secret: basic-auth
#设置认证窗口的提示信息
#    nginx.ingress.kubernetes.io/auth-realm: 'wo ai zhailiming'nginx.ingress.kubernetes.io/rewrite-target: https://www.xy102.com
#设定重定向流量的目标连接
spec:tls:- hosts:- www.zlm.comsecretName: tls.secret
#指定加密通信的域名,上下文一直,指定secret加密的名称,获取私钥和证
书rules:- host: www.zlm.comhttp:[root@master01 ingress]# vim /etc/hosts127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.168.81 master01 www.xy102.com www.zlm.com

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

3、ingress-traefik和ingress-nginx之间的区别。

igress-nginx 使用nginx作为前端的负载均衡,ingress-controller和k8s的api交互来实现后端服务器的发现,pod的ip地址的变化。

动态实现nginx的配置修改。

ingress-traefik:

本身就能和k8s的api的交互,感知后端的service以及pod的变化。

traefik更简单,更方便。

go语言写的,和k8s的兼容性更好。并发能力只有ingress-nginx的6成。

DaemonSet+hostPort:

[root@master01 ingress]# vim mandatory.yaml apiVersion: apps/v1
#kind: Deployment
kind: DaemonSet
metadata:name: nginx-ingress-controllernamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
spec:
#  replicas: 1selector:matchLabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginxtemplate:metadata:labels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginxannotations:prometheus.io/port: "10254"prometheus.io/scrape: "true"spec:# wait up to five minutes for the drain of connectionsterminationGracePeriodSeconds: 300serviceAccountName: nginx-ingress-serviceaccountnodeSelector:kubernetes.io/os: linuxhostNetwork: true
#      nodeSelector:
#        ingress: "true"
---------------------------------------------------

ingress-traefik

[root@master01 ingress]# mkdir traefik
[root@master01 ingress]# cd traefik/
[root@master01 traefik]# pwd
/opt/ingress/traefik----------------
wget  https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-deployment.yaml
wget  https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-rbac.yaml
wget  https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-ds.yaml
wget  https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/ui.yaml
----------------
[root@master01 traefik]# ll
总用量 16
-rw-r--r--. 1 root root 1114 9月  11 10:26 traefik-deployment.yaml
-rw-r--r--. 1 root root 1294 9月  11 10:26 traefik-ds.yaml
-rw-r--r--. 1 root root  788 9月  11 10:26 traefik-rbac.yaml
-rw-r--r--. 1 root root  471 9月  11 10:27 ui.yaml[root@master01 traefik]# kubectl apply -f traefik-rbac.yaml 
[root@master01 traefik]# kubectl apply -f traefik-deployment.yaml 
[root@master01 traefik]# kubectl apply -f ui.yaml [root@master01 traefik]# kubectl get svc -n kube-system 
NAME                      TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                       AGE
kube-dns                  ClusterIP   10.96.0.10     <none>        53/UDP,53/TCP,9153/TCP        15d
traefik-ingress-service   NodePort    10.96.27.248   <none>        80:30789/TCP,8080:31818/TCP   71s
traefik-web-ui            ClusterIP   10.96.45.60    <none>        80/TCP                        61s[root@master01 traefik]# cd ..
[root@master01 ingress]# kubectl delete -f mandatory.yaml[root@master01 ingress]# cp ingress-nginx1.yaml traefik/traefik-nginx1.yaml
[root@master01 ingress]# cd traefik/
[root@master01 traefik]# ls
traefik-deployment.yaml  traefik-nginx1.yaml  ui.yaml
traefik-ds.yaml          traefik-rbac.yaml[root@master01 traefik]# cd ..
[root@master01 ingress]# ls
auth   ingress-nginx1.yaml  service-nodeport.yaml
https  mandatory.yaml       traefik
[root@master01 ingress]# kubectl delete -f ingress-nginx1.yaml [root@master01 traefik]# vim traefik-nginx1.yaml apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: nfs-pvc
spec:accessModes:- ReadWriteManystorageClassName: nfs-client-storageclassresources:requests:storage: 2Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-traefiklabels:app1: nginx1
spec:replicas: 3selector:matchLabels:app1: nginx1template:metadata:labels:app1: nginx1spec:containers:- name: nginx1image: nginx:1.22ports:- containerPort: 80volumeMounts:- name: nfs-pvcmountPath: /usr/share/nginx/htmlvolumes:- name: nfs-pvcpersistentVolumeClaim:claimName: nfs-pvc
---
apiVersion: v1
kind: Service
metadata:name: nginx-traefik-svc
spec:type: ClusterIPports:- protocol: TCPport: 80targetPort: 80selector:app1: nginx1
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: nginx-traefik-ingressannotations:
#设置认证的类型::
#    nginx.ingress.kubernetes.io/auth-type: basic
#设置认证的secret的名称
#    nginx.ingress.kubernetes.io/auth-secret: basic-auth
#设置认证窗口的提示信息
#    nginx.ingress.kubernetes.io/auth-realm: 'wo ai zhailiming'
#    nginx.ingress.kubernetes.io/rewrite-target: https://www.xy102.com
#设定重定向流量的目标连接
spec:rules:- host: www.xy102.comhttp:paths:- path: /pathType: Prefix
#前缀匹配,匹配/ /test1 /test1/test2backend:
#匹配的svc的名称----podservice:name: nginx-traefik-svcport:number: 80[root@master01 traefik]# kubectl apply -f traefik-nginx1.yaml [root@k8s5 k8s]# cd default-nfs-pvc-pvc-305bce77-a15a-4b27-bef7-e58529eadace/
[root@k8s5 default-nfs-pvc-pvc-305bce77-a15a-4b27-bef7-e58529eadace]# ll
总用量 0
[root@k8s5 default-nfs-pvc-pvc-305bce77-a15a-4b27-bef7-e58529eadace]# echo 123 > index.html
[root@k8s5 default-nfs-pvc-pvc-305bce77-a15a-4b27-bef7-e58529eadace]# ll
总用量 4
-rw-r--r--. 1 root root 4 9月  11 10:52 index.html
[root@k8s5 default-nfs-pvc-pvc-305bce77-a15a-4b27-bef7-e58529eadace]# [root@master01 traefik]# kubectl get pod -o wide
NAME                             READY   STATUS    RESTARTS   AGE    IP             NODE       NOMINATED NODE   READINESS GATES
nfs1-76f66b958-68wpl             1/1     Running   0          5d1h   10.244.2.173   node02     <none>           <none>
nginx-traefik-7c5f68df5b-9zxqc   1/1     Running   0          44m    10.244.1.242   node01     <none>           <none>
nginx-traefik-7c5f68df5b-fx46k   1/1     Running   0          44m    10.244.0.29    master01   <none>           <none>
nginx-traefik-7c5f68df5b-zjlzt   1/1     Running   0          44m    10.244.2.242   node02     <none>           <none>[root@master01 traefik]# curl 10.244.1.242
123[root@master01 traefik]# kubectl get svc -o wide -n kube-system 
NAME                      TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                       AGE    SELECTOR
kube-dns                  ClusterIP   10.96.0.10     <none>        53/UDP,53/TCP,9153/TCP        15d    k8s-app=kube-dns
traefik-ingress-service   NodePort    10.96.27.248   <none>        80:30789/TCP,8080:31818/TCP   134m   k8s-app=traefik-ingress-lb
traefik-web-ui            ClusterIP   10.96.45.60    <none>        80/TCP                        134m   k8s-app=traefik-ingress-lb
[root@master01 traefik]# curl www.xy102.com:30789
123

在这里插入图片描述

4、Deployment+nodeport----四个yaml文件都执行

[root@master01 ingress]# vim mandatory.yaml apiVersion: apps/v1
kind: Deployment
#kind: DaemonSet
metadata:name: nginx-ingress-controllernamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
spec:replicas: 1selector:matchLabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginxtemplate:metadata:labels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginxannotations:prometheus.io/port: "10254"prometheus.io/scrape: "true"spec:# wait up to five minutes for the drain of connectionsterminationGracePeriodSeconds: 300serviceAccountName: nginx-ingress-serviceaccountnodeSelector:kubernetes.io/os: linux
#      hostNetwork: true
#      nodeSelector:
#        ingress: "true"[root@master01 ingress]# kubectl apply -f mandatory.yaml [root@master01 ingress]# vim service-nodeport.yaml apiVersion: v1
kind: Service
metadata:name: ingress-nginxnamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
spec:type: NodePortports:- name: httpport: 80targetPort: 80protocol: TCP- name: httpsport: 443targetPort: 443protocol: TCPselector:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx[root@master01 ingress]# kubectl apply -f service-nodeport.yaml[root@master01 ingress]# mkdir traefik
[root@master01 ingress]# cd traefik/
[root@master01 traefik]# pwd
/opt/ingress/traefik----------------
wget  https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-deployment.yaml
wget  https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-rbac.yaml
wget  https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-ds.yaml
wget  https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/ui.yaml
----------------
[root@master01 traefik]# ll
总用量 16
-rw-r--r--. 1 root root 1114 9月  11 10:26 traefik-deployment.yaml
-rw-r--r--. 1 root root 1294 9月  11 10:26 traefik-ds.yaml
-rw-r--r--. 1 root root  788 9月  11 10:26 traefik-rbac.yaml
-rw-r--r--. 1 root root  471 9月  11 10:27 ui.yaml[root@master01 traefik]# kubectl apply -f traefik-rbac.yaml 
[root@master01 traefik]# kubectl apply -f traefik-deployment.yaml 
[root@master01 traefik]# kubectl apply -f ui.yaml [root@master01 traefik]# vim traefik-nginx1.yaml kind: Deployment
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: nfs-pvc
spec:accessModes:- ReadWriteManystorageClassName: nfs-client-storageclassresources:requests:storage: 2Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-traefiklabels:app1: nginx1
spec:replicas: 3selector:matchLabels:app1: nginx1template:metadata:labels:app1: nginx1spec:containers:- name: nginx1image: nginx:1.22ports:- containerPort: 80volumeMounts:- name: nfs-pvcmountPath: /usr/share/nginx/htmlvolumes:- name: nfs-pvcpersistentVolumeClaim:claimName: nfs-pvc
---
apiVersion: v1
kind: Service
metadata:name: nginx-traefik-svc
spec:type: ClusterIPports:- protocol: TCPport: 80targetPort: 80selector:app1: nginx1
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: nginx-traefik-ingressannotations:
#设置认证的类型::
#    nginx.ingress.kubernetes.io/auth-type: basic
#设置认证的secret的名称
#    nginx.ingress.kubernetes.io/auth-secret: basic-auth
#设置认证窗口的提示信息
#    nginx.ingress.kubernetes.io/auth-realm: 'wo ai zhailiming'
#    nginx.ingress.kubernetes.io/rewrite-target: https://www.xy102.com
#设定重定向流量的目标连接
spec:rules:- host: www.xy102.comhttp:paths:- path: /pathType: Prefix
#前缀匹配,匹配/ /test1 /test1/test2backend:
#匹配的svc的名称----podservice:name: nginx-traefik-svcport:number: 80[root@master01 traefik]# kubectl get pod -o wide
NAME                             READY   STATUS    RESTARTS   AGE    IP             NODE       NOMINATED NODE   READINESS GATES
nfs1-76f66b958-68wpl             1/1     Running   0          5d3h   10.244.2.173   node02     <none>           <none>
nginx-traefik-849b6f9457-5cj9x   1/1     Running   0          16m    10.244.1.244   node01     <none>           <none>
nginx-traefik-849b6f9457-jmznh   1/1     Running   0          16m    10.244.0.31    master01   <none>           <none>
nginx-traefik-849b6f9457-kj2rx   1/1     Running   0          16m    10.244.2.245   node02     <none>   [root@master01 traefik]# kubectl get svc -o wide -n kube-system 
NAME                      TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                       AGE     SELECTOR
kube-dns                  ClusterIP   10.96.0.10     <none>        53/UDP,53/TCP,9153/TCP        15d     k8s-app=kube-dns
traefik-ingress-service   NodePort    10.96.27.248   <none>        80:30789/TCP,8080:31818/TCP   3h15m   k8s-app=traefik-ingress-lb
traefik-web-ui            ClusterIP   10.96.45.60    <none>        80/TCP                        3h14m   k8s-app=traefik-ingress-lb[root@k8s5 k8s]# ll
总用量 0
drwxrwxrwx. 2 root root 6 9月  11 13:35 default-nfs-pvc-pvc-667cee0d-c02c-421c-b850-1b9087c2c02d
[root@k8s5 k8s]# cd default-nfs-pvc-pvc-667cee0d-c02c-421c-b850-1b9087c2c02d/
[root@k8s5 default-nfs-pvc-pvc-667cee0d-c02c-421c-b850-1b9087c2c02d]# ls
[root@k8s5 default-nfs-pvc-pvc-667cee0d-c02c-421c-b850-1b9087c2c02d]# [root@master01 traefik]# curl www.xy102.com
curl: (7) Failed connect to www.xy102.com:80; 拒绝连接
[root@master01 traefik]# curl www.xy102.com:30789
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.22.1</center>
</body>
</html>[root@k8s5 k8s]# ll
总用量 0
drwxrwxrwx. 2 root root 6 9月  11 13:35 default-nfs-pvc-pvc-667cee0d-c02c-421c-b850-1b9087c2c02d
[root@k8s5 k8s]# cd default-nfs-pvc-pvc-667cee0d-c02c-421c-b850-1b9087c2c02d/
[root@k8s5 default-nfs-pvc-pvc-667cee0d-c02c-421c-b850-1b9087c2c02d]# ls
[root@k8s5 default-nfs-pvc-pvc-667cee0d-c02c-421c-b850-1b9087c2c02d]# echo 123 > index.html
[root@k8s5 default-nfs-pvc-pvc-30489c95-7b49-4f10-b139-b5942d1a3fc1]# mkdir test1
[root@k8s5 default-nfs-pvc-pvc-30489c95-7b49-4f10-b139-b5942d1a3fc1]# cd test1/
[root@k8s5 test1]# echo 456 > index.html
[root@k8s5 test1]# mkdir test2
[root@k8s5 test1]# cd test2/
[root@k8s5 test2]# ls
[root@k8s5 test2]# echo 789 > index.html[root@master01 traefik]# curl -L www.xy102.com:30733
123
[root@master01 traefik]# curl -L www.xy102.com:30733/test1
curl: (7) Failed connect to www.xy102.com:80; 拒绝连接
[root@master01 traefik]# vim traefik-nginx1.yaml 
[root@master01 traefik]# kubectl apply -f traefik-ds.yaml 
serviceaccount/traefik-ingress-controller unchanged
daemonset.apps/traefik-ingress-controller created
service/traefik-ingress-service configured
[root@master01 traefik]# curl -L www.xy102.com:30733/test1
curl: (7) Failed connect to www.xy102.com:30733; 拒绝连接
[root@master01 traefik]# curl -L www.xy102.com:30733/test1
curl: (7) Failed connect to www.xy102.com:30733; 拒绝连接
[root@master01 traefik]# kubectl get svc -n kube-system 
NAME                      TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                  AGE
kube-dns                  ClusterIP   10.96.0.10     <none>        53/UDP,53/TCP,9153/TCP   16d
traefik-ingress-service   ClusterIP   10.96.231.58   <none>        80/TCP,8080/TCP          21m
traefik-web-ui            ClusterIP   10.96.119.46   <none>        80/TCP                   21m
[root@master01 traefik]# kubectl apply -f traefik-rbac.yaml 
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/traefik-ingress-controller unchanged
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/traefik-ingress-controller unchanged
[root@master01 traefik]# kubectl apply -f traefik-deployment.yaml 
serviceaccount/traefik-ingress-controller unchanged
deployment.apps/traefik-ingress-controller unchanged
service/traefik-ingress-service configured
[root@master01 traefik]# kubectl apply -f ui.yaml 
service/traefik-web-ui unchanged
Warning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
ingress.extensions/traefik-web-ui configured
[root@master01 traefik]# kubectl get svc -n kube-system 
NAME                      TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                       AGE
kube-dns                  ClusterIP   10.96.0.10     <none>        53/UDP,53/TCP,9153/TCP        16d
traefik-ingress-service   NodePort    10.96.231.58   <none>        80:31767/TCP,8080:32510/TCP   22m
traefik-web-ui            ClusterIP   10.96.119.46   <none>        80/TCP                        22m
[root@master01 traefik]# curl -L www.xy102.com:31767
123
[root@master01 traefik]# curl -L www.xy102.com:31767/test1
456
[root@master01 traefik]# ls
traefik-deployment.yaml  traefik-nginx1.yaml  ui.yaml
traefik-ds.yaml          traefik-rbac.yaml
[root@master01 traefik]# curl -L www.xy102.com:31767/test1/test2
789

在这里插入图片描述

在这里插入图片描述

三、ingress的总结+项目部署

ingress: 对外提供访问:

ingress----根据servicename选择service-----service把服务把请求根据匹配的标签转发pod。

支持http 80 https 443

deployment+NodePort

daemonset+hostnetwork

ingress-traefik

ingress-nginx

四、作业

1、Deployment+nodeport----四个yaml文件都执行

[root@master01 ingress]# cd traefik/
[root@master01 traefik]# ls
traefik-deployment.yaml  traefik-nginx1.yaml  ui.yaml
traefik-ds.yaml          traefik-rbac.yaml
[root@master01 traefik]# vim traefik-
[root@master01 traefik]# vim traefik-deployment.yaml 
[root@master01 traefik]# ls
traefik-deployment.yaml  traefik-nginx1.yaml  ui.yaml
traefik-ds.yaml          traefik-rbac.yaml
[root@master01 traefik]# vim traefik-nginx1.yaml 
[root@master01 traefik]# ls
traefik-deployment.yaml  traefik-nginx1.yaml  ui.yaml
traefik-ds.yaml          traefik-rbac.yaml
[root@master01 traefik]# kubectl apply -f traefik-rbac.yaml 
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/traefik-ingress-controller unchanged
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/traefik-ingress-controller unchanged
[root@master01 traefik]# kubectl get svc -n kube-system 
NAME                      TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                       AGE
kube-dns                  ClusterIP   10.96.0.10     <none>        53/UDP,53/TCP,9153/TCP        16d
traefik-ingress-service   NodePort    10.96.231.58   <none>        80:31767/TCP,8080:32510/TCP   71m
traefik-web-ui            ClusterIP   10.96.119.46   <none>        80/TCP                        71m
[root@master01 traefik]# kubectl get pod -o wide
NAME                   READY   STATUS    RESTARTS   AGE    IP             NODE     NOMINATED NODE   READINESS GATES
nfs1-76f66b958-68wpl   1/1     Running   0          5d5h   10.244.2.173   node02   <none>           <none>
[root@master01 traefik]# vim traefik-nginx1.yaml 
[root@master01 traefik]# cd ..
[root@master01 ingress]# ls
auth  https  ingress-nginx1.yaml  mandatory.yaml  service-nodeport.yaml  traefik
[root@master01 ingress]# cd traefik/
[root@master01 traefik]# kubectl apply -f traefik-nginx1.yaml 
persistentvolumeclaim/nfs-pvc created
deployment.apps/nginx-traefik created
service/nginx-traefik-svc created
ingress.networking.k8s.io/nginx-traefik-ingress created
[root@master01 traefik]# kubectl get pod -o wide
NAME                             READY   STATUS    RESTARTS   AGE    IP             NODE       NOMINATED NODE   READINESS GATES
nfs1-76f66b958-68wpl             1/1     Running   0          5d5h   10.244.2.173   node02     <none>           <none>
nginx-traefik-64f4cf4c65-cr6m8   1/1     Running   0          7s     10.244.1.251   node01     <none>           <none>
nginx-traefik-64f4cf4c65-ls2j8   1/1     Running   0          7s     10.244.0.38    master01   <none>           <none>
nginx-traefik-64f4cf4c65-qxmt7   1/1     Running   0          7s     10.244.2.254   node02     <none>           <none>
[root@master01 traefik]# kubectl get svc -n kube-system 
NAME                      TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                       AGE
kube-dns                  ClusterIP   10.96.0.10     <none>        53/UDP,53/TCP,9153/TCP        16d
traefik-ingress-service   NodePort    10.96.231.58   <none>        80:31767/TCP,8080:32510/TCP   76m
traefik-web-ui            ClusterIP   10.96.119.46   <none>        80/TCP                        76m
[root@master01 traefik]# curl www.xy102.com
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.22.1</center>
</body>
</html>[root@k8s5 k8s]# ll
总用量 0
drwxrwxrwx. 2 root root 6 9月  11 15:57 default-nfs-pvc-pvc-b63d202e-f6f6-4078-b765-f05bf2d3f777
[root@k8s5 k8s]# cd default-nfs-pvc-pvc-b63d202e-f6f6-4078-b765-f05bf2d3f777/
[root@k8s5 default-nfs-pvc-pvc-b63d202e-f6f6-4078-b765-f05bf2d3f777]# echo 123 > index.html
[root@k8s5 default-nfs-pvc-pvc-b63d202e-f6f6-4078-b765-f05bf2d3f777]# ls
index.html
[root@k8s5 default-nfs-pvc-pvc-b63d202e-f6f6-4078-b765-f05bf2d3f777]# mkdir test1
[root@k8s5 default-nfs-pvc-pvc-b63d202e-f6f6-4078-b765-f05bf2d3f777]# cd test1/
[root@k8s5 test1]# echo 456 > index.html
[root@k8s5 test1]# mkdir test2
[root@k8s5 test1]# cd test2/
[root@k8s5 test2]# echo 789 > index.html[root@master01 traefik]# curl www.xy102.com
123
[root@master01 traefik]# curl www.xy102.com
123
[root@master01 traefik]# curl www.xy102.com:31767
123
[root@master01 traefik]# curl www.xy102.com:31767/test1
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.22.1</center>
</body>
</html>
[root@master01 traefik]# curl -L www.xy102.com:31767/test1
456
[root@master01 traefik]# curl -L www.xy102.com:31767/test1/test2
789

2、DaemonSet+hostPort----三个yaml文件都执行

[root@master01 traefik]# vim traefik-nginx1.yaml apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: nfs-pvc
spec:accessModes:- ReadWriteManystorageClassName: nfs-client-storageclassresources:requests:storage: 2Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-traefiklabels:app1: nginx
spec:replicas: 3selector:matchLabels:app1: nginxtemplate:metadata:labels:app1: nginxspec:containers:- name: nginximage: nginx:1.22ports:- containerPort: 80volumeMounts:- name: nfs-pvcmountPath: /usr/share/nginx/htmlvolumes:- name: nfs-pvcpersistentVolumeClaim:claimName: nfs-pvc
---
apiVersion: v1
kind: Service
metadata:name: nginx-traefik-svc
spec:type: ClusterIPports:- protocol: TCPport: 80targetPort: 80selector:app1: nginx
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: nginx-traefik-ingressannotations:
#设置认证的类型::
#    nginx.ingress.kubernetes.io/auth-type: basic
#设置认证的secret的名称
#    nginx.ingress.kubernetes.io/auth-secret: basic-auth
#设置认证窗口的提示信息
#    nginx.ingress.kubernetes.io/auth-realm: 'wo ai zhailiming'
#    nginx.ingress.kubernetes.io/rewrite-target: https://www.xy102.com
#设定重定向流量的目标连接
spec:rules:- host: www.xy102.comhttp:paths:- path: /pathType: Prefix
#前缀匹配,匹配/ /test1 /test1/test2backend:
#匹配的svc的名称----podservice:name: nginx-traefik-svcport:number: 80[root@master01 traefik]# kubectl apply -f traefik-nginx1.yaml wget  https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-deployment.yaml
wget  https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-rbac.yaml
wget  https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-ds.yaml
wget  https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/ui.yaml[root@master01 traefik]# kubectl apply -f traefik-rbac.yaml 
[root@master01 traefik]# kubectl apply -f traefik-deployment.yaml 
[root@master01 traefik]# kubectl apply -f ui.yaml [root@master01 traefik]# kubectl get svc -n kube-system 
NAME                      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                       AGE
kube-dns                  ClusterIP   10.96.0.10      <none>        53/UDP,53/TCP,9153/TCP        16d
traefik-ingress-service   NodePort    10.96.251.209   <none>        80:31552/TCP,8080:30058/TCP   3m33s
traefik-web-ui            ClusterIP   10.96.71.175    <none>        80/TCP                        23s[root@master01 traefik]# curl -L www.xy102.com
curl: (7) Failed connect to www.xy102.com:80; 拒绝连接
[root@master01 traefik]# curl -L www.xy102.com:31552
123
[root@master01 traefik]# curl -L www.xy102.com:31552/test1
curl: (7) Failed connect to www.xy102.com:80; 拒绝连接

在这里插入图片描述

[root@master01 traefik]# kubectl apply -f traefik-ds.yaml [root@master01 traefik]# kubectl apply -f traefik-rbac.yaml [root@master01 traefik]# kubectl apply -f ui.yaml [root@master01 traefik]# kubectl get svc -n kube-system 
NAME                      TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                  AGE
kube-dns                  ClusterIP   10.96.0.10     <none>        53/UDP,53/TCP,9153/TCP   16d
traefik-ingress-service   ClusterIP   10.96.201.30   <none>        80/TCP,8080/TCP          39s
traefik-web-ui            ClusterIP   10.96.71.175   <none>        80/TCP                   16m
[root@master01 traefik]# curl -L www.xy102.com:30023/test1
curl: (7) Failed connect to www.xy102.com:30023; 拒绝连接
[root@master01 traefik]# curl -L www.xy102.com/test1
456
[root@master01 traefik]# curl -L www.xy102.com/test1/test2
789
[root@master01 traefik]# curl -L www.xy102.com
123##发现只要apply-------traefik-ds.yaml----------traefik-rbac.yaml-----------------ui.yaml

3、Deployment+nodeport

[root@master01 traefik]# kubectl apply -f traefik-ds.yaml 
serviceaccount/traefik-ingress-controller unchanged
daemonset.apps/traefik-ingress-controller unchanged
service/traefik-ingress-service unchanged
[root@master01 traefik]# kubectl apply -f traefik-rbac.yaml 
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/traefik-ingress-controller unchanged
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/traefik-ingress-controller unchanged
[root@master01 traefik]# kubectl apply -f traefik-deployment.yaml 
serviceaccount/traefik-ingress-controller unchanged
deployment.apps/traefik-ingress-controller created
service/traefik-ingress-service configured
[root@master01 traefik]# kubectl apply -f ui.yaml 
service/traefik-web-ui unchanged
Warning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
ingress.extensions/traefik-web-ui configured
[root@master01 traefik]# kubectl get svc -n kube-system 
NAME                      TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                       AGE
kube-dns                  ClusterIP   10.96.0.10     <none>        53/UDP,53/TCP,9153/TCP        16d
traefik-ingress-service   NodePort    10.96.201.30   <none>        80:31318/TCP,8080:32115/TCP   9m38s
traefik-web-ui            ClusterIP   10.96.71.175   <none>        80/TCP                        25m
[root@master01 traefik]# curl -L www.xy102.com
123
[root@master01 traefik]# curl -L www.xy102.com/test1
456
[root@master01 traefik]# curl -L www.xy102.com/test1/test2
789
[root@master01 traefik]# curl -L www.xy102.com:31318
123
[root@master01 traefik]# curl -L www.xy102.com:31318/test1
456
[root@master01 traefik]# curl -L www.xy102.com:31318/test1/test2
789

is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
ingress.extensions/traefik-web-ui configured
[root@master01 traefik]# kubectl get svc -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.96.0.10 53/UDP,53/TCP,9153/TCP 16d
traefik-ingress-service NodePort 10.96.201.30 80:31318/TCP,8080:32115/TCP 9m38s
traefik-web-ui ClusterIP 10.96.71.175 80/TCP 25m
[root@master01 traefik]# curl -L www.xy102.com
123
[root@master01 traefik]# curl -L www.xy102.com/test1
456
[root@master01 traefik]# curl -L www.xy102.com/test1/test2
789
[root@master01 traefik]# curl -L www.xy102.com:31318
123
[root@master01 traefik]# curl -L www.xy102.com:31318/test1
456
[root@master01 traefik]# curl -L www.xy102.com:31318/test1/test2
789



http://www.ppmy.cn/embedded/112298.html

相关文章

国产视频转换HDMI1.4转单/双MIPI DSI/CSI LT6911C芯片方案,带音频输出,QFN64封装 Lontium

LT6911C:HDMI 1.4 TO MIPI DSI/CSI 芯片简介&#xff1a; LT6911C是一款高性能的HDMI1.4转换器MIPI DSI/CSI芯片用于VR/智能手机/显示应用。对于MIPI DSI/CSI输出&#xff0c;LT6911C功能可配置单端口或双端口MIPIDSI/CSI 1高速时钟通道和1~4个高速数据通道最大1.5Gb/s/lane&am…

如何在Oracle中实现数据的加密

在Oracle数据库中实现数据加密是一项重要的安全措施&#xff0c;它可以保护存储在数据库中的敏感信息不被未授权访问。Oracle提供了多种数据加密方法&#xff0c;包括透明数据加密&#xff08;TDE&#xff09;、列级加密和使用内置加密函数等。以下是一些在Oracle中实现数据加密…

通过SQLAlchemy存储聊天历史:使用Python轻松管理对话数据

# 引言在现代应用中&#xff0c;管理和存储聊天历史是一个重要的功能。特别是在需要持久化用户与AI对话的场景中&#xff0c;正确使用数据库工具能极大提高效率。本文将探讨如何使用SQLAlchemy和LangChain来构建一个灵活的聊天历史存储系统。# 主要内容## SQLAlchemy简介SQLAlc…

算子加速(3):自定义cuda扩展

需要自定义某个层,或有时候用c++实现你的操作(c++扩展)可能会更好: 例如:需要实现一个新型的激活函数例如: bevfusion用cuda实现bevpool加速自定义扩展的步骤 (1) 首先用纯pytorch和python 实现我们所需的功能,看看效果再决定要不要进一步优化(2) 明确优化方向,用C++ (或CU…

2. 变量和指令(omron 机器自动化控制器)——1

机器自动化控制器——第二章 变量和指令 1 2-1 变量一览表MC通用变量轴变量▶ 轴组变量 运动控制指令的输入变量输入变量的有效范围▶ 枚举体一览表 运动控制指令的输出变量运动控制指令的输入输出变量 2-1 变量一览表 MC功能模块使用的变量分为两类。 一类是监视轴等的状态及…

3.js - 着色器设置点材质(螺旋星系特效)

上图 着色器设置点材质时&#xff0c;在顶点着色器中&#xff0c;最好设置gl_PointSize&#xff0c;不然看不到你在页面中添加的点 main.js import * as THREE from three import { OrbitControls } from three/examples/jsm/controls/OrbitControlsimport gsap from gsapimp…

【AWDP】 AWDP 赛制详解应对方法赛题实践 量大管饱

文章首发于【先知社区】&#xff1a;https://xz.aliyun.com/t/15535 一、AWDP概述 AWDP是什么 AWDP是一种综合考核参赛团队攻击、防御技术能力、即时策略的攻防兼备比赛模式。每个参赛队互为攻击方和防守方&#xff0c;充分体现比赛的实战性、实时性和对抗性&#xff0c;对参…

闭包+面试真题

对闭包的理解 闭包是内层函数使用外层变量 (子级可以访问父级的变量&#xff0c;但是父级不可以访问子级的) 闭包是指有权访问另一个函数作用域中变量的函数&#xff0c;创建闭包的最常见的方式就是在一个函数内创建另一个函数&#xff0c;创建的函数可以访问到当前函数的局部…