20 Eylül 2021 Pazartesi

stat metodu

Giriş
Şu satırı dahil ederiz.
#include <sys/stat.h>
stat() sistem çağrısı Linux'ta çok kullanılır. fstat metodu ile kardeştir.

stat yapısı
İçi şöyledir. Bu yapı işletim sistemine göre değişiklik gösterebilir. Alanların isimleri biraz farklılaşıyor ancak temel olarak aynı işi görüyor.
struct stat {
  dev_t     st_dev;     /* ID of device containing file */
  ino_t     st_ino;     /* inode number */
  mode_t    st_mode;    /* protection */
  nlink_t   st_nlink;   /* number of hard links */
  uid_t     st_uid;     /* user ID of owner */
  gid_t     st_gid;     /* group ID of owner */
  dev_t     st_rdev;    /* device ID (if special file) */
  off_t     st_size;    /* total size, in bytes */
  blksize_t st_blksize; /* blocksize for filesystem I/O */
  blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
  time_t    st_atime;   /* time of last access */
  time_t    st_mtime;   /* time of last modification */
  time_t    st_ctime;   /* time of last status change */
};
st_blksize  Alanı
Açıklaması şöyle
st_blksize is called "the optimum I/O size" and unrelated to the units used for st_blocks. The optimum I/O size of course is filesystem specific. This is a result from the fast filesystem development from Berlekey in 1981/1982. Before, there was no optimum block size in the available filesystem
st_blocks  Alanı
Bu alanın değier HP-UX'te biraz farklı. Açıklaması şöyle
st_blocks is expressed in units of DEV_BSIZE that indeed is 1024 on HP-UX. DEV_BSIZE is a platform specific constant. Later, when FFS was renamed to UFS, there was a second filesystem in BSD UNIX with different behavior related to indirect blocks and that required this new stat() field. 
stat metodu
Şöyle kullanırız.
struct stat info;
stat("test.txt", &info);
Döndürdüğü Sonuç
stat() çağrısının döndürdüğü sonuç 0'an faklı ise errno değişkenine ENOENT, ENOTDIR, EACCESS gibi değerler atanır.

ENOTDIR için açıklama şöyle
If it is ENOTDIR then part of the path you provided is not a directory,
EACCES için açıklama şöyle
If it's EACCESS then you didn't have read permission on one of the directories in the path and so stat can't give you an answer.
ENOENT için açıklama şöyle
ENOENT means "No such file and directory", and is for path operations.
stat ve macrolar
dizin olup olmadığını kontrol için şöyle yaparız.
//Test for directory
if(S_ISDIR(info.st_mode))
{
  ..
}
Normal bir dosya (sembolik link olmayan) olup olmadığını kontrol için şöyle yaparız.
//Test for a regular file.
if(S_ISREG(info.st_mode))
{
  ..
}
Character Device olup olmadığını kontrol için şöyle yaparız.
if (S_ISCHR(info.st_mode)) {...}
dosyanın mevcudiyeti
Şöyle yaparız.
int file_exist (char *filename)
{
  struct stat   buffer;   
  return (stat (filename, &buffer) == 0);
}
en son değişme zamanı - last modification time
Şöyle buluruz.
struct stat info;
stat("test.txt", &info);
printf("%s", ctime(&info.st_mtime));

17 Eylül 2021 Cuma

Kubernetes Deployment İçin Volumes

Giriş
Çeşitli volume tipleri var.
1. emptyDir
2. PersistentVolume
gibi

emptyDir
Şu cümleler önemli
emptyDir are volumes that get created empty when a Pod is created.
Deleting a Pod deletes all its emptyDirs.
emptyDir are meant for temporary working disk space.

Örnek
Şöyle yaparız
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dune-quote-service
spec:
  replicas: 1
  selector:
    matchLabels:
      app: dune-quote-service
  template:
    metadata:
      labels:
        app: dune-quote-service
    spec:
      containers:
        - image: gamussa/reactive-quote-service:0.0.3
          imagePullPolicy: Always
          name: dune-quote-service
          ports:
            - containerPort: 9001
          env:
            ...
            - name: GRPC_SERVER_SECURITY_CERTIFICATECHAIN
              value: "file:/mnt/grpc-cert-chain/server.crt"
            - name: GRPC_SERVER_SECURITY_PRIVATEKEY
              value: "file:/mnt/grpc-pk/server.key"
          volumeMounts:
            - mountPath: /mnt/grpc-cert-chain
              name: grpc-cert-chain
            - mountPath: /mnt/grpc-pk
              name: grpc-pk
      volumes:
        - name: grpc-cert-chain
          secret:
            secretName: grpc-cert-chain
        - name: grpc-pk
          secret:
            secretName: grpc-pk
Örnek - sadece tmp Dizini Hakkı
Açıklaması şöyle
Applications running in a containerized environment seldom write data, as that practically goes against the logic of having an immutable system. However, at times, it may be needed for caching or temporary swapping/processing of files. Hence, to provide this functionality to the developer, we can mount an emptyDir as an ephemeral volume which is lost once the container is killed.

With this in place, we can also add another security context attribute called “readOnlyRootFilesystem” and set it as true, since the application running inside the container no longer needs to write anywhere on the file-system other than the ‘tmp’ directory.
Şöyle yaparız
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: springbootmaven
  name: springbootmaven
  namespace: boot
spec:
  replicas: 1
  selector:
    matchLabels:
      app: springbootmaven
  template:
    metadata:
      labels:
        app: springbootmaven
    spec:
      securityContext:
        fsGroup: 1337
        runAsNonRoot: true
        runAsUser: 1337
      containers:
      - image: salecharohit/springbootmaven
        name: springbootmaven
        ports:
        - containerPort: 8080
        securityContext:
          allowPrivilegeEscalation: false
          readOnlyRootFilesystem: true
          privileged: false
          runAsUser: 1337
          capabilities:
            drop: ["SETUID", "SETGID"]
        volumeMounts:
        - mountPath: /tmp
          name: tmp
      serviceAccountName: ""
      automountServiceAccountToken: false
      volumes:
      - emptyDir: {}
        name: tmp

16 Eylül 2021 Perşembe

Kubernetes Servis Nedir?

Giriş
Açıklaması şöyle. Dış dünyaya açılan yüzümüz diye düşünebiliriz.
A Service enables network access to a set of Pods in Kubernetes.

Services select Pods based on their labels. When a network request is made to the service, it selects all Pods in the cluster matching the service's selector, chooses one of them, and forwards the network request to it.
Service ve Deployment Farkı Nedir?
Açıklaması şöyle.
A deployment is responsible for keeping a set of pods running.
A service is responsible for enabling network access to a set of pods.
Servis Tipleri Nedir?
Açıklaması şöyle
The type property in the Service's spec determines how the service is exposed to the network. It changes where a Service is able to be accessed from. The possible types are ClusterIP, NodePort, and LoadBalancer

ClusterIP – The default value. The service is only accessible from within the Kubernetes cluster – you can’t make requests to your Pods from outside the cluster!

NodePort – This makes the service accessible on a static port on each Node in the cluster. This means that the service can handle requests that originate from outside the cluster.

LoadBalancer – The service becomes accessible externally through a cloud provider's load balancer functionality. GCP, AWS, Azure, and OpenStack offer this functionality. The cloud provider will create a load balancer, which then automatically routes requests to your Kubernetes Service
servisleri görmek için kubectl get services kullanılır

ClusterIP Service yazısına bakabilirsiniz
LoadBalanceService yazısına bakabilirsiniz
IngressService yazısına bakabilirsiniz


Kubernetes Pod Nedir?

Giriş
Pod, Kubernetes mimarisinde Kubernetes Node'un bir parçası. Kısaca container'ları çalıştırır deyebiliriz.

Açıklaması şöyle. Aslında bir pod birden fazla docker container çalıştırabilir. Açıklaması şöyle
The primary reason that Pods can have multiple containers is to support helper applications that assist a primary application. Typical examples of helper applications are data pullers, data pushers, and proxies. Helper and primary applications often need to communicate with each other. Typically this is done through a shared filesystem, as shown in this exercise, or through the loopback network interface, localhost. An example of this pattern is a web server along with a helper program that polls a Git repository for new updates. 
Ancak bu tavsiye edilmediği için pod = container gibi düşünülebilir.
The containers are called as ‘Pod’ in Kubernetes terms. So a single Kubernetes node can have multiple containers.
Pod örneğin bir servis olabilir.
Kubernetes Servis Nedir? yazısına bakabilirsiniz

Örnek
Buradaki şekilde aynı Pod içinde çalışan farklı container'lar görülebilir.

15 Eylül 2021 Çarşamba

chpasswd komutu

Örnek
gitlab-ci isimli kullanıcının şifresini changemepassword yapmak için şöyle yaparız
echo 'gitlab-ci:changemepassword' | chpasswd

anacron

Giriş
Açıklaması şöyle
On many systems, if you turn your computer off at night and boot it or wake it up from suspension in the morning, it'll run various daily jobs, via anacron. This starts a few minutes after the computer wakes up, so you'd typically have time to log in but the resource usage would intensify after a few minutes. Anacron jobs are configured in /etc/anacrontab; a common configuration is for it to run the jobs listed in /etc/cron.daily. Maybe one of these jobs is using an unreasonable amount of resources for some reason. Use a tool like pstree to check what children of anacron are running.

13 Eylül 2021 Pazartesi

Kubernetes Configuration Examples

Giriş
Konfigürasyon için iki yöntem var
1. Environment variable
2. ConfigMap
Her iki yöntem de Pod, Service veya herhangi bir başka Kubernetes kind için kullanılabilir.

1. Environment variable
env başlığı altında
- name + value 
- name + valueFrom + configMapKeyRef
- name + valueFrom + secretKeyRef
şeklinde kullanılır

Örnek
Şöyle yaparız
apiVersion: v1
kind: Pod
metadata:
  name: static-web
  labels:
    role: myrole
spec:
  containers:
  - name: nginx
    image: nginx
    env:
    - name: DB_NAME
      value: MyDB
    - name: DB_URL
      valueFrom:
        configMapKeyRef:
          name: config-url
          key: db_url
    - name: DB_PASSWORD
      valueFrom:
        secretKeyRef:
          name: config-passwd
          key: db_password
2. ConfigMap
Kubernetes ConfigMap yazısına taşıdım