kubernetes를 쉽게 설치하게 도와주는 자동화 도구로 ansible을 통해 구축하고자하는 설정 값만 맞게 변경해주어 실행하면 kubernetes cluster 구축을 자동으로 해주는 편리한 도구다. Github에 오픈소스로서 공개되어있어 누구나 쉽게 구축할 수 있다는 장점이 있다.
[구축 서버 목록]
- ansible_bastion_host
- k8s-master
- k8s-worker-1
- k8s-worker-2
- k8s-worker-3
$ git clone https://github.com/kubespray/kubespray.git
$ sudo apt install -y python3-pip
이후 clone 받은 kubespray 디렉토리 내부에서 진행
위 패키지 모음을 설치할 때 ansible도 같이 설치되어 별도 설치 필요 x
# 설치 목록은 clone 받은 kubespray 폴더의 requirements.txt 내부에 작성되어 있음
$ sudo pip3 install -r requirements.txt
ansible ssh 연결 실행을 위해 ansible_bastion_host의 rsa pub 키를 remote host들의 authorized_keys 파일에 저장해주어야한다.
$ ssh-keygen -t rsa -C ubuntu
$ sudo vi ~/.ssh/authorized_keys
$ cp -rfp inventory/sample inventory/mycluster
구성할 K8s-cluster의 내용을 각 그룹에 알맞게 설정하여준다.
$ vi inventory/mycluster/inventeory.ini
[all]
master ansible_host=10.178.0.45 ip=10.178.0.45 etcd_member_name=etcd1
node1 ansible_host=10.178.0.46 ip=10.178.0.46
node2 ansible_host=10.178.0.48 ip=10.178.0.48
node3 ansible_host=10.178.0.65 ip=10.178.0.65
# node1 ansible_host=95.54.0.12 # ip=10.3.0.1 etcd_member_name=etcd1
# node2 ansible_host=95.54.0.13 # ip=10.3.0.2 etcd_member_name=etcd2
# node3 ansible_host=95.54.0.14 # ip=10.3.0.3 etcd_member_name=etcd3
# node4 ansible_host=95.54.0.15 # ip=10.3.0.4 etcd_member_name=etcd4
# node5 ansible_host=95.54.0.16 # ip=10.3.0.5 etcd_member_name=etcd5
# node6 ansible_host=95.54.0.17 # ip=10.3.0.6 etcd_member_name=etcd6
# ## configure a bastion host if your nodes are not directly reachable
# [bastion]
# bastion ansible_host=x.x.x.x ansible_user=some_user
[kube-master]
master
# node1
# node2
# node3
[etcd]
master
# node1
# node2
# node3
[kube-node]
node1
node2
node3
# node4
# node5
# node6
[calico-rr]
[k8s-cluster:children]
kube-master
kube-node
calico-rr
$ vi /etc/ansible/ansible.cfg
[defaults]
host_key_checking = False
$ ansible all -m ping -i inventory.ini
node3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
node2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
master | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
node1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
Ansible 세팅 끝
kubespray 설치 playbook인 cluster.yml 파일을 실행하여준다.
# kubespray 디렉토리에서 실행
ansible-playbook -i inventory/mycluster/inventory.ini -become --become-user=root cluster.yml
설치가 완료 된 후 구축 확인을 위해 ansible_bation_host가 아닌 구축된 k8s-master-node에 터미널로 접속
$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready control-plane,master 15m v1.20.4
node1 Ready <none> 14m v1.20.4
node2 Ready <none> 14m v1.20.4
node3 Ready <none> 14m v1.20.4
$ kubectl get all -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system pod/calico-kube-controllers-596bd759d5-wlpdj 1/1 Running 0 14m
kube-system pod/calico-node-gwr45 1/1 Running 0 15m
kube-system pod/calico-node-j6f7w 1/1 Running 0 15m
kube-system pod/calico-node-jnsm8 1/1 Running 0 15m
kube-system pod/calico-node-qp7xg 1/1 Running 0 15m
kube-system pod/coredns-657959df74-dc5bj 1/1 Running 0 14m
kube-system pod/coredns-657959df74-mbxx7 1/1 Running 0 14m
kube-system pod/dns-autoscaler-b5c786945-sv225 1/1 Running 0 14m
kube-system pod/kube-apiserver-master 1/1 Running 0 16m
kube-system pod/kube-controller-manager-master 1/1 Running 0 16m
kube-system pod/kube-proxy-6n8k5 1/1 Running 0 15m
kube-system pod/kube-proxy-ct48q 1/1 Running 0 15m
kube-system pod/kube-proxy-lrx79 1/1 Running 0 15m
kube-system pod/kube-proxy-pktzv 1/1 Running 0 15m
kube-system pod/kube-scheduler-master 1/1 Running 0 16m
kube-system pod/nginx-proxy-node1 1/1 Running 0 15m
kube-system pod/nginx-proxy-node2 1/1 Running 0 15m
kube-system pod/nginx-proxy-node3 1/1 Running 0 15m
kube-system pod/nodelocaldns-7mq62 1/1 Running 0 14m
kube-system pod/nodelocaldns-hnf29 1/1 Running 0 14m
kube-system pod/nodelocaldns-skqgj 1/1 Running 0 14m
kube-system pod/nodelocaldns-vmvjj 1/1 Running 0 14m
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default service/kubernetes ClusterIP 10.233.0.1 <none> 443/TCP 16m
kube-system service/coredns ClusterIP 10.233.0.3 <none> 53/UDP,53/TCP,9153/TCP 14m
NAMESPACE NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
kube-system daemonset.apps/calico-node 4 4 4 4 4 <none> 15m
kube-system daemonset.apps/kube-proxy 4 4 4 4 4 kubernetes.io/os=linux 16m
kube-system daemonset.apps/nodelocaldns 4 4 4 4 4 <none> 14m
NAMESPACE NAME READY UP-TO-DATE AVAILABLE AGE
kube-system deployment.apps/calico-kube-controllers 1/1 1 1 14m
kube-system deployment.apps/coredns 2/2 2 2 14m
kube-system deployment.apps/dns-autoscaler 1/1 1 1 14m
NAMESPACE NAME DESIRED CURRENT READY AGE
kube-system replicaset.apps/calico-kube-controllers-596bd759d5 1 1 1 14m
kube-system replicaset.apps/coredns-657959df74 2 2 2 14m
kube-system replicaset.apps/dns-autoscaler-b5c786945 1 1 1 14m