集群环境
一、环境初始化
- 设置各节点主机名称:
hostnamectl set-hostname master hostnamectl set-hostname node01 hostnamectl set-hostname node02
- 配置主机映射(所有节点需要):
cat <<EOF > /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.0.8 master 192.168.0.9 node01 192.168.0.10 node02 EOF
- 关闭防火墙:
systemctl stop firewalld && systemctl disable firewalld
- 关闭Selinux:
setenforce 0 # 临时禁用selinux sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux # 永久关闭 sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
- 关闭Swap,否则kubelet会出错:
swapoff -a # 临时关闭swap sed -i 's/.*swap.*/#&/' /etc/fstab # 永久关闭
- 配置路由:
cat <<EOF > /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl --system # 使其立即生效
- 安装依赖包配置ntp:
yum install -y epel-release yum install -y yum-utils device-mapper-persistent-data lvm2 net-tools conntrack-tools wget vim ntpdate libseccomp libtool-ltdl systemctl enable ntpdate.service echo '*/30 * * * * /usr/sbin/ntpdate time7.aliyun.com >/dev/null 2>&1' > /tmp/crontab2.tmp crontab /tmp/crontab2.tmp systemctl start ntpdate.service
- 添加kubernetes的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
设置内核(可选):
echo "* soft nofile 65536" >> /etc/security/limits.conf echo "* hard nofile 65536" >> /etc/security/limits.conf echo "* soft nproc 65536" >> /etc/security/limits.conf echo "* hard nproc 65536" >> /etc/security/limits.conf echo "* soft memlock unlimited" >> /etc/security/limits.conf echo "* hard memlock unlimited" >> /etc/security/limits.conf
二、安装与配置Docker
- 安装Docker(参考《Centos7安装Docker最新版》)。
- 配置Docker镜像下载代理:
vi /usr/lib/systemd/system/docker.service
Add the following line beforeExecStart
:Environment="HTTPS_PROXY=http://ik8s.io:10080" Environment="NO_PROXY=127.0.0.0/8,172.20.0.0/16"
- 重启Docker:bashCopy code
systemctl daemon-reload && systemctl restart docker
三、安装与配置kubeadm, kubelet和kubectl
- 安装kubeadm, kubelet和kubectl:
yum install -y kubelet kubeadm kubectl
- 配置kubeadm:
vi /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
Modify the file to include:Environment="KUBELET_NETWORK_ARGS=--network-plugin=cni --cni-conf-dir=/etc/cni/ --cni-bin-dir=/opt/cni/bin"
Enable and start kubelet:systemctl enable kubelet && systemctl start kubelet
- 安装命令补全:
yum install -y bash-completion source /usr/share/bash-completion/bash_completion source <(kubectl completion bash) echo "source <(kubectl completion bash)" >> ~/.bashrc
四、使用kubeadm初始化master
kubeadm init --kubernetes-version=v1.11.2 --pod-network-cidr=10.244.0.0/16
After initialization, follow the provided instructions to set up kubectl
and deploy a pod network to the cluster.
五、将node加入集群
- 配置kubelet,从master将kubelet文件分别复制到node01、node02:
scp /etc/sysconfig/kubelet node01:/etc/sysconfig/kubelet scp /etc/sysconfig/kubelet node02:/etc/sysconfig/kubelet
- 执行
kubeadm join
命令即可。如果 forgotten, use the following command to retrieve:bashCopy codekubeadm token create --print-join-command
六、测试
kubectl get nodes
七、初始化集群报错及问题解决:
问题一:
[kubeadm] WARNING: kubeadm is in beta, please do not use it for production clusters. Unable to fetch release information. URL: “https://storage.googleapis.com/kubernetes-release/release/stable-1.7.5.txt” Status: 404 Not Found 解决: 添加版本信息 --kubernetes-version=v1.7.5
,执行 kubeadm reset
,再次执行 kubeadm init
.
问题二:
W1205 18:49:21.323220 106548 cni.go:171] Unable to update cni config: No networks found in /etc/cni/net.d 解决: 执行 kubeadm reset
,然后重新执行 kubeadm init --kubernetes-version=v1.7.5 --pod-network-cidr=10.244.0.0/16
.
暂无评论内容