詳解kubernetes(k8s)安裝及常見坑

kubernetes-install

秉承著爛筆頭不如好記性的的歪門邪道,特此系統(tǒng)的記錄kubernetes的安裝全流程,及踩坑記錄。默默說一句真多。
來吧,我們一起來快速拿下它并且有意識的規(guī)避各種坑,請指教
@[toc]

操作系統(tǒng)初始化

  • 關閉防火墻(all)
# 臨時關閉防火墻
systemctl stop firewalld
# 永久關閉防火墻
systemctl disable firewalld
# 驗證 
systemctl status firewalld 
  • 關閉selinux(all)
# 臨時關閉
setenforce 0
# 永久
sed -i 's/enforcing/disabled/' /etc/selinux/config
  • 關閉swap(all)
# 臨時
 swapoff -a 
# 永久
sed  -ri 's/.*swap.*/#&/' /etc/fstab
  • 設置主機名稱(all)
# 設置名稱(k8s-m-1)忽略大寫字母
hostnamectl set-hostname k8s-m-1
# 驗證
hostname
  • Master添加Hostname(master)
# 設置
cat >> /etc/hosts << EOF
masterIp master
node1Ip node1
node2Ip node2
EOF
# eg
cat >> /etc/hosts << EOF
192.168.50.212 k8s-m-1
192.168.50.87 k8s-n-1
192.168.50.85 k8s-n-2
EOF
  • 將橋接的IPV4 流量傳遞到iptables的鏈(all)
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
# 生效
sysctl --system
  • 時間同步(All)
yum install -y ntpdate 
ntpdate time.windows.com
# 三臺機子輸出如下則成功(相差幾秒或幾分為正?,F象)
image

安裝Docker

官方文檔-安裝

  • Docker安裝sh Script:(All)
# You can use scripts for one click installation,You may need to type enter at the end
# remove docker 
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
# Set up repository
sudo yum install -y yum-utils
# Use Aliyun Docker
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# install docker from yum
yum install  -y docker-ce docker-ce-cli containerd.io
# restart docker
systemctl restart docker
# cat version 
docker --version
image
  • 配置加速(all)
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://etdea28s.mirror.aliyuncs.com"]
}
EOF

# reload
sudo systemctl daemon-reload
sudo systemctl restart docker

# 檢查阿里云加速
image

kubernetes安裝

  • 配置kubernetes源(all)
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

由于官網kubernetes源在國外有墻,直接使用官方源會導致安裝失敗。所以我們配置國內的阿里源

  • 安裝 kubectl kubelet kubeadm(all)
# install kubectl kubelet kubeadm
yum install -y kubectl kubelet kubeadm
# set boot on opening computer
systemctl enable kubelet
  • 初始化k8s部署(Master)
kubeadm init \
--apiserver-advertise-address=youselfIp of Master   \
--image-repository registry.aliyuncs.com/google_containers  \
# 不沖突即可
--service-cidr=10.10.0.0/16 \
--pod-network-cidr=10.122.0.0/16

# eg  
kubeadm init \
--apiserver-advertise-address=192.168.50.212   \
--image-repository registry.aliyuncs.com/google_containers  \
--service-cidr=10.10.0.0/16 \
--pod-network-cidr=10.122.0.0/16

常見錯誤:running with swap on is not supported. Please disable swap

[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-

errors=...`

原因:系統(tǒng)自動進行分區(qū)

解決:

# 臨時
 swapoff -a 
# 永久
sed  -ri 's/.*swap.*/#&/' /etc/fstab
  • following as a regular user(Master)
 mkdir -p $HOME/.kube
 sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
 sudo chown $(id -u):$(id -g) $HOME/.kube/config
  • join master node(node)
kubeadm join 172.16.164.136:6443 --token 9oilao.bpbxcm5zkk0jjcgm --discovery-token-ca-cert-hash sha256:609794bd03915be382bdb130c4c180e89cdc863d35cf99be79cf4ddcbfacee24

加入成功,如下圖

image

此時我們在Master節(jié)點上使用命令kubectl get nodes查看節(jié)點信息:如下圖所示

image

此時的kubectl get nodes的status都是NotNotReady:

查看kubernetes運行狀態(tài):

kubectl get pods -n kube-system

如圖:

image

果然,兩個Pending猶豫未決

此時我們部署CNI網絡,配置如下

# 根據官方文檔提示配置CNI網絡
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# 報錯:The connection to the server raw.githubusercontent.com was refused - did you specify the right host or port? 原因:外網不可訪問 -> 在https://www.ipaddress.com/查詢raw.githubusercontent.com的真實IP。
sudo vi /etc/hosts
199.232.28.133 raw.githubusercontent.com
# 如下
image
# 開啟IPVS,修改ConfigMap的kube-system/kube-proxy中的模式為ipvs

kubectl edit cm kube-proxy -n kube-system 
# 將空的data -> ipvs -> mode中替換如下
mode: "ipvs"
image

在此運行kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml成功,如圖

image

此時運行kubectl get nodes效果圖如下->成功。(肯能并不一定會立馬成功,上面??確定沒問題,請稍等片刻即可)

image

測試kubernetes

# 創(chuàng)建nginx鏡像 Create a deployment with the specified name
# kubectl create deployment NAME --image=image -- [COMMAND] [args...] [options]
kubectl create deployment nginx --image=nginx
# 對外暴露端口
kubectl expose deployment nginx --port=80 --type=NodePort
# 查看pod服務
kubectl get pod,svc

成功

image
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容