Pokazywanie postów oznaczonych etykietą Kubernetes. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą Kubernetes. Pokaż wszystkie posty

piątek, 13 czerwca 2025

Kubernetes init database in a dirty way

Sometimes you need a dirty trick initialized the kubernetes running database with a set of sql statements (ddls). Here you are a simple example.

init-db:
kubectl exec -i --namespace timescaledb svc/csdb -c timescaledb -- \
        psql -U postgres -d cas --file=- <ddls/00-init-database.sql;

init-db-tables:
@for f in $(shell ls ddls/$(FILE_PREFIX)*.sql | grep -v 00-init); do \
kubectl exec -i --namespace timescaledb svc/csdb -c timescaledb -- \
            psql -U postgres -d cas --file=- <$$f; \
done


But what if you would like to variables in your script.

Here's a solution:


bash -c "cat <<EOF
$(< ddls/00-init-database.sql_tpl)
EOF" | kubectl exec -i --tty --namespace timescaledb ${PGMASTERPOD} -c timescaledb -- \
    psql -U postgres -d ${DBNAME}

piątek, 7 kwietnia 2023

How to intercept requests with lua script in Istio service mesh

Add custom header to the responses comming out the given Kubernetes application

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: add-custom-header-envoyfilter
  namespace: istio-system
spec:
  workloadSelector:
    labels:
      app: httpd-proxy
  configPatches:
  - applyTo: HTTP_FILTER
    match:
      context: SIDECAR_INBOUND
      listener:
        # portNumber: 80
        filterChain:
          filter:
            name: envoy.filters.network.http_connection_manager
            subFilter:
              name: envoy.filters.http.router
    patch:
      operation: INSERT_BEFORE
      value:
        name: envoy.filters.http.lua
        typed_config:
          "@type": "type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua"
          inlineCode: |
            function envoy_on_response(response_handle)
              response_handle:logDebug("Adding custom header to the response")
              response_handle:headers():add("X-Custom-Header", "1.2.3.4")
            end

The above works on the response. To modify the Lua `inlineCode` should look like this
  function envoy_on_request(request_handle)
  	request_handle:logInfo("Received request for " .. request_handle:headers():get(":path"))
  end
Note that by default the istio logging level is set to warning. You need to turn loggin to info to make it appear in the log file.