部署方式:kubeadm ---k8s部署成容器
部署信息:
IP地址 | 主机名 | 系统版本 | 角色 |
192.168.10.10 | k8s-master | 7.5 | master |
192.168.10.20 | k8s-node01 | 7.5 | node |
192.168.10.30 | k8s-node02 | 7.5 | node |
一、系统基础配置
1、设定时钟同步:
# yum install chrony -y # timedatectl set-timezone Asia/Shanghai (更改系统时区为上海) #启动服务 # systemctl start chronyd.service # systemctl enable chronyd.service
2、设置主机名称解析
# hostnamectl set-hostname k8s-master cat >> /etc/hosts << EOF 192.168.10.10 k8s-master 192.168.10.20 k8s-node01 192.168.10.30 k8s-node02 EOF
3、关闭iptables或firewalld防火墙
# systemctl stop firewalld.service
# systemctl disable firewalld.service
4、关闭selinux
# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config && setenforce 0
5、关闭swap设备
# sed -i '/ swap / s/^(.*)$/#1/g' /etc/fstab #永久 # swapoff -a # 临时
6、内核调整,将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF # sysctl -p
二、安装docker服务
# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo # yum -y install docker-ce-18.06.1.ce-3.el7 # systemctl enable docker && systemctl start docker # docker --version
三、安装Kubernetes
1、配置kubenetes的yum仓库(这里使用阿里云仓库)
cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF
2、安装kubeadm,kubelet和kubectl
# yum -y install kubelet-1.15.2 kubeadm-1.15.2 kubectl-1.15.2 # rpm -aq kubelet kubectl kubeadm # systemctl enable kubelet
3、配置忽略swap报错
# vim /etc/sysconfig/kubelet KUBELET_EXTRA_ARGS="--fail-swap-on=false"
4、初始化 Kubernetes Master (只在master节点执行)
kubeadm init --apiserver-advertise-address=192.168.10.10 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.15.2 --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=Swap ...... 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.10.10:6443 --token 8nvmlq.wbmmws0ymbszk1yb --discovery-token-ca-cert-hash sha256:ec5385c0abbc1cc14c3c9e40f6ca021e8ae24a11e1c557c285882ffe0b20124d
参数解析:
--kubernetes-version #指定Kubernetes版本 --image-repository #由于kubeadm默认是从官网k8s.grc.io下载所需镜像,国内无法访问,所以这里通过--image-repository指定为阿里云镜像仓库地址 --pod-network-cidr #指定pod网络段 --service-cidr #指定service网络段 --ignore-preflight-errors=Swap #忽略swap报错信息
5、按照上面初始化成功提示创建配置文件
[root@k8s-master ~]# mkdir -p $HOME/.kube [root@k8s-master ~]# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config [root@k8s-master ~]# chown $(id -u):$(id -g) $HOME/.kube/config
6、初始化完成后可以看到所需镜像也拉取下来了
[root@k8s-master ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE registry.aliyuncs.com/google_containers/kube-controller-manager v1.15.2 9f5df470155d 12 months ago 159MB registry.aliyuncs.com/google_containers/kube-scheduler v1.15.2 88fa9cb27bd2 12 months ago 81.1MB registry.aliyuncs.com/google_containers/kube-proxy v1.15.2 167bbf6c9338 12 months ago 82.4MB registry.aliyuncs.com/google_containers/kube-apiserver v1.15.2 34a53be6c9a7 12 months ago 207MB registry.aliyuncs.com/google_containers/coredns 1.3.1 eb516548c180 19 months ago 40.3MB registry.aliyuncs.com/google_containers/etcd 3.3.10 2c4adeb21b4f 21 months ago 258MB registry.aliyuncs.com/google_containers/pause 3.1 da86e6ba6ca1 2 years ago 742kB [root@k8s-master ~]#
7、查看master状态:
[root@k8s-master ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master Ready master 60m v1.15.2
8、添加flannel网络组件
添加flannel网络组件,(在master执行) 经过实践发现,node节点上必须有quay.io/coreos/flannel的镜像,,不然状态一直为NotReady
flannel地址:https://github.com/coreos/flannel
[root@k8s-master ~]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
报错:The connection to the server raw.githubusercontent.com was refused - did you specify the right host or port?
解决办法:
# 在https://www.ipaddress.com/查询raw.githubusercontent.com的真实IP。 sudo vim /etc/hosts 199.232.28.133 raw.githubusercontent.com 如果flannel的pod状态还不正常,可以卸载掉网络 # kubectl delete -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
四、将node加入集群:
[root@k8s-node01 ~]# kubeadm join 192.168.10.10:6443 --token 8nvmlq.wbmmws0ymbszk1yb --discovery-token-ca-cert-hash sha256:ec5385c0abbc1cc14c3c9e40f6ca021e8ae24a11e1c557c285882ffe0b20124d --ignore-preflight-errors=Swap
查看状态:
# kubectl config view # kubectl get pods -n kube-system|grep flannel
使用命令查看节点信息时候 node 节点的STATUS 为NotReady
[root@k8s-master ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master Ready master 108m v1.15.2 k8s-node01 NotReady <none> 16m v1.15.2 k8s-node02 NotReady <none> 12m v1.15.2
出现这个错误可以在节点机器上执行journalctl -f -u kubelet查看kubelet的输出日志信息.
# journalctl -f -u kubelet 发现以下错误: Container runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized
我们可以执行命令docker images|grep flannel来查看flannel镜像是否已经成功拉取下来.经过排查master端的已经安装好,node节点没有flannel插件,安装即可.
master需要flannel,node也需要flannel。
# docker save -o flannel.tar quay.io/coreos/flannel
# docker load -i flannel.tar