********************* Cluster API Hands-on ********************* Cluster API 란? ======================== Cluster API는 cloud(or baremetal)에 kubernetes 스타일로 정의된 api로 kubernetes cluster를 생성/설정/관리하는 기능이다. 사용자는 설치에 필요한 몇몇 yaml파일을 clusterctl 명령어를 이용해서 배포하면 target cloud에 auto-healing, auto-managing되는 kubernetes cluster를 손쉽게 생성할 수 있다. .. figure:: _static/clusterapi-is.png :: See also: cluster-api-provider-openstack: https://github.com/kubernetes-sigs/cluster-api-provider-openstack Kind: https://github.com/kubernetes-sigs/kind Cluster API Hands-on 구조 ========================== .. figure:: _static/taco-clusterapi-diagram.png --------------- Hands-on 후 알게 되는 내용 =========================== CRD (Custom Resource Definition) --------------------------------- Custom resources can appear and disappear in a running cluster through dynamic registration, and cluster admins can update custom resources independently of the cluster itself. Once a custom resource is installed, users can create and access its objects using kubectl, just as they do for built-in resources like Pods. Controller Pattern ------------------- In applications of robotics and automation, a control loop is a non-terminating loop that regulates the state of the system. In Kubernetes, a controller is a control loop that watches the shared state of the cluster through the API server and makes changes attempting to move the current state towards the desired state. Examples of controllers that ship with Kubernetes today are the replication controller, endpoints controller, namespace controller, and serviceaccounts controller. ---------------- 00-clone-scripts ================== clusterapi-scripts를 clone 받는다. .. code-block:: bash $ git clone https://github.com/openinfradev/openinfraday-2019-clusterapi-scripts.git $ cd openinfraday-2019-clusterapi-scripts 이후에는 ``./01-install-tools.sh`` 와 같이 scripts를 하나씩 실행하면 된다. ------------------ 01-install-tools ================== go, yq, clusterctl 등 필요한 tool들을 설치한다. go install ---------- binary file을 받아 go를 설치하고, PATH 및 GOPATH 설정을 한다. binary file 다운 및 압축 해제 .. code-block:: bash $ wget https://dl.google.com/go/go1.12.7.linux-amd64.tar.gz $ tar -C /usr/local -xzf go1.12.7.linux-amd64.tar.gz $ rm -f ./go1.12.7.linux-amd64.tar.gz bashrc에 다음 두 줄을 추가한다. .. code-block:: yaml $ cat >> ~/.bashrc < ~/clouds.yaml </CentOS-7-1905/g" out/machines.yaml $ sed -i "s//centos/g" out/machines.yaml $ sed -i "s//${NETWORK_UUID}/g" out/machines.yaml $ sed -i "s//${SECURITY_GROUP}/g" out/machines.yaml $ sed -i "s/1.14.0/1.14.3/g" out/machines.yaml 2. floating ip 설정 * 사용되고 있지 않은 floating ip를 조회하여, master와 worker에 각각 하나씩 부여한다. .. code-block:: bash $ FLOATING_IP_1=$(openstack floating ip list | grep None | head -n 1 | awk '{print $4}') $ FLOATING_IP_2=$(openstack floating ip list | grep None | head -n 2 | tail -n 1 | awk '{print $4}') $ FLOATING_IP_LINENUM_1=$(cat out/machines.yaml | grep -n floatingIP | awk '{print $1}' | tr -d ':' | head -n 1) $ FLOATING_IP_LINENUM_2=$(cat out/machines.yaml | grep -n floatingIP | awk '{print $1}' | tr -d ':' | tail -n 1) $ sed -i "${FLOATING_IP_LINENUM_1}s//${FLOATING_IP_1}/" out/machines.yaml $ sed -i "${FLOATING_IP_LINENUM_2}s//${FLOATING_IP_2}/" out/machines.yaml 3. tags, serverMeta 등 불필요한 내용 삭제 * tags와 serverMeta에 대한 내용을 삭제한다. .. code-block:: bash $ TAGS_LINENUM_1=$(cat out/machines.yaml | grep -n tags | awk '{print $1}' | tr -d ':' | head -n 1) $ if [[ ! -z "$TAGS_LINENUM_1" ]]; then sed -i "${TAGS_LINENUM_1},$(($TAGS_LINENUM_1+3))d" out/machines.yaml fi $ TAGS_LINENUM_2=$(cat out/machines.yaml | grep -n tags | awk '{print $1}' | tr -d ':' | tail -n 1) if [[ ! -z "$TAGS_LINENUM_2" ]]; then sed -i "${TAGS_LINENUM_2},$(($TAGS_LINENUM_2+3))d" out/machines.yaml fi 4. home directory로 복사 * 설정이 완료된 yaml 파일을 홈으로 복사한다. .. code-block:: bash $ cp -f out/cluster.yaml ~/ $ cp -f out/machines.yaml ~/ $ cp -f out/provider-components.yaml ~/ --------------- 04-make-cluster ================ clusterctl 로 cluster를 생성한다. create k8s cluster on openstack ------------------------------- *clusterctl이 실행되는 동안 멈추지 말고 기다려야 한다.* .. code-block:: bash $ clusterctl create cluster --bootstrap-type kind --provider openstack -c ~/cluster.yaml -m ~/machines.yaml -p ~/provider-components.yaml $ Useful Commands ---------------- * 140-get-kind-cluster.sh: kind에 생성된 pod를 모두 조회한다. * 141-get-nodes.sh: clsterctl이 끝난 후 구축된 k8s cluster의 node를 조회한다. * 142-logs-kind-controller.sh: clusterctl이 실행될 때 kind의 clusterapi-controller log를 확인한다. * 143-check-user-data-vm.sh: 생성된 master vm의 user-data 파일을 확인한다. YOUR-NODE-IP가 host의 ip로 잘 바뀌었는지 확인한다. * 144-logs-cloud-init-vm.sh: master vm이 생성되고 init되는 과정의 log를 확인한다. * 145-logs-k8s-install-vm.sh: init 이후, vm에 k8s가 구축되는 과정의 log를 확인한다. * 146-delete-kind.sh: kind cluster와 생성된 master 및 worker vm을 삭제한다. clusterctl 도중 문제가 발생했을 경우 이 script를 실행하고 03 script 부터 다시 실행하여 새로운 cluster를 구축한다. ----------------- 05-check-pivot =============== clsuterctl 이 종료된 후, kind에 있던 clusterapi-controller가 kubernetes cluster 내부로 pivot 되었는지 확인한다. master vm으로 ssh 접속하여 k8s namespace를 확인한다. openstack-provider-system namespace의 clusterapi-controller pod를 확인한다. .. code-block:: bash $ MASTER_VM_IP=$(openstack server list | grep master | awk '{print $9}') $ ssh -i ~/.ssh/openstack_tmp centos@${MASTER_VM_IP} -t "sudo kubectl get namespaces" $ ssh -i ~/.ssh/openstack_tmp centos@${MASTER_VM_IP} -t "sudo kubectl get pods -n openstack-provider-system" ---------------- 06-check-clusterapi ==================== cluster가 clusterapi-controller를 통해서 self-healing & self-management가 되고 있는지 확인한다. worker vm을 삭제하고 다시 생성되는지 테스트한다. .. code-block:: bash $ WORKER=$(openstack server list | grep node | awk '{print $4}') $ openstack server delete ${WORKER} worker vm을 삭제했을 때, clusterapi-controller의 log를 확인한다. .. code-block:: bash $ MASTER_VM_IP=$(openstack server list | grep master | awk '{print $9}') $ ssh -i ~/.ssh/openstack_tmp centos@${MASTER_VM_IP} -t "sudo kubectl logs -f clusterapi-controllers-0 -n openstack-provider-system"