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
    • Helm Chart
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log & Monitor
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
  • 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

  • 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

[Istio] Workload injected Istio that can’t connect Database(Redis, PostgreSQL) – excludeOutboundPorts or excludeIPRanges Isito-EnvoyFilter
[Vault] Using Service Acount of Kubernetes to login Vault system. Kubernetes
[Terraform] – Terraform Beginner – Lesson 5: Terraform Provisioners and creating EC2 AWS - Amazon Web Service
[Kubernetes] Lesson8: k8s Easy – Service – Service account – ConfigMaps and Secrets Kubernetes
[Cilium/Hubble] Research together and install cilium instead of using kube-proxy and other CNI providers. Kubernetes
[GRPC] Checking GPRC Port by fullstorydev/grpcurl Coding

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

  • [Smartctl] Instruction check the health disk of Raspberry. January 16, 2023
  • [kubectl/Argocd] How to create a kubectl config file for serviceaccount or from the cluster secret of Argocd January 12, 2023
  • [Helm/Github] Create a public Helm chart repository with GitHub Pages January 8, 2023
  • [AWS] How to increase the disk size of a Windows EC2 machine? January 4, 2023
  • [Redis] ElastiCache-Redis Cross-Region Replication|Global DataStore January 3, 2023

Archives

  • 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
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • 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
    • Kubernetes
      • Ingress
    • Longhorn – Storage
    • Vault
    • VictoriaMetrics
  • Log & Monitor
    • ELK
      • Kibana
      • Logstash
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2023 NimTechnology.