主机准备
主机名 |
操作系统 |
CPU |
内存 |
硬盘 |
master1 |
ubuntu18.04 |
1*2核 |
2G |
20G |
node1 |
ubuntu18.04 |
1*2核 |
2G |
20G |
node2 |
ubuntu18.04 |
1*2核 |
2G |
20G |
集群安装
运行时安装(docker)
切换root账号sudo su -
1 2 3 4 5 6 7 8 9 10 11 12 13
| sudo apt-get update && sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common gnupg2
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key --keyring /etc/apt/trusted.gpg.d/docker.gpg add -
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 -y \ containerd.io=1.2.13-2 \ docker-ce=5:19.03.11~3-0~ubuntu-$(lsb_release -cs) \ docker-ce-cli=5:19.03.11~3-0~ubuntu-$(lsb_release -cs)
|
配置docker并设置开机启动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| sudo mkdir /etc/docker
cat <<EOF | sudo tee /etc/docker/daemon.json { "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2" } EOF
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo systemctl daemon-reload sudo systemctl restart docker sudo systemctl enable --now docker
|
集群工具安装
添加配置
1 2 3 4 5 6 7 8 9
| cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf br_netfilter EOF
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sudo sysctl --system
|
安装kubeadm
,kubelet
和kubectl
1 2 3 4 5 6 7 8 9 10 11 12
| # 添加镜像源公钥 curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
# 添加镜像源 echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" >/etc/apt/sources.list.d/kubernetes.list
# 安装 sudo apt-get update sudo apt-get install -y apt-transport-https curl sudo apt-get install -y kubelet=1.20.1-00 kubeadm=1.20.1-00 kubectl=1.20.1-00
sudo apt-mark hold kubelet kubeadm kubectl
|
初始化集群
每个节点都拉取镜像(选择国内源):
1 2
| kubeadm config images pull \ --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers
|
关闭swap
:
1 2 3
| swapoff -a vim /etc/fstab # 注释 /swap.img
|
Master节点初始化
地址必须是master节点地址:
1
| kubeadm init --apiserver-advertise-address 192.168.32.131 --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers
|
配置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
|
Node节点初始化
加入集群:
1
| kubeadm join 192.168.32.131:6443 --token c5brd6.qc9zfje7kpcbk6sv --discovery-token-ca-cert-hash sha256:aaa4365860781c328b2cc363447fff6dc0b61f1b5e96d05e2fa26bce1ad2585d
|
重置集群(退出集群):
查看token:
如果token过期,可创建token并直接打印join命令:
1
| kubeadm token create --print-join-command
|
插件安装
插件下载地址:百度网盘,提取码:akxk
安装网络插件
安装CNI插件calico
:
1
| kubectl create -f calico.yaml
|
安装MetricsServer
:
1
| kubectl create -f metrics-server.yaml
|
然后将master节点的/etc/kubernetes/pki/front-proxy-ca.crt
拷贝到各node节点下。
安装图形化界面
1
| kubectl create -f dashboard.yaml -f user.yaml
|
查看图形界面web服务访问端口(kubernetes-dashboard
服务对外的端口):
1
| kubectl get svc -n kubernetes-dashboard
|
查看访问用户:
1
| kubectl get secret -n kube-system
|
找到用户admin-user-token-xxx
,查看它的详细信息:
1
| kubectl describe secret admin-user-token-xxx -n kube-system
|
显示出访问token,输入到web登录界面中,进入图形化管理界面。
安装Ingress
设置某个节点标签:
1
| kubectl label node cka-node2 ingress=true
|
创建资源:
1
| kubectl create -f ingress.yaml
|
查看ingress节点ip:
1
| kubectl get po -n ingress-nginx -owide
|