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

[Sidecar/Kubernestes] Inject sidecar into a Pod automatically

Posted on March 13, 2022March 25, 2022 By nim No Comments on [Sidecar/Kubernestes] Inject sidecar into a Pod automatically

Sau khi sài istio thì mình thấy cách mà istio thêm container “istio=proxy” vào trong 1 pod của 1 workload.
Cái này khá là hay.
Mình thử mò trên mạng sem có anh nào demo làm được việc này ko?

Và bạn có thể tham khảo repo này!
https://github.com/morvencao/kube-sidecar-injector

Lý do mình chọn repo trên thì ảnh code bằng golang và đến thới điểm hiện tại (Sun, Mar 13th, 2022) thì mình thấy team của anh vần còn cập nhật!

Mình cũng chưa soi từng dòng code để hiểu họ run ntn?
Nhưng sẽ note 1 vài thứ để ý nếu chúng ta muốn custom như ý mình.

Ở file main.go

Liên quan service trong code file config service
namespace: thì lấy từ trong ENV OS
Giờ vào view log

Đầu tiên mình thấy chạy dòng này!

chúng ta có thể đặt tên cho web hook
này show trong file manifest
Chố này chắc chúng ta có thể thay.

Như mình hay chỉnh istio thì mình thấy có 2 phần này là quan trong:

Contents

Toggle
  • NamespaceSelector
  • ObjectSelector
    • LabelSelector
    • MatchExpressions
  • Annotation.

NamespaceSelector

NamespaceSelector decides whether to run the webhook on an object based on whether the namespace for that object matches the selector. If the object itself is a namespace, the matching is performed on object.metadata.labels. If the object is another cluster scoped resource, it never skips the webhook. For example, to run the webhook on any objects whose namespace is not associated with “runlevel” of “0” or “1”; you will set the selector as follows:

"namespaceSelector":
{
  "matchExpressions": [
    {
      "key": "runlevel",
      "operator": "NotIn",
      "values": [
        "0",
        "1"
      ]
    }
  ]
}

If instead you want to only run the webhook on any objects whose namespace is associated with the “environment” of “prod” or “staging”; you will set the selector as follows:

"namespaceSelector": 
{
  "matchExpressions": [
    {
      "key": "environment",
      "operator": "In",
      "values": [
        "prod",
        "staging"
      ]
    }
  ]
}

See https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/ for more examples of label selectors. Default to the empty LabelSelector, which matches everything. +optional NamespaceSelector on pkg.go.dev

Chỗ này chắc kiểm tra mutatingWebhookConfiguration
Nếu giống thì thực hiện update.

Giờ mình change text từ morven.me -> nimtechnology.com

Anh đó có chỉ lệnh make.
Nhưng mà gõ nó ko chạy!
Đại loại sau khi sửa xong thig bạn build image và push lên docker hub

>>>>>>
docker build -t nimtechnology/sidecar-injector:latest --force-rm -f Dockerfile .
>>>>>>
docker push nimtechnology/sidecar-injector:latest

Tiếp theo là deploy:
https://github.com/morvencao/kube-sidecar-injector/tree/master/deploy

Bạn cần sửa image trong file deployment.yaml
cho giống image bạn mới build

https://github.com/morvencao/kube-sidecar-injector/blob/master/deploy/deployment.yaml#L20

Giờ bạn tạo namespace mới và pod nằm trên namespace đó:

kubectl create ns test-ns
kubectl label namespace test-ns sidecar-injection=enabled

kubectl -n test-ns run alpine \
    --image=alpine \
    --restart=Never \
    --command -- sleep infinity

Bạn để ý pod sẽ được inject thêm container nginx

Bạn để ý configmap trong mấy file manifest ở thư mục deploy là sẽ hiểu
https://github.com/morvencao/kube-sidecar-injector/blob/master/deploy/configmap.yaml

trường hợp pod cứ ở namespace test-ns là inject cũng ko hay lắm!

ObjectSelector

Case của chúng ta phải có 2 điều kiện:
– workload ở trong ns đã được sidecar-injection=enabled
– workload cần có khai báo 1 label nào đó thì mới inject container.

LabelSelector

MatchExpressions

https://github.com/kubernetes/apiserver/blob/master/pkg/admission/plugin/webhook/testing/testcase.go#L374-L379

Một cách khác với label

nhưng mà đây vẫn là thêm vào label

https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#LabelSelectorOperator

Annotation.

chúng ta thấy thường các application istio, vault, …
thì khi chúng ta add annotion thì pod sẽ được inject-container.

https://github.com/morvencao/kube-sidecar-injector/blob/master/cmd/webhook.go#L75-L106

Mình check thì mặc định là sẽ vẫn inject container.

Còn nếu sidecar-injector-webhook.nimtechnology.com/inject là false or not or off or n: thì sẽ ko inject
bạn có thể sửa lại chỗ này nếu bạn cần custom liên quan đến annotation

Kubernetes, Uncategorized

Post navigation

Previous Post: [Lenses/kafka] Configure authentication and authorization for lenses
Next Post: [Jenkins] Using pod template to run Jenkins agent on Kubernetes.

More Related Articles

[KUBECONFIG] Tìm hiểu về –kubeconfig và –context trong kubeconfig để quan lý nhiều cluster k8s Kubernetes
[Jenkins] Lưu ý nho nhỏ trong jenkins Jenkins
[Metrics Server] Failed to make webhook authorizer request: the server could not find the requested resource Kubernetes
[Helmify] CLI that creates Helm charts from Kubernetes yamls. Helm Chart
[Network/Kubernetes] Latency is too hight Kubernetes
[Rancher/K8s] Install rancher on K8s by Helm 3 Kubernetes

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.