微服务学习 - 使用kubeadm安装k8s集群

本文记录了怎么使用kubeadm安装k8s集群的过程,基本为根据官网文档步骤结合实际情况进行一些修改和简化。
环境系统为ubuntu 18.04 x64。
首先,需要一个代理,用于下载k8s服务的镜像。假设此处代理地址为:http://192.168.0.1:1080。

配置代理

配置用于curl、wget的代理

1
2
3
export http_proxy=http://192.168.0.1:1080
export https_proxy=http://192.168.0.1:1080
export no_proxy=127.0.0.1,192.168.* # 配置bypass的地址

配置用于apt的代理

1
2
3
4
5
cat <<EOF> /etc/apt/apt.conf
Acquire::http::proxy "http://192.168.0.1:1080/";
Acquire::https::proxy "http://192.168.0.1:1080/";
Acquire::ftp::proxy "http://192.168.0.1:1080/";
EOF

安装容器服务

默认选择docker,安装步骤如下

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
// 下载安装
apt-get update && apt-get install -y apt-transport-https ca-certificates curl software-properties-common gnupg2
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
apt-get update && apt-get install -y containerd.io=1.2.13-1 docker-ce=5:19.03.8~3-0~ubuntu-$(lsb_release -cs) docker-ce-cli=5:19.03.8~3-0~ubuntu-$(lsb_release -cs)

// 配置
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF

// 配置代理
mkdir -p /etc/systemd/system/docker.service.d
cat <<EOF> /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://192.168.0.1:1080/"
EOF

// 重载、启动服务
systemctl daemon-reload
systemctl restart docker

安装kubeadm、kubectl、kubelet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 添加源
apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

// 下载安装
apt update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

// 重载、启动服务
systemctl daemon-reload
systemctl restart kubelet

创建k8s集群

1
kubeadm init --pod-network-cidr=10.244.0.0/16

配置集群

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

配置集群网络

选择flannel网络

1
2
wget https://github.com/coreos/flannel/blob/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml

配置界面

选择dashboard界面

1
2
3
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml dashboard.yaml
// 修改文件,将其中的

修改文件,将其中的kubernetes-dashboard Service资源更改为如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
type: NodePort # 修改为node port模式
ports:
- port: 443
targetPort: 8443
nodePort: 30000 # 配置node port
selector:
k8s-app: kubernetes-dashboard

新增名为admin用于访问dashboard的ServiceAccount,绑定名为cluster-admin的ClusterRole,内容示例如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: admin
annotations:
rbac.authorization.kubernetes.io/autoupdate: "true"
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
name: admin
namespace: kube-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin
namespace: kube-system
labels:
kubernetes.io/cluster-service: "true"
addonmanager.kubernetes.io/mode: Reconcile

使用kubectl命令apply上述资源,即可访问dashboard界面,地址参考上述的Service服务,或可使用命令查看服务信息

1
kubectl get svc -n kubernetes-dashboard

登录方式选择Token,集群token的获取方法如下

1
kubectl describe secret/$(kubectl get secret -nkube-system |grep admin|awk '{print $1}') -nkube-system|grep ^token|python3 -c "s = input();print(s.split(':')[-1].strip())"

其他配置

如果是all in one模式,那么想在master上运行pod,需要应用以下配置

1
kubectl taint node {你的master名称} node-role.kubernetes.io/master-

如果需要加入其他节点

1
2
3
4
5
6
7
// 生成token的hash
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
kubeadm join {你集群的api地址,如http://xx.xx.xx.xx:6443} --token zpw89f.cdr3jz4arcorj9aw \
--discovery-token-ca-cert-hash {此处为上一步骤生成的hash值}

// 在待加入节点执行
kubeadm join --token <token> <control-plane-host>:<control-plane-port> --discovery-token-ca-cert-hash sha256:<hash>

命令补全

1
echo "source <(kubectl completion bash)" >> ~/.bashrc