Kubernetes微型实战(5): Kubernetes集群搭建

本篇讲介绍一下如何搭建一个多节点的Kubernetes集群。但是,但是,但是,其实这个只是笔记,学习的来源是这个Kubernetes集群搭建

参考

Kubernetes集群搭建

关闭交换分区

1
swapoff -a

安装Docker

参考官网教程

1
sudo apt-get remove docker docker-engine docker.io containerd runc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
sudo apt-get update

sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo apt-key fingerprint 0EBFCD88

sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io

apt-cache madison docker-ce

安装kubeadm

1
2
3
4
5
6
7
8
9
10
11
12
13
apt-get update && apt-get install -y apt-transport-https

curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF

apt-get update

apt-get install -y kubelet kubeadm kubectl

apt-mark hold kubelet kubeadm kubectl

使用kubeadm创建集群

准备镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# a. 查看都需要哪些镜像需要下载
kubeadm config images list --kubernetes-version=v1.12.3

k8s.gcr.io/kube-apiserver:v1.12.3
k8s.gcr.io/kube-controller-manager:v1.12.3
k8s.gcr.io/kube-scheduler:v1.12.3
k8s.gcr.io/kube-proxy:v1.12.3
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.2.24
k8s.gcr.io/coredns:1.2

# b. 创建一个自动处理脚本下载镜像->重新tag->删除老tag
vim ./load_images.sh

#!/bin/bash
### config the image map
declare -A images map=()
images["k8s.gcr.io/kube-apiserver:v1.12.3"]="registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.12.3"
images["k8s.gcr.io/kube-controller-manager:v1.12.3"]="registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.12.3"
images["k8s.gcr.io/kube-scheduler:v1.12.3"]="registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.12.3"
images["k8s.gcr.io/kube-proxy:v1.12.3"]="registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.12.3"
images["k8s.gcr.io/pause:3.1"]="registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1"
images["k8s.gcr.io/etcd:3.2.24"]="registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.2.24"
images["k8s.gcr.io/coredns:1.2.2"]="registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.2.2"
### re-tag foreach
for key in ${!images[@]}
do
docker pull ${images[$key]}
docker tag ${images[$key]} $key
docker rmi ${images[$key]}
done
### check
docker images

# c. 执行脚本准镜像
sudo chmod +x load_images.sh
./load_images.sh

初始化集群(master)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
### 执行初始化命令
sudo kubeadm init --kubernetes-version=v1.12.3 --pod-network-cidr=10.244.0.0/16

### 最后的结果如下
... ...
Your Kubernetes master 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/

You can now join any number of machines by running the following on each node
as root:

kubeadm join 192.168.132.152:6443 --token ymny55.4jlbbkxiggmn9ezh --discovery-token-ca-cert-hash sha256:70265fafdb22d524c15616543d0b76527c686329221340b3b8da3652abed46b9

根据成功信息配置非管理员账号使用kubectl

1
2
3
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

集群分节点

安装Docker即可


使用网络插件

Installing a pod network add-on

分节点可能也需要用到前面的 ./load_images.sh 文件

主节点安装成功后,会有相应的 kubeadm join 命令


core-dns

core dsn arror loop ???? 还是没解决
参考链接1
参考连接2


Dashboard

关于它的安装请参考 K8S文件夹下的笔记中的doc文件

配置完成后执行如下命令

1
kubectl proxy --address='0.0.0.0'  --accept-hosts='^*$'

访问如下链接

1
http://你的Kubernetes所在IP:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy

更换网络插件

更换网络:/etc/kubernetes/manifests 中有kube-controller-manager相关的yaml文件,向其中配置两个参数

1
2
--allocate-node-cidrs=true
--cluster-cidr=10.244.0.0/16