一、基礎(chǔ)環(huán)境
云主機(jī)
下載軟件包
將所有軟件下載至/data目錄,下載地址見(jiàn)文后鏈接。
master到node做免密認(rèn)證
ssh-keygen
ssh-copy-id root@192.168.1.237
ssh-copy-id root@192.168.1.100
ssh-copy-id root@192.168.1.188
設(shè)定主機(jī)名與host文件
# 分別設(shè)定node與master的主機(jī)名
hostnamectl set-hostname master
exec bash
# 同步所有主機(jī)的hosts文件
vim /etc/hosts
192.168.1.78 master localhost
192.168.1.237 node1
192.168.1.100 node2
192.168.1.188 node3
解決DNS解析localhost
此云主機(jī)的DNS解析localhost會(huì)解析到一個(gè)鬼地址,這是個(gè)大坑。kubeadm初始化是會(huì)用到localhost。如果你的主機(jī)能解析到自己的IP,那么這步可以跳過(guò)。如果不能則需要自己搭建一個(gè)DNS,將localhost解析到自己。
# 1.檢測(cè)
[root@node2 ~]# nslookup localhost
Server: 118.118.118.9
Address: 118.118.118.9#53
Non-authoritative answer:
Name: localhost.openstacklocal
Address: 183.136.168.91
# 2.搭建DNS
yum -y install dnsmasq
cp /etc/resolv.conf{,.bak}
rm -rf /etc/resolv.conf
echo -e “nameserver 127.0.0.1nnameserver $(hostname -i)” >> /etc/resolv.conf
chmod 444 /etc/resolv.conf
chattr +i /etc/resolv.conf
echo -e “server=8.8.8.8nserver=8.8.4.4” > /etc/dnsmasq.conf
echo -e “$(hostname -i)tlocalhost.$(hostname -d)” >> /etc/hosts
service dnsmasq restart
# 3.再次檢測(cè)
[root@master ~]# nslookup localhost
Server: 127.0.0.1
Address: 127.0.0.1#53
Name: localhost
Address: 192.168.1.78
# 4.添加域名解析
vim /etc/dnsmasq.conf
address=/www.baidu.com/123.123.123.123
同步系統(tǒng)時(shí)間
ntpdate 0.CentOS.pool.ntp.org
關(guān)閉防火墻
iptables -F
systemctl stop firewalld
systemctl disable firewalld
關(guān)閉SELinux & 關(guān)閉swap
swapoff -a
sed -i ‘s/.*swap.*/#&/’ /etc/fstab
setenforce 0
確認(rèn)時(shí)區(qū)
timedatectl set-timezone Asia/Shanghai
systemctl restart chronyd.service
修改系統(tǒng)參數(shù)
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl –system
安裝docker
tar -xvf docker-packages.tar
cd docker-packages
yum -y install local *.rpm
systemctl start docker && systemctl enable docker
配置鏡像加速器
vim /etc/docker/daemon.json
{
“registry-mirrors”: [“https://lw9sjwma.mirror.aliyuncs.com”]
}
systemctl daemon-reload
systemctl restart docker
配置k8s的yum源
vim /etc/yum.repos.d/k8s.repo
[k8s]
name=k8s
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=0
獲取kube軟件包
cd kube-packages-1.10.1 # 軟件包在網(wǎng)盤(pán)中下載
tar -xvf kube-packages-1.10.1.tar
cd kube-packages-1.10.1
yum -y install local *.rpm
systemctl start kubelet && systemctl enable kubelet
統(tǒng)一k8s與docker的驅(qū)動(dòng)
# 1.查看docker驅(qū)動(dòng)
docker info | Cgroup Driver
Cgroup Driver: cgroupfs
# 修改k8s配置文件與docker保持一致
sed -i “s/cgroup-driver=systemd/cgroup-driver=cgroupfs/g” /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
導(dǎo)入基礎(chǔ)鏡像
cd /data
docker load -i k8s-images-1.10.tar.gz
二、初始化master節(jié)點(diǎn)
# 初始化master 指定的版本要與kubeadm版本一致
# kubeadm只給定了最少選項(xiàng),集群名稱等等都沒(méi)有指定,kubeadm init
[root@master ~]# kubeadm init –kubernetes-version=v1.10.1 –pod-network-cidr=10.244.0.0/16
# 初始化完成后得到如下信息
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 192.168.1.78:6443 –token qabol0.c2gq0uyfxvpqr8bu –discovery-token-ca-cert-hash sha256:2237ec7b8efd5a8f68adcb04900a0b17b9df2a78675a7d62b4aef644a7f62c05
# kubeadm join 是node節(jié)點(diǎn)加入集群的命令,注意token的有效期
如果以后要通過(guò)其他普通用戶運(yùn)行k8s,那么切換用戶后執(zhí)行,否則root下直接執(zhí)行
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
基本命令
# 查看pods
kubectl get pods
# 查看系統(tǒng)pods
[root@master ~]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
etcd-master 0/1 Pending 0 1s
kube-apiserver-master 0/1 Pending 0 1s
kube-controller-manager-master 0/1 Pending 0 1s
kube-dns-86f4d74b45-d42zm 0/3 Pending 0 8h
kube-proxy-884h6 1/1 NodeLost 0 8h
kube-scheduler-master 0/1 Pending 0 1s
# 查看集群各組件狀態(tài)信息
[root@master ~]# kubectl get componentstatuses
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-0 Healthy {“health”: “true”}
You have new mail in /var/spool/mail/root
三、node加入集群
# 確保node節(jié)點(diǎn)cgroup驅(qū)動(dòng)保持一致
sed -i “s/cgroup-driver=systemd/cgroup-driver=cgroupfs/g” /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
# 命令來(lái)自集群初始化之后額顯示中
kubeadm join 192.168.1.78:6443 –token v0866r.u7kvg5js1ah2u1bi –discovery-token-ca-cert-hash sha256:7b36794f4fa5121f6a5e309d0e312ded72997a88236a93ec7da3520e5aaccf0e
# master節(jié)點(diǎn)查看nodes信息
[root@master data]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master NotReady master 57m v1.10.1
node1 NotReady <none> 27m v1.10.1
node2 NotReady <none> 11s v1.10.1
node3 NotReady <none> 4s v1.10.1
You have new mail in /var/spool/mail/root
四、部署網(wǎng)絡(luò)
部署
flannel官網(wǎng)
flannel下載時(shí)不用科學(xué)上網(wǎng),flannel的yml文件會(huì)自動(dòng)去quay.io網(wǎng)站中下載鏡像。
# 1.1使用軟件包中的flannel,并指pod映射到哪個(gè)主機(jī)的網(wǎng)卡上面。
vim kube-flannel.yml
command: [ “/opt/bin/flanneld”, “–ip-masq”, “–kube-subnet-mgr”,”-iface=eth0″ ]
# 以下要按順序創(chuàng)建,先創(chuàng)建rbac,之前沒(méi)有穿件rbac導(dǎo)致pod正常創(chuàng)建,但是pin不同
kubectl apply -f kube-flannel-rbac.yml
kubectl apply -f kube-flannel.yml
# 后,節(jié)點(diǎn)的狀態(tài)會(huì)變?yōu)閞eady
[root@master1 kubernetes1.10]# kubectl get node
NAME STATUS ROLES AGE VERSION
master Ready master 57m v1.10.1
node1 Ready <none> 27m v1.10.1
node2 Ready <none> 11s v1.10.1
node3 Ready <none> 4s v1.10.1
# 2.從官網(wǎng)下載最新的flannel,k8s1.7+ 直接執(zhí)行以下命令即可
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
flannel配置文件修改
kube-flannel.yml中指定使用的網(wǎng)段
“Network”: “10.244.0.0/16”
默認(rèn)使用16位掩碼,則在各node中都分配一個(gè)10.244.0.0/8的網(wǎng)絡(luò)
五、部署dashboard
kubectl apply -f kubernetes-dashboard-http.yam
kubectl apply -f admin-role.yaml
kubectl apply -f kubernetes-dashboard-admin.rbac.yaml
命令行常用命令
# 查看pod信息,默認(rèn)顯示default名稱空間下的pod
[root@master ~]# kubectl get pods
No resources found.
# 指定名稱空間寫(xiě)pod
[root@master ~]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
etcd-master 1/1 Running 0 3h
kube-apiserver-master 1/1 Running 0 3h
kube-controller-manager-master 1/1 Running 0 3h
kube-dns-86f4d74b45-bzbvc 3/3 Running 0 3h
kube-flannel-ds-5ghhj 1/1 Running 0 2h
kube-flannel-ds-ht4xd 1/1 Running 0 3h
kube-flannel-ds-kbm5g 1/1 Running 0 3h
kube-flannel-ds-mlj4r 1/1 Running 0 2h
kube-proxy-9xxnd 1/1 Running 0 3h
kube-proxy-n9w5x 1/1 Running 0 3h
kube-proxy-nkn8c 1/1 Running 0 2h
kube-proxy-shd6l 1/1 Running 0 2h
kube-scheduler-master 1/1 Running 0 3h
kubernetes-dashboard-5c469b58b8-rjfx6 1/1 Running 0 1h
# 顯示更詳細(xì)的pod信息,此時(shí)各pod中都運(yùn)行了一個(gè)kube-proxy和flannel容器
-o wide 顯示更詳細(xì)的信息,報(bào)錯(cuò)node節(jié)點(diǎn)iP、主機(jī)名
[root@master ~]# kubectl get pods -n kube-system -o wide
NAME READY STATUS RESTARTS AGE IP NODE
etcd-master 1/1 Running 0 3h 192.168.1.78 master
kube-apiserver-master 1/1 Running 0 3h 192.168.1.78 master
kube-controller-manager-master 1/1 Running 0 3h 192.168.1.78 master
kube-dns-86f4d74b45-bzbvc 3/3 Running 0 3h 10.244.0.2 master
kube-flannel-ds-5ghhj 1/1 Running 0 2h 192.168.1.188 node3
kube-flannel-ds-ht4xd 1/1 Running 0 3h 192.168.1.78 master
kube-flannel-ds-kbm5g 1/1 Running 0 3h 192.168.1.237 node1
kube-flannel-ds-mlj4r 1/1 Running 0 2h 192.168.1.100 node2
kube-proxy-9xxnd 1/1 Running 0 3h 192.168.1.237 node1
kube-proxy-n9w5x 1/1 Running 0 3h 192.168.1.78 master
kube-proxy-nkn8c 1/1 Running 0 2h 192.168.1.100 node2
kube-proxy-shd6l 1/1 Running 0 2h 192.168.1.188 node3
kube-scheduler-master 1/1 Running 0 3h 192.168.1.78 master
kubernetes-dashboard-5c469b58b8-rjfx6 1/1 Running 0 1h 10.244.0.3 master
六、kubeadm清空配置
# 清空kubectl
kubeadm reset
# 清空網(wǎng)絡(luò)信息
ip link del cni0
ip link del flannel.1
七、踩過(guò)的那些坑
•確保master與node的DNS解析localhost能解析到自己的IP
•node加入master確保token不過(guò)期
•node確保kubelet正常啟動(dòng)并運(yùn)行
•flannel網(wǎng)絡(luò)要先創(chuàng)建kube-flannel-rbac.ymal再創(chuàng)建 kube-flannel.yml
八、token過(guò)期的解決辦法
# 1.查看已經(jīng)存在的token
kubeadm token list
# 2.創(chuàng)建token
kubeadm token create
# 3.查看ca證書(shū)的sha256編碼
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed ‘s/^.* //’
# 4.node使用新的token加入集群
kubeadm join –token acb123 –discovery-token-ca-cert-hash sha256:efg456 172.16.6.79:6443 –skip-preflight-checks
# abc123 新創(chuàng)建的Token
# efg456 證書(shū)的sha256編碼
# IP+Port Master的IP+Port
——————————————分割線——————————————
本文用到的相關(guān)資料可從以下信息得到下載:
點(diǎn)擊這個(gè)http://www.linuxidc.com/Linux/2013-12/93755.htm 鏈接關(guān)注 Linux公社官方微信,關(guān)注后回復(fù)數(shù)字155392。即可得到網(wǎng)友的分享密碼。
如果取消關(guān)注Linux公社公眾號(hào),即使再次關(guān)注,也將無(wú)法提供本服務(wù)!
鏈接:https://pan.baidu.com/s/1AEVFswevLLwcf6kKjwd4kg 密碼:獲得見(jiàn)上面的方法,地址失效請(qǐng)?jiān)谙旅媪粞浴?/p>
——————————————分割線——————————————