概述
本篇文章主要介绍在CentOS7.3系统上使用Kubeadm安装kubernetes1.7.5。安装过程中,会通过阿里云的yum镜像和docker镜像站点访问各种被墙的资源。
1. 基础环境
除了下述信息,最好有一台可以访问google资源的云主机或者代理。此教程使用root帐户执行安装操作。
主机信息
k8s-master 10.23.118.35 2core,2G,20G CentOS Linux release 7.3.1611 (Core)
k8s-node01 10.23.118.36 2core,2G,20G CentOS Linux release 7.3.1611 (Core)
k8s-node02 10.23.118.37 2core,2G,20G CentOS Linux release 7.3.1611 (Core)
Hostname设置
hostnamectl --static set-hostname k8s-master
hostnamectl --static set-hostname k8s-node01
hostnamectl --static set-hostname k8s-node02
写入/etc/hosts
cat >> /etc/hosts << EOF
10.23.118.35 k8s-master
10.23.118.36 k8s-node01
10.23.118.37 k8s-node02
EOF
关闭防火墙与SELINUX
systemctl disable firewalld.service
systemctl stop firewalld.service
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
2. Kubernetes安装与配置
三台主机都要执行以下操作。
Docker安装
yum install docker -y
systemctl enable docker && systemctl start docker
安装完成后运行“docker version”检查下,版本应该是“1.12.6”。
kubernetes 阿里云yum源
cat >> /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
EOF
查看当前各个组件的版本
yum list kubeadm --showduplicates
yum list kubernetes-cni --showduplicates
yum list kubelet --showduplicates
yum list kubectl --showduplicates
安装
yum install kubeadm-1.7.5-0.x86_64
安装kubeadm时,会自动将Kubernetes的其它组件安装完成,对应版本如下
kubeadm 1.7.5-0, kubectl 1.7.5-0, kubelet 1.7.5-0, kubernetes-cni 0.5.1-0
kubelet配置
基础pause镜像设置
cat > /etc/systemd/system/kubelet.service.d/20-pod-infra-image.conf << EOF
[Service]
Environment="KUBELET_EXTRA_ARGS=--pod-infra-container-image=registry.cn-qingdao.aliyuncs.com/haitao/pause-amd64:3.0"
EOF
安装docker 1.12.6及版本需要设置cgroup-driver=cgroupfs
sed -i 's/cgroup-driver=systemd/cgroup-driver=cgroupfs/g' /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
说明:https://github.com/kubernetes/kubernetes/issues/43805此处有说明
启动服务
systemctl enable kubelet && systemctl start kubelet
Kubernetes Docker镜像下载
通过此网址查看依赖镜像的版本号:https://kubernetes.io/docs/admin/kubeadm/
因为google的资源国内无法访问,已将Kubernetes安装过程中所需求的镜像上传至阿里云
registry.cn-qingdao.aliyuncs.com/haitao/etcd-amd64
registry.cn-qingdao.aliyuncs.com/haitao/kube-apiserver-amd64
registry.cn-qingdao.aliyuncs.com/haitao/kube-controller-manager-amd64
registry.cn-qingdao.aliyuncs.com/haitao/kube-proxy-amd64
registry.cn-qingdao.aliyuncs.com/haitao/kube-scheduler-amd64
registry.cn-qingdao.aliyuncs.com/haitao/pause-amd64
registry.cn-qingdao.aliyuncs.com/haitao/k8s-dns-sidecar-amd64
registry.cn-qingdao.aliyuncs.com/haitao/k8s-dns-kube-dns-amd64
registry.cn-qingdao.aliyuncs.com/haitao/k8s-dns-dnsmasq-nanny-amd64
下载和上传镜像脚步** **[请在可以访问google资源的主机上运行]
#!/bin/bash
KUBE_VERSION=v1.7.5
KUBE_PAUSE_VERSION=3.0
ETCD_VERSION=3.0.17
DNS_VERSION=1.14.4
GCR_URL=gcr.io/google_containers
ALIYUN_URL=registry.cn-qingdao.aliyuncs.com/haitao
images=(kube-proxy-amd64:${KUBE_VERSION}
kube-scheduler-amd64:${KUBE_VERSION}
kube-controller-manager-amd64:${KUBE_VERSION}
kube-apiserver-amd64:${KUBE_VERSION}
pause-amd64:${KUBE_PAUSE_VERSION}
etcd-amd64:${ETCD_VERSION}
k8s-dns-sidecar-amd64:${DNS_VERSION}
k8s-dns-kube-dns-amd64:${DNS_VERSION}
k8s-dns-dnsmasq-nanny-amd64:${DNS_VERSION})
for imageName in ${images[@]} ; do
docker pull $GCR_URL/$imageName
docker tag $GCR_URL/$imageName $ALIYUN_URL/$imageName
docker push $ALIYUN_URL/$imageName
docker rmi $ALIYUN_URL/$imageName
done
3. 创建集群
首先在主机“k8s-master"上执行init操作。
api-advertise-addresses为“k8s-master" ip,pod-network-cidr指定IP段需要和kube-flannel.yml文件中配置的一致(kube-flannel.yaml在下面flannel的安装中会用到)
export KUBE_REPO_PREFIX="registry.cn-qingdao.aliyuncs.com/haitao"
export KUBE_ETCD_IMAGE="registry.cn-qingdao.aliyuncs.com/haitao/etcd-amd64:3.0.17"
kubeadm init --apiserver-advertise-address=10.23.118.35 --kubernetes-version=v1.7.5 --pod-network-cidr=10.244.0.0/16
如果一切顺利, 可以达到如下提示:
Your Kubernetes master has initialized successfully!
To start using your cluster, you need to run (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:
http://kubernetes.io/docs/admin/addons/
You can now join any number of machines by running the following on each node
as root:
kubeadm join --token c071b2.d57d76cd7d69a79d 10.23.118.35:6443
kubectl的kubeconfig配置
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
flannel安装
wget https://raw.githubusercontent.com/coreos/flannel/v0.9.0/Documentation/kube-flannel.yml
sed -i 's/quay.io\/coreos\/flannel:v0.9.0-amd64/registry.cn-qingdao.aliyuncs.com\/haitao\/flannel:v0.9.0-amd64/g' ./kube-flannel.yml
kubectl apply -f ./kube-flannel.yml
master节点安装验证
kubectl get cs
NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-0 Healthy {"health": "true"}
node节点安装和加入集群
执行如下指令:
export KUBE_REPO_PREFIX="registry.cn-qingdao.aliyuncs.com/haitao"
export KUBE_ETCD_IMAGE="registry.cn-qingdao.aliyuncs.com/haitao/etcd-amd64:3.0.17"
kubeadm join --token c071b2.d57d76cd7d69a79d 10.23.118.35:6443
node节点安装验证
kubectl get nodes
NAME STATUS AGE VERSION
k8s-master Ready 1d v1.7.5
k8s-node01 Ready 1d v1.7.5
k8s-node02 Ready 1d v1.7.5
4. 参考
使用kubeadm安装kubernetes1.7 http://blog.csdn.net/zhuchuangang/article/details/76572157