kubeadm工具安装Kubernetes集群
一、环境准备
1.1 修改hostname
hostnamectl set-hostname master
1.2 关闭防火墙
systemctl stop firewalld systemctl disable firewalld
1.3 关闭swap
1 swapoff -a 2 修改/etc/fstab 注释掉关于swap部分
二、安装docker
2.1 安装
安装docker 1.更新yum包 yum update 2.卸载旧版本 yum remove docker 3.安装依赖 yum install -y yum-utils device-mapper-persistent-data lvm2 4.设置yum源 yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 5.安装docker yum install docker-ce 6.启动docker systemctl start docker systemctl enable docker 7.验证安装是否成功 docker version
2.2 替换国内源
vi /etc/docker/daemon.json { "registry-mirrors": ["https://registry.docker-cn.com"] } systemctl restart docker.service
2.3 配置k8s源
9 10 11 wget https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg 12 rpm --import rpm-package-key.gpg 13 yum repolist
三、(Master)安装Kubernetes
3.1 安装kubeadm和相关工具
yum install -y kubelet-1.18.3 kubeadm-1.18.3 kubectl-1.18.3 --disableexcludes=kubernets
此时kubectl可能会起不来,可以在kubeadm init完成后再启动
3.2 kubeadm config 将该文件保存备用
执行kubeadm config print init-defaults,获取默认的初始化参数文件 kubeadm config print init-defaults > init.default.yaml
3.3 列出所需的镜像列表
用这个下载镜像 vim images_down.sh
#!/bin/bash images_name=`kubeadm config images list|awk -F '/' '{print $2}'|xargs` for i in $images_name do docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/$i docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/$i k8s.gcr.io/$i docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/$i done
3.4 初始化环境
kubeadm init \
--apiserver-advertise-address=192.168.40.130 \
--service-cidr=10.1.0.0/16 \
--token-ttl 0 \
--pod-network-cidr=10.244.0.0/16
# 如果没有前面下载镜像或者前面那个不好使可以加个参数
--image-repository registry.cn-hangzhou.aliyuncs.com/google_containers
这一步完了会出现连接信息
例如
kubeadm join 192.168.126.133:6443 --token d4k8y9.p0ufoc8sc1bucxta \
--discovery-token-ca-cert-hash sha256:fe7f34f021e4e3a9c975a00bea0d164086497d100b12dc38dcd9de44d4ee1614
--discovery-token-ca-cert-hash sha256:fe7f34f021e4e3a9c975a00bea0d164086497d100b12dc38dcd9de44d4ee1614
3.5 配置授权信息
按照init最后部分提示执行 mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
3.6 配置网络信息(flannel)
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
3.7 Master配置完成
systemctl start kubelet && systemctl enable kubelet
使用命令查看pod情况
kubectl get pods -n kube-system
3.8 Kubernetes官方默认策略是worker节点运行Pod,master节点不运行Pod。如果为了测试或开发目的部署单节点集群,可以通过以下命令设置:
kubectl taint nodes --all node-role.kubernetes.io/master-
3.6 配置node节点
yum install -y kubelet-1.18.3 kubeadm-1.18.3 --disableexcludes=kubernets
kubeadm join 192.168.126.138:6443 --token hoo73q.z19cr577usp5bfj0 --discovery-token-ca-cert-hash sha256:01bd2257b4593479708f7804bd53fa11083fbf5dbf110e00cb1bd6488c8b600b
3.7 查看node节点
kubectl get node
kubectl get pods -n kube-system
四、故障排查
systemctl status kubelet journalctl -xeu kubelet 查看POD详情 kubectl describe pod kube-flannel-ds-amd64-9trbq -n kube-system
删除节点
kubectl delete node node2
节点重置
kubeadm reset
清空单个节点网络信息
ifconfig cni0 down
ip link delete cni0
ifconfig flannel.1 down
ip link delete flannel.1
ifconfig docker0 down
rm -rf /var/lib/cni/
rm -rf /etc/cni/net.d
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables
五、安装nginx测试
5.1 编写pod_nginx.yml
apiVersion: v1 kind: Pod metadata: name: nginx labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80
5.2 创建POD
创建POD
kubectl create -f pod_nginx.yml
查看POD
kubectl get pod
端口映射
kubectl port-forward nginx 8080:80
删除POD
kubectl delete -f pod_nginx.yml
查看POD详情
get pod -o wide
查看资源详细信息
kubectl describe pod kube-flannel-ds-amd64-9trbq -n kube-system
至此,通过kubeadm工具就实现了Kubernetes集群的安装。
Nice to see you all!