Ubuntu16.04 部署 Kubernetes 笔记

仅仅为一个笔记,持续更新

参考

Kubernetes 集群搭建

关闭交换分区

1
swapoff -a

同时永久禁用交换分区

1
sudo sed -i.bak '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

获取手动注释掉 /etc/fstab 中出现 swap 的行

执行 cat /proc/swaps 查看是否禁用成功


安装 Docker

参考官网教程

1
sudo apt-get remove docker docker-engine docker.io containerd runc

先配置一下 Ubuntu 的基础源镜像,使用清华源

1
2
3
4
5
6
7
8
sudo apt-get update

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

由于国外源下载 Docker 较慢,使用国内镜像

1
2
3
4
5
6
7
8
9
10
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository \
"deb https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
$(lsb_release -cs) \
stable"

sudo apt-get update

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

启动同时设置为开机启动

1
systemctl start docker && systemctl enable docker

注意,由于之后需要使用 Docker 下载容器镜像,建议为 Docker 配置国内镜像源


安装 kubeadm

1
2
3
4
5
6
7
8
9
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

备注:由于 Kubelet 版本和下面的 Kubernetes 的版本需要保持一致,所以安装的时候建议指定版本

1
2
apt-get install apt-show-versions
apt-show-versions -a kubelet

随后指定版本安装

1
apt-get install -y kubelet=1.17.0-00 kubeadm kybectl

使用 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
38
# a. 查看都需要哪些镜像需要下载
kubeadm config images list --kubernetes-version=v1.17.0

# 输出
# k8s.gcr.io/kube-apiserver:v1.17.0
# k8s.gcr.io/kube-controller-manager:v1.17.0
# k8s.gcr.io/kube-scheduler:v1.17.0
# k8s.gcr.io/kube-proxy:v1.17.0
# k8s.gcr.io/pause:3.2
# k8s.gcr.io/etcd:3.4.3-0
# k8s.gcr.io/coredns:1.6.7

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

#!/bin/bash
### config the image map
declare -A images map=()
images["k8s.gcr.io/kube-apiserver:v1.17.0"]="registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.17.0"
images["k8s.gcr.io/kube-controller-manager:v1.17.0"]="registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.17.0"
images["k8s.gcr.io/kube-scheduler:v1.17.0"]="registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.17.0"
images["k8s.gcr.io/kube-proxy:v1.17.0"]="registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.17.0"
images["k8s.gcr.io/pause:3.2"]="registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2"
images["k8s.gcr.io/etcd:3.4.3-0"]="registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0"
images["k8s.gcr.io/coredns:1.6.7"]="registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.7"
### 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.17.0

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

安装网络插件

Installing a pod network add-on

修改 kube-controller 配置文件,默认情况下为 /etc/kubernetes/manifests/kube-controller-manager.yaml

添加 --cluster-cidr=10.244.0.0/16 配置

1
2
3
4
5
6
7
8
9
# ...
spec:
containers:
- command:
- kube-controller-manager
# ...
- --cluster-cidr=10.244.0.0/16
- --bind-address=127.0.0.1
# ...

安装网络插件,这里选取 calico

执行命令安装

1
2
kubectl taint nodes --all node-role.kubernetes.io/master-
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

重启 kubelet

1
systemctl daemon-reload && systemctl restart kubelet

集群分节点(略,未完成)

安装 Docker 即可

安装 Kubelet

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


core-dns

core dsn arror loop ???? 还是没解决


其他

fix Linux locale issue

1
export LC_ALL="en_US.UTF-8"

修改 locale