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
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[Cert manager] Tìm hiểu cách sử cert-manager để create ssl cho website sử dụng ingress nginx.

Posted on September 26, 2021March 6, 2023 By nim No Comments on [Cert manager] Tìm hiểu cách sử cert-manager để create ssl cho website sử dụng ingress nginx.

Contents

Toggle
  • 1) Letsencrypt
  • Using Let’s Encrypt in Production
  • 2) Self Signed Certificates on Kubernetes

1) Letsencrypt

Bài này thiên về việc tìm hiểu cert-manager hoạt động ntn.
Mô hình của mình sẽ giống mô hình chuẩn công ty hay nhiều ip public

Để thực hiện được bài này thì điều kiên quyết là bạn cận có 1 public ip và 1 port 80 nhé

Và đây là mô hình đang chạy của mình.
Kong đang giữ wildcard ssl

Vậy giờ mình muốn test cert-manager

Mình sẽ get cert ssl của letsencrypt nên chúng cần tìm hiểu “challenge-types” để nhận được cert ssl nhé
Link bên dưới:

https://letsencrypt.org/docs/challenge-types/
nếu bạn sử dụng google, aws, azure thì mình nghĩ là nên chơi DNS-01 challenge, tại sao đọc link trên và search thêm trên mạng
Còn mình sài on-premise thì mình chọn HTTP-01 challenge

Bạn có thể tưởng tượng là khi bạn đăng ký ssl cho trang nimtechnology.com thì letsencrypt sẽ check sem trang web này có tồn tại ngoài internet ko? đấy là HTTP-01 challenge

Chúng ta sẽ download các file yaml của cert-manager:

https://github.com/jetstack/cert-manager/releases

Tạo namespace:

Deploy thôi

show các components cert-manager

Giờ action cho workload thôi

Bạn cần có file ClusterIssuer và apply nhé, ko yêu cầu namespace.

apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  #Bạn có thể change ở đây
  name: letsencrypt-staging
spec:
  acme:
    server: https://acme-staging-v02.api.letsencrypt.org/directory
    #điền email của bạn vào
    email: mr.nim94@gmail.com
    privateKeySecretRef:
      #Bạn có thể change ở đây
      name: letsencrypt-staging
    solvers:
      - http01:
          ingress:
            class: nginx

Link dưới để bạn tìm hiều về ClusterIssuer
https://docs.cert-manager.io/en/release-0.8/tasks/issuers/index.html

Đăng ký tài khoản noip và tạo 1 domain

Xong mình lên Kong cấu hình cho gọi vào ingress-nginx, về Kong chắc mình hướng ở post khác he

Test truy cập ok, là web đã public port 80

Giờ bạn cấu hình deployment, service như bình thường nhưng ingress sẽ có thứ đặc biệt

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app
          image: wordpress:latest
          ports:
            - containerPort: 80
 
---

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - port: 80
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    #điền tên cluster issuer
    cert-manager.io/cluster-issuer: letsencrypt-staging
spec:
  rules:
    #điền domain của web dô
    - host: test-cert.ddnsking.com
      http:
        paths:
          - path: /
            backend:
              serviceName: my-service
              servicePort: 80
  tls:
    - hosts:
      #điền domain của web dô
      - test-cert.ddnsking.com
      #secretName bạn đặt j cũng được
      secretName: example-tls

Apply và sem điều j xảy ra

Nó tạo pod trên và xoá

Các thư trên là cert-manager tạo ra để recheck web site của bạn có pass được HTTP-01 challenge
Ở trên là mình chỉnh site domain, thì mới có thể thấy mấy resource nó tạo ra. Chứ nó nhanh lắm

Ví như bạn tạo chờ một lúc mà mấy cái trên hình ko mất đi thì bạn bị lỗi rồi đó.

Link dưới để debug lỗi he, tiện debug chơi nào
https://cert-manager.io/docs/faq/acme/

kubectl describe clusterissuer
check clusterissuer mà như hình là ok
kubectl describe certificaterequest
Như này là có vẫn đề rồi
kubectl describe challenge
Cluster k8s của bạn đăng ko thể phần giải tên miền này
Bạn thấy trong ingress sẽ có đường link thì kiểm tra có truy cập vào đường link đó được ko?
từ local và từ internet nhé
Rồi apply
Nếu đúng là ko thấy pod cm-amce nữa
KHông còn ingress check token nữa.
Lúc này thì ingress đã sử dụng cert được tạo trong secret
nó đây
kubectl describe certificates

Dùng command để test
wget --save-headers -O- test-cert.ddnsking.com

thấy có letsencrypt
Cũng chưa hiểu tại sao nó lại màu đỏ
Vì mình đia qua Kong nên đổi khi link check token bị 404
kiểm tra route trên kong và bật như ảnh

Này giờ bạn để ý config ở trên là dành cho môi trường Staging

https://www.cloudsavvyit.com/14069/how-to-install-kubernetes-cert-manager-and-configure-lets-encrypt/

theo như docx trên.

Using Let’s Encrypt in Production

Once you’ve successfully acquired a staging certificate, you can migrate to the Let’s Encrypt production servers. Staging certificates are valid but not trusted by browsers so you must get a production replacement before putting your site live.

It’s best to add a separate cluster issuer for the production server. You can then reference the appropriate issuer in each of your Ingress resources, depending on whether they’re production-ready.

Copy the issuer configuration shown above and change the name fields to letsencrypt-production. Next, replace the server URL with the value shown below:

https://acme-v02.api.letsencrypt.org/directory

Create the new issuer in your cluster:

kubectl create -f issuer-production.yml

Update your Ingress resource to request a production certificate by changing the value of the cert-manager.io/cluster-issuer annotation to letsencrypt-production (or the name you assigned to your own production issuer). Use kubectl to apply the change:

kubectl apply -f my-ingress.yaml

You should now have fully functioning HTTPS enabled for your Ingress resource. Cert-Manager will automatically manage your certificates and renew them before they expire.

change lại như ảnh
Giờ truy cập xanh lè òi

2) Self Signed Certificates on Kubernetes

Lúc đầu mình hay cấu hình như sau:

đây values khi mình khai báo ingress

khi mình apply:

Và ingress-nginx-controller cũng chửi:

W0305 18:44:20.056697       7 controller.go:1384] Error getting SSL certificate "openvscode-server/tls-oauth2-proxy-ingress": local SSL certificate openvscode-server/tls-oauth2-proxy-ingress was not found. Using default certificate
42
W0305 18:44:20.056732       7 controller.go:1384] Error getting SSL certificate "default/verdaccio-ingress": local SSL certificate default/verdaccio-ingress was not found. Using default certificate
41
W0305 18:44:20.056749       7 controller.go:1384] Error getting SSL certificate "default/tls-oauth2-proxy-ingress": local SSL certificate default/tls-oauth2-proxy-ingress was not found. Using default certificate
40
I0305 18:44:20.162683       7 admission.go:149] processed ingress via admission controller {testedIngressLength:6 testedIngressTime:0.107s renderingIngressLength:6 renderingIngressTime:0s admissionTime:54.3kBs testedConfigurationSize:0.107}
39
I0305 18:44:20.162737       7 main.go:100] "successfully validated configuration, accepting" ingress="oauth2-proxy/oauth2-proxy"
38
I0305 18:44:20.170000       7 event.go:285] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"oauth2-proxy", Name:"oauth2-proxy", UID:"2f227be7-65b3-4856-9dc6-6f8929c92d65", APIVersion:"networking.k8s.io/v1", ResourceVersion:"2860652", FieldPath:""}): type: 'Normal' reason: 'Sync' Scheduled for sync
37
W0305 18:44:20.170040       7 backend_ssl.go:47] Error obtaining X.509 certificate: no object matching key "oauth2-proxy/tls-oauth2-proxy-ingress" in local store
36
W0305 18:44:20.170692       7 controller.go:1384] Error getting SSL certificate "openvscode-server/tls-oauth2-proxy-ingress": local SSL certificate openvscode-server/tls-oauth2-proxy-ingress was not found. Using default certificate
35
W0305 18:44:20.170776       7 controller.go:1384] Error getting SSL certificate "default/verdaccio-ingress": local SSL certificate default/verdaccio-ingress was not found. Using default certificate
34
W0305 18:44:20.171130       7 controller.go:1384] Error getting SSL certificate "default/tls-oauth2-proxy-ingress": local SSL certificate default/tls-oauth2-proxy-ingress was not found. Using default certificate

Vậy chúng ta cần làm như sau:
https://cert-manager.io/docs/configuration/selfsigned/

apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
  labels:
    app: <app-name>
    app.kubernetes.io/instance: <app-name>
  name: selfsigned-issuer-<app-name>
  namespace: <app-name>
spec:
  selfSigned: {}
---
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  labels:
    app: <app-name>
    app.kubernetes.io/instance: <app-name>
  name: selfsigned-cluster-issuer-<app-name>
  namespace: <app-name>
spec:
  selfSigned: {}

Example:
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
  labels:
    app: oauth2-proxy
    app.kubernetes.io/instance: oauth2-proxy
  name: selfsigned-issuer-oauth2-proxy
  namespace: oauth2-proxy
spec:
  selfSigned: {}
---
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
  labels:
    app: oauth2-proxy
    app.kubernetes.io/instance: oauth2-proxy
  name: selfsigned-cluster-issuer-oauth2-proxy
  namespace: oauth2-proxy
spec:
  selfSigned: {}
Bạn cũng sẽ thấy đã có ClusterIssuer và Issuer
Ingress không còn lỗi nữa

trong khai bao ingress thì bạn chú ý chỗ này

Phần annotation bạn link tới Issuer:

annotations:
    cert-manager.io/issuer: selfsigned-issuer-oauth2-proxy
    cert-manager.io/issuer-kind: Issuer
secretName thì bạn đặt sao cũng được
Ingress

Post navigation

Previous Post: [Helm Chart] Tìm hiểu – (gạch giữa), _ (gạch dưới), dấu chấm “.” trong helm chart
Next Post: [OPA Gatekeeper] Sử dụng openpolicyagent để ngăn chặn việc apply yaml tuỳ tiện và sai lên kubernetes!

More Related Articles

[GRPC/Ingress-Nginx] Add annotation for ingress-nginx to work GRPC Ingress
[Ingress Nginx/MetalLB] Install Ingress Nginx trên K8s bằng helm Ingress
[ingress-nginx] ingress /contains invalid path Ingress
[Ingress Nginx] Internal error occurred: failed calling webhook “validate.nginx.ingress.kubernetes.io” Ingress
Hướng dẫn sử dụng ingress nginx và control ingress bằng annotations trên k8s Ingress
[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

  • [Azure] The subscription is not registered to use namespace ‘Microsoft.ContainerService’ May 8, 2025
  • [Azure] Insufficient regional vcpu quota left May 8, 2025
  • [WordPress] How to add a Dynamic watermark on WordPress. May 6, 2025
  • [vnet/Azure] VNet provisioning via Terraform. April 28, 2025
  • [tracetcp] How to perform a tracert command using a specific port. April 3, 2025

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • 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
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.