k8s master节点以及node节点搭建 + 踩坑详解

在淘宝上购买了相应课程的博主在一步一步按照视频讲解搭建的过程中发现有很多视频中提到的问题并没有被很好解决,而且因为更新速度过快的原因,视频中的一些指令会带来一些问题,这里博主总结了一下最新实践成功的k8s master 节点以及 node 节点的搭建过程,希望对大家有所帮助。

主机规划:
master虚拟机一台, 2C4G, 域名:master.example.com, IP地址:192.168.0.175
node1虚拟机一台, 2C4G, 域名:node1.example.com, IP地址:192.168.0.130

软件要求:
操作系统版本: CentOS Linux release 7.3.1611 (Core)
Docker版本:1.13.1, build 07f3374/1.13.1
Kubernetes版本: v1.13.1

#停止firewalld服务
systemctl stop firewalld && systemctl disable firewalld

#关闭selinux
sed -i 's/^SELINUX=enforing$/SELINUX=disabled' /etc/selinux/config && setenforce 0

#关闭swap设置
swapoff -a
yes | cp /etc/fstab /etc/fstab_bak
cat /etc/fstab_bak |grep -v swap > /etc/fstab

#解决流量路径不正确问题
cat < /etc/sysctl.d/k8s.conf
vm.swappiness = 0
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
#使配置生效
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf

#更改hosts文件
cat >> /etc/hosts <> /etc/yum.repos.d/k8s.repo <

(具体脚本如下)

#unicoding:utf8
import os

S_registry = 'registry.cn-beijing.aliyuncs.com/kubernetesdevops/'
D_registry = 'k8s.gcr.io/'

master_image = ['kube-apiserver:v1.13.1','kube-controller-manager:v1.13.1',
                'kube-scheduler:v1.13.1','kube-proxy:v1.13.1','pause:3.1',
				'etcd:3.2.24','coredns:1.2.6','flannel:v0.10.0-amd64']
				

def PullImage(registry,images):
	print("一共%s个镜像"%(len(images),))
	index = 1
	for image in images:
		print("开始下载第[%d]个镜像 ---> [%s]"%(index,image))
		cmd = "docker pull " + registry + image
		os.system(cmd)
		print("done!")
		index+=1
		
def TagImage(sregistry,dregistry,images):
	print("一共%s个镜像"%(len(images),))
	index = 1
	for image in images:
		print("开始tag第[%d]个镜像 ---> [%s] ---> [%s]"%(index,sregistry+image,dregistry+image))
		cmd = "docker tag " + sregistry+image + " " + dregistry + image
		os.system(cmd)
		print("done!")
		index+=1 
		
if __name__ == '__main__':
	PullImage(S_registry,master_image)
	TagImage(S_registry,D_registry,master_image)

完成后查看镜像

[root@master master]# docker images
REPOSITORY                                                                  TAG                 IMAGE ID            CREATED             SIZE
registry.cn-beijing.aliyuncs.com/kubernetesdevops/kube-proxy                v1.13.1             fdb321fd30a0        7 months ago        80.2 MB
k8s.gcr.io/kube-proxy                                                       v1.13.1             fdb321fd30a0        7 months ago        80.2 MB
k8s.gcr.io/kube-apiserver                                                   v1.13.1             40a63db91ef8        7 months ago        181 MB
registry.cn-beijing.aliyuncs.com/kubernetesdevops/kube-apiserver            v1.13.1             40a63db91ef8        7 months ago        181 MB
k8s.gcr.io/kube-controller-manager                                          v1.13.1             26e6f1db2a52        7 months ago        146 MB
registry.cn-beijing.aliyuncs.com/kubernetesdevops/kube-controller-manager   v1.13.1             26e6f1db2a52        7 months ago        146 MB
k8s.gcr.io/kube-scheduler                                                   v1.13.1             ab81d7360408        7 months ago        79.6 MB
registry.cn-beijing.aliyuncs.com/kubernetesdevops/kube-scheduler            v1.13.1             ab81d7360408        7 months ago        79.6 MB
k8s.gcr.io/coredns                                                          1.2.6               f59dcacceff4        8 months ago        40 MB
registry.cn-beijing.aliyuncs.com/kubernetesdevops/coredns                   1.2.6               f59dcacceff4        8 months ago        40 MB
k8s.gcr.io/etcd                                                             3.2.24              3cab8e1b9802        10 months ago       220 MB
registry.cn-beijing.aliyuncs.com/kubernetesdevops/etcd                      3.2.24              3cab8e1b9802        10 months ago       220 MB
k8s.gcr.io/flannel                                                          v0.10.0-amd64       f0fad859c909        18 months ago       44.6 MB
registry.cn-beijing.aliyuncs.com/kubernetesdevops/flannel                   v0.10.0-amd64       f0fad859c909        18 months ago       44.6 MB
k8s.gcr.io/pause                                                            3.1                 da86e6ba6ca1        19 months ago       742 kB
registry.cn-beijing.aliyuncs.com/kubernetesdevops/pause                     3.1                 da86e6ba6ca1        19 months ago       742 kB

master节点-配置kubectl、flannel网络(注意这里的ip地址用本机ip)

kubeadm init --kubernetes-version=v1.13.1 --apiserver-advertise-address 192.168.0.109 --pod-network-cidr=10.244.0.0/16

注意这里完成操作后会有这样一段提示:

You can now join any number of machines by running the following on each node
as root:

  kubeadm join 192.168.0.175:6443 --token vatur3.j0y4md12rz4h04mp --discovery-token-ca-cert-hash sha256:f684729944a449a5ab20d40a664b1b3fca99a8d33c09fb5db8e1ce125c6e4632

将kubeadm这段指令记好,随后添加节点需要用到

配置kubectl

echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
source ~/.bash_profile

此时进行确认,master节点 not ready

kubectl get nodes
NAME                 STATUS     ROLES    AGE     VERSION
master.example.com   NotReady   master   4m56s   v1.13.0

于是我们进行安装kube-flannel

curl https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml -o kube-flannel.yml
docker tag k8s.gcr.io/flannel:v0.10.0-amd64 quay.io/coreos/flannel:v0.11.0-amd64
kubectl apply -f kube-flannel.yml

此时若如果仍然未运行,重启docker即可

systemctl restart docker


kubectl get nodes
NAME                 STATUS   ROLES    AGE   VERSION
master.example.com   Ready    master   25m   v1.13.0

至此master节点已经初步部署完成,接下来我们进行node1的添加

前面的步骤和master节点一致,于是我们可以用脚本进行完成,节省时间

#停止firewalld服务
systemctl stop firewalld && systemctl disable firewalld

#关闭selinux
sed -i 's/^SELINUX=enforing$/SELINUX=disabled' /etc/selinux/config && setenforce 0

#关闭swap设置
swapoff -a
yes | cp /etc/fstab /etc/fstab_bak
cat /etc/fstab_bak |grep -v swap > /etc/fstab

#解决流量路径不正确问题
cat < /etc/sysctl.d/k8s.conf
vm.swappiness = 0
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
#使配置生效
modprobe br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf

#更改hosts文件
cat >> /etc/hosts <> /etc/yum.repos.d/k8s.repo <

将上述代码写入一个init.sh文件中,然后sh init.sh进行脚本运行即可

随后进行镜像拉取(与master节点操作相同)

随后将之前的整句token输入

kubeadm join 192.168.0.175:6443 --token vatur3.j0y4md12rz4h04mp --discovery-token-ca-cert-hash sha256:f684729944a449a5ab20d40a664b1b3fca99a8d33c09fb5db8e1ce125c6e4632

(如果忘记可在master终端输入kubeadm token list查看)

kubeadm token list
TOKEN                     TTL       EXPIRES                     USAGES                   DESCRIPTION                                                EXTRA GROUPS
ifq7qv.3beevw9jgs73c049   22h       2019-07-26T14:27:56+08:00   authentication,signing   The default bootstrap token generated by 'kubeadm init'.   system:bootstrappers:kubeadm:default-node-token

随后进行安装kube-flannel,重启docker即可(与master节点操作一致)

这样当你在master节点中查看时就已经完成添加了

kubectl get nodes
NAME                 STATUS   ROLES    AGE     VERSION
master.example.com   Ready    master   91m     v1.13.0
node1.example.com    Ready       7m55s   v1.13.0

到此我们的部署便初步完成了~~

但是!当博主关闭电脑再次打开虚拟机的时候,k8s并没有正常启动,由于 K8s 必须保持全程关闭交换内存,之前我安装是只是使用 swapoff -a 命令暂时关闭 swap。而机器重启后,swap 还是会自动启用,从而导致 kubelet 无法启动。

(1)首先执行如下命令关闭 swap。

swapoff -a

(2)接着编辑 /etc/fstab 文件。

vi /etc/fstab

(3)将 /dev/mapper/centos-swap swap swap default 0 0 这一行前面加个 # 号将其注释掉。

(4)编辑完毕后保存退出。这样机器重启后 kubelet 也可以正常自动启动了。

你可能感兴趣的:(k8s master节点以及node节点搭建 + 踩坑详解)