1.腳本部署
#/bin/bash
hostnamectl set-hostname k8s-master1
echo "172.19.16.10 k8s-master1" >> /etc/hosts
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
swapoff -a
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
modprobe br_netfilter
lsmod | grep br_netfilter
cd /etc/yum.repos.d
mv CentOS-Base.repo CentOS-Base.repo.bak
curl -o CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
sed -i 's/gpgcheck=1/gpgcheck=0/g' /etc/yum.repos.d/CentOS-Base.repo
curl -o docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[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
yum clean all
yum makecache
yum repolist
yum list docker-ce --showduplicates | sort -r
yum install docker-ce-19.03.9 docker-ce-cli-19.03.9 containerd.io -y
systemctl start docker
systemctl enable docker
tee /etc/docker/daemon.json <<-'EOF'
{"registry-mirrors":["https://reg-mirror.qiniu.com/"]}
EOF
systemctl daemon-reload
systemctl restart docker
#安裝kubeadm、kubelet和kubectl(根據(jù)需求 指定版本號 如果不指定 默認(rèn)拉取最新的版本)
yum -y install kubelet-1.20.5 kubeadm-1.20.5 kubectl-1.20.5
systemctl enable kubelet
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> /etc/profile
source /etc/profile
#address=172.19.16.10需要填寫服務(wù)器內(nèi)網(wǎng),用公網(wǎng)無法啟動(dòng)
kubeadm init \
--apiserver-advertise-address=172.19.16.10 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.20.5 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16\
--ignore-preflight-errors=NumCPU
#安裝calico網(wǎng)絡(luò)插件
wget https://docs.projectcalico.org/v3.8/manifests/calico.yaml
#value改成第4步中的pod-network-cidr的IP:10.244.0.0/16
sed -i "s/192.168/10.244/g" calico.yaml
kubectl apply -f calico.yaml
默認(rèn)token有效期為24小時(shí),當(dāng)過期之后,該token就不可用了。這時(shí)就需要重新創(chuàng)建token,可以直接使用命令快捷生成:
kubeadm token create --print-join-command
2.部署dashboard
Dashboard是官方提供的一個(gè)UI,可用于基本管理K8s資源。
1、YAML下載地址:
https://raw.githubusercontent.com/kubernetes/dashboard/v2.4.0/aio/deploy/recommended.yaml
課件中文件名是:kubernetes-dashboard.yaml
默認(rèn)Dashboard只能集群內(nèi)部訪問,修改Service為NodePort類型,暴露到外部:
# 默認(rèn) dashboad 只能集群內(nèi)部訪問,修改 service 為 nodeport 類型,暴露到外部
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.4.0/aio/deploy/recommended.yaml
vi recommended.yaml
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
ports:
- port: 443
targetPort: 8443
nodePort: 30001
selector:
k8s-app: kubernetes-dashboard
type: NodePort
# 安裝dashboard
kubectl apply -f recommended.yaml
kubectl get pods -n kubernetes-dashboard
創(chuàng)建 service account 并綁定默認(rèn) cluster-admin 管理員集群角色:
# 創(chuàng)建用戶
$ kubectl create serviceaccount dashboard-admin -n kube-system
# 用戶授權(quán)
$ kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
# 獲取用戶Token
$ kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
訪問地址:https://nodeip:30001,使用輸出的 token 登錄 dashboard
當(dāng)創(chuàng)建單機(jī)版的 k8s 時(shí),這個(gè)時(shí)候 master 節(jié)點(diǎn)是默認(rèn)不允許調(diào)度 pod 。
kubectl taint nodes --all node-role.kubernetes.io/master-
將 master 標(biāo)記為可調(diào)度即可
設(shè)置污點(diǎn)
NoSchedule: 一定不能被調(diào)度
PreferNoSchedule: 盡量不要調(diào)度
NoExecute: 不僅不會調(diào)度, 還會驅(qū)逐Node上已有的Pod
kubectl taint nodes node1 key1=value1:NoSchedule
kubectl taint nodes node1 key1=value1:NoExecute
kubectl taint nodes node1 key2=value2:NoSchedule
刪除污點(diǎn)
kubectl taint node node1 key1:NoSchedule- # 這里的key可以不用指定value
kubectl taint node node1 key1:NoExecute-
kubectl taint node node1 key1- # 刪除指定key所有的effect
kubectl taint node node1 key2:NoSchedule-
卸載K8s
關(guān)于下載大家不要有什么心里壓力,想卸載就卸載,想重新安裝就安裝,就是依賴鏡像的版本需要注意下,別還了版本忘了換以來鏡像
kubeadm reset -f
yum -y remove kubelet kubeadm kubectl
rm -rvf $HOME/.kube
rm -rvf ~/.kube/
rm -rvf /etc/kubernetes/
rm -rvf /etc/systemd/system/kubelet.service.d
rm -rvf /etc/systemd/system/kubelet.service
rm -rvf /usr/bin/kube*
rm -rvf /etc/cni
rm -rvf /opt/cni
rm -rvf /var/lib/etcd
rm -rvf /var/etcd
實(shí)際測試清理的比較干凈,然后可以繼續(xù)重新安裝啦
3…錯(cuò)誤總結(jié)
問題:第3第4步版本拉取不一致導(dǎo)致出現(xiàn)
this version of kubeadm only supports deploying clusters with the control plane version >= 1.27.0. Current version: v1.20.5 To see the stack trace of this error execute with --v=5 or higher
解決方法:移除后指定對應(yīng)版本
yum remove -y kubelet kubeadm kubectl
yum -y install kubelet-1.20.5 kubeadm-1.20.5 kubectl-1.20.5
問題:因?yàn)榈?步環(huán)境變量設(shè)置的是臨時(shí)的,重啟或其他一些行為就會導(dǎo)致這個(gè)問題
The connection to the server localhost:8080 was refused - did you specify the right host or port?
解決方法:設(shè)置永久環(huán)境變量
vim /etc/profile
export KUBECONFIG=/etc/kubernetes/admin.conf
source /etc/profile
#安裝Calico網(wǎng)絡(luò)插件
wget https://docs.projectcalico.org/v3.8/manifests/calico.yaml #如果下載不了就用瀏覽器訪問,復(fù)制源碼粘貼。記得在calico.yaml文件里的625行處把192.168.0.0/16修改為10.244.0.0/16。
報(bào)錯(cuò)詳情:
您可以嘗試添加 --skip-broken 選項(xiàng)來解決該問題
您可以嘗試執(zhí)行:rpm -Va --nofiles --nodigest文章來源:http://www.zghlxwxcb.cn/news/detail-729495.html
yum makecache fast
curl -o /etc/yum.repos.d/ContOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
然后從新部署之后成功文章來源地址http://www.zghlxwxcb.cn/news/detail-729495.html
到了這里,關(guān)于k8s單節(jié)點(diǎn)部署(僅master)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!