쿠버네티스 v1.13 버전대에서 HA 구성 (마스터 노드 2개)
HA in Kubernetes v1.13
VM 설정 및 클러스터 설정
VM settings and Configure HA cluster
Ubuntu 18.04에서 쿠버네티스 v1.13.4와 vagrant VM을 사용하여 클러스터 구축 방법을 설명합니다.
This article explains that creating kubernets cluster (v1.13.4) on Ubuntu 18.04 vagrant VM.
$ vagrant status
Current machine states:
k8s-1 poweroff (virtualbox)
k8s-2 poweroff (virtualbox)
k8s-3 poweroff (virtualbox)
k8s-4 poweroff (virtualbox)
k8s-1, k8s-2 노드 2개를 마스터 노드로, 나머지 2개를 워커 노드로 사용하는 환경입니다.
Vagrant 파일에 사용할 VM 스펙을 설정해 줍니다. network 설정 시 private을 사용해도 무방하지만 리눅스 라우팅 순서로 인해 클러스터링이 잘 묶이지 않는 이슈가 있습니다. 따라서 public으로 진행하는 것을 추천합니다.
Vagrant 파일에 쿠버네티스 클러스터에 필요한 구성요소 설치 과정이 포함되어 있습니다.
There are two master nodes (k8s-1 and k8s-2) and two worker nodes.
Specify VMs in Vagrant file. In this case, we are going to use public network configuration for VMs. If you set up private, it's okay but the clustering would not going well. Installation process of componets like docker, kubeadm is included.
$ cat Vagrantfile
# -*- mode: ruby -*-# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box_check_update = false
config.vm.box = "ubuntu/bionic64"
node_subnet = "10.254.1"
# config.vm.network "private_network", type: "dhcp"
(1..4).each do |i|
config.vm.define "k8s-#{i}" do |node|
node.vm.box = "ubuntu/bionic64"
node.vm.hostname = "k8s-#{i}"
# node.vm.network "private_network", ip: "#{node_subnet}.#{i+1}", virtualbox__intnet: true, gateway: "10.254.1.1"
node.vm.network "public_network", bridge: "[Network_Interface_name]", gateway: "192.168.1.1"
node.vm.provider "virtualbox" do |vb|
vb.name = "k8s-#{i}"
vb.gui = false
vb.cpus = 2
vb.memory = "4096"
node.vm.provision "bootstrap", type: "shell", inline: <<-SHELL
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo add-apt-repository "deb https://apt.kubernetes.io/ kubernetes-xenial main"
sudo apt-get update
kube_version="1.13.4-00"
kube_cni_version="0.6.0-00"
docker_version="18.06.1~ce~3-0~ubuntu"
packages="${packages}
docker-ce=${docker_version}
kubernetes-cni=${kube_cni_version}
kubelet=${kube_version}
kubeadm=${kube_version}
kubectl=${kube_version}"
sudo apt-get -y --allow-unauthenticated install ${packages}
sudo usermod -aG docker vagrant
sudo systemctl enable docker.service
sudo apt-get -y install keepalived
sudo systemctl enable keepalived.service
sudo echo "#{node_subnet}.#{i + 1} k8s-#{i}" | sudo tee -a /etc/hosts
sudo swapoff -a
SHELL
end
end
end
end
구성을 마친 뒤 VM을 실행 시킨 뒤 k8s-1 노드에 SSH로 접속한 후 keepalived를 설정 해 줍니다.
쿠버네티스 클러스터 대표 IP는 192.168.123.234 입니다. k8s-1 노드의 해당 네트워크 대역 IP를 가지고 있는 인터페이스 이름을 지정해 줍니다.
After provisioning the VMs, connect to k8s-1 first. Set up the keepalived for creating master IP of our kubernetes cluster.
k8s-1 $ sudo vi /etc/keepalived/keepalived.conf
vrrp_instance VI_123 {
state MASTER
interface enp0s8
virtual_router_id 123
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass k8stest
}
virtual_ipaddress {
192.168.123.234/17
}
}
설정을 마친 뒤 keepalived 서비스를 재시작 해 주고, status 및 대표 IP를 새로 띄웠는지 확인해 줍니다.
Restart the keepalived service and check it is working.
$sudo systemctl restart keepalived
$sudo systemctl status keepalived
$ip addr |grep global
keepalived 설정 과정을 k8s-2 노드에서도 진행해 줍니다. 이 때, 설정 파일에 priority를 낮은 숫자로, state를 BACKUP으로 설정해 줍니다.
Repeat the set up process of keepalived in k8s-2 node. You should set the priority to lower than k8s-1, and state to BACKUP.
k8s-2 $ sudo vi /etc/keepalived/keepalived.conf
vrrp_instance VI_123 {
state BACKUP
interface enp0s8
virtual_router_id 123
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass k8stest
}
virtual_ipaddress {
192.168.123.234/17
}
}
다시 k8s-1 노드로 돌아와 클러스터 설정을 진행합니다. 클러스터 생성은 다음 설정 파일을 기준으로 합니다.
Return to k8s-1, create the kubernetes cluster with kubeadm configuration file.
$ cat kubeadm-config.yml
apiVersion: kubeadm.k8s.io/v1beta1
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: "192.168.123.234"
bindPort: 6443
---
apiVersion: kubeadm.k8s.io/v1beta1
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controlPlaneEndpoint: 192.168.123.234:6443
kind: ClusterConfiguration
kubernetesVersion: v1.13.4
v1.13에서 사용하는 v1beta1 문서는 다음을 참고 했습니다.
v1beta1 docs references here.
https://godoc.org/k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta1
클러스터를 생성해 줍니다.
Create cluster
k8s-1 $ sudo kubeadm init --config kubeadm-config.yml
완료 후 kubectl 명령 사용을 위한 kubeconfig 설정을 진행합니다.
After creation, make kubeconfig for using kubectl.
k8s-1$ sudo mkdir $HOME/.kube
k8s-1$ sudo cp -a /etc/kubernetes/admin.conf $HOME/.kube/config
k8s-1$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl을 사용해서 현재 클러스터 상태를 확인할 수 있습니다.
You can check the cluster node status with command.
$ kubectl get node -owide
Additional control plane (master node)
추가 마스터노드 설정은 k8s-2 노드에서 진행합니다. 먼저 첫번째 마스터 노드인 k8s-1 에서 클러스터 조인을 위한 명령어 및 토큰을 만듭니다.
Additional master node is k8s-2. You can print out the join command on k8s-1.
k8s-1$ sudo kubeadm token create --print-join-command
그리고 가장 중요한 인증서 복사를 진행합니다. k8s-1 노드에서 클러스터를 생성하면서 같이 만들어진 인증서 들을 k8s-2 노드에 복사해 줍니다. 일일히 복사도 가능하지만 해당 디렉토리의 인증서를 모두 복사한 뒤 필요한 파일을 제외한 나머지 인증서를 삭제해 주었습니다.
k8s-1$ sudo cp -a /etc/kubernetes/pki ./
k8s-1$ sudo rm ./pki/apiserver*
k8s-1$ sudo rm ./pki/front-proxy-client.*
k8s-1$ sudo rm ./pki/etcd/healthcheck-client.*
k8s-1$ sudo rm ./pki/etcd/peer.*
k8s-1$ sudo rm ./pki/etcd/server.*
추가 마스터노드 생성을 위한 인증서 파일은 다음과 같습니다.
This is very important part to create HA cluster. You must copy these cert files on k8s-1 to k8s-2 in same directory.
/etc/kubernetes/pki/ca.crt
/etc/kubernetes/pki/ca.key
/etc/kubernetes/pki/sa.key
/etc/kubernetes/pki/sa.pub
/etc/kubernetes/pki/front-proxy-ca.crt
/etc/kubernetes/pki/front-proxy-ca.key
/etc/kubernetes/pki/etcd/ca.crt
/etc/kubernetes/pki/etcd/ca.key
k8s-2 노드에 동을 경로에 해당 인증서들을 복사해 준 뒤 조인 커맨드를 입력합니다. 이 때, --experimental-control-plane 파라미터를 추가하여 명령어를 실행합니다.
Give --experimental-control-plane argument end of the join command. Now you can join the cluster with command without problem.
k8s-2$ sudo kubeadm join 192.168.123.234:6443 --token TOEKN.TOKEN --discovery-token-ca-cert-hash sha256:HASHES --experimental-control-plane
Worker nodes
남은 워커 노드들은 추가 파라미터 없이 조인 커맨드를 입력해 줍니다.
Other worker nodes could join with join command without additional argument.
k8s-3$ sudo kubeadm join 192.168.123.234:6443 --token TOEKN.TOKEN --discovery-token-ca-cert-hash sha256:HASHES
Network plugin
클러스터에 파드를 배포하고 서비스를 하기 위해서는 네트워크 플러그인 설치가 필요합니다.
Network plugin must be installed for services.
Weavenet CNI
master-node$ kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
Kubernetes Dashboard
쿠버네티스 버전에 맞는 대시보드를 설치합니다.
You can check the kubernetes and dashboard compatibility on this link.
github.com/kubernetes/dashboard/releases?after=v2.0.0-beta7
'Devops' 카테고리의 다른 글
Geofront server with automatic colonize: ssh key management (0) | 2021.06.17 |
---|---|
Prometheus & Grafana: Docker swarm monitoring (0) | 2021.06.17 |
Jenkins: Restart the server with URL (0) | 2020.05.25 |
ElasticSearch: Install and configure the Curator (0) | 2020.05.25 |
AWX: Inventory sync fail when the SCM has vault files (0) | 2020.05.22 |