Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

Hướng dẫn sử dụng ingress nginx và control ingress bằng annotations trên k8s

Posted on June 10, 2021March 7, 2022 By nim No Comments on Hướng dẫn sử dụng ingress nginx và control ingress bằng annotations trên k8s

Links video:

Nếu theo như video thì cài ingress nginx thông qua helm.

Hoặc cách các file yaml qua đây:

https://xuanthulab.net/trien-khai-nginx-ingress-controller-trong-kubernetes.html

Links git trong video nằm ở đây:

https://github.com/justmeandopensource/kubernetes/tree/master/yamls/ingress-demo

Ta có 3 file deployment tượng trưng cho 3 web khách nhau:

nginx-deploy-main.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: nginx
  name: nginx-deploy-main
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx-main
  template:
    metadata:
      labels:
        run: nginx-main
    spec:
      containers:
      - image: nginx
        name: nginx
---

nginx-deploy-green.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: nginx
  name: nginx-deploy-green
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx-green
  template:
    metadata:
      labels:
        run: nginx-green
    spec:
      volumes:
      - name: webdata
        emptyDir: {}
      initContainers:
      - name: web-content
        image: busybox
        volumeMounts:
        - name: webdata
          mountPath: "/webdata"
        command: ["/bin/sh", "-c", 'echo "<h1>I am <font color=green>GREEN</font></h1>" > /webdata/index.html']
      containers:
      - image: nginx
        name: nginx
        volumeMounts:
        - name: webdata
          mountPath: "/usr/share/nginx/html"

---

nginx-deploy-blue.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: nginx
  name: nginx-deploy-blue
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx-blue
  template:
    metadata:
      labels:
        run: nginx-blue
    spec:
      volumes:
      - name: webdata
        emptyDir: {}
      initContainers:
      - name: web-content
        image: busybox
        volumeMounts:
        - name: webdata
          mountPath: "/webdata"
        command: ["/bin/sh", "-c", 'echo "<h1>I am <font color=blue>BLUE</font></h1>" > /webdata/index.html']
      containers:
      - image: nginx
        name: nginx
        volumeMounts:
        - name: webdata
          mountPath: "/usr/share/nginx/html"

Sau khi apply 3 file trên thì ra tạo nhanh service

Chúng ta đi qua 3 file ingress:

File đầu tiên thì cấu hình nginx rất bình thường:

ingress-resource-1.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-resource-1
spec:
  rules:
  - host: nginx.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-deploy-main
            port: 
              number: 80

File thứ 2 đặc biệt hơn là ingress sẽ route cho 3 trang web

ingress-resource-2.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-resource-2
spec:
  rules:
  - host: nginx.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-deploy-main
            port:
              number: 80
  - host: blue.nginx.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-deploy-blue
            port:
              number: 80
  - host: green.nginx.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-deploy-green
            port:
              number: 80

File thứ 3 ta sử dụng annotations để controller ingress nginx và đây là ví dụ sub path “/”

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
  name: ingress-resource-3
spec:
  rules:
  - host: nginx.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx-deploy-main
            port:
              number: 80
      - path: /blue
        pathType: Prefix
        backend:
          service:
            name: nginx-deploy-blue
            port:
              number: 80
      - path: /green
        pathType: Prefix
        backend:
          service:
            name: nginx-deploy-green
            port:
              number: 80

Nếu bạn đã sử dụng nginx bạn sẽ thấy dễ hiểu:

Thêm một số rewriting mà các bạn muốn tham khảo:

https://kubernetes.github.io/ingress-nginx/examples/rewrite/

Ingress

Post navigation

Previous Post: [Helm Chart] Learning Helm on Kubernetes in 60 minutes!!
Next Post: Các Lệnh query với Model của Laravel eloquent

More Related Articles

[Ingress Nginx] Internal error occurred: failed calling webhook “validate.nginx.ingress.kubernetes.io” Ingress
[Cert manager] Tìm hiểu cách sử cert-manager để create ssl cho website sử dụng ingress nginx. Ingress
[Ingress Nginx/MetalLB] Install Ingress Nginx trên K8s bằng helm Ingress
[ingress-nginx] Error=”ingress does not contain a valid IngressClass”, After upgrade ingress-nginx(helm 4.x.x, ingress-nginx/controller:v1.1.0) Ingress
[GRPC/Golang] Learning GRPC and Golang through the easy and actual lessons. Golang
[Ingress-nginx/GRPC] Let’s practice the load balancing with Ingress-Nginx and GRPC together Ingress

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [Monitoring] Understanding Different Types of Metrics in Monitoring Systems: Gauge, Counter, Histogram, and Summary December 6, 2023
  • [Golang/Protocol Buffers] Binarilize data by Proto Buf December 5, 2023
  • [Helm] Fail when running helm upgrade November 30, 2023
  • [AWS/EKS] Unlocking Simplicity and Security of Amazon EKS Pod Identity. November 30, 2023
  • [Gitleaks/njsscan/semgrep] Secure Your code by CAST and SAST tools November 27, 2023

Archives

  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2023 NimTechnology.