三台百度云云主机,OS:CentOS 7.3
创建的vpc子网是192.168.0.0/24
vi /etc/hosts
#加入下面3行
192.168.0.49 k8s-master centos-1.shared
192.168.0.48 k8s-node01 centos-2.shared
192.168.0.47 k8s-node02 centos-3.shared
#修改对应的主机名,然后重启
hostnamectl set-hostname k8s-master
reboot
#下面两项在云主机上不需要设置
#临时禁用
swapoff -a
#永久禁用
vim /etc/fstab
注释掉 /dev/mapper/VloGroup-lv_swap swap 所在的行
sed -i -r "/^SELINUX=/c SELINUX=disabled" /etc/selinux/config
systemctl stop firewalld
systemctl disable firewalld
systemctl stop iptables
systemctl disable iptables
创建内核模块载入相关的脚本文件/etc/sysconfig/modules/ipvs.modules,设定自动载入的内核模块。文件内容如下:
#!/bin/bash
ipvs_mods_dir="/usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs"
for i in $(ls $ipvs_mods_dir | grep -o "^[^.]*"); do
/sbin/modinfo -F filename $i &> /dev/null
if [ $? -eq 0 ]; then
/sbin/modprobe $i
fi
done
#运行并检查
chmod +x /etc/sysconfig/modules/ipvs.modules
/etc/sysconfig/modules/ipvs.modules
lsmod | grep ip_vs
yum remove docker*
#更新yum-utils device-mapper-persistent-data lvm2 xfsprogs
yum update -y yum-utils device-mapper-persistent-data lvm2 xfsprogs
#更新Docker CE源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#安装最新版本的Docker CE
yum install -y docker-ce
#启动Docker并设置开机运行
systemctl enable docker
systemctl start docker
systemctl status docker
cat < /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl -p /etc/sysctl.d/k8s.conf
vi /etc/yum.repos.d/k8s.repo
[kubernetes]
name=Kubernetes Repository
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enble=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.pgp
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-key.gpg
如果在下一步安装时报如下错误
GPG key retrieval failed: [Errno 14] HTTPS Error 404 - Not Found
把上面的gpgcheck=1改为0
yum install -y kubeadm kubectl kubelet
systemctl enable kubelet.service
#查看所需的镜像列表,因为google镜像无法国内无法访问,拿到镜像列表后从阿里云镜像站点拉取
kubeadm config images list
#拉取镜像,使用下面的脚本
vi pull-k8s.sh
#!/bin/bash
images=(
kube-apiserver:v1.17.3
kube-controller-manager:v1.17.3
kube-scheduler:v1.17.3
kube-proxy:v1.17.3
pause:3.1
etcd:3.4.3-0
coredns:1.6.5
)
for imageName in ${images[@]} ; do
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName k8s.gcr.io/$imageName
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/$imageName
done
#保存退出
chmod +x pull-k8s.sh
./pull-k8s.sh
#初始化集群
kubeadm init \
--kubernetes-version=v1.17.3 \
--pod-network-cidr=10.244.0.0/16
#集群初始化完成后,创建运行用户
groupadd k8s
useradd -g k8s kube
vi /etc/sudoers
#在root ALL=(ALL) ALL行下面添加一行:kube ALL=(ALL) ALL
#切换到kube用户
su - kube
mkdir .kube
sudo cp -i /etc/kubernetes/admin.conf .kube/config
sudo chown kube:k8s .kube/config
#安装Pod网络插件
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
#以kube用户身份查看是否部署成功
kubectl get pods -n kube-system
#查看集群初始化是否完成
kubectl get node
docker save -o k8s-node.tar k8s.gcr.io/coredns quay.io/coreos/flannel k8s.gcr.io/pause k8s.gcr.io/kube-proxy
scp root@k8s-master:/etc/yum.repos.d/docker-ce.repo /etc/yum.repos.d/
yum install -y docker-ce
systemctl enable docker
systemctl start docker
scp root@k8s-master:/etc/yum.repos.d/k8s.repo /etc/yum.repos.d/
yum install -y kubeadm kubectl kubelet
systemctl enable kubelet
mkdir /run/flannel
scp root@k8s-master:/run/flannel/subnet.env /run/flannel/
scp root@k8s-master:/etc/sysctl.d/k8s.conf /etc/sysctl.d/
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
scp root@k8s-master:/root/k8s-node.tar /root/
docker load --input k8s-node.tar
在Master节点第七步初始化集群成功后会提示加入集群的命令。
kubeadm join 192.168.0.49:6443 --token kxz8tp.8r304lvpx9qjetey \
--discovery-token-ca-cert-hash sha256:3fd5b74ad191fbaeba87d643e617e153a32f523be0a40e05c5537ead36f8ec28
#192.168.0.49为master节点
#token只有24小时有效,重新生成:
kubeadm token create
#token和ca证书sha256编码hash值可通过以下命令获取:
kubeadm token list
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed ‘s/^.* //’
#查看日志
journalctl -f -u kubelet