이번에는 Ubuntu:20.04 위에 쿠버네티스 설치를 해보려고 한다.
이 장을 함께 따라해보고 싶다면 아래 글 2개를 다시 보고 오면 된다.
https://syhwang.tistory.com/43?category=1220695
https://syhwang.tistory.com/44?category=1196270
이제 부터 본격적으로 쿠버네티스를 설치해보자!!
1. master, node1, node2 전원 켜기
2. kubernetes.io 사이트 접속해서 kubeadm 툴을 이용해 Kubernetes 설치하기
https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
2-1. Installing kubeadm
# swap disable
swapoff -a && sed -i '/swap/s/^/#/' /etc/fstab |
# Letting iptables see bridge traffic
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sudo sysctl --system |
# Disable firewall
sudo ufw disable |
# Installing Runtime
Docker 설치 완료, 동작 중..
# Installing kubeadm, kubelet and kubectl
sudo apt-get update && sudo apt-get install -y apt-transport-https ca-certificates curl sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list deb https://apt.kubernetes.io/ kubernetes-xenial main EOF sudo apt-get update sudo apt-get install -y kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubectl systemctl daemon-reload systemctl restart kubelet |
# master component 초기화(*반드시 master에서 실행)
root@master:~# kubeadm init .... (중략) .... 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 Alternatively, if you are the root user, you can run: export KUBECONFIG=/etc/kubernetes/admin.conf 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/ Then you can join any number of worker nodes by running the following on each as root: kubeadm join 10.100.0.104:6443 --token w15f2h.cvpjuo73ex0a1g9a \ --discovery-token-ca-cert-hash sha256:4f2277f0db4708acc70569ca9583485ec6459349625a87fc7dc2958ce4afc8d5 |
이후에 빨간색 부분을 master 장비에서 명령어 실행 시켜준다.
보라색 토큰 값은 나중에 node1, node2에서 master와 cluster 연결시 사용한다.
# Installing a Pod network add-on (Weave Net)
root@master:~# kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')" 이후, weave net이 구성되었는지 확인. root@master:~# kubectl get pod --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE kube-system coredns-78fcd69978-lzt2s 1/1 Running 0 73m kube-system coredns-78fcd69978-rsdgr 1/1 Running 0 73m kube-system etcd-master.example.com 1/1 Running 0 73m kube-system kube-apiserver-master.example.com 1/1 Running 0 73m kube-system kube-controller-manager-master.example.com 1/1 Running 0 73m kube-system kube-proxy-7pmff 1/1 Running 0 73m kube-system kube-scheduler-master.example.com 1/1 Running 0 73m kube-system weave-net-hjhhs 2/2 Running 1 (64m ago) 65m |
# worker node를 master에 join 시키기.
아까 위에서 저장한 토큰 값을 node1, node2에 복사한다.
root@node1:~# kubeadm join 10.100.0.104:6443 --token w15f2h.cvpjuo73ex0a1g9a \ > --discovery-token-ca-cert-hash sha256:4f2277f0db4708acc70569ca9583485ec6459349625a87fc7dc2958ce4afc8d5 [preflight] Running pre-flight checks [preflight] Reading configuration from the cluster... [preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml' [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml" [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env" [kubelet-start] Starting the kubelet [kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap... This node has joined the cluster: * Certificate signing request was sent to apiserver and a response was received. * The Kubelet was informed of the new secure connection details. Run 'kubectl get nodes' on the control-plane to see this node join the cluster. |
* 잠깐 토큰 추가시 아래와 같은 에러 메세지가 발생한다면??
[kubelet-check] It seems like the kubelet isn't running or healthy.
kubelet 상태가 정상적이지 않을 수 있다.
> 확인 방법은 $ systemctl status kubelet
> kubelet이 Running 상태가 아니라면 아래와 같이 kubeadm을 reset하고 다시 시작해보자.
mkdir /etc/docker cat <<EOF | sudo tee /etc/docker/daemon.json { "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2" } EOF 적용 후에, kubeadm reset 후 리부팅 후 다시하면 정상적으로 Join 됨. |
# kubectl get node로 클러스터 확인
root@master:~# kubectl get nodes NAME STATUS ROLES AGE VERSION master.example.com Ready control-plane,master 91m v1.22.2 node1.example.com Ready <none> 7m50s v1.22.2 node2.example.com Ready <none> 4m34s v1.22.2 |
3. kubectl 자동완성 기능 추가
https://kubernetes.io/docs/reference/kubectl/cheatsheet/
위 URL 내용을 보고 좀 쉽게 명령어를 입력할 수 있게 Command 자동 완성 기능을 추가한다.
source <(kubectl completion bash) echo "source <(kubectl completion bash)" >> ~/.bashrc |
4. 심플 테스트
root@master:~# kubectl create deployment web-server --image=nginx deployment.apps/web-server created root@master:~# kubectl scale deployment web-server --replicas=3 deployment.apps/web-server scaled ... root@master:~# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES web-server-6775fb8ff8-k22wf 1/1 Running 0 70s 10.47.0.1 node2.example.com <none> <none> web-server-6775fb8ff8-ldqcq 1/1 Running 0 70s 10.47.0.2 node2.example.com <none> <none> web-server-6775fb8ff8-lx5zn 1/1 Running 0 73s 10.44.0.1 node1.example.com <none> <none> root@master:~# kubectl scale deployment web-server --replicas=2 deployment.apps/web-server scaled NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES web-server-6775fb8ff8-ldqcq 1/1 Running 0 107s 10.47.0.2 node2.example.com <none> <none> web-server-6775fb8ff8-lx5zn 1/1 Running 0 110s 10.44.0.1 node1.example.com <none> <none> root@master:~# kubectl scale deployment web-server --replicas=10 deployment.apps/web-server scaled NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES web-server-6775fb8ff8-4xmgv 1/1 Running 0 54s 10.47.0.5 node2.example.com <none> <none> web-server-6775fb8ff8-5fmdw 1/1 Running 0 54s 10.47.0.4 node2.example.com <none> <none> web-server-6775fb8ff8-65dwh 1/1 Running 0 54s 10.44.0.4 node1.example.com <none> <none> web-server-6775fb8ff8-76fjg 1/1 Running 0 54s 10.44.0.2 node1.example.com <none> <none> web-server-6775fb8ff8-7gpp7 1/1 Running 0 54s 10.44.0.5 node1.example.com <none> <none> web-server-6775fb8ff8-gg7pd 1/1 Running 0 54s 10.47.0.1 node2.example.com <none> <none> web-server-6775fb8ff8-hl5s5 1/1 Running 0 54s 10.47.0.3 node2.example.com <none> <none> web-server-6775fb8ff8-ldqcq 1/1 Running 0 2m47s 10.47.0.2 node2.example.com <none> <none> web-server-6775fb8ff8-lx5zn 1/1 Running 0 2m50s 10.44.0.1 node1.example.com <none> <none> web-server-6775fb8ff8-sdq47 1/1 Running 0 54s 10.44.0.3 node1.example.com <none> <none> |
> nginx 이미지로 pod 생성 후, replicas로 pod 수를 조절해본다.
정상적으로 scale-out이 되는 것을 볼 수 있다.
이상. Ubuntu 20.04에 Kubernetes 설치를 완료했다~
쿠버네티스 구성시 참고하기 좋은 블로그 글 추천
- https://cla9.tistory.com/91?category=814452
- https://cla9.tistory.com/92?category=814452