部署环境
一个Master节点,两个Node节点。配置所有节点计算机名,并配置hosts指向,最后配置好统一时间同步服务器。
Master:192.168.6.102(kubernetes-master)
Node1:192.168.6.104(kubernetes-node1)
Node2:192.168.6.105(kubernetes-node2)
系统版本:CentOS Linux release 7.5.1804 (Core)
Docker版本:docker-ce-17.03.0
Kubernetes版本:1.12.0
部署方法:Kubeadm
关闭SELinux,清空防火墙规则
Docker部署
注意:所有节点都需要部署
下载安装包
cd /usr/local/src/ wget https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-17.03.0.ce-1.el7.centos.x86_64.rpm wget https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm安装
yum -y install docker-ce-17.03.0.ce-1.el7.centos.x86_64.rpm docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm
启动Docker并配置开机自启动
systemctl start docker && systemctl enable docker
配置专属阿里云Docker官方镜像仓库加速器
首先需要注册一个阿里云账号,然后进入“容器镜像服务”找到镜像加速器,根据官方文档说明配置即可
部署Master节点
配置添加yum配置文件
[Kubernetes] name=Kubernetes repo baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg enable=0
安装软件:不可能在集群初始化前启动,后面在初始化集群时会自动启动,但需要设置成开机自启动,否则在初始化时会有警告信息
yum install kubeadm kubectl systemctl enable kubelet
下载所需要镜像
kubeadm config images pull
注意:默认情况下此站点在国内无法下载,为保证能顺利安装,最好是使用墙外的服务器部署好Docker,安装好kubeadm,然后执行此命令将镜像下载到本地再打包复制到墙内
准备集群初始化环境
swapoff -a echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.conf
临时需要关闭交换分区,否则在初始化的时候会报错,永久在fstab文件注释掉交换分区那行即可
打开ipv4透明网桥
开始初始化Master集群
kubeadm init --pod-network-cidr=10.42.0.0/16 --service-cidr=10.8.0.0/16 --kubernetes-version=v1.12.0 --apiserver-advertise-address=0.0.0.0 --apiserver-bind-port=6443 --ignore-preflight-errors=Swap
--pod-network-cidr: 指定Pod网段
--service-cidr: 指定服务网络
--kubernetes-version: 指定kubernetes版本,不同时间安装的kubeadm版本不同,所支持部署的kubernetes版本也不同,如果不支持会有报错提示
--apiserver-advertise-address: 指定apiserver监听地址,默认监听0.0.0.0
--apiserver-bind-port: 指定apiserver监听端口,默认6443
--ignore-preflight-errors: 忽略指定错误信息,默认情况下如果swap打开会报错,如果关闭了Swap此项可以不指定
如果成功安装,会显示大概如下所示信息,一个是配置kubectl控制台,一个是提示node节点加入方法
[markmaster] Marking the node kubernetes-master as master by adding the label "node-role.kubernetes.io/master=''" [markmaster] Marking the node kubernetes-master as master by adding the taints [node-role.kubernetes.io/master:NoSchedule] [patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "kubernetes-master" as an annotation [bootstraptoken] using token: 9jefj3.czyghatvpi8mqamz [bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials [bootstraptoken] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token [bootstraptoken] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster [bootstraptoken] creating the "cluster-info" ConfigMap in the "kube-public" namespace [addons] Applied essential addon: CoreDNS [addons] Applied essential addon: kube-proxy Your Kubernetes master 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/ You can now join any number of machines by running the following on each node as root: kubeadm join 192.168.6.102:6443 --token 9jefj3.czyghatvpi8mqamz --discovery-token-ca-cert-hash sha256:4126634b062c39f2bc3ef3ba5aa717e95a54aa16889ccb820bdb548c961b8d97
此时查看message日志,如果有类似于如下错误提示信息
Container runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized
修改“/var/lib/kubelet/kubeadm-flags.env”配置文件,删除“--network-plugin=cni”,最后重启kubelet服务
配置kubectl控制台
注意:提示推荐我们使用普通用户执行,所以需要sudo权限,我这里就直接root
mkdir -p $HOME/.kube cp -i /etc/kubernetes/admin.conf $HOME/.kube/config chown $(id -u):$(id -g) $HOME/.kube/config此时我们可以使用下面的命令查看Kubernetes 的基本信息
kubectl get ns 查看所有名称空间
kubectl get cs 查看健康状况
kubectl get nodes 查看所有节点信息
kubectl get pods 查看所有pods信息
部署flannel网络组件
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml sed -i "/10.244/s/244/42/" kube-flannel.yml kubectl apply -f kube-flannel.yml
在部署前需要修改pod网络为我们指定的网络,如果我们使用默认的10.244.0.0/16就无需修改,直接执行下面命令安装即可
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
注意:在部署flannel的时候也会自动下载相关镜像,可以先打开yml文件看下需要哪个镜像先pull下载再执行部署
检查Master节点是否运行正常