一、基礎環境 雲主機 下載軟體包 將所有軟體下載至 目錄 master到node做免密認證 設定主機名與host文件 解決DNS解析localhost 此雲主機的DNS解析localhost會解析到一個鬼地址,這是個大坑。kubeadm初始化是會用到localhost。如果你的主機能解析到自己的IP ...
一、基礎環境
雲主機
下載軟體包
將所有軟體下載至/data
目錄
# 鏈接:https://pan.baidu.com/s/13DlR1akNBCjib5VFaIjGTQ 密碼:1l69
master到node做免密認證
ssh-keygen
ssh-copy-id [email protected]
ssh-copy-id [email protected]
ssh-copy-id [email protected]
設定主機名與host文件
# 分別設定node與master的主機名
hostnamectl set-hostname master
exec bash
# 同步所有主機的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
此雲主機的DNS解析localhost會解析到一個鬼地址,這是個大坑。kubeadm初始化是會用到localhost。如果你的主機能解析到自己的IP,那麼這步可以跳過。如果不能則需要自己搭建一個DNS,將localhost解析到自己。
# 1.檢測
[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.1\nnameserver $(hostname -i)" >> /etc/resolv.conf
chmod 444 /etc/resolv.conf
chattr +i /etc/resolv.conf
echo -e "server=8.8.8.8\nserver=8.8.4.4" > /etc/dnsmasq.conf
echo -e "$(hostname -i)\tlocalhost.$(hostname -d)" >> /etc/hosts
service dnsmasq restart
# 3.再次檢測
[root@master ~]# nslookup localhost
Server: 127.0.0.1
Address: 127.0.0.1#53
Name: localhost
Address: 192.168.1.78
同步系統時間
ntpdate 0.centos.pool.ntp.org
關閉防火牆
iptables -F
systemctl stop firewalld
systemctl disable firewalld
關閉SELinux & 關閉swap
swapoff -a
sed -i 's/.*swap.*/#&/' /etc/fstab
setenforce 0
確認時區
timedatectl set-timezone Asia/Shanghai
systemctl restart chronyd.service
修改系統參數
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 # 軟體包在網盤中下載
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
統一k8s與docker的驅動
# 1.查看docker驅動
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
導入基礎鏡像
cd /data
docker load -i k8s-images-1.10.tar.gz
二、初始化master節點
# 初始化master 指定的版本要與kubeadm版本一致
# kubeadm只給定了最少選項,集群名稱等等都沒有指定,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節點加入集群的命令,註意token的有效期
若要通過普通用戶使用群集,需要執行以下步驟
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
# 查看系統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
# 查看集群各組件狀態信息
[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節點cgroup驅動保持一致
sed -i "s/cgroup-driver=systemd/cgroup-driver=cgroupfs/g" /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
# 命令來自集群初始化之後額顯示中
kubeadm join 192.168.1.78:6443 --token v0866r.u7kvg5js1ah2u1bi --discovery-token-ca-cert-hash sha256:7b36794f4fa5121f6a5e309d0e312ded72997a88236a93ec7da3520e5aaccf0e
# master節點查看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
四、部署網路
部署
flannel官網
flannel下載時不用科學上網,flannel的yml文件會自動去quay.io網站中下載鏡像。
# 1.1使用軟體包中的flannel,並指pod映射到哪個主機的網卡上面。
vim kube-flannel.yml
command: [ "/opt/bin/flanneld", "--ip-masq", "--kube-subnet-mgr","-iface=eth0" ]
# 以下要按順序創建,先創建rbac,之前沒有穿件rbac導致pod正常創建,但是pin不同
kubectl apply -f kube-flannel-rbac.yml
kubectl apply -f kube-flannel.yml
# 後,節點的狀態會變為ready
[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.從官網下載最新的flannel,k8s1.7+ 直接執行以下命令即可
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
flannel配置文件修改
kube-flannel.yml中指定使用的網段
"Network": "10.244.0.0/16"
預設使用16位掩碼,則在各node中都分配一個10.244.0.0/8的網路
五、部署dashboard
kubectl apply -f kubernetes-dashboard-http.yam
kubectl apply -f admin-role.yaml
kubectl apply -f kubernetes-dashboard-admin.rbac.yaml
命令行常用命令
# 查看pod信息,預設顯示default名稱空間下的pod
[root@master ~]# kubectl get pods
No resources found.
# 指定名稱空間寫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
# 顯示更詳細的pod信息,此時各pod中都運行了一個kube-proxy和flannel容器
-o wide 顯示更詳細的信息,報錯node節點iP、主機名
[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
# 清空網路信息
ip link del cni0
ip link del flannel.1
七、踩過的那些坑
- 確保master與node的DNS解析localhost能解析到自己的IP
- node加入master確保token不過期
- node確保kubelet正常啟動並運行
- flannel網路要先創建kube-flannel-rbac.ymal再創建 kube-flannel.yml
八、token過期的解決辦法
# 1.查看已經存在的token
kubeadm token list
# 2.創建token
kubeadm token create
# 3.查看ca證書的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 新創建的Token
# efg456 證書的sha256編碼
# IP+Port Master的IP+Port