Kubernetes
[K8S][폐쇄망]Kubespray로 Kubernetes 설치하기 - Ubuntu
판다조아
2025. 3. 20. 23:59
설치 환경
- OS : Ubuntu 22.04
- Kubernetes : v1.27.7
- Kubespray : v2.23
- ansible-core : v2.12.5
- docker : v20.10
- Kubernetes 설치 최소 버전 : v1.25
- Python 필요 버전 : v3.9 - v3.11
설치 파일
설치 디렉토리 (ex. install/)에 필요 파일들 준비
☞ [K8S][폐쇄망] Kubespray 폐쇄망 설치 파일 준비하기
☞ [Ubuntu] local-repo + docker-ce repo 생성하기
☞ https://www.python.org/downloads/
install/ 🤐 kubespray_cache_2.23.0.tgz 🤐 kubespray-2.23.0.tgz 🤐 k9s_Linux_amd64.tar.gz 🤐 Python-3.10.12.tgz 📁 repo ㄴ🤐 offline-files.tar.gz ㄴ🤐 docker-ce-jammy.tar.gz ㄴ🤐 ubuntu20-repo.tar.gz 📁 pip ㄴ📁 kubespray ㄴ🤐 ansible-5.7.1.tar.gz ㄴ🤐 ansible-core-2.12.5.tar.gz ㄴ📄 ansible 필요 패키지들.whl ㄴ📁 virtualenv ㄴ📄 virtualenv 패키지들.whl |
환경 설정
- 방화벽 및 SELINUX 끄기 (모든 노드에 설정)
sudo su -
# swap해제
swapoff -a
# 방화벽 해제 (방화벽 없을 시, 생략)
sudo ufw disable
# selinux 해제 (selinux 없을 시, 생략)
vi /etc/selinux/config
'''
SELINUX=disabled
'''
reboot
- sudo 권한 설정 (모든 노드에 설정)
vi /etc/sudoers
# 파일 하단에 입력
hyun ALL=(ALL:ALL) ALL
hyun ALL=NOPASSWD: ALL
Repo 설정) 1. python 설치하기
- python 3.10 설치
cd python3.10
dpkg -i *.deb
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
update-alternatives --config python3
ln -s /usr/lib/python3/dist-packages/apt_inst.cpython-38-x86_64-linux-gnu.so /usr/lib/python3/dist-package/apt_inst.so
ln -s /usr/lib/python3/dist-packages/apt_pkg.cpython-38-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_pkg.so
python3.10 -m ensurepip --default-pip
Repo 설정) 2. local-repo를 webserver로 띄워서 사용하기
- install/repo → tar 파일들 모두 압축 해제
tar xvf offline-files-2.23.0.tar.gz
tar xvf ubuntu20-repo.tar.gz
tar xvf docker-ce-jammy.tgz
- ubuntu repository 설정 (모든 노드에 설정)
- vi /etc/apt/sources.list 파일은 모두 주석 처리
vi /etc/apt/sources.list.d/sources.list
deb [trusted=yes] http://{Master1번 IP}:50000/ubuntu jammy main restricted universe multiverse
deb [trusted=yes] http://{Master1번 IP}:50000/ubuntu jammy-security main restricted universe multiverse
deb [trusted=yes] http://{Master1번 IP}:50000/ubuntu jammy-updates main restricted universe multiverse
deb [trusted=yes] http://{Master1번 IP}:50000/ubuntu jammy-proposed main restricted universe multiverse
deb [trusted=yes] http://{Master1번 IP}:50000/ubuntu jammy-backports main restricted universe multiverse
- local-repo 파일이 있는 디렉토리에서 다음 명령어 실행 (/repo)
cd repo/
python3 -m http.server 50000 &
# repo 적용 & 필수 패키지 설치
apt update
apt upgrade
apt install build-essential
- repo webserver 접속 확인
- http://IP:PORT
Kubernetes 설치
- /tmp 디렉토리에 kubespray_cache.tgz 파일 풀기
tar xvf kubespray_cache_2.23.0.tgz -C /tmp
- kubespray 압축해제 및 이동
tar xvf kubespray-2.23.0.tgz
cd kubespray-2.23/
- 필수 패키지 설치
apt install -y sshpass
- virtualenv 설치
# 오프라인 설치 명령 --no-index -f [파일 찾을 경로] [찾을 파일]
pip3 install --no-index -f /root/install/pip/virtualenv virtualenv
- virtualenv 생성 및 활성화
VENVDIR=kubespray-venv
virtualenv --python=$(which python3) $VENVDIR
source $VENVDIR/bin/activate
- ansible 설치
pip3 install -U --no-index -f /root/install/pip/kubespray -r /root/install/kubespray-2.23/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: 'hyun'
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: "hyun"
ansible_ssh_private_key_file: '/home/hyun/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'
- inventory/mycluster/group_vars/all/offline.yml 파일 수정
files_repo: "http://{Master 1번 IP}:50000/offline-files"
### If using CentOS, RedHat, AlmaLinux or Fedora
ubuntu_repo: "http://{Master 1번 IP}:50000/ubuntu"
## Ubuntu
### Docker
docker_ubuntu_repo_base_url: "http://{repo server IP:port}/docker-ce"
docker_ubuntu_repo_gpgkey: "http://{repo server IP:port}/docker-ce/gpg"
# {files_repo} 포함하는 부분 전부 주석 해제!!!
- 설치 (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