Ubuntu 16.04搭建K8S集群:

vi /etc/apt/sources.list

deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable

deb http://mirrors.aliyun.com/ubuntu/ xenial mail restricted

deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main

:wq

apt-get update

apt-get install docker-ce

apt-get install kubeadm kubectl kubelet

初始化master节点:

kubeadm config images list

vi images.sh (根据上步的image名修改脚本内容,去掉k8s.gcr.io/)

#!/bin/bash

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.15.3
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.15.3 k8s.gcr.io/kube-apiserver:v1.15.3
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.15.3
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.15.3
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.15.3 k8s.gcr.io/kube-controller-manager:v1.15.3
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.15.3
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.15.3
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.15.3 k8s.gcr.io/kube-scheduler:v1.15.3
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.15.3
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.15.3
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.15.3 k8s.gcr.io/kube-proxy:v1.15.3
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.15.3
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1 k8s.gcr.io/pause:3.1
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.10
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.3.10 k8s.gcr.io/etcd:3.3.10
docker rmi 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/coredns:1.3.1 k8s.gcr.io/coredns:1.3.1
docker rmi registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.3.1

:wq

sh images.sh

注:
执行这个脚本的原因是国内无法访问k8s.gcr.io,所以通过下载阿里云上的image再改名

swapoff -a

kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=10.0.1.5

注:这步结束会给出slave加入集群的命令

mkdir ~/.kube

cp -rv /etc/kubernetes/admin.conf ~/.kube/config

chown root:root ~/.kube/config

安装网络插件flannel(也可以使用canal):

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

slave加入集群:

swapoff -a

kubeadm join 10.0.1.5:6443 --token yio1o6.zq85eudygtevzvd1 --discovery-token-ca-cert-hash sha256:318eebe90133c8f9892ac00c072c584025f36f1b6dae3ae4b5948f8c981bd90f

验证(在master上执行):

kubectl get nodes (状态显示为Ready即正常,如果是NotReady,可能是没安装网络插件)

kubectl get pod -n kube-system -o wide (看到状态为Running即为正常)