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

 

 

example.com에 대해서 새로 변경된 DNS Record 정보를 가져오게 하려면

기존에 내 Local Host의 DNS Cache에 저장된 DNS Record를 삭제해야 한다.

 

macOS를 사용하는 경우라면, 아래와 같이 명령을 수행하면 DNS Cache를 삭제할 수 있다. (또는 DNS 초기화, Flush, Reset)

 

$ sudo dscacheutil -flushcache

$ sudo killall -HUP mDNSResponder

 

 

## DNS Cache를 flush하기 전에는 CURL 같은 Application이 
## 예전에 조회해서 얻었던 Old IP address를 사용한다.
$ curl -v my-example.kr:8080
  Trying 12.15.20.38:8080...
  ... 중간 생략 ...


## DNS Cache를 flush
$ sudo dscacheutil -flushcache

$ sudo killall -HUP mDNSResponder

## DNS Cache를 flush한 후에는 CURL 같은 Application이
## 새 DNS Record를 조회하고, 새로 얻은 IP Address를 사용한다.
$ curl -v my-example.kr:8080
  Trying 19.12.25.113:8080...
  ... 중간 생략 ...

 

 

 

 

 

 

 


 

반응형
작성일: 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-conf.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 서비스 데몬을 재기동

 

아래 예제 설정과 Python 소스 코드를 이용하면 잘 동작함 ^^

 

설정 파일 작성  [ 파일명: my-conf.yaml ]

 

$ cat my-conf.yaml

zone_file: /var/cache/bind/my-domain.kr.zone
json_file: /var/cache/bind/my-domain.kr.json

domain_name: my-domain.kr

services:
  - kube_svc_name: almighty
    host_name: almighty
  - kube_svc_name: myhappyserver
    host_name: myhappynacserver
  - kube_svc_name: my-ingress-nginx-controller
    host_name: myproxyserver

 

 

 

BIND9 Zone File 생성을 위한 Jinja2 Template 파일 작성  [ 파일명: zone.j2 ]

 

$ cat zone.j2

; ------------------------------------------------------------------------------------------
; NOTE:
;   이 파일은 대한민국 서울에 사는 Andrew Jeong이 만든 "DNS Auto Configuration Application"이
;   자동으로 생성한 파일입니다.
;   사용자가 이 파일을 직접 편집해도 일정 시간 이후에 Application에 의해서 재작성될 것입니다.
;   따라서 이 Zone file을 직접 편집하는 것을 금합니다.
;   만약, Zone file에 반영할 내용이 있다면 같은 폴더에 있는 JSON 파일을 수정해야 합니다.
; ------------------------------------------------------------------------------------------

; Internet Domain Zone :  {{ origin }}
; Exported date        :  {{ now }}

$ORIGIN {{ origin }}
$TTL {{ ttl }}

;
; SOA Record
;
@    IN    SOA    {{ soa['mname'] }}    {{ soa['rname'] }} (
    {{ soa['serial'] }} 	; serial
    {{ soa['refresh'] }} 	; refresh
    {{ soa['retry'] }} 	; retry
    {{ soa['expire'] }} 	; expire
    {{ soa['minimum'] }} 	; minimum ttl
)


{% if ns is defined -%}
;
; NS Records
;
{% for entry in ns -%}
@   IN  NS  {{ entry['host'] }}
{% endfor %}
{% endif %}


{%- if mx is defined -%}
;
; MX Records
;
{% for entry in mx -%}
@	IN	MX	{{ entry['preference'] }}    {{ entry['host'] }}
{% endfor %}
{% endif %}


{%- if spf is defined -%}

; SPF Records
{% for entry in spf -%}
{{ entry['domain'] }}		IN	TXT	"{{ entry['record'] }}"
{% endfor %}
{% endif %}


{%- if a is defined -%}
;
; A Records
;
{% for entry in a -%}
	{{ "{:<17}".format(entry['name']) }}  IN  A      {{ entry['ip'] }}
{% endfor %}
{% endif %}


{%- if aaaa is defined -%}

; AAAA Records
{%- for entry in aaaa -%}
{{ entry['name'] }}		IN	AAAA	{{ entry['ip'] }}
{% endfor %}
{% endif %}


{%- if cname is defined -%}
;
; CNAME Records
;
{% for entry in cname -%}
	{{ "{:<17}".format(entry['name']) }}  IN  CNAME  {{ entry['alias'] }}
{% endfor %}
{% endif %}


{%- if ptr is defined -%}
;
; PTR Records
;
{% for entry in ptr -%}
{{ entry['name'] }}		IN	PTR	"{{ entry['host'] }}"
{% endfor %}
{% endif %}


{%- if txt is defined -%}
;
; TXT Records
;
{% for entry in txt -%}
{{ entry['name'] }}		IN	TXT	"{{ entry['txt'] }}"
{% endfor %}
{% endif %}


{%- if srv is defined -%}
;
; SRV Records
;
{% for entry in srv -%}
{{ entry['name'] }}		IN	SRV	{{ entry['priority'] }}   {{ entry['weight'] }} {{ entry['port'] }}   {{ entry['target'] }}
{% endfor %}
{%- endif %}

; End of Zone

 

 

 

BIND9 Zone File 생성을 위한 JSON 파일 작성  [ 파일명: /var/cache/bind/my-domain.kr.json ]

$ cat /var/cache/bind/my-domain.kr.json

{
    "origin": "my-domain.kr.",
    "ttl": 600,
    "soa": {
        "mname": "ns.my-domain.kr.",
        "rname": "sejong.my-domain.kr.",
        "serial": "2024041500",
        "refresh": 3600,
        "retry": 600,
        "expire": 604800,
        "minimum": 86400
    },
    "ns": [
        {
            "host": "ns.my-domain.kr."
        },
        {
            "host": "ns1.my-domain.kr."
        }
    ],
    "a": [
        {
            "name": "@",
            "ip": "16.6.80.3"
        },
        {
            "name": "ns",
            "ip": "16.6.80.3"
        },
        {
            "name": "ns1",
            "ip": "13.2.88.15"
        },
        {
            "name": "myhappyserver",
            "ip": "13.93.28.9"
        },
        {
            "name": "my-ingress-nginx-controller",
            "ip": "10.1.7.6"
        },
        
        ... 중간 생략 ...
        
        {
            "name": "almighty",
            "ip": "129.154.195.186"
        }
    ],
    "cname": [
        {
            "name": "my-nicname-server",
            "alias": "almighty"
        },
        {
            "name": "toy",
            "alias": "my-ingress-nginx-controller"
        },
        
        ... 중간 생략 ...
        
        {
            "name": "my-supersvc",
            "alias": "mail.sogang.ac.kr"
        }
    ]
}

 

 

로그 파일 생성을 위한 Python Script 작성  [ 파일명: my_log.py ]

$ cat my_log.py

#!/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}")

 

 

Kubernetes External-IP를 조회 및 DNS 자동 설정을 위한 Python Script 작성 [  파일명: kube-dns-auto-config.py ]

 

#!/usr/bin/python3

import os
import time
import socket
import shutil
import subprocess
import yaml
import json
import datetime
#from datetime import datetime
from kubernetes import client, config
from jinja2 import Environment, FileSystemLoader

import my_log


###############################################################
## NOTE: You have to fix the following variable 'my_run_dir'
###############################################################
my_run_dir = "/opt/kube-dns-auto-config/"


def search_host_from_json_data(host_name):
    for item in json_data['a']:
        if item['name'] == host_name:
            return item
    return None


def chk_ip_addr_changed(conf_svc_item):
    kube_svc_name = conf_svc_item["kube_svc_name"]
    host_name = conf_svc_item["host_name"]

    log.logger.info(f"\n")
    log.logger.info(f"[ Checking configuration '{kube_svc_name}' / '{host_name}' ]")

    for item in kube_svc_info.items:
        if item.metadata.name == kube_svc_name:
            ip_addr_k8s_svc = item.status.load_balancer.ingress[0].ip
            log.logger.info(f"  {kube_svc_name:<11} from K8S SVC EXT-IP : {ip_addr_k8s_svc:>15}")

            dns_record = search_host_from_json_data(host_name)
            if dns_record == None:
                log.logger.info("  host_name {host_name} is not found.")
                return 0
            log.logger.info(f"  {host_name:<11} from JSON file      : {dns_record['ip']:>15}")
            if ip_addr_k8s_svc == dns_record['ip']:
                log.logger.info("  IP address of domain name is not changed. Nothing to do.")
                return 0
            else:
                log.logger.info(f"  IP address of domain name is changed;   {dns_record['ip']} -> {ip_addr_k8s_svc}")
                dns_record['ip'] = ip_addr_k8s_svc
                return 1
    return 0



def get_new_serial(old_serial):
    curr_date = datetime.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"

    log.logger.info(f"old_serial: [{old_serial}]")
    log.logger.info(f"new_serial: [{new_serial}]")

    return new_serial


def make_bkup_file(serial):
    zone_file = my_conf_info['zone_file']
    bkup_zone_file = f"{zone_file}_{serial}"
    shutil.copy(zone_file, bkup_zone_file)

    bkup_json_file = f"{json_file}_{serial}"
    shutil.copy(json_file, bkup_json_file)

    return None


def make_zone_file():
    ## Jinja2 environment object and refer to templates directory
    env = Environment(loader=FileSystemLoader(my_run_dir))
    template = env.get_template('zone.j2')

    template.globals['now'] = datetime.datetime.now().isoformat()
    rendered_data = template.render(json_data)

    log.logger.info(f"rendered_data: \n{rendered_data}")

    fp = open(my_conf_info['zone_file'], "w")
    fp.write(rendered_data)
    fp.close()

    return None


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

if __name__ == '__main__':
    log = my_log.MyLog("log_kube_dns", logname="kb_log")
    log.logger.info("#####  Start program  #####")

    ## Load configration from YAML file.
    conf_path = my_run_dir + "my-conf.yaml"
    with open(conf_path) as fp:
        my_conf_info = yaml.load(fp, Loader = yaml.FullLoader)

    json_file = my_conf_info['json_file']

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

    ##
    ## !!! Main service logic !!!
    ##
    while True:
        svc_ip_changed = 0

        with open(json_file) as json_fp:
            json_data = json.load(json_fp)

        kube_svc_info = v1.list_service_for_all_namespaces(watch=False)
        for conf_svc_item in my_conf_info['services']:
            svc_ip_changed += chk_ip_addr_changed(conf_svc_item)

        if svc_ip_changed > 0:
            log.logger.info(f"IP address is changed")
            log.logger.info(f"json_data: \n\n{json_data}\n")

            new_serial = get_new_serial(json_data['soa']['serial'])
            json_data['soa']['serial'] = new_serial

            ## BKUP file 만들기
            make_bkup_file(new_serial)

            ## JSON 파일에 변경된 내용을 Update한다.
            json_str = json.dumps(json_data, indent=4)
            with open(json_file, "w") as json_outfile:
                json_outfile.write(json_str)

            ## JSON Data를 Zone DB 포맷으로 변환하여 Zone file에 저장
            make_zone_file()

            ## Restart BIND9 service
            ret_value = subprocess.call("systemctl restart bind9", shell=True)
            log.logger.info(f"\n\n")
            log.logger.info(f"[ BIND9 is restarted ] 'systemctl' return: {ret_value}\n\n")
        time.sleep(60)

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

 

 

 

실행하기 (테스트하기)

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

 

 

위와 같이 Python script를 실행하고, 로그 파일을 열람한다.

$ tail -f /opt/kube-dns-auto-config/log_kube_dns/kb_log/kb_log.log

 

 

 

 

 

주의 사항

위 Python script를 실행하는 DNS 서버 장비에 $KUBE_CONFIG 파일이 존재해야 한다.

예를 들어, /home/sejong/.kube/config 같은 파일이 있어야 이 Python 프로그램이 Kubernetes API server에 접근이 가능하다.

 

 

 


 

반응형

 

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
... 내용 생략 ...

 

 

 


 

반응형

 

작성일: 2024년 3월 25일

 

 

Bind9 Zone 파일의 Serial 값을 변경하는 Python Script

지금 급하게 DNS 서버(Bind9)의 Serial 값을 자동 갱신하도록하는 기능이 필요해서 작성해본 Python script이다.

동작 방식을 간단하게 설명하면 이렇다.

 

만약 오늘이 "2024년 03월 25일"이라고 가정하면,

  • 기존 Zone 파일의 Serial 값이 2024032501 이면, 2024032502 으로 끝자리면 1 증분시켜줌.
  • 기존 Zone 파일의 Serial 값이 2024032401 이면, 2024032500 으로 날짜를 오늘로 변경하고 끝 2자리는 00으로 설정

아래 Script를 복사해서 바로 실행하면 잘 동작할 것이다.

$ cat update_zone_serial.py


#!/usr/bin/python3

from datetime import datetime
import shutil


##
## 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:
        if line.find('Serial') > 0:
            words = line.split(';')
            old_serial = words[0].strip()
    return old_serial



zone_file = '/var/cache/bind/mydomain.kr.zone'

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"

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

replace_in_file(zone_file, old_serial, new_serial)


## 실행 테스트
$ ./update_zone_serial.py

 

 

Bind9 Zone 파일의 Serial 값을 변경하는 Bash Script

만약, Python 인터프리터가 없는 경우라면 아래와 같이 bash 스크립트를 작성해서 실행해도 동일한 결과를 얻을 수 있다.

$ cat my-dns-zone-serial-increase.sh


#!/usr/bin/env bash

set -euo pipefail

: ${1?"Usage: $0 <zone file>"}

IFILE=$1

if [ ! -w "${IFILE}" ]; then
    echo "Error cannot write to ${IFILE}"
    exit
fi

if [ ! -w $(pwd) ]; then
    echo "Error, sed needs write permission for temp file, add w to current directory"
    exit
fi

PREV_SERIAL=$(grep -i Serial "${IFILE}" | awk '{print $1}')
echo "PREV_SERIAL: ${PREV_SERIAL}"
TODAY=$(date +%Y%m%d00)

if [ "$PREV_SERIAL" -ge "${TODAY}" ]; then
    NEW_SERIAL=$((PREV_SERIAL+1))
else
    NEW_SERIAL=${TODAY}
fi

echo "NEW_SERIAL: ${NEW_SERIAL}"

sed -i "s/${PREV_SERIAL}/${NEW_SERIAL}/" "${IFILE}"

printf "Zone: %s [%d -> %d]\n" "${IFILE}" "${PREV_SERIAL}" "${NEW_SERIAL}"


## 실행 테스트
$ ./my-dns-zone-serial-increase.sh  /var/cache/bind/mydomain.kr.zone

 

 

 

 

 

 

 


 

반응형

 

 


 

테스트한 날짜: 2024년 2월 15일
테스트 환경:  Ubuntu 22.04  /  Ubuntu 20.04  /  Ubuntu 18.04  (이 3개 버전 모두 잘 동작했다)

 


BIND 설치에 관해 참고하면 좋은 문서:
  https://www.hiroom2.com/2018/05/06/ubuntu-1804-bind-en/
BIND(DNS) 설정, 운영에 관해 참고하면 좋은 문서:   <-- 진짜 잘 만들어진 문서, 꼭 읽어볼 것 !!
  https://joungkyun.gitbook.io/annyung-3-user-guide/chapter5
    - BIND 기본 설정
    - 새 도메인 설정
    - Slave DNS 구성
    - Inverse domain 설정
    - DNSSEC 설정
    - GeoDNS 설정
    - Domain 위임
    - IDN

 

 

Install BIND pkg

 

##
## root 계정으로 아래 명령을 수행
##

$  apt  update

$  apt  install -y bind9

 

 

 

Configuration

 

파일명: /etc/bind/named.conf.options

options {
  directory "/var/cache/bind";
}

   참고: 원래 기본 설정이 위와 같기 때문에 수정할 내용은 없다.

 

파일명: /etc/bind/named.conf.local

zone "andrew.space" IN {
  type master;
  file "andrew.space.zone";
};

 

파일명: /var/cache/bind/andrew.space.zone

$TTL 86400

@ IN SOA andrew.space root.andrew.space (
  2021090500
  3600
  900
  604800
  86400
)

              A  10.10.9.11
              
@          IN NS ns1
           IN NS ns2

ns1        IN A  10.10.2.3
ns1        IN A  10.10.9.3

www        IN A  10.10.2.3
andrew     IN A  10.10.9.71

 

 

 

Validation (설정 값 유효성 확인)

 

$  named-checkzone  andrew.space  /var/cache/bind/andrew.space.zone
zone andrew.space/IN: loaded serial 2021090500
OK

 

 

 

Run BIND

 

$  sudo systemctl enable bind9
$  sudo systemctl restart bind9

 

 

Test

 

$  nslookup

##
## 방금 위에서 구성한 DNS 서버의 주소
##
> server 10.10.9.11
Default server: 10.10.9.11
Address: 10.10.9.11#53

##
## name zone db에 추가했던 domain name이 잘 resolution 되는 확인
##
> www.andrew.space
Server:		10.10.9.11
Address:	10.10.9.11#53

Name: www.andrew.space
Address: 10.10.2.3
>

 

 

'Ubuntu' 카테고리의 다른 글

추가 장착한 Disk Mount  (0) 2021.12.28
Root 계정의 SSH 로그인 허용  (0) 2021.12.28
openssl command example  (0) 2021.11.04
.bashrc 또는 .bash_profile 설정  (0) 2021.07.22
.vimrc 작성  (0) 2021.07.22
반응형

 


 

작성일: 2023년 12월 15일
준비물: Raspberry Pi 5
   (Pi 3B, 4, 5 모두 테스트해봤는데, 설정 내용이 동일했다. 그러니까 아무 버전의 Raspberry Pi 다 된다는 뜻.)

 

 

 

Raspberry Pi 5를 아래와 같이 Wi-Fi AP로 구축한다면, 아래의 설명을 쭉~ 따라하면 잘 동작한다.

 

Raspberry Pi를 Wi-Fi Access Point로 사용하기 위한 구성도

 

 

아래의 명령만 복붙(Copy & Paste)하면 동작할 것이다.

(자세한 설명은 이 링크의 공식 문서를 참고)

 

 

 

DNSMasq, HostAPD를 설치

$ sudo apt install dnsmasq hostapd

## 참고: 설정 작업을 위해 임시로 서비스를 종료시킨다.
$ sudo systemctl stop dnsmasq
$ sudo systemctl stop hostapd

 

Raspberry Pi의 무선 Interface wlan0에 Static IP 설정

$ sudo vim /etc/dhcpcd.conf

... 중간 생략 ...

## 이 파일의 마지막 부분에 아래의 3줄을 추가한다.
interface wlan0
    static ip_address=192.168.4.1/24
    nohook wpa_supplicant
    
$ sudo systemctl restart dhcpcd

 

DHCP Server 설정

$ sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.orig

$ sudo vim /etc/dnsmasq.conf
## 이 파일에 아래 2줄을 추가한다.
interface=wlan0
dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h


$ sudo systemctl start dnsmasq

 

Access Point Host 프로그램 설정

$ sudo vim /etc/hostapd/hostapd.conf
country_code=KR
interface=wlan0
ssid=YOURSSID
hw_mode=g
channel=7
auth_algs=1
wpa=2
wpa_passphrase=YOURPWD
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
ignore_broadcast_ssid=0


$ sudo vim /etc/default/hostapd
... 중간 생략 ...
## 아래 설정 내용을 추가한다.
DAEMON_CONF="/etc/hostapd/hostapd.conf"
... 중간 생략 ...

 

 

위와 같이 따라하면, Wi-Fi 구간의 설정은 끝 !!!

 

아래는 Raspberry Pi의 Wi-Fi 인터페이스 wlan0 으로 들어온 IP 트래픽을 유선 인터페이스 eth0 방향으로 보내면서 Masquerading 하는 설정이다.  

이 설정이 잘 되어야 무선으로 접속한 PC 또는 스마트폰이 유선 방향(즉, 인터넷 방향)으로 트래픽이 잘 전달된다.

 

IP Routing 및 Masquerading 설정

$ sudo nano /etc/sysctl.conf
... 중간 생략 ...
## 아래의 1줄을 추가한다.
net.ipv4.ip_forward=1
... 중간 생략 ...

## 라즈베리 파이에서 외부로 나가는 트래픽을 eth0의 IP address로 변경하는 설정.
## (일반적인 Source NAT 설정과 동일함)
$ sudo iptables -t nat -A  POSTROUTING -o eth0 -j MASQUERADE

## 재기동 후, 위 IPTables의 NAT 설정이 유지되도록 아래와 같이 설정을 저장함.
$ sudo netfilter-persistent save

$ reboot

 

 

위 설정 작업이 끝나면, 라즈베리 파이는 무선 AP로 동작할 것이다.

스마트폰이나 노트북으로 라즈베리 파이의 Wi-Fi 접속해보면 인터넷이 잘 될 것이다.

 

 

참고하면 좋은 문서

https://raspberrypi-guide.github.io/networking/create-wireless-access-point


 

+ Recent posts