Kubernetes

[K8S] Kubespray로 Kubernetes 설치하기 - CentOS7/Rocky8

판다조아 2024. 3. 1. 18:31

kubespray 란?

Ansible 기반의 Kubernetes 클러스터 배포 자동화 및 관리하는 오픈 소스 도구

GitHub : https://github.com/kubernetes-sigs/kubespray

 

GitHub - kubernetes-sigs/kubespray: Deploy a Production Ready Kubernetes Cluster

Deploy a Production Ready Kubernetes Cluster. Contribute to kubernetes-sigs/kubespray development by creating an account on GitHub.

github.com

 

 

 

 

 

설치 환경

  • OS : Rocky8.9
  • Kubernetes : v1.27.7
  • Kubespray : v2.23
    • ansible-core : v2.14
    • docker : v20.10
    • Kubernetes 설치 최소 버전 : v1.25
    • Python 필요 버전 : v3.9 - v3.11

 

 

 

 

Kubernetes 설치하기

  • swap 끄기
    • swap : 메모리가 부족할 경우 사용되는 가상 메모리
      => kubernetes에서 메모리를 더 효율적으로 관리하므로 메모리 스와핑으로 인한 성능 저하 방지를 위해 off
swapoff -a

 

 

  • 방화벽 끄기
    • 노드간의 원활한 통신을 위함
systemctl disable firewalld

 

 

  • SELINUX 끄기
    • SELINUX : 리눅스에서 추가적인 보안 기능 제공 (프로세스 제어, 파일 및 네트워크 접근 제한 등)
vi /etc/sysconfig/selinux

'''
SELINUX=disabled
'''

# 재시작해야 적용
reboot

 

 

  • CentOS7 - python3.1x 수동 설치
# epel-release repo 추가
yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

# OS Update
yum update -y
    
# Development Tools 설치
yum groupinstall -y "Development Tools"
    
# yum 관리도구 설치
yum install -y yum-utils

# 커널 헤더 설치 및 적용
yum install -y kernel-devel-$(uname -r) kernel-headers-$(uname -r)

reboot

# 관련 패키지 설치
yum install -y wget bzip2-devel zlib-devel ncurses-devel tcl-devel tk-devel gdbm-devel libffi-devel sqlite-devel xz-devel openssl-devel openssl11 openssl11-devel

# 설치파일 다운로드
wget https://www.python.org/ftp/python/3.10.11/Python-3.10.11.tar.xz
tar -xvf Python-3.10.11.tar.xz
cd Python-3.10.11

# 컴파일 및 설치
export CFLAGS=$(pkg-config --cflags openssl11)
export LDFLAGS=$(pkg-config --libs openssl11)

./configure --enable-optimizations
make altinstall

# 심볼릭 링크 생성
ln -s /usr/local/bin/python3.10 /usr/bin/python3
ln -s /usr/local/bin/pip3.10 /usr/bin/pip3

 

 

  • Rocky8 - python3.1x 수동 설치
# 필요 패키지 설치
yum install -y curl gcc openssl-devel bzip2-devel libffi-devel zlib-devel tar wget make

# python 설치 파일 다운로드
wget https://www.python.org/ftp/python/3.10.12/Python-3.10.12.tgz

# 압축해제 및 폴더 이동
tar xvf Python-3.10.12.tgz
cd Python-3.10.12

# 빌드 및 컴파일
./configure --enable-optimizations
make altinstall

# 심볼릭 링크 설정
ln -s /usr/local/bin/python3.10 /usr/bin/python3
ln -s /usr/local/bin/pip3.10 /usr/bin/pip3

# 확인
python3 --version

 

 

  • kubespray 파일 다운로드 및 이동
git clone https://github.com/kubernetes-sigs/kubespray

# release 지정
# git clone https://github.com/kubernetes-sigs/kubespray -b release-2.23

cd kubespray

 

 

  • 필수 패키지 설치
    • sshpass : ssh 연결 시 비밀번호 자동 제공 (ansible이 ssh 기반으로 작업을 수행하기 때문에 필요)
    • virtualenv : 개발환경 격리 및 의존성 관리를 위함
yum install -y sshpass
pip3 install -U pip virtualenv

# pip3 없을 경우
# yum install python3-pip

### rocky8에서는 kubespray 도는 중 runc 패키지 충돌 에러가 발생하여 미리 제거
yum remove runc

 

 

  • virtualenv 생성 및 활성화
# 생성
virtualenv  --python=$(which python3) kubespray-venv

# 활성화
source kubespray-venv/bin/activate

### 비활성화 시 deactivate

 

 

  • ansible 설치
pip install -U -r requirements.txt

 

 

  • inventory 폴더 복사 및 스크립트 실행
    • IP : 구성할 노드 ip (ex. 192.168.0.100 192.168.0.101 192.168.0.102)
    • HOST_PREFIX : hosts명
    • KUBE_CONTROL_HOSTS : Master 갯수
    • CONFIG_FILE : 설정할 hosts.yaml 파일 경로
# 복사
cp -rfp inventory/sample inventory/mycluster

# 환경변수 지정 및 스크립트 실행
# Update Ansible inventory file with inventory builder
# declare -a IPS=(Master_IP Node1_IP Node2_IP)
declare -a IPS=(IP)
HOST_PREFIX=test KUBE_CONTROL_HOSTS=1 CONFIG_FILE=inventory/mycluster/hosts.yaml python3 contrib/inventory_builder/inventory.py ${IPS[@]}

 

 

  • hosts.yaml 파일 수정
    • vars : 모든 hosts에 공통변수 작성 (공통변수가 아닐 경우 hosts아래에 개별 작성)
    • ansible_user : 앤서블 실행 유저
    • ansible_password : 실행 유저 비밀번호
    • ansible_become_password: root계정 비밀번호
    • master, etcd는 홀수로 구성
all:
  vars:
    ansible_user: 'root'
    ansible_password: 1234
    ansible_become_password: 1234
  hosts:
    master-name:
      ansible_host: {Master_IP}
      ip: {Master_IP}
      access_ip: {Master_IP}
    node1-name:
      ansible_host: {Node1_IP}
      ip: {Node1_IP}
      access_ip: {Node1_IP}
    node2-name:
      ansible_host: {Node2_IP}
      ip: {Node2_IP}
      access_ip: {Node2_IP}
  children:
    kube_control_plane:
      hosts:
        master-name:
    kube_node:
      hosts:
        master-name:
        node1-name:
        node2-name:
    etcd:
      hosts:
        master-name:
    k8s_cluster:
      children:
        kube_control_plane:
        kube_node:
    calico_rr:
      hosts: {}

 

 

  • +) pem키 사용 방법
    • ansible_ssh_private_key_file : pem 키 경로
all:
  vars:
    ansible_user: "centos"
    ansible_ssh_private_key_file: '/home/centos/test.pem'

 

 

  • +) label 추가
    • hosts.yaml에서 전체(vars에 기재) 또는 개별로 node label 부여 가능
    node1-name:
      ansible_host: {Node1_IP}
      ip: {Node1_IP}
      access_ip: {Node1_IP}
      node_labels:
        nodetype: 'worker'
    node2-name:
      ansible_host: {Node2_IP}
      ip: {Node2_IP}
      access_ip: {Node2_IP}
      node_labels:
        gputype: 'V100'

 

 

  • 설치 (cluster.yml 파일이 위치한 kubespray 디렉토리 안에서 명령어 실행)
    • ansible-playbook : ansible 실행 명령어
    • -i 경로 : hosts파일 경로 지정 
    • --become  : root 권한으로 수행
    • cluster.yml : kubernetes 배포 yml
ansible-playbook -i inventory/mycluster/hosts.yaml  --become cluster.yml

 

 

  • 설치 확인
# 노드 확인
kubectl get nodes

# 모든 파드 확인
kubectl get pod -A

 

 

 

 

 

 

 

 

Reference