在每个节点上执行一下主机名解析,此处我们不仅仅在主机上设置了,还在dns解析上进行对应的主机名解析了。d
cat << EOF >> /etc/hosts192.168.10.234 dev-k8s-master192.168.10.235 dev-k8s-node1192.168.10.236 dev-k8s-node2
EOF
systemctl disable --now firewalld
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config && setenforce 0
cat << EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1net.bridge.bridge-nf-call-iptables = 1net.ipv4.ip_forward = 1EOF
modprobe br_netfilter # 生效内核优化参数
# 切换centos的源为阿里云的镜像源wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.reposudo yum remove -y docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine \
docker-cesudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.reposudo yum makecache fast
yum list docker-ce.x86_64 --showduplicates | sort -r # 查看可以安装的docker版本yum remove container-selinux -y# 如果在安装低版本的docker之前已经安装过高版本的docker,那么需要删除/var/lib/docker目录然后重新启动docker.serviceyum install -y --setopt=obsoletes=0 \
docker-ce-17.03.1.ce-1.el7.centos \
docker-ce-selinux-17.03.1.ce-1.el7.centossudo systemctl enable --now docker # 设置docker开机自启cat << EOF > /etc/docker/daemon.json
{ "registry-mirrors": ["https://52szbbbs.mirror.aliyuncs.com"
]
}
EOF
systemctl restart docker.service
cat << EOF > /root/k8s-master-image.sh
docker pull cnych/kube-apiserver-amd64:v1.10.0docker pull cnych/kube-scheduler-amd64:v1.10.0docker pull cnych/kube-controller-manager-amd64:v1.10.0docker pull cnych/kube-proxy-amd64:v1.10.0docker pull cnych/k8s-dns-kube-dns-amd64:1.14.8docker pull cnych/k8s-dns-dnsmasq-nanny-amd64:1.14.8docker pull cnych/k8s-dns-sidecar-amd64:1.14.8docker pull cnych/etcd-amd64:3.1.12docker pull cnych/flannel:v0.10.0-amd64
docker pull cnych/pause-amd64:3.1docker tag cnych/kube-apiserver-amd64:v1.10.0 k8s.gcr.io/kube-apiserver-amd64:v1.10.0docker tag cnych/kube-scheduler-amd64:v1.10.0 k8s.gcr.io/kube-scheduler-amd64:v1.10.0docker tag cnych/kube-controller-manager-amd64:v1.10.0 k8s.gcr.io/kube-controller-manager-amd64:v1.10.0docker tag cnych/kube-proxy-amd64:v1.10.0 k8s.gcr.io/kube-proxy-amd64:v1.10.0docker tag cnych/k8s-dns-kube-dns-amd64:1.14.8 k8s.gcr.io/k8s-dns-kube-dns-amd64:1.14.8docker tag cnych/k8s-dns-dnsmasq-nanny-amd64:1.14.8 k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64:1.14.8docker tag cnych/k8s-dns-sidecar-amd64:1.14.8 k8s.gcr.io/k8s-dns-sidecar-amd64:1.14.8docker tag cnych/etcd-amd64:3.1.12 k8s.gcr.io/etcd-amd64:3.1.12docker tag cnych/flannel:v0.10.0-amd64 quay.io/coreos/flannel:v0.10.0-amd64
docker tag cnych/pause-amd64:3.1 k8s.gcr.io/pause-amd64:3.1EOF
cat << EOF > /root/k8s-node-image.sh
docker pull cnych/kube-proxy-amd64:v1.10.0docker pull cnych/flannel:v0.10.0-amd64
docker pull cnych/pause-amd64:3.1docker pull cnych/kubernetes-dashboard-amd64:v1.8.3docker pull cnych/heapster-influxdb-amd64:v1.3.3docker pull cnych/heapster-grafana-amd64:v4.4.3docker pull cnych/heapster-amd64:v1.4.2docker pull cnych/k8s-dns-kube-dns-amd64:1.14.8docker pull cnych/k8s-dns-dnsmasq-nanny-amd64:1.14.8docker pull cnych/k8s-dns-sidecar-amd64:1.14.8docker tag cnych/flannel:v0.10.0-amd64 quay.io/coreos/flannel:v0.10.0-amd64
docker tag cnych/pause-amd64:3.1 k8s.gcr.io/pause-amd64:3.1docker tag cnych/kube-proxy-amd64:v1.10.0 k8s.gcr.io/kube-proxy-amd64:v1.10.0docker tag cnych/k8s-dns-kube-dns-amd64:1.14.8 k8s.gcr.io/k8s-dns-kube-dns-amd64:1.14.8docker tag cnych/k8s-dns-dnsmasq-nanny-amd64:1.14.8 k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64:1.14.8docker tag cnych/k8s-dns-sidecar-amd64:1.14.8 k8s.gcr.io/k8s-dns-sidecar-amd64:1.14.8docker tag cnych/kubernetes-dashboard-amd64:v1.8.3 k8s.gcr.io/kubernetes-dashboard-amd64:v1.8.3docker tag cnych/heapster-influxdb-amd64:v1.3.3 k8s.gcr.io/heapster-influxdb-amd64:v1.3.3docker tag cnych/heapster-grafana-amd64:v4.4.3 k8s.gcr.io/heapster-grafana-amd64:v4.4.3docker tag cnych/heapster-amd64:v1.4.2 k8s.gcr.io/heapster-amd64:v1.4.2EOF
部分的安装包可以在packages目录下获取
# 国内的阿里云源,不需要翻墙cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1gpgcheck=0repo_gpgcheck=0gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum makecache fast
yum install -y kubernetes-cni-0.5.1-0.x86_64
yum install -y kubeadm-1.10.0-0 kubectl-1.10.0-0 kubelet-1.10.0-0 #如果安装过程出现问题,可以将包下载下来进行安装wget https://packages.cloud.google.com/yum/pool/94d062f2d86b8f4f55f4d23a3610af25931da9168b7f651967c269273955a5a2-kubelet-1.10.5-0.x86_64.rpm
wget https://packages.cloud.google.com/yum/pool/1eed768852fa3e497e1b7bdf4e93afbe3b4b0fdcb59fda801d817736578b9838-kubectl-1.10.5-0.x86_64.rpm
wget https://packages.cloud.google.com/yum/pool/fe33057ffe95bfae65e2f269e1b05e99308853176e24a4d027bc082b471a07c0-kubernetes-cni-0.6.0-0.x86_64.rpm
wget https://packages.cloud.google.com/yum/pool/3ea9c50d098c50a7e968c35915d3d8af7f54c58c0cedb0f9603674720743de4e-kubeadm-1.10.5-0.x86_64.rpm
yum localinstall -y ./*.rpm#yum install -y kubeadm kubelet kubectl
因为使用yum安装的Kubectl使用的--cgroups-drive=systemd
,而docker在安装的时候默认使用的是cgroup
,因此需要将kubectl的cgroup-drive
修改成cgroup
sed -i "s/KUBELET_CGROUP_ARGS=--cgroup-driver=systemd/KUBELET_CGROUP_ARGS=--cgroup-driver=cgroupfs/g" /etc/systemd/system/kubelet.service.d/10-kubeadm.conf# Kubernetes设计将最大的可能使用主机的资源,在1.8版本的时候禁止使用swapsed -i "s/^ExecStart=$/Environment=\"KUBELET_EXTRA_ARGS=--fail-swap-on=false\"\nExecStart=/g" /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
systemctl daemon-reload
kubeadm init \
--kubernetes-version=v1.10.0 \
--pod-network-cidr=10.244.0.0/16 \
--apiserver-advertise-address=192.168.10.234 \
--ignore-preflight-errors=Swap
mkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/config
kubeadm join 192.168.10.234:6443 --token 27mjib.p20ygnztj1gyd16a --discovery-token-ca-cert-hash sha256:bc70e95b95ffd86ce393c3c22826ce71577286c9bad6de0c5a8e4abb1d43e65a
$ kubeadm reset
$ ifconfig cni0 down && ip link delete cni0
$ ifconfig flannel.1 down && ip link delete flannel.1$ rm -rf /var/lib/cni/