Centos 7 버전에 쿠버네티스(kubernetes)를 설치하는 과정을 정리한다. 보다 자세한 내용은 다음 문서를 참고한다.
0. Centos 7 준비
쿠버네티스 테스트 용도로 세 개의 가상 머신을 준비했다. 각 가상 머신에 Centos 7을 설치했고 IP와 호스트 이름을 다음과 같이 설정했다.
- 172.16.1.100 k8s-master
- 172.16.1.101 k8s-node1
- 172.16.1.102 k8s-node2
# hostnamectl set-hostname 호스트이름
1. 도커 설치
전체 서버에 도커를 설치한다. https://docs.docker.com/install/linux/docker-ce/centos/ 문서를 참고해서 설치했다.
# yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# yum install docker-ce
# systemctl start docker && systemctl enable docker
2. kubeadm 설치 준비
kubeadm을 설치하려면 몇 가지 준비를 해야 한다. 전체 서버에서 다음을 진행한다.
SELinux 설정을 permissive 모드로 변경
# setenforce 0
# sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
iptable 설정
# cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
$ sysctl --system
firewalld 비활성화
# systemctl stop firewalld
# systemctl disable firewalld
스왑 오프
스왑 끄기:
# swapoff -a
/etc/fstab 파일에 아래 코드 주석 처리:
#/dev/mapper/centos-swap swap swap defaults 0 0
서버 재시작:
# reboot
3. 쿠버네티스 설치 준비
쿠버네티스 YUM 리포지토리 설정:
# cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kube*
EOF
kubeadm 설치:
# yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
# systemctl enable kubelet && systemctl start kubelet
[쿠버네티스 구성 요소]
쿠버네티스를 구성하는 컴포넌트에 대해 알고 싶다면 https://kubernetes.io/ko/docs/concepts/overview/components/ 문서를 읽어보자. 이 문서를 빠르게 훑어보고 다음 내용을 진행하면 설치 과정에서 용어나 메시지를 이해하는데 도움이 된다.
4. 마스터 컴포넌트 설치
kubeadm init 명령으로 마스터 노드 초기화
kubeadm init 명령어를 이용해서 마스터 노드를 초기화한다. --pod-network-cidr 옵션은 사용할 CNI(Container Network Interface)에 맞게 입력한다. 이 글에서는 CNI로 Flannel을 사용한다고 가정한다.
# kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=172.16.1.100
...생략
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
Your Kubernetes master has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
You can now join any number of machines by running the following on each node
as root:
kubeadm join 172.16.1.100:6443 --token yrc47a.55b25p2dhe14pzd1 --discovery-token-ca-cert-hash sha256:2a7a31510b9a0b0da1cf71c2c29627b40711cdd84be12944a713ce2af2d5d148
마스터 초기화에 성공하면 마지막에 'kubeadm join ....'으로 시작하는 명령어가 출력된다. 이 명령어를 이용해서 작업 노드를 설치하므로 잘 복사해 놓자.
환경 변수 설정
root 계정을 이용해서 kubectl을 실행할 경우 다음 환경 변수를 설정한다.
# export KUBECONFIG=/etc/kubernetes/admin.conf
CNI 설치
이 글에서는 Flannel을 설치한다. 설치 명령어는 다음과 같다.
# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/bc79dd1505b0c8681ece4de4c0d86c5cd2643275/Documentation/kube-flannel.yml
각 CNI별 설치 명령어는 Creating a single master cluster with kubeadm 문서를 참고한다.
마스터 실행 확인
마스터를 설치했다. 다음 명령어를 실행해서 결과를 확인한다.
# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-86c58d9df4-78jbg 1/1 Running 0 9m3s
kube-system coredns-86c58d9df4-q7mwf 1/1 Running 0 9m3s
kube-system etcd-k8s-master 1/1 Running 0 13m
kube-system kube-apiserver-k8s-master 1/1 Running 0 13m
kube-system kube-controller-manager-k8s-master 1/1 Running 0 13m
kube-system kube-flannel-ds-amd64-zv8nc 1/1 Running 0 3m11s
kube-system kube-proxy-xj7hg 1/1 Running 0 14m
kube-system kube-scheduler-k8s-master 1/1 Running 0 13m
5. 노드 컴포넌트 설치
kubeadm init 명령을 이용해서 설치할 때 콘솔에 출력된 메시지에 kubeadm join 명령어가 있었다. 이 명령어를 노드 컴포넌트로 사용할 서버에서 실행한다. 이 예에서는 k8s-node1 서버에서 아래 명령어를 실행했다.
# kubeadm join 172.16.1.100:6443 --token yrc47a.55b25p2dhe14pzd1 --discovery-token-ca-cert-hash sha256:2a7a31510b9a0b0da1cf71c2c29627b40711cdd84be12944a713ce2af2d5d148
첫 번째 슬레이브 노드 추가 후 마스터 노드에서 kubectl get nodes 명령을 실행해보자. master 역할을 하는 k8s-master 노드와 방금 추가한 k8s-node1이 노드 목록에 표시된다. 노드를 추가하자 마자 노드 목록을 조회하면 다음 처럼 아직 사용 준비가 안 된 NotReady 상태임을 알 수 있다.
[root@k8s-master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 18m v1.13.2
k8s-node1 NotReady <none> 30s v1.13.2
k8s-node2 노드에서 두 번째 슬레이브 노드를 추가한 뒤에 다시 노드 목록을 살펴보자. 새로 추가한 노드가 목록에 보인다.
[root@k8s-master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 21m v1.13.2
k8s-node1 Ready <none> 3m28s v1.13.2
k8s-node2 NotReady <none> 15s v1.13.2
kubectl cluster-info 명령어를 실행하면 클러스터 정보를 확인할 수 있다.
[root@k8s-master ~]# kubectl cluster-info
Kubernetes master is running at https://172.16.1.100:6443
KubeDNS is running at https://172.16.1.100:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
6. 클러스터 테스트
마스터와 슬레이브를 설치했으니 간단한 예제를 실행해보자.
luksa/kubia 도커 컨테이너 이미지를 이용해서 팟 만들기:
요즘 학습하고 있는 '쿠버네티스 인 액셕' 책의 예제로 테스트했다. 다음 명령을 실행하자.
# kubectl run kubia --image=luksa/kubia --port 8080 --generator=run-pod/v1
replicationcontroller/kubia created
이 명령은 도커 이미지(luksa/kubia)를 이용해서 쿠버네티스 배포 단위인 파드(pod)를 클러스터에서 실행한다. 참고로 luksa/kubia 이미지는 호스트 이름을 응답하는 간단한 웹 서버다.
파드가 포함한 컨테이너에 연결할 수 있도록 서비스를 생성한다.
[root@k8s-master ~]# kubectl expose rc kubia --type=LoadBalancer --name kubia-http
service/kubia-http exposed
kubectl get services 명령어로 생성한 서비스 정보를 보자. 내가 테스트한 환경에서는 LoadBalancer 타입 서비스가 클러스터 IP로 10.101.195.144를 사용하고 있다.
[root@k8s-master ~]# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3h22m
kubia-http LoadBalancer 10.101.195.144 <pending> 8080:31701/TCP 15s
쿠버네티스 클러스터를 설치한 서버에서 이 클러스터 IP를 이용해서 8080 포트로 연결해보자. 다음과 비슷한 결과가 나오면 쿠버네티스가 정상적으로 동작하고 있는 것이다.
# curl 10.101.195.144:8080
You've hit kubia-ckh8w
-
castinglife 2019.11.29 14:29
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 설치시 제대로 되지 않는d아래와 같은 에러가 발생합니다.
==============================================================================================
Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
Could not fetch/save url https://download.docker.com/linux/centos/docker-ce.repo to file /etc/yum.repos.d/docker-ce.repo: [Errno 14] curl#60 - "Peer's Certificate has expired."
rdate 패키지 설치 ( rdate가 없을 시 yum 으로 설치해준다 )
# yum install -y rdate
명령어를 통해 설치
# rdate -s time.bora.net
# date
Thu Nov 28 23:58:06 EST 2019
정상적으로 현재시간이 찍히는것을 확인할수 있다.
# sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
=============================================================================================
Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
이제는 정상적으로 진행됨..^^
=============================================================================================
-
castinglife 2019.11.29 15:50
————————————————————————————————————
# cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kube*
EOF
————————————————————————————————————
위의 설정을 사용하면 저장소를 못찾는 에러가 발생 합니다.
아래의 설정을 사용하면 정상 설치 됩니다.
————————————————————————————————————
# cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl = http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled = 1
gpgcheck = 0
repo_gpgcheck = 0
gpgkey = http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
exclude=kube*
EOF
————————————————————————————————————
설정후 설치하면 정상적으로 설치 됩니다.
——————————————————————————————————————————————————————————————————
# yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: data.aonenetworks.kr
* extras: data.aonenetworks.kr
* updates: data.aonenetworks.kr
base | 3.6 kB 00:00:00
https://download.docker.com/linux/centos/7/x86_64/stable/repodata/repomd.xml: [Errno 14] curl#60 - "Peer's Certificate has expired."
Trying other mirror.
It was impossible to connect to the CentOS servers.
This could mean a connectivity issue in your environment, such as the requirement to configure a proxy,
or a transparent proxy that tampers with TLS security, or an incorrect system clock.
You can try to solve this issue by using the instructions on https://wiki.centos.org/yum-errors
If above article doesn't help to resolve this issue please use https://bugs.centos.org/.
extras | 2.9 kB 00:00:00
kubernetes | 1.4 kB 00:00:00
updates | 2.9 kB 00:00:00
kubernetes/primary | 59 kB 00:00:01
kubernetes 430/430
Resolving Dependencies
--> Running transaction check
---> Package kubeadm.x86_64 0:1.16.3-0 will be installed
--> Processing Dependency: kubernetes-cni >= 0.7.5 for package: kubeadm-1.16.3-0.x86_64
--> Processing Dependency: cri-tools >= 1.13.0 for package: kubeadm-1.16.3-0.x86_64
---> Package kubectl.x86_64 0:1.16.3-0 will be installed
---> Package kubelet.x86_64 0:1.16.3-0 will be installed
--> Processing Dependency: socat for package: kubelet-1.16.3-0.x86_64
--> Processing Dependency: conntrack for package: kubelet-1.16.3-0.x86_64
--> Running transaction check
---> Package conntrack-tools.x86_64 0:1.4.4-5.el7_7.2 will be installed
--> Processing Dependency: libnetfilter_cttimeout.so.1(LIBNETFILTER_CTTIMEOUT_1.1)(64bit) for package: conntrack-tools-1.4.4-5.el7_7.2.x86_64
--> Processing Dependency: libnetfilter_cttimeout.so.1(LIBNETFILTER_CTTIMEOUT_1.0)(64bit) for package: conntrack-tools-1.4.4-5.el7_7.2.x86_64
--> Processing Dependency: libnetfilter_cthelper.so.0(LIBNETFILTER_CTHELPER_1.0)(64bit) for package: conntrack-tools-1.4.4-5.el7_7.2.x86_64
--> Processing Dependency: libnetfilter_queue.so.1()(64bit) for package: conntrack-tools-1.4.4-5.el7_7.2.x86_64
--> Processing Dependency: libnetfilter_cttimeout.so.1()(64bit) for package: conntrack-tools-1.4.4-5.el7_7.2.x86_64
--> Processing Dependency: libnetfilter_cthelper.so.0()(64bit) for package: conntrack-tools-1.4.4-5.el7_7.2.x86_64
---> Package cri-tools.x86_64 0:1.13.0-0 will be installed
---> Package kubernetes-cni.x86_64 0:0.7.5-0 will be installed
---> Package socat.x86_64 0:1.7.3.2-2.el7 will be installed
--> Running transaction check
---> Package libnetfilter_cthelper.x86_64 0:1.0.0-10.el7_7.1 will be installed
---> Package libnetfilter_cttimeout.x86_64 0:1.0.0-6.el7_7.1 will be installed
---> Package libnetfilter_queue.x86_64 0:1.0.2-2.el7_2 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==========================================================================================================================================================
Installing:
kubeadm x86_64 1.16.3-0 kubernetes 9.5 M
kubectl x86_64 1.16.3-0 kubernetes 10 M
kubelet x86_64 1.16.3-0 kubernetes 22 M
Installing for dependencies:
conntrack-tools x86_64 1.4.4-5.el7_7.2 updates 187 k
cri-tools x86_64 1.13.0-0 kubernetes 5.1 M
kubernetes-cni x86_64 0.7.5-0 kubernetes 10 M
libnetfilter_cthelper x86_64 1.0.0-10.el7_7.1 updates 18 k
libnetfilter_cttimeout x86_64 1.0.0-6.el7_7.1 updates 18 k
libnetfilter_queue x86_64 1.0.2-2.el7_2 base 23 k
socat x86_64 1.7.3.2-2.el7 base 290 k
Transaction Summary
==========================================================================================================================================================
Install 3 Packages (+7 Dependent packages)
Total download size: 57 M
Installed size: 262 M
Downloading packages:
(1/10): conntrack-tools-1.4.4-5.el7_7.2.x86_64.rpm | 187 kB 00:00:00
(2/10): 14bfe6e75a9efc8eca3f638eb22c7e2ce759c67f95b43b16fae4ebabde1549f3-cri-tools-1.13.0-0.x86_64.rpm | 5.1 MB 00:00:01
(3/10): fd6465355a85b8ddbc0b2e7cb073e3a40160c7c359576b86e9b8eca0a2d7805b-kubectl-1.16.3-0.x86_64.rpm | 10 MB 00:00:02
(4/10): b45a63e77d36fc7e1ef84f1cd2f7b84bccf650c8248191a37d20c69564d8b8df-kubeadm-1.16.3-0.x86_64.rpm | 9.5 MB 00:00:04
(5/10): libnetfilter_cthelper-1.0.0-10.el7_7.1.x86_64.rpm | 18 kB 00:00:00
(6/10): libnetfilter_cttimeout-1.0.0-6.el7_7.1.x86_64.rpm | 18 kB 00:00:00
(7/10): socat-1.7.3.2-2.el7.x86_64.rpm | 290 kB 00:00:00
(8/10): libnetfilter_queue-1.0.2-2.el7_2.x86_64.rpm | 23 kB 00:00:00
(9/10): 8a0e2b605c7a616d7cb72c25c9058b2327e41d869046c7c6cb3930f10a3dc012-kubelet-1.16.3-0.x86_64.rpm | 22 MB 00:00:04
(10/10): 548a0dcd865c16a50980420ddfa5fbccb8b59621179798e6dc905c9bf8af3b34-kubernetes-cni-0.7.5-0.x86_64.rpm | 10 MB 00:00:04
----------------------------------------------------------------------------------------------------------------------------------------------------------
Total 6.2 MB/s | 57 MB 00:00:09
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : libnetfilter_cttimeout-1.0.0-6.el7_7.1.x86_64 1/10
Installing : socat-1.7.3.2-2.el7.x86_64 2/10
Installing : kubectl-1.16.3-0.x86_64 3/10
Installing : cri-tools-1.13.0-0.x86_64 4/10
Installing : libnetfilter_queue-1.0.2-2.el7_2.x86_64 5/10
Installing : libnetfilter_cthelper-1.0.0-10.el7_7.1.x86_64 6/10
Installing : conntrack-tools-1.4.4-5.el7_7.2.x86_64 7/10
Installing : kubernetes-cni-0.7.5-0.x86_64 8/10
Installing : kubelet-1.16.3-0.x86_64 9/10
Installing : kubeadm-1.16.3-0.x86_64 10/10
Verifying : kubeadm-1.16.3-0.x86_64 1/10
Verifying : libnetfilter_cthelper-1.0.0-10.el7_7.1.x86_64 2/10
Verifying : conntrack-tools-1.4.4-5.el7_7.2.x86_64 3/10
Verifying : kubelet-1.16.3-0.x86_64 4/10
Verifying : libnetfilter_queue-1.0.2-2.el7_2.x86_64 5/10
Verifying : cri-tools-1.13.0-0.x86_64 6/10
Verifying : kubectl-1.16.3-0.x86_64 7/10
Verifying : kubernetes-cni-0.7.5-0.x86_64 8/10
Verifying : socat-1.7.3.2-2.el7.x86_64 9/10
Verifying : libnetfilter_cttimeout-1.0.0-6.el7_7.1.x86_64 10/10
Installed:
kubeadm.x86_64 0:1.16.3-0 kubectl.x86_64 0:1.16.3-0 kubelet.x86_64 0:1.16.3-0
Dependency Installed:
conntrack-tools.x86_64 0:1.4.4-5.el7_7.2 cri-tools.x86_64 0:1.13.0-0 kubernetes-cni.x86_64 0:0.7.5-0
libnetfilter_cthelper.x86_64 0:1.0.0-10.el7_7.1 libnetfilter_cttimeout.x86_64 0:1.0.0-6.el7_7.1 libnetfilter_queue.x86_64 0:1.0.2-2.el7_2
socat.x86_64 0:1.7.3.2-2.el7
Complete!
——————————————————————————————————————————————————————————————————