此文档使用kubeadm搭建Kubernetes1.14 做个记录。此文档在安装了docker的centos7.x的版本下实际操作
环境
此次准备两台机器用于安装。后面可以按需求增加节点
172.16.77.189
172.16.77.162
前期准备
前期准备很重要,不然后面的安装可能会出现意想不到的坑。
- 禁用防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
- 关闭swap
swapoff -a
sed -i '/swap/d' /etc/fstab
- 禁用SELINUX
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
- 配置k8s.conf
cat <<EOF > /etc/sysctl.d/k8s.conf
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
安装 kubeadm、kubelet、kubectl
在确保docker安装之后再进行以下操作。安装kubeadm,指定yum源安装:
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kube*
EOF
注意:以上的源需要科学上网。如果不能科学上网的话,可以使用阿里云的源进行安装:
cat <<EOF > /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
EOF
安装
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable kubelet && systemctl start kubelet
配置 kubelet
<font color=#DC143C> 如果和docker的cgroup-driver一致,则可跳过这一步</font>
kubelet的cgroup-driver参数要和docker的cgroup-driver保持一致:
查看docker的cgroup-driver
[root@k8s-77-189 ~]# docker info |grep Cgroup
Cgroup Driver: systemd
#注意:有的可能是Cgroup Driver: cgroupfs
查看配置文件:/var/lib/kubelet/kubeadm-flags.env
[root@k8s-77-189 ~]# cat /var/lib/kubelet/kubeadm-flags.env
KUBELET_KUBEADM_ARGS=--cgroup-driver=systemd --network-plugin=cni --pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
#与docker的cgroup-driver参数保持一致
如果你使用其他CRI 请参考此处进行对应修改
<font color=#DC143C> 以上所有,master节点和node节点都需要配置</font>
集群安装
初始化
在master节点上使用kubeadm命令进行初始化
kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=172.16.77.189
这里选择flannel作为pod的网络插件,所以指定–pod-network-cidr=10.244.0.0/16 。然后是apiserver的通信地址,就是master节点的ip地址。注意:以上命令会在gcr拉取一些镜像 如果你不能科学上网,可能会因为镜像拉取不下来而导致初始化失败。可以加上<font color=#DC143C> --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers</font>来使用阿里云拉取镜像。如果需要指定版本需要使用--kubernetes-version=v1.14.0参数
最后出现以下相似内容说明初始化成功
[root@k8s-77-189 ~]# kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=172.16.77.189
......
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 172.16.77.189:6443 --token ye60xn.k9ogcgs2hffr9k97 \
--discovery-token-ca-cert-hash sha256:277074c8da66ff7008af7ba0b637806facca21e441a5650fc54a83a0a1397bb5
记录下以上内容,里面有配置如何使用kubectl
访问集群的方式及将节点加入集群的命令
在master节点上做以下操作操作
[root@k8s-77-189 ~]# mkdir -p $HOME/.kube
[root@k8s-77-189 ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@k8s-77-189 ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
配置好之后可以使用kubectl来查看集群了
[root@k8s-77-189 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-77-189 Ready master 5h30m v1.14.2
如果你集群安装过程中遇到了其他问题,可以使用下面的命令来重置:
kubeadm reset
ifconfig cni0 down && ip link delete cni0
ifconfig flannel.1 down && ip link delete flannel.1
rm -rf /var/lib/cni/
安装Pod Network
安装flannel
网络插件
[root@k8s-77-189 ~]# wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
[root@k8s-77-189 ~]# kubectl apply -f kube-flannel.yml
如果是多个网卡的话,需要在kube-flannel.yml
中使用--iface
参数指定集群主机内网网卡的名称,否则可能会出现dns无法解析。flanneld
启动参数加上--iface=<iface-name>
args:
- --ip-masq
- --kube-subnet-mgr
- --iface=eth0
安装完成后使用kubectl get pods
命令可以查看到集群中的组件运行状态,如果都是Running
状态的话,那么恭喜你,你的master
节点安装成功了。
[root@k8s-77-189 ~]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-d5947d4b-jmvbf 1/1 Running 0 3m29s
kube-system coredns-d5947d4b-tplqp 1/1 Running 0 3m29s
kube-system etcd-k8s-77-189 1/1 Running 0 2m33s
kube-system kube-apiserver-k8s-77-189 1/1 Running 0 2m22s
kube-system kube-controller-manager-k8s-77-189 1/1 Running 0 2m22s
kube-system kube-flannel-ds-amd64-7wdl8 1/1 Running 0 64s
kube-system kube-proxy-9jsrh 1/1 Running 0 3m29s
kube-system kube-scheduler-k8s-77-189 1/1 Running 0 2m24s
添加节点
将集群安装之前所有的环境配置完成后,就可以将node加入集群了
[root@k8s-77-162 ~]# kubeadm join 172.16.77.189:6443 --token ye60xn.k9ogcgs2hffr9k97 \
> --discovery-token-ca-cert-hash sha256:277074c8da66ff7008af7ba0b637806facca21e441a5650fc54a83a0a1397bb5
[preflight] Running pre-flight checks
[WARNING Service-Docker]: docker service is not enabled, please run 'systemctl enable docker.service'
[WARNING Hostname]: hostname "k8s-77-162" could not be reached
[WARNING Hostname]: hostname "k8s-77-162": lookup k8s-77-162 on 172.16.170.198:53: server misbehaving
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.14" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
[root@k8s-77-162 ~]#
显示上面输出,说明已经加入到集群了。不过此时还不能使用kubectl来获取集群信息。需要将主节点的config文件拷贝到当前节点上
[root@k8s-77-162 ~]# mkdir -p $HOME/.kube
[root@k8s-77-162 ~]# scp root@172.16.77.189:/root/.kube/config $HOME/.kube
config 100% 5449 4.3MB/s 00:00
查看集群信息
[root@k8s-77-162 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-77-162 Ready <none> 7m39s v1.14.2
k8s-77-189 Ready master 10h v1.14.2
到此集群就算是部署成功了