19 Aralık 2022 Pazartesi

Kubernetes kind: PodSecurityPolicy - Namespace Level

Giriş
Açıklaması şöyle
Previously, there was PodSecurityPolicy (PSP) but from Kubernetes v1.21, PodSecurityPolicy was deprecated and removed from Kubernetes in v1.25.
Yeni Yöntem
Söz dizimi şöyle
# Mandatory
pod-security.kubernetes.io/<MODE>: <LEVEL> 

# Optional        
pod-security.kubernetes.io/<MODE>-version: <VERSION>
Level Ne Demek
Açıklaması şöyle. Kısıtlamalar azdan çoğa doğru sıralı
we have to define one of the pod security standards. Currently, there are three pod security standards available.

● Privileged—Unrestricted policy, providing the widest possible level of permissions.

● Baseline — Minimally restrictive policy which prevents known privilege escalations. There is a comprehensive list of controls that should be enforced or disallowed. For instance, Privileged Pods must be disallowed if we want to create a pod in the Baseline labeled namespace.

● Restricted — Heavily restricted policy, the main purpose of this policy is to follow current pod hardening best practices. Similar to Baseline standards there is a list of controls that should be enforced or disallowed. For instance, no container can have root user permissions. Containers must be run as non-root users.
Mode Ne Demek?
Açıklaması şöyle. Enforce ise eğer bir Level ihlal edilirse Pod reddedilir. Audit veya Warn ise Pod reddedilmez ve çalışır ama uyarı verilir
● enforce — Policy violations will cause the pod to be rejected.

● audit — Policy violations will trigger the addition of an audit annotation to the event recorded in the audit log, but are otherwise allowed.

● warn — Policy violations will trigger a user-facing warning but are otherwise allowed.
Baseline Level
Pod şunu yaparsa Baseline kabul edilir
    securityContext:
       privileged: true
Restricted Level
Pod şunu yaparsa Restricted kabul edilir
    securityContext:
      runAsUser: 0

Örnek - Baseline + enforce -  Yani Privileged Pod Reject
Elimizde şöyle bir namespace ayarı olsun
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: "2022-11-08T09:26:17Z"
  labels:
    kubernetes.io/metadata.name: baseline-ns-1
    pod-security.kubernetes.io/enforce: baseline
    pod-security.kubernetes.io/enforce-version: v1.25
  name: baseline-ns-1
  resourceVersion: "4779"
  uid: 2a8708f3-6f25-4cfb-84c1-ef6c825e23c5
spec:
  finalizers:
  - kubernetes
status:
  phase: Active
Açıklaması şöyle
Since we applied labels with “enforce” mode and “baseline” pod security standards, if a pod does not respect the “baseline” standard it will be rejected. 
Şu pod başarısız olur
# pod-1.yaml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: pod-1
  name: pod-1
  namespace: baseline-ns-1
spec:
  containers:
  - image: nginx
    name: pod-1
    securityContext:
       privileged: true
Hata şöyle
Error from server (Forbidden): error when creating "pod-1.yaml": pods "pod-1" is forbidden: violates PodSecurity "baseline:v1.25": privileged (container "pod-1" must not set securityContext.privileged=true)
Örnek - Baseline + Warn - Yani Sadece Log
Elimizde şöyle bir namespace ayarı olsun
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: "2022-11-08T09:26:17Z"
  labels:
    kubernetes.io/metadata.name: baseline-ns-1
    pod-security.kubernetes.io/warn: baseline
    pod-security.kubernetes.io/warn-version: v1.25
  name: baseline-ns-1
  resourceVersion: "4779"
  uid: 2a8708f3-6f25-4cfb-84c1-ef6c825e23c5
spec:
  finalizers:
  - kubernetes
status:
  phase: Active
Bu sefer çalışır. Çıktı şöyle
Warning: would violate PodSecurity "baseline:v1.25": privileged (container "pod-1" must not set securityContext.privileged=true) pod/pod-1 created
Örnek
Elimizde şöyle bir namespace ayarı olsun
apiVersion: v1
kind: Namespace
metadata:
  name: baseline-ns-2
  labels:
    # baseline Standard with enforce Mode 
    pod-security.kubernetes.io/enforce: baseline
    pod-security.kubernetes.io/enforce-version: v1.25
    # restricted Standard with warn Mode
    pod-security.kubernetes.io/warn: restricted
    pod-security.kubernetes.io/warn-version: v1.25
Şöyle yaparız
# pod-2.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-2
  namespace: baseline-ns-2
spec:
  containers:
  - name: pod-2
    image: nginx
    securityContext:
      runAsUser: 0
Pod çalışır. Çünkü root user kullansa bile restricted yani en sıkı kurallar sadece warn. Baseline root user'a izin verdiği için sıkıntı yok.

Eski Yöntem
Örnek
Şöyle yaparız. Böylece işletim sisteminin her türlü yeteneğine erişebilen privileged pod'lar yasaklanır
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: example
spec:
  privileged: false  # Don't allow privileged pods!

13 Aralık 2022 Salı

kind komutu - minikube Alternatifi

Giriş
Açıklaması şöyle
kind is a tool for running local Kubernetes clusters using Docker container “nodes”. kind was primarily designed for testing Kubernetes itself, but may be used for local development or CI.
Kurulum
Şöyle yaparız
brew install kind
Eğer podman ile kullanmak istiyorsak şöyle yaparız
export KIND_EXPERIMENTAL_PROVIDER=podman
create cluster
Örnek
Şöyle yaparız
kind create cluster
Örnek
Elimizde şöyle bir YAML olsun. 3 tane düğümlü bir cluster yaratır
# kind.config.yaml

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
name: api-server-discovery

nodes:
- role: control-plane
- role: worker
- role: worker
Şöyle yaparız
kind create cluster --config=kind.config.yaml
Silmek için şöyle yaparız
kind delete cluster --name api-server-discovery







8 Aralık 2022 Perşembe

bash auto-completion on Linux

Giriş
Tüm açıklamalar burada

1. bash autocompletion kurulur. Şöyle yaparız
apt-get install bash-completion
# veya
yum install bash-completion
Eğer kurmadan önce zaten kurulumu diye kontrol etmek istersek şöyle yaparız. Burada type komutu ile bir bash fonksiyonu aranıyor. Bulunursa bir çıktı verecektir.
type _init_completion

2. kubectl autocompletion etkinleştirilir
Şöyle yaparız
# User
echo 'source <(kubectl completion bash)' >>~/.bashrc

# veya System
kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/null
Daha sonra bir alias tanımlarız ve alias için auto-completion etkinleştirilir. Şöyle yaparız
# If you have an alias for kubectl, you can extend shell completion to work with that alias:
echo 'alias k=kubectl' >>~/.bashrc
echo 'complete -o default -F __start_kubectl k' >>~/.bashrc
Daha sonra "k -" yazıp tab tuşuna iki kere basınca kullanılabilecek seçenekler gösterilir.

Alias Kullanmak

.bash_rc ( her yeni kabuk için çalışır) veya .bash_profile (sadece login shell ile çalışır) dosyasına yerleştirilir.

Örnek
Bazı faydalı aliaslar şöyle
alias k='kubectl'
alias kc='k config view --minify | grep name'
alias kdp='kubectl describe pod'
alias krh='kubectl run --help | more'
alias ugh='kubectl get --help | more'
alias c='clear'
alias kd='kubectl describe pod'
alias ke='kubectl explain'
alias kf='kubectl create -f'
alias kg='kubectl get pods --show-labels'
alias kr='kubectl replace -f'
alias kh='kubectl --help | more'
alias krh='kubectl run --help | more'
alias ks='kubectl get namespaces'
alias l='ls -lrt'
alias ll='vi ls -rt | tail -1'
alias kga='k get pod --all-namespaces'
alias kgaa='kubectl get all --show-labels'

kubectl config set-context seçeneği - Context Güncelleme

Örnek
Varsayılan namespace'i atamak için şöyle yaparız
kubectl config set-context $(kubectl config current-context) --namespace=mynamespace


4 Aralık 2022 Pazar

helm search seçeneği - Search for a keyword in charts

Örnek
Şöyle yaparız
$ helm search repo webserver
--------------------------------------------------------------------
NAME                    CHART VERSION   APP VERSION     DESCRIPTION                
customrepo/webserver    0.1.0           1.16.0          A Helm chart for Kubernetes

$ helm install app websrever
--------------------------------------------------------------------
NAME: app
LAST DEPLOYED: Wed Nov  2 05:55:44 2022
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None

helm repo seçeneği - Add, List, Remove, Update, Index Chart Repositories

Giriş
1. Yeni bir repo add seçeneği ile eklenir
2. Repodaki chart güncellemelerini almak için update seçeneği kullanılır
2. Repodaki chartlara bakmak için list seçeneği kullanılır
3. Eğer bir chartlarda bir string aramak istersek search seçeneği kullanılır
Özet şöyle
helm repo list — List all repos your system connected with
helm repo add <Repo-Name> <URL> — will add helm repo to pull various chart from <URL>
helm search hub <search-text> — searches the Artifact Hub, which lists helm charts from dozens of different repositories.
helm search repo <search-text> — searches the repositories that you have added to your local helm client (with helm repo add).
helm repo remove <Repo-Name> — Will remove the added repo to helm client

add
Örnek
Şöyle yaparız
> helm repo add grafana https://grafana.github.io/helm-charts
> helm pull grafana/grafana --untar --untardir helm
index
Yerel bir dizini helm ile sunulacak hale getirir
Örnek
Şöyle yaparız
$ helm repo index /var/www/html/charts/
ls/list
Örnek
Şöyle yaparız
$ helm repo add customrepo http://172.31.3.183/charts/ --username admin --password Admin@123

$ helm repo ls
NAME            URL                        
customrepo      http://172.31.3.183/charts/
Örnek
Benim bilgisayarımdaki çıktı şöyle
> helm repo list
NAME    URL
grafana https://grafana.github.io/helm-charts
search seçeneği
Örnek
Şöyle yaparız
$ helm search repo eck
# Search will not just look for package name, but it will look for string in other fields
as well like description
update
Örnek
Şöyle yaparız
helm repo add jenkinsci https://charts.jenkins.io
helm repo update

Kubernetes 1.33 - Octarine

Giriş Yenilikler şöyle 1. Native Sidecar Containers 2. In-Place Pod Resizing: The End of Disruptive Scaling Artık pod'un belleğini çalış...