[CentOS7] Kubernetes(k8s) ์„ค์น˜

2022. 9. 10. 01:50ใ†System ์ž‘์—…์‹ค/DevOps

728x90
๋ฐ˜์‘ํ˜•

 

 

 

 

 

 

๐Ÿš€ Kubernetes ์„ค์น˜

    ๐Ÿ”ฝ  CentOS 7

        ๐Ÿ“ฆ ์ดˆ๊ธฐ ์„ค์ •(Master, Node Worker ๋ชจ๋‘ ์ง„ํ–‰)

setenforce 0

 

sestatus



sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

728x90


์ตœ์ดˆ ์œ„์™€ ๊ฐ™์ด SELinux๊ฐ€ ํ™œ์„ฑํ™” ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ๋‚ด๋ ค์ฃผ์–ด์•ผ ํ•ด์š”.



์ด์ œ ๋ถ€ํ„ฐ ๊ฐ€์ƒํ™”๋กœ ๊ตฌ์„ฑ๋œ Server๋“ค์— SSH๋กœ ๋ถ™์–ด ์ž‘์—…์„ ์ง„ํ–‰ํ• ๊ฒŒ์š”.


systemctl stop firewalld && systemctl disable firewalld

 


๋‹ค์Œ์€ ๋ฐฉํ™”๋ฒฝ์ด ์˜ฌ๋ผ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ๋ฐฉํ™”๋ฒฝ์„ ๋‚ด๋ ค ์ค๋‹ˆ๋‹ค.

์ฐธ๊ณ ๋กœ Network Manager๋ฅผ ๋‚ด๋ ค๋ฒ„๋ฆฌ๋ฉด ์ธํ„ฐ๋„ท์ด ๋Š๊ฒจ ๋ฒ„๋ฆฌ๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.




์ด๋ฒˆ์—๋Š” SWAP์„ ๋น„ํ™œ์„ฑํ™” ์‹œ์ผœ์ฃผ์—ˆ์–ด์š”.

swapoff -a && sed -i '/ swap / s/^/#/' /etc/fstab

๋ฐ˜์‘ํ˜•






 

cat <<EOF>> /etc/modules-load.d/k8s.conf
br_netfilter
EOF




 

cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF



sysctl --system


๋‹ค์Œ์œผ๋กœ ์œ„์™€ ๊ฐ™์ด Iptables Kernel Option์„ ํ™œ์„ฑํ™” ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.


 

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
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF



์ด๋ฒˆ์—๋Š” Kubernetes๋ฅผ ์œ„ํ•œ yum repository๋ฅผ ์„ค์ •ํ•ด ์ฃผ์—ˆ์–ด์š”.
์ด ๊ณณ์—์„œ Kubernetes ๊ด€๋ จ ํ”„๋กœ๊ทธ๋žจ ๋“ฑ์„ ๋‚ด๋ ค๋ฐ›๊ฒŒ ๋  ๊ฒƒ์ด์—์š”.


 


yum repository ์ˆ˜์ • ์‚ฌํ•ญ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— yum update ๋ช…๋ น์–ด๋กœ ์ตœ์‹ ํ™” ํ•ด์ค๋‹ˆ๋‹ค.


192.168.32.10 -> $ hostname k8s-master
192.168.32.11 -> $ hostname k8s-wocker1
192.168.32.12 -> $ hostname k8s-wocker2

# ๋˜๋Š” ์•„๋ž˜ ๋ช…๋ น์–ด๋กœ hostname ๋ณ€๊ฒฝ

$ hostnamectl set-hostname k8s-master

 

vim /etc/hosts


Hostname File์„ ์ˆ˜์ •ํ•ด ์ค„๊ฒŒ์š”.

Kubernetes Node๋“ค์„ ๋ชจ๋‘ ๋“ฑ๋กํ•ด ์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ, ์ด๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ฉด Kubeadm join ์‹œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ด์š”.

์ฃผ๋‹ˆํ•˜๋ž‘์€ ์ผ๋‹จ Master์™€ ๋ชจ๋“  Node์— hosts File์„ ์œ„์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•ด ์ฃผ์—ˆ์–ด์š”.





Hostname์œผ๋กœ Ping Test ์‹œ ์ •์ƒ์ ์œผ๋กœ Ping์ด ๊ฐ€๋Š”๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.


 

 

        ๐Ÿ“ฆ Docker ์„ค์น˜ ๋ฐ ์‹คํ–‰ (Master, Node Worker ๋ชจ๋‘ ์ง„ํ–‰)

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 -y docker


์œ„์™€ ๊ฐ™์ด Docker์™€ ํ•จ๊ป˜ ํ•„์š”ํ•œ Package๋“ค์„ ๋‚ด๋ ค๋ฐ›๊ธฐ ํ•˜์˜€์–ด์š”.

 

systemctl enable --now docker && systemctl start docker


Docker๊ฐ€ Booting ์‹œ ์ž๋™์œผ๋กœ ๊ธฐ๋™๋˜๊ฒŒ ํ•˜๊ณ , ์ง€๊ธˆ ๋ฐ”๋กœ ๊ธฐ๋™๋˜๊ฒŒ ํ•ด ์ฃผ์—ˆ์–ด์š”.



useradd dockeradmin


Docker ์ „์šฉ ๊ณ„์ •์„ ์œ„์™€ ๊ฐ™์ด ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”.



passwd dockeradmin


Docker ๊ด€๋ฆฌ์ž ๊ณ„์ •์˜ Password๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”.





Docker group์„ ๋งŒ๋“ค๊ณ , dockeradmin์„ ํ•ด๋‹น group ์•ˆ์— ๋„ฃ์–ด์ฃผ์—ˆ์–ด์š”.


curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose



curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose




 

chmod +x /usr/local/bin/docker-compose

ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

docker-compose -version





์ด๋ฒˆ์—๋Š” Docker compose๋ฅผ ์„ค์น˜ํ•ด ์ฃผ์—ˆ์–ด์š”.


docker run hello-world


Docker๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์„ค์น˜ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ์ฃผ์—ˆ์–ด์š”.









    ๐Ÿ”ฝ  Kubernetes ์„ค์น˜

        ๐Ÿ“ฆ Master / Node ๋ชจ๋‘ ์ง„ํ–‰

1. 1.15 Version ์„ค์น˜

yum install -y kubeadm-1.15.5-0.x86_64 kubectl-1.15.5-0.x86_64 kubelet-1.15.5-0.x86_64 --disableexcludes=kubernetes



2. 1.20 Version ์„ค์น˜

yum install -y kubeadm-1.20.5-0.x86_64 kubectl-1.20.5-0.x86_64 kubelet-1.20.5-0.x86_64 --disableexcludes=kubernetes

 


์ฃผ๋‹ˆํ•˜๋ž‘์€ 1.20 Version์œผ๋กœ ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ•˜์˜€์–ด์š”.

 

 




    ๐Ÿ”ฝ  ์„ค์ •

        ๐Ÿ“ฆ Master


kubeadm ์ดˆ๊ธฐํ™”๋ฅผ ํ•˜๋ ค๊ณ  ํ–ˆ๋”๋‹ˆ ์•„๋ž˜์™€ ๊ฐ™์ด Error๊ฐ€ ๋ฐœ์ƒํ–ˆ์–ด์š”.
์ตœ์†Œ CPU ๊ฐœ์ˆ˜์™€ Memory ํฌ๊ธฐ๊ฐ€ ๋งž์ง€ ์•Š๋Š”๋‹ค๋Š” ๋‚ด์šฉ์ด์—์š”.

์ฃผ๋‹ˆํ•˜๋ž‘์€ ํ•ด๋‹น ๊ฐ€์ƒ ๋จธ์‹ ์„ ์„ค์ •ํ•  ๋•Œ, CPU ๊ฐœ์ˆ˜ 1๊ฐœ์™€ Memory๋ฅผ 1024MB ๋กœ ๋งž์ถ”์–ด์ฃผ์—ˆ๋Š”๋ฐ, ์ด์— ๋Œ€ํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ด์—์š”.

 

CPU ๊ฐœ์ˆ˜ ๋ณ€๊ฒฝ

 

Memory ํฌ๊ธฐ ๋ณ€๊ฒฝ

 

kubeadm init --pod-network-cidr=10.96.0.0/16 --apiserver-advertise-address={Master Server IP}


์œ„์™€ ๊ฐ™์ด ์ •์ƒ ์ž‘์—…์ด ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.




๋งŒ์•ฝ ์œ„์˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ–ˆ๋Š”๋ฐ, ๋‹ค๋ฅธ ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค๋ฉด kubeadmin init ๋ช…๋ น์–ด๋ฅผ ๋จผ์ € ์‹คํ–‰ํ•ด ์ฃผ์„ธ์š”.

systemctl enable --now kubelet

 

  • apiserver-advertise-address : master1.net์˜ IP ์ฃผ์†Œ
  • pod-network-cidr : Flannel ์—์„œ ๊ถŒ์žฅํ•˜๋Š” ๋„คํŠธ์›Œํฌ ๋Œ€์—ญ

 


์ด๋ ‡๊ฒŒ ์‚ฌ์šฉ์ž ์„ค์ •๊ณผ kubeadmin join ๋ช…๋ น์–ด๋กœ ๊ฐ๊ฐ์˜ Node๋“ค์ด Master Node์— Joinํ•  ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น์–ด์™€ Token ๊ฐ’์ด ์ƒ์„ฑ๋œ ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.



์œ„์—์„œ ๋‚˜์˜จ ๋ช…๋ น์–ด ๊ทธ๋Œ€๋กœ Wocker Node์—์„œ ์‹คํ–‰์„ ํ•ด ์ค๋‹ˆ๋‹ค.


 


Kubernetes ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •์„ ํ•ด ์ฃผ์—ˆ์–ด์š”.
๊ทธ๋Ÿฐ ๋’ค ๋ชจ๋“  PODS๊ฐ€ Running ์ƒํƒœ์ธ์ง€ ํ™•์ธํ•ด ์ฃผ์—ˆ์–ด์š”.

 

kubectl apply -f https://docs.projectcalico.org/v3.8/manifests/calico.yaml

 

kubectl get pods --all-namespaces


์ด๋ฒˆ์—๋Š” Calico ๊ธฐ๋ณธ ์„ค์น˜๋ฅผ ์ง„ํ–‰ ํ•ด ์ฃผ์—ˆ์–ด์š”.
(Kubernetes Cluster Networking Plugin ์„ค์น˜)

๊ทธ๋Ÿฐ ๋’ค ๋ชจ๋“  PODS๊ฐ€ Running ์ƒํƒœ์ธ์ง€ ํ™•์ธํ•ด ์ฃผ์—ˆ์–ด์š”.


Calico๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ 192.68.0.0/16 ๋Œ€์—ญ์„ ์‚ฌ์šฉํ•ด์š”.
๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— IP๊ฐ€ ์ค‘๋ณต๋œ๋‹ค๋Š” Error๊ฐ€ ๋‚œ๋‹ค๋ฉด ์œ„์˜ ๋ฐฉ๋ฒ•(Kubectl apply) ๋ง๊ณ , calico.yaml File์„ ๋‚ด๋ ค๋ฐ›๊ธฐ ํ•œ ๋’ค Code๋ฅผ ์ˆ˜์ •ํ•ด์ค€ ๋’ค Calico๋ฅผ ์„ค์น˜ํ•ด ์ฃผ๋ฉด ๋˜์š”.

curl -O https://docs.projectcalico.org/v3.8/manifests/calico.yaml  
sed s/192.168.0.0\\/16/10.96.0.0\\/12/g -i calico.yaml
kubectl apply -f calico.yaml










    ๐Ÿ”ฝ  ์„ค์ •

        ๐Ÿ“ฆ Woker Node

 

kubectl get nodes


master์™€ Wocker Node๋“ค์— ์ƒํƒœ๋ฅผ ํ™•์ธํ•ด ์ฃผ์—ˆ์–ด์š”.

 







    ๐Ÿ”ฝ  Dashboard ์„ค์น˜

        ๐Ÿ“ฆ Master

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.3.1/aio/deploy/recommended.yaml

์ตœ์ดˆ ์œ„์™€ ๊ฐ™์ด Program์„ ์„ค์น˜ํ•˜์—ฌ ์ค๋‹ˆ๋‹ค.


nohup kubectl proxy --port=8000 --address={Master IP} --accept-hosts='^*$' >/dev/null 2>&1 &


๊ทธ๋Ÿฐ ๋’ค ์œ„์™€ ๊ฐ™์ด Proxy ์„ค์ •์„ ํ•ด ์ฃผ์—ˆ์–ด์š”.


 

kubectl get services -n kubernetes-dashboard

kubectl proxy



Dashboard Service๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ๊ตฌ๋™์ค‘์ธ์ง€ ํ™•์ธํ•œ ๋’ค Proxy๋ฅผ ๊ธฐ๋™ํ•˜์—ฌ ์ฃผ์—ˆ์–ด์š”.



curl http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/


๋‹ค๋ฅธ ํ„ฐ๋ฏธ๋„์„ ์—ด์–ด ์œ„์™€ ๊ฐ™์ด API ํ†ต์‹ ์„ ํ™•์ธํ•œ ๊ฒฐ๊ณผ ์ •์ƒ ํ†ต์‹ ์ด ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.

์œ„์˜ curl์€ kube-proxy๋ฅผ ํ†ตํ•ด API Server๋กœ https:kubernetes-dashboard: ์š”์ฒญ์„ ๋ณด๋‚ด Html ์‘๋‹ต์„ ๋ฐ˜ํ™˜ํ•ด ์ค€ ๊ฒƒ์ด์—์š”.

 

 




    ๐Ÿ”ฝ  TEST

        ๐Ÿ“ฆ Master

kubectl run nginx-test --image=nginx --port 80 --generator=run-pod/v1


์ตœ์ดˆ POD๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์ฃผ์—ˆ์–ด์š”.


 

kubectl expose pod nginx-test 
kubectl get services



๊ทธ๋Ÿฐ ๋’ค ์œ„์™€ ๊ฐ™์ด Service ์ •๋ณด๋ฅผ ํ™•์ธํ•ด ๋ณด์•˜์–ด์š”.



kubectl edit service nginx-test # (ClusterIp -> NodePort) - ๋ณ€๊ฒฝ ์ „



kubectl edit service nginx-test # (ClusterIp -> NodePort) - ๋ณ€๊ฒฝ ํ›„





Service Type์„ ๋ณ€๊ฒฝํ•˜์—ฌ ์ค๋‹ˆ๋‹ค.







๐Ÿง ์ฐธ๊ณ  ์ž๋ฃŒ

https://bono915.tistory.com/258

 

 

 

 

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•