我是靠谱客的博主 暴躁夕阳,最近开发中收集的这篇文章主要介绍k8s学习(十七)使用traefik做服务网关前言一、Ingress的方式二、IngressRoute的方式,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目录

  • 前言
  • 一、Ingress的方式
  • 二、IngressRoute的方式


前言

可以使用Ingress或者IngressRoute结合traefik做服务的网关。

一、Ingress的方式

1、创建默认服务

(1)default-backend.yaml

[root@k8s-master 2]# cat default-backend.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: default-http-backend
  labels:
    app: default-http-backend
    namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: default-http-backend
  template:
    metadata:
      labels:
        app: default-http-backend
    spec:
      terminationGracePeriodSeconds: 60
      containers:
      - name: default-http-backend
        # Any image is permissible as long as:
        # 1. It serves a 404 page at /
        # 2. It serves 200 on a /healthz endpoint
        image: registry.cn-hangzhou.aliyuncs.com/google_containers/defaultbackend:1.4
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 30
          timeoutSeconds: 5
        ports:
        - containerPort: 8080
        resources:
          limits:
            cpu: 10m
            memory: 20Mi
          requests:
            cpu: 10m
            memory: 20Mi
---

apiVersion: v1
kind: Service
metadata:
  name: default-http-backend
  namespace: default
  labels:
    app: default-http-backend
spec:
  ports:
  - port: 80
    targetPort: 8080
  selector:
    app: default-http-backend

(2)创建

[root@k8s-master 2]# kubectl create -f default-backend.yaml
deployment.apps/default-http-backend created
service/default-http-backend created

2、创建应用服务

这里用nginx代替应用服务

(1)nginx-service.yaml

[root@k8s-master 2]# cat nginx-service.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    app: nginx-deployment-test-04
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 32001
  selector:
    app: nginx-deployment-test-04
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment-04
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-deployment-test-04
  template:
    metadata:
      labels:
        app: nginx-deployment-test-04
    spec:
      containers:
      - name: nginx-deployment-test-04
        image: nginx:1.20
        ports:
        - containerPort: 80

(2)创建

[root@k8s-master 2]# kubectl create -f nginx-service.yaml
service/nginx-service created
deployment.apps/nginx-deployment-04 created

3、创建Ingress

(1)nginx-ingress.yaml

[root@k8s-master 2]# cat nginx-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress-01
  annotations:
    kubernetes.io/ingress.class: traefik-v2.5
spec:
  defaultBackend:
    service:
      name : default-http-backend
      port:
        number: 80
  rules:
    - http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name : nginx-service
                port:
                  number: 80

      host: www.iscas.com

(2)创建

[root@k8s-master 2]# kubectl create -f nginx-ingress.yaml
ingress.networking.k8s.io/test-ingress-01 created

4、配置ip域名

172.16.10.158   www.iscas.com

5、访问

http://www.iscas.com/

在这里插入图片描述

二、IngressRoute的方式

1、删除上面创建的Ingress

[root@k8s-master 2]# kubectl delete -f nginx-ingress.yaml
ingress.networking.k8s.io "test-ingress-01" deleted

2、创建IngressRoute

(1)nginx-ingressroute.yaml

[root@k8s-master 2]# cat nginx-ingressroute.yaml
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: nginx-route
  annotations:
    kubernetes.io/ingress.class: traefik-v2.5
spec:
  entryPoints:
    - web
  routes:
  - match: Host(`www.iscas.com`) && PathPrefix(`/`)
    kind: Rule
    services:
    - name: nginx-service
      port: 80

(2)创建

[root@k8s-master 2]# kubectl create -f nginx-ingressroute.yaml
ingressroute.traefik.containo.us/nginx-route created

3、访问

访问 http://www.iscas.com/ ,同样能够访问到nginx服务

最后

以上就是暴躁夕阳为你收集整理的k8s学习(十七)使用traefik做服务网关前言一、Ingress的方式二、IngressRoute的方式的全部内容,希望文章能够帮你解决k8s学习(十七)使用traefik做服务网关前言一、Ingress的方式二、IngressRoute的方式所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部