kubeadm进行K8S集群部署
环境说明:
节点类型 主机名 管理ip
master master 192.168.2.10
nodes node1 192.168.2.11
nodes node2 192.168.2.12
podSubnet: 10.244.0.0/16
serviceSubnet: 10.96.0.0/16
一、基础环境配置
1、主机名修改
2、关闭防火墙,修改selinux为disable
3、/etc/hosts文件解析
4、关闭swap,或者删除swap分区
5、NTP时间同步
6、Master节点和node节点免密登录
7、配置yum源
vim /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
二、master节点部署
1、安装docker、kubelet、kubeadm、kubectl软件
yum install -y docker kubelet kubeadm kubectl
systemctl restart docker.service
systemctl enable docker.service
systemctl status docker.service
systemctl enable kubelet.service
2、修改kubeadm镜像源
查看kubeadm需要的镜像,默认需要访问Google,国内一般无法访问,需要修改镜像下载地址
kubeadm config images list
kubeadm config print init-defaults > kubeadm.conf
将kubeadm.conf配置文件中的imageRepository: k8s.gcr.io改为新的地址,如imageRepository:registry.aliyuncs.com/google_containers,或imageRepository: docker.io/mirrorgooglecontainers
将podSubnet: ''改为需要的地址段,比如podSubnet: 10.244.0.0/16
3、下载需要的镜像
kubeadm config images list --config kubeadm.conf
kubeadm config images pull --config kubeadm.conf
4、设置bridge-nf-call-iptables
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables
注意:如果不设置,下步执行命令会报错提示
5、初始化kubeadm
kubeadm init --config kubeadm.conf
初始化完成会有如下信息输出:
---------------------------------------------------------------------------------------------------
Your Kubernetes control-plane 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/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.2.10:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:06e6c6592362baa34e15ea169b43d16b57361110546b9c5d65b585a8ad09cdd5
---------------------------------------------------------------------------------------------------------
6、初始化完成执行如下命令
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
7、设置命令自动补全
echo "source <(kubectl completion bash)" >> ~/.bashrc
三、node节点操作
1、安装docker、kubelet、kubeadm、kubectl软件
yum install -y docker kubelet kubeadm kubectl
systemctl restart docker.service
systemctl enable docker.service
systemctl status docker.service
2、设置bridge-nf-call-iptables
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 > /proc/sys/net/bridge/bridge-nf-call-ip6tables
3、node节点接入集群
kubeadm join 192.168.2.10:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:06e6c6592362baa34e15ea169b43d16b57361110546b9c5d65b585a8ad09cdd5
四、master节点部署flannel网络
1、master节点部署flannel网络
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml
2、查看节点信息
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 3h6m v1.14.1
node1 Ready <none> 125m v1.14.1
node2 NotReady <none> 115m v1.14.1
3、修改node节点的role属性
kubectl edit nodes node1
将node-role.kubernetes.io/node: ""写在label字段里
kubectl get pods -n kube-system -o wide