반응형
작성일: 2024년 4월 12일

 

 

Jinja 파일(*.j2)과 YAML 파일(*.yml *.yaml)을 VIM으로 편집할 때

문서 내용이 적절한 Syntax Color로 보여지지 않는다면, 아래 절차를 따라서 VIM 편집기를 설정해보자.

 

 

Pathogen 설치하기

## 관련 폴더를 미리 만든다.
$ mkdir -p ~/.vim/autoload ~/.vim/bundle 

## pathogen.vim 파일을 다운로드한다.
$ curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim

## ~/.vimrc 파일에 아래의 내용을 추가한다.
$ cat ~/.vimrc
... 중간 생략 ...
execute pathogen#infect()
syntax on
filetype plugin indent on
... 중간 생략 ...

 

 

VIM Bundle 추가하기

$ cd ~/.vim/bundle

## Ansible, YAML 관련 번들 파일을 다운로드
$ git clone https://github.com/chase/vim-ansible-yaml.git

## Jinja 관련 번들 파일을 다운로드
$ git clone https://github.com/lepture/vim-jinja.git

 

 

테스트하기

$ vim test.j2

 

Jinja 파일 하이라이트 기능

 

 


 

반응형

 

작성일: 2024년 4월 9일

 

Amazon AWS, Microsoft Azure, Google GCP, Oracle Cloud Infra 등 CSP에서 Compute resource를 생성하려고 보면,

이 Compute instance(또는 VM instance)를 생성한 것 때문에 비용이 얼마나 되는지 감을 잡기가 어렵다.

 

마침, Oracle Cloud의 연간 Credit 구입한 것이 많이 남아 있고, 5일 후에 Expire되기 때문에 남는 Credit을 소진해볼겸

VM instnace를 Scale-up, Scale-down하면서 비용 증가/감소 정도를 체크해봤다.

 

1개 VM instance를 대상으로 CPU/Memory Scale-up 수행 후 비용 증감을 체크

초기 VM instance의 Spec Scale-up 이후의 Spec 비용 증가분(Delta)
CPU: 1 core (2 thread)
MEM: 2GB
Storage: 50 GB (Boot volume)
CPU: 8 core (16 thread)
MEM: 128 GB
Storage: 50 GB (Boot volume)
11,479 원   (Daily Cost)

 

참고:
  CPU 종류: AMD EPYC 7J13
  CPU Caches: 
      L1d:  512 KiB (8 instances)
      L1i:   512 KiB (8 instances)
      L2:   4 MiB (8 instances)
      L3:   32 MiB (2 instances)

 

 

 

위에 내가 실제로 VM instance를 Scale-up한 이후의 비용 증가분을 계산한 것은
클라우드 테넌트 계약 조건과 현재 원/달러 환율 따라 30% 정도 차이가 날 것이다.
특히 클라우드 테넌트 계약시, 3년 이상 장기 계약한 경우 많은 할인율이 적용되서  Scale-up에 대한 증가분이 크지 않을 수 있다.
1년 단위로 단기 계약한 테넌트라면, CPU/Memory를 Scale-up할 때 비용 증가분이 위의 계산 결과보다 클 수 있다.

 

 

반응형
작성일: 2024년 4월 9일

 

Microsoft Azure, AWS(Amazon), GCP(Google Cloud Platform), Oracle Cloud Infra 등 Cloud Infra 서비스를 사용하다보면

예상한 것보다 비용이 더 지출되는 경우가 있다.

내 경우, Cloud Infra를 1년간 사용하는 중에 Storage(Block Volume)과 VCN(Virtual Cloud Network) 리소스 사용 비용이 계속 증가만 하길래 이 비용 증가 원인을 찾는 작업을 했다.

 


 

나는 회사 업무 때문에 CSP(Cloud Service Provider)가 제공하는 Managed Kubernetes를 종종 이용하는데, 

이 Managed Kubernetes의 cluster를 생성하고 삭제하는 과정에서 찌꺼기 리소스가 계속 발생하고 있었다.

찌꺼기 리소스가 발생하게 된 이유에 대해 좀더 자세히 설명하면 아래와 같다.

 

  1. Managed Kubernetes 서비스를 이용하여 Cluster-A를 생성한다.
  2. Cluster-A에 Pod, LoadBalancer Type의 Service 리소스 등을 생성한다.
  3. 몇달 동안 사용 후 Cluster-A를 삭제한다.    <-- 이 순간부터 찌꺼기 리소스에 대한 과금이 시작된다 (일명, 숨겨진 비용 발생)

 

위와 같은 순서로 Managed Kubernetes Cluster를 사용하면,

Pod가 사용했던 Storage(즉, PV)와 Public IP Address를 사용했던 LoadBalancer 리소스가 찌꺼기로 남게 된다.

즉, 실제로는 K8s Cluster가 없지만 Storage와 Public IP address는 내 테넌트 내의 자원으로 할당된 상태로 남게 된다.

이 찌꺼기 자원이 계속 비용을 유발하므로, 발견한 즉시 삭제해야 한다.

내 경우, 1년 동안 사용하면서 Pod, PVC, PV, Service Resoruce(LB Type) 등을 먼저 삭제하지 않은 상태에서

K8s cluster를 삭제한 경우가 2번 있었는데... 이것들을 정리하고 나니까 Cloud 사용 요금이 50% 수준으로 떨어졌다. ㅠㅠ

그 동안 쓰지 않아도 될 돈을 지출하고 있었던 것이다.

 

Cloud 서비스를 사용할 때는 Kubernetes cluster를 삭제하는 순서에 대해서 꼭 주의하자!

 

  1. Kubernetes cluster 내부에서 사용했던 Pod, PVC, PV, Service Resoruce를 모두 삭제한다.
  2. PV, Service Resource가 삭제되었는지 여부를 반드시 확인한 후,
  3. Kubernetes cluster를 삭제한다.

 


 

반응형

 

수정일: 2024년 4월 23일
작성일: 2024년 4월 23일

 

 

아래 글은 나의 주관적 생각이 포함된 것이므로 정확한 기술 Spec을 원하는 분은 이 글을 참고용으로만 읽고,
이 블로그의 중간에 링크를 달아둔 Official document를 열람하는 것을 권장함.

 

 

XDP 개요  (들어가는 글)

XDP(eXpress Data Path)는 OS network stack을 우회하여 별도의 user application으로 보내고 받도록 하는데 사용되는 eBPF 기반의 data path이다.

Linux kernel version 4.8부터 XDP를 포함하므로 XDP 프로그래밍을 위해서 별도로 library를 설치할 필요는 없다.

 

아래 그림만 잘 들여다보면, XDP_* 처리(Action)에 대한 감이 온다.

 

XDP를 이용한 Network Packet Flow를 제거하기

 

위 그림에서 (start), interface output을 물리 네트워크 포트 또는 NIC port 라고 가정하고 보면 Packet 흐름이 자연스러워진다.

그리고, 이 블로그에서는 (start), interface output 가 동일 NIC port라고 가정하고 나머지 내용을 설명할 것이다.   

 

XDP_*  Action Code Description
XDP_PASS 원래의 network packet이 처리되는 것처럼 Network stack으로 보낸다.
(즉, XDP가 없더라도 원래 동작하던 network packet 처리)
XDP_DROP 패킷을 drop (폐기)
XDP_ABORTED Trace point exception과 함께 해당 packet을 drop (폐기)
XDP_TX 위 그림에 묘사된 것처럼 아무 처리하지 않고, 바로 NIC port로 되돌려 보낸다. (반송 처리)
XDP_REDIRECT AF_XDP Address Family를 통해 packet을 다른 NIC port로 보내거나 
User Space Socket으로 redirect한다.
---
  참고로 AF_XDP address family는 Linux kernel 4.18부터 추가되었다.

 

 

예제 / 실습 자료

위 내용을 바탕으로 실습하고자 한다면, 아래 tutorial을 참고할 것!

 

  XDP tutorial : 문서 링크 열기

        아래 git repo에 여러 실습 예제가 있는데, 그 중에서 "basic01-xdp-pass"와 같은 쉬운 예제부터 실습하면서

        전체 처리 절차를 이해하는 추천함!

 

  Tutorial one liners (bpftrace) : 문서 링크 열기

 

  Go Packet Generator (go-pktgen) : 문서 링크 열기

    -> 위 go-pktgen 프로그램은 아래 go package를 사용하므로, 아래 golang pkg source code 분석하는 것을 추천.

          XDP Go Package : 소스 코드 링크 열기   

 

 

Linux kernel source code에서 XDP 관련 코드를 찾아보기 (예시)

Linux kernel source code에서 network driver와 관련한 code를 찾아 보면, 

XDP_*와 관련있는 부분을 쉽게 만날 수 있다.

그 중에서 몇 개를 여기에 적어보겠다.

 

##
## File: linux/latest/source/drivers/net/ethernet/intel/i40e/i40e_txrx.c
##

... 중간 생략 ...
/**
 * i40e_run_xdp - run an XDP program
 * @rx_ring: Rx ring being processed
 * @xdp: XDP buffer containing the frame
 * @xdp_prog: XDP program to run
 **/
static int i40e_run_xdp(struct i40e_ring *rx_ring, struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
{
	int err, result = I40E_XDP_PASS;
	struct i40e_ring *xdp_ring;
	u32 act;

	if (!xdp_prog)
		goto xdp_out;

	prefetchw(xdp->data_hard_start); /* xdp_frame write */

	act = bpf_prog_run_xdp(xdp_prog, xdp);
    
	switch (act) {
    
	case XDP_PASS:   ## NOTE: 따로 처리할 것이 없다.
		break;       ##       그냥, 원래 하던대로 network stack으로 보내면 된다.
        
	case XDP_TX:     ## NOTE: RX 패킷이 들어온 NIC을 찾아서 TX 패킷을 그 NIC으로 보내도록 한다.
		xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index];
		result = i40e_xmit_xdp_tx_ring(xdp, xdp_ring);
		if (result == I40E_XDP_CONSUMED)
			goto out_failure;
		break;
        
	case XDP_REDIRECT:
		err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog);
		if (err)
			goto out_failure;
		result = I40E_XDP_REDIR;
		break;
        
	default:
		bpf_warn_invalid_xdp_action(rx_ring->netdev, xdp_prog, act);
		fallthrough;
        
	case XDP_ABORTED:  ## NOTE: 패킷을 폐기
out_failure:
		trace_xdp_exception(rx_ring->netdev, xdp_prog, act);
		fallthrough; /* handle aborts by dropping packet */
        
	case XDP_DROP:     ## NOTE: 패킷을 폐기
		result = I40E_XDP_CONSUMED;
		break;
	}
    
xdp_out:
	return result;
}
... 중간 생략 ...

 

 

 

 


 

eBPF 공식 문서

What is eBPF?

  문서 링크: https://ebpf.io/what-is-ebpf/

 

The eBPF Library for Go

  문서 링크: https://ebpf-go.dev/

 

 

 


추천 글

 

https://medium.com/@khushichhillar02/unlocking-network-performance-with-xdp-and-ebpf-67c712128025

 

Unlocking Network Performance with XDP and eBPF

XDP, eXpress Data Path, is a high-performance networking technology in the Linux kernel that allows for fast and efficient packet…

medium.com

 

 

 


 

반응형

 

2024년 4월 2일

 

 

일단, 메모만 해놓고 나중에 자세히 스터디하기~

 

 

https://www.ntop.org/products/packet-capture/pf_ring/

 

PF_RING

 

 

 

 

PF_RING Modules

 

 

 

 

https://www.ntop.org/products/packet-capture/pf_ring/pf_ring-zc-zero-copy/

 

 

 

Zero Copy Operations to KVM VM instances

 

 

 

 

 

 


 

 

반응형

 

 

작성일: 2024년 3월 27일

 

 

업무 때문에 이틀 정도 Python 코드를 작성할 일이 생겼는데, On-line tutorial을 보고 너무 잘 만들어져 있어서 깜짝 놀랐다.

Python 책을 따로 구입하지 않아도 될 정도로 공식 Tutorial 문서만으로 스터디가 가능하다.

 

Python 학습에 도움이 되는 문서

Python 자습서 (Tutorial)

Python에 관한 모든 문서(공식 문서)

  위 문서에 담겨있는 내용

  • Library reference
  • Language reference
  • Python setup, usage
  • Howto
  • Python Module 설치
  • C/C++ 언어를 이용한 구현 확장(Extending and embedding)
  • Python's C API
  • FAQ

 

 

 


 

반응형

 

작성일: 2024월 3월 26일

 

내 밥벌이가 Python 언어로 개발하는 일이 아니다보니, Python으로 뭔가 만드는 일이 1년에 한 번 있을까 말까한다.
(누가 시켜서 만든다기 보다는 일하다가 자동화가 필요한 상황이 생길 때 ~~~)

오늘도 Python Logging 기능을 5년 만에 쓸 일이 있어서 다시 작성하려고 보니,
구글링해야 하고.. 예제 코드 작성해서 확인하고, 그런 후에 작성하려고 한 application에 적용하고...
이렇게 시간을 보내고 나니까 나 스스로 참 답답한 느낌이다.
다음 5년 뒤에도 이런 답답함이 없기를 바라면서 오늘 작성한 것을 잘 메모해야 겠다 ^^

 

 

#!/usr/bin/python3

import logging
import logging.handlers
import os
import datetime
import threading
import time


class MyLog():
    def __init__(self, dir, logname) -> None:
        self.logname = logname
        self.dir = os.path.join(dir,logname)
        self.InitLogger()

    def InitLogger(self):
        ## Log format 설정하기
        formatter = logging.Formatter("[%(asctime)s] %(levelname)s %(filename)s:%(lineno)d - %(message)s")
        if os.path.exists(self.dir) == False :
            os.makedirs(self.dir)

        log_File = os.path.join(self.dir,  self.logname + ".log")
        timedfilehandler = logging.handlers.TimedRotatingFileHandler(filename=log_File, when='midnight', interval=1, encoding='utf-8')
        timedfilehandler.setFormatter(formatter)
        timedfilehandler.suffix = "%Y%m%d.log"

        self.logger = logging.getLogger(self.logname)
        self.logger.addHandler(timedfilehandler)
        self.logger.setLevel(logging.INFO)

        self.delete_old_log(self.dir, 3)

        now = datetime.datetime.now()
        self.toDay = "%04d-%02d-%02d" % (now.year, now.month, now.day)
        self.th_auto_delete = threading.Thread(target=self.auto_delete, daemon=True)
        self.th_auto_delete.start()

    '''
    함수 인자 설명:
      - fpath:삭제할 파일이 있는 디렉토리,
      - age:경과일수
    '''
    def delete_old_log(self, fpath, age):
        for fp in os.listdir(fpath):
            fp = os.path.join(fpath, fp)
            if os.path.isfile(fp):
                timestamp_now = datetime.datetime.now().timestamp()
                if os.stat(fp).st_mtime < timestamp_now - (age * 24 * 60 * 60):
                    try:
                        os.remove(fp)
                    except OSError:
                        print(fp, 'this log file is not deleted')

    def auto_delete(self):
        while True:
            now = datetime.datetime.now()
            day = "%04d-%02d-%02d" % (now.year, now.month, now.day)
            if self.toDay != day:
                self.toDay = day
                self.delete_old_log(self.dir, 3)
            time.sleep(600)


## This is test code.
if __name__ == '__main__':
    log = MyLog("my_log_test_dir", logname="sejong")
    for idx in range(3):
        log.logger.info(f"로그 메시지가 잘 기록되는지 테스트 중 {idx}")

 

 

테스트는 아래와 같이 하면 된다. 일단, 잘 동작함, OK !!

$ ./my_log.py
...

 

 

위 'MyLog' 클래스를 다른 Application code에서 참고한다면, 아래 예제와 같이 작성하면 된다.

참고: my_log.py는 아래 소스 코드 my_sample_app.py와 같은 디렉토리에 있어야 한다.
#!/usr/bin/python3

import my_log

...
... 중간 생략 ...
...

if __name__ == '__main__':
    log = my_log.MyLog("log_my_sample_dir", logname="my_sample")
    log.logger.info("#####  Start program  #####")
    
... 중간 생략 ...

 

$ ./my_sample_app.py
...

 


 

반응형

 

2024년 4월 15일

 

 

Kubernetes를 사용하면서 만나는 문제 상황

Kubernetes service resource를 삭제하고 다시 생성하면, Public IP(또는 EXTERNAL-IP) 값이 변경된다.

AWS, Oracle Cloud, Azure, GCP(Google Cloud Platform) 같은 Public Cloud Infra에서 제공하는 

Reserved Public IP 기능과 Service YAML에 'externalIPs' 값을 설정하면 Public IP Address를 고정시킬 수 있지만,

Helm Chart를 작성하거나 Kubernetes Service Manifest 파일을 작성할 때마다 

  1. Reserved Public IP 생성 요청
  2. 위에서 생성된 Public IP 값을 확인 후 Service Manifest 작성(또는 Helm chart) 작성

한다는 것은 참 불편한 작업이다.

 

 

그래서 아래의 기능을 수행하는 Python script를 만들었다.

 

  1. my.yaml 파일에서 Public IP address 정보를 갱신할 'Kubernetes service' 정보와 'Internet domain name' 목록을 설정
  2. DNS 서버에서 직접 Kubernetes API server에게 매번 변경되는 Service resource의 public IP address를 조회 요청
  3. 이렇게 얻은 public IP address(즉, EXTERNAL-IP)를 자동으로 DNS 서버의 Zone file에 반영
  4. BIND9 서비스 데몬을 재기동

 

#!/usr/bin/python3


##############################################################
##
## Written by "Andrew Jeong"
## Date: 2024.03.25
## File: kube-dns-auto-config.py
##############################################################



import os
import time
import socket
import shutil
import subprocess
import yaml
from datetime import datetime
from kubernetes import client, config


##
## File 내용 중에서 일부 문자열을 찾아서 바꿈.
##
def replace_in_file(file_path, old_str, new_str):
    # 파일 읽어들이기
    fr = open(file_path, 'r')
    lines = fr.readlines()
    fr.close()

    # old_str -> new_str 치환
    fw = open(file_path, 'w')
    for line in lines:
        fw.write(line.replace(old_str, new_str))
    fw.close()


##
## Zone file에 있는 Serial 값을 찾기
## NOTE: 주의할 점
##   Zone file 안에 Serial 값이 아래 포맷으로 저장되어 있어야 한다.
##
##   @ IN SOA mydomain.kr root.mydomain.kr (
##     2024032502    ; Serial
##     3600          ; Refresh
##     900           ; Update retry
##     604800        ; Expiry
##     600           ; TTL for cache name server (for 30 minutes)
##   )
##
def search_old_serial(file_path):
    fp = open(file_path)
    lines = fp.readlines()
    fp.close()

    for line in lines:
        # print(f"line: {line}")
        if line.find('Serial') > 0:
            # print(f"I found serial value:  {line}")
            words = line.split(';')
            old_serial = words[0].strip()
            # print(f"old_serial: [{old_serial}]")
    return old_serial



##
## Update 'serial number' of zone file
##
def update_serial_for_zone_file(file_path):
    old_serial = search_old_serial(zone_file)
    curr_date = datetime.today().strftime("%Y%m%d")
    bool_today = old_serial.startswith(curr_date)
    if bool_today is True:
        ## Serial 값이 오늘 한번이라도 Update된 경우
        int_old_serial = int(old_serial)
        new_serial = str(int_old_serial + 1)
    else:
        ## Serial 값이 오늘 처음으로 Update되는 경우
        new_serial = f"{curr_date}00"

    print(f"old_serial: [{old_serial}]")
    print(f"new_serial: [{new_serial}]")

    ## copy file for backup
    bkup_file = f"{zone_file}_{new_serial}"
    shutil.copy(zone_file, bkup_file)

    ## Update serial number
    replace_in_file(zone_file, old_serial, new_serial)



def update_dns(conf):
    kube_svc_name = conf['kube_svc_name']
    kube_domain_name = conf['kube_domain_name']

    print(f"\n[ Checking configuration '{kube_svc_name}' / '{kube_domain_name}' ]")

    for item in svc_info.items:
        if item.metadata.name == kube_svc_name and item.metadata.namespace:
            ip_k8s_svc = item.status.load_balancer.ingress[0].ip
            print(f"  Kubernetes Service External-IP: {ip_k8s_svc}")

    ip_addr_domain_name = socket.gethostbyname(kube_domain_name)
    print(f"  {kube_domain_name}: {ip_addr_domain_name}")

    if ip_addr_domain_name == ip_k8s_svc:
        print("  IP address of domain name is not changed. Nothing to do.")
        return -1
    else:
        update_serial_for_zone_file(zone_file)
        print("  IP address of domain name is changed.")
        print("  Make a backup file of zone file and then")
        print(f"  update '{kube_domain_name} IN A {ip_addr_domain_name}' record")
        print(f"    {ip_addr_domain_name} -> {ip_k8s_svc}")
        replace_in_file(zone_file, ip_addr_domain_name, ip_k8s_svc)
        return 0





## ------------------------------------------------------------------------
##     Begin of Main
## ------------------------------------------------------------------------

## Load configration from YAML file.
with open('/root/WorkSpace/kube-python-client/my.yaml') as fp:
    my_conf_info = yaml.load(fp, Loader = yaml.FullLoader)

zone_file = my_conf_info['zone_file']

##
## Get new IP address from Kubernetes service resource
## and
## Get old IP address from DNS
##

## Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()
v1 = client.CoreV1Api()

while True:
    svc_restart_flag = 0
    svc_info = v1.list_service_for_all_namespaces(watch=False)
    for conf in my_conf_info['services']:
        ret = update_dns(conf)
        if ret == 0:
            svc_restart_flag = 1
    if svc_restart_flag == 1:
        ret_value = subprocess.call("systemctl restart bind9", shell=True)
        print(f"\n\n[ BIND9 is restarted ] Return: {ret_value}\n\n")
    time.sleep(300)

## ------------------------------------------------------------------------
##     End of Main
## ------------------------------------------------------------------------

 

 

아래 예시와 같이 my.yaml 설정 파일을 작성한다.

 

## File: my.yaml

zone_file: /var/cache/bind/mydomain.kr.zone
services:
  - kube_svc_name: almighty
    kube_domain_name: almighty.mydomain.kr
  - kube_svc_name: my-test-server
    kube_domain_name: my-test-server.mydomain.kr

 

참고:
'kube_domain_name'은 실제로 존재하는 domain_name 이어야 한다.
YesNIC, Whois 같은 도메인 등록 기관에서 등록 후 위 테스트를 진행해야 한다.

 

 

 

아래 예시와 같이 실행하면 자동으로 Kubernetes cluster에 있는 Service resource 정보를 반영하여

DNS Zone file이 수정될 것이다.

 

$ ./kube-dns-auto-config.py

[ Checking configuration 'almighty' / 'almighty.mydomain.kr' ]
  Kubernetes Service External-IP: 146.53.17.8
  almighty.mydoamin.kr: 143.26.1.4
  ... 중간 생략 ...

[ Checking configuration 'my-test-server' / 'my-test-server.mydomain.kr' ]
  Kubernetes Service External-IP: 193.13.25.23
  my-test-server.mydomain.kr: 193.13.25.23
  ... 중간 생략 ...
  ...
  ...

 

 

아래와 같이 Kubernetes cluster에 있는 almighty 서비스로 HTTP 트래픽을 보내보면, 트래픽을 잘 갈거다.

$ curl -v almighty.mydomain.kr
... 내용 생략 ...

 

 

 


 

+ Recent posts