1 首先更新apt为阿里源,目的是为了下载更快,以及添加了k8s的的源
sudo vim /etc/apt/sources.list 将内容替换为如下
deb http://mirrors.aliyun.com/ubuntu/ xenial main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main
2 关闭swap,安装docker 并更改/etc/docker/daemon.json 内容如下,替换镜像源是因为k8s和calico需要的镜像都很慢,这两个源是测试多个源后比较靠谱的
关闭swap: 永久 echo "vm.swappiness = 0">> /etc/sysctl.conf
临时: sudo swapoff -a
/etc/docker/daemon.json 如下
{
"registry-mirrors": ["http://hub-mirror.c.163.com","https://registry.docker-cn.com"],
"exec-opts":["native.cgroupdriver=systemd"]
}
3 安装, 指定k8s版本,k8s初始环境需要下载各种docker镜像,但是很慢,需要,所以提前从阿里云拉取k8s需要的镜像并重新tag,
----- 查看自己版本需要的镜像命令
kubeadm config images list --kubernetes-version=v1.11.1
==============开始
sudo apt-get install kubeadm=1.15.0-00
sudo apt-get install kubectl=1.15.0-00
sudo apt-get install kubelet=1.15.0-00
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.15.0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.15.0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.15.0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.15.0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.10
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.3.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.15.0 k8s.gcr.io/kube-apiserver:v1.15.0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.15.0 k8s.gcr.io/kube-controller-manager:v1.15.0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.15.0 k8s.gcr.io/kube-scheduler:v1.15.0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.15.0 k8s.gcr.io/kube-proxy:v1.15.0
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1 k8s.gcr.io/pause:3.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.10 k8s.gcr.io/etcd:3.3.10
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.3.1 k8s.gcr.io/coredns:1.3.1
====================请注意以上流程所有机器结点都需要执行
4 初始化k8s , 注意自己的版本和--pod-network-cidr 设置的网络最好别和自己主机网段一致,会有问题,这儿加了--image-repository 按理可以不用上一步提前下载镜像,各位可以自己试试
kubeadm init --kubernetes-version="v1.15.0" --pod-network-cidr=10.0.8.0/24 --image-repository=registry.aliyuncs.com/google_containers | tee kubeadm-init.log
5 执行成功后会如下日志,
================
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.236.141:6443 --token 1y3ptk.5034wtr79tnhojg0 \
--discovery-token-ca-cert-hash sha256:dd42ff4f9ce3580fb1f7e4bafcc2412b704e072cb60f80cf4f06cc405cf4db3a
=====================
可以看到要建集群需要运行执行
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
这个时候可以通过命令 kubectl get pods -n kube-system 查看是否系统pod都启动成功,可以看到coredns 这个pod 没成功,这是因为没有安装k8s网络插件,本文选择的是calico,flannel据说性能差些
6 安装网络插件calico
这儿首先下载calico的yaml文件,如果初始化中更改了设置了网段,需要更改calico.yaml
wget -c https://docs.projectcalico.org/v3.8/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml
kubectl apply -f calico.yaml
然后 kubectl get pods -n kube-system 查看calico相关pod是否启动起来,
可以通过以下两个命令查看日志,
kubectl describe pod xxx -n kube-system 查看pod详细信息
kubectl logs xxx (-c container_name) -n kube-system pod中有多个容器可以加括号中内容选择容器看日志
=== 坑1(研究了一天) 如果calico-node无法启动或者出现ready 0/1 running的情况,请修改calico.yaml, 其中value可以通过ifconfig查看自己的网卡 是ens还是什么,
==坑2(又是一天)如果calico-node启动起来后doredns还是无法running状态, 并且通过log 或者describe发现 CrashLoopBackOff
修改 /run/resolvconf/resolv.conf 添加一行 nameserver 114.114.114.114
然后重启主机网络
systemctl restart networking.service
systemctl restart network-manager.service
6 最后如果所有pod都启动成功,那么久可以再node节点执行kubeadm init后提示的 kubeadm join xxx,
执行 kubectl get nodes 如果所有节点都ready,则恭喜你,k8s整个基础环境搭建成功
可能有些地方会有遗漏,各位提出来
有问题的朋友可以可以添加微信374055610,可以一起交流学习