赞
踩
节点名 Node | 角色 Role | 操作系统 OS | 容器运行时 Container Runtime | IP地址 IP Address | 版本 Version |
---|---|---|---|---|---|
k8s-01 | master | Ubuntu 20.04.2 LTS | Docker v20.10.17 | 10.117.136.98 | v1.22.1 |
k8s-02 | worker | Ubuntu 20.04.2 LTS | Docker v20.10.17 | 10.117.136.32 | v1.22.1 |
k8s-03 | worker | Ubuntu 20.04.2 LTS | Docker v20.10.17 | 10.117.136.77 | v1.22.1 |
以下命令根据实际情况,分别在三台VM上执行。
hostnamectl set-hostname k8s-0x #x为具体编号
下面以k8s-01
为例
vim /etc/hosts
修改和增加内容如下所示:
127.0.1.1 k8s-01
10.117.136.98 k8s-01
10.117.136.32 k8s-02
10.117.136.77 k8s-03
以下命令均在三台VM上无差别执行
将cn.archive.ubuntu.com
改为阿里云的源,然后更新系统源。
sed -i 's/http:\/\/cn.archive.ubuntu.com/http:\/\/mirrors.aliyun.com/g' /etc/apt/sources.list
apt-get update
实验环境下可以直接关闭防火墙或放行所有流量
#关闭防火墙 / 允许所有流量经过
iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat
iptables -P FORWARD ACCEPT
# 或
firewall-cmd --set-default-zone=trusted
关于 Kubernetes 组件使用了哪些端口和协议详见:Kubernetes 端口和协议
sed -i '/swap/d' /etc/fstab # 永久关闭swap
swapoff -a #临时关闭swap
此版本的Ubuntu 提供 AppArmor 作为 SELinux 的替代品。Kubernetes安全也需要用到AppArmor模块。
因此以下命令可执行也可不执行,建议执行,防止系统开启了SELinux。
# 将 SELinux 设置为 permissive 模式(相当于将其禁用)
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
# 安装setenforce,查看SELinux情况
apt-get install selinux-utils
setenforce 0
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf overlay br_netfilter EOF # 显式加载模块 modprobe overlay modprobe br_netfilter # 设置所需的 sysctl 参数,参数在重新启动后保持不变 cat <<EOF > /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 EOF # 应用 sysctl 参数而不重新启动 sysctl -p /etc/sysctl.d/k8s.conf
apt-get update
apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
apt-get update
apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
将cgroupfs 驱动改为systemd;为Docker增加国内开源镜像仓库
cat <<EOF > /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2",
"registry-mirrors": [
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn",
"https://registry.docker-cn.com"
]
}
EOF
systemctl enable docker --now
systemctl restart docker
apt-get update
apt-get install -y apt-transport-https ca-certificates curl
curl 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
sudo apt-get install kubelet=1.22.1-00 kubeadm=1.22.1-00 kubectl=1.22.1-00
systemctl start kubelet
sudo systemctl enable --now kubelet
以下命令在master节点执行
K8S官方指定的镜像仓库为Google 开源镜像仓库,但在国内因一些原因无法正常访问,因此需要使用 --image-repository
参数指定阿里云开源仓库,此仓库会从Google 开源镜像仓库完全复制镜像。
kubeadm init --kubernetes-version=v1.22.1 --pod-network-cidr=10.244.0.0/16 --image-repository registry.aliyuncs.com/google_containers
等待2-5min后,当初始化到最后时,系统会给出类似下面的提示(包括三行命令)
Your Kubernetes control-plane 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 Alternatively, if you are the root user, you can run: export KUBECONFIG=/etc/kubernetes/admin.conf 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/ Then you can join any number of worker nodes by running the following on each as root: kubeadm join 10.117.136.98:6443 --token t0j6m0.70oob6ala18mukbv \ --discovery-token-ca-cert-hash sha256:047cf4348e5f3076abf1ed1b3512201f0fe20724e4d4be81ae71aca104173375
此时需要在master节点执行以下三行命令。
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
在终端输入这些命令的目的:创建 kubeconfig文件,并将其导入k8s默认的config读取目录中。
在 k8s-02 和 k8s-03 上执行
kubeadm join 10.117.136.98:6443 --token t0j6m0.70oob6ala18mukbv \
--discovery-token-ca-cert-hash sha256:047cf4348e5f3076abf1ed1b3512201f0fe20724e4d4be81ae71aca104173375
等待一段时间后,标准输出会提示加入集群成功。
完成上述步骤后,在 master 上检查集群节点情况
# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-01 NotReady control-plane,master 23h v1.22.1
k8s-02 NotReady <none> 23h v1.22.1
k8s-03 NotReady <none> 23h v1.22.1
此时所有节点状态均为 NotReady
状态。因为节点间还无法正常通信,需要在节点间建立通信通道,例如安装calico或者flannel等
如果执行了 export KUBECONFIG=/etc/kubernetes/admin.conf
,那么K8S集群默认的配置文件目录不再是 $HOME/.kube/config
,而是 /etc/kubernetes/admin.conf
。那么之后我们配置多集群切换时,就需要注意修改config文件的路径。
假如没有执行初始化后的三行命令,那么在终端中输入:kubectl get nodes
时系统会报错,错误提示如下所示:
# kubectl get nodes
The connection to the server localhost:8080 was refused - did you specify the right host or port?
如果出现上面的错误,原因是没有指定合适的kubeconfig文件。
如果新的节点想要加入集群,需要重新让 kubeadm 打印新的加入集群命令,该命令会每隔一段时间(1天左右?)发生变化。
kubeadm token create --print-join-command
以下命令在master节点执行
curl https://docs.projectcalico.org/manifests/calico.yaml -O
这两条命令的意思是将 # - name: CALICO_IPV4POOL_CIDR
的注释取消。
将 # value: "192.168.0.0\/16
的注释取消,并将192.168改为10.244.
sed -i 's/# - name: CALICO_IPV4POOL_CIDR/- name: CALICO_IPV4POOL_CIDR/' calico.yaml
sed -i 's/# value: "192.168.0.0\/16"/ value: "10.244.0.0\/16"/' calico.yaml
kubectl apply -f calico.yaml
等待1-2分钟后,再次查看节点状态
# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-01 Ready control-plane,master 23h v1.22.1
k8s-02 Ready <none> 23h v1.22.1
k8s-03 Ready <none> 23h v1.22.1
此时一个完整的Kubernetes集群安装完成。
此脚本为 【3. 安装K8S】的脚本,其它步骤顺序执行即可。
#!/bin/bash # # 替换ubuntu国内源 sed 's/cn.archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list #实验环境设置 #关闭防火墙/允许所有流量经过 #firewall-cmd --set-default-zone=trusted iptables -F && iptables -X && iptables -F -t nat && iptables -X -t nat iptables -P FORWARD ACCEPT #关闭swap sudo sed -i '/swap/d' /etc/fstab sudo swapoff -a # 将 SELinux 设置为 permissive 模式(相当于将其禁用) #sudo apt-get install selinux-utils sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config # 查看SELinux情况 sudo apt-get install selinux-utils sudo setenforce 0 #安装docker #更新apt及安装相关依赖 sudo apt-get update sudo apt-get install \ ca-certificates \ curl \ gnupg \ lsb-release #添加docker的gpg key curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - #添加docker下载源 sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" #下载docker引擎 sudo apt-get update #安装最新稳定版本 sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin cat <<EOF > /etc/docker/daemon.json { "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2", "registry-mirrors": [ "http://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn", "https://registry.docker-cn.com" ] } EOF # 设置开机启动docker systemctl enable docker --now systemctl restart docker # 转发 IPv4 并让 iptables 看到桥接流量 cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf br_netfilter EOF cat <<EOF > /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 EOF sysctl -p /etc/sysctl.d/k8s.conf # 安装kubernetes # 设置kubernetes源 # 更新apt包并安装相关依赖 sudo apt-get update sudo apt-get install -y apt-transport-https ca-certificates curl # 下载kubernetes公共签名gpg密钥 curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - # 添加kubernetes的apt源 cat <<EOF >/etc/apt/sources.list.d/kubernetes.list deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main EOF sudo apt-get update #sudo apt-get install -y kubelet kubeadm kubectl sudo apt-get install kubelet=1.22.1-00 kubeadm=1.22.1-00 kubectl=1.22.1-00 #sudo apt-mark hold kubelet kubeadm kubectl systemctl start kubelet sudo systemctl enable --now kubelet sed -i '2i source <(kubectl completion bash)' /etc/profile source /etc/profile echo "在master上执行如下命令: " echo "" #echo "kubeadm init --kubernetes-version=v1.20.1 --pod-network-cidr=10.244.0.0/16" echo "kubeadm init --kubernetes-version=v1.22.1 --pod-network-cidr=10.244.0.0/16 --image-repository registry.aliyuncs.com/google_containers" echo ""
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。