文章目录
- 1、启动集群
- 1.1 新增 kubernetes yum源:(所有节点)
- 1.2 安装kubelet kubeadm kubectl (master节点)
- 1.3 启动k8s-master节点 (master节点)
- 二、部署 flannel (master节点)
- 2.1 下载flannel插件的yml文件
- 2.2 修改 Network、image 字段;
- 2.3 创建flannel网络插件
- 2.4 获取下kube-system空间下的pod
- 三、添加 worker 节点
- 3.1 workr节点服务器系统初始化 (workr节点执行)
- 3.2 安装 kubeadm、kubelet (workr节点执行)
- 3.3 maser节点生成token (master节点执行)
- 3.4 workr节点加入k8s集群 (workr节点执行)
- 3.5 查看workr节点是否加入 (master节点执行)
1、启动集群
1.1 新增 kubernetes yum源:(所有节点)
CentOS、RHEL、Fedora系统可使用以下方式
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
1.2 安装kubelet kubeadm kubectl (master节点)
yum -y install kubeadm-1.18.6 kubectl-1.18.6 kubelet-1.18.6
注:安装时不指定版本号,默认安装最新版本
yum install kubeadm kubectl kubelet
1.3 启动k8s-master节点 (master节点)
启动kubelet服务
systemctl enable kubelet && systemctl start kubelet
初始化master节点
kubeadm init \
--apiserver-advertise-address=172.17.59.65 \
--image-repository registry.cn-beijing.aliyuncs.com/mykubeadm \
--kubernetes-version v1.18.6 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.2.0.0/16
执行以下命令
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
初始化后生成的以下命令需要保存好,用以添加work节点使用 (暂不执行)
kubeadm join 172.17.59.64:6443 --token k1ib2r.0vqhoueuzxiq063u \--discovery-token-ca-cert-hash sha256:422a1485a08f01743f140a7dfa68da5b02fe1ba75bad932ad015a155e8169391
二、部署 flannel (master节点)
2.1 下载flannel插件的yml文件
mkdir -p /opt/kubernetes/yml
cd /opt/kubernetes/yml/
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
2.2 修改 Network、image 字段;
Network:修改为 pod-network-cidr:10.2.0.0/16
# 默认为10.244.0.0/16,如果不是,修改对应的地址即可image: registry.cn-beijing.aliyuncs.com/mayaping/flannel:v0.12.0-amd64
# 在安装k8s过程中,执行kubectl apply -f kube-flannel.yml,会出现flannel Init:ImagePullBackOff错误,原因是quay.io镜像地址在国内访问不了,需要修改为国内镜像地址
2.3 创建flannel网络插件
# kubectl apply -f kube-flannel.yml
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds created
2.4 获取下kube-system空间下的pod
此命令可查看到flannel插件已经创建成功
kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-d7fd48dd-7bcqh 1/1 Running 0 6h2m
coredns-d7fd48dd-s7vzl 1/1 Running 0 6h2m
etcd-k8a-master 1/1 Running 0 6h2m
kube-apiserver-k8a-master 1/1 Running 0 6h2m
kube-controller-manager-k8a-master 1/1 Running 0 6h2m
kube-flannel-ds-rkng2 1/1 Running 0 47m
kube-proxy-46kdx 1/1 Running 0 6h2m
kube-scheduler-k8a-master 1/1 Running 0 6h2m
三、添加 worker 节点
3.1 workr节点服务器系统初始化 (workr节点执行)
3.2 安装 kubeadm、kubelet (workr节点执行)
yum install kubeadm-1.18.6 kubelet-1.18.6 -y
3.3 maser节点生成token (master节点执行)
- 重新产生master节点token
kubeadm token create --print-join-command
注:kubeadm init产生的token的有效期是24个小时
命令输出内容:
W1010 16:14:30.547944 2157 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
kubeadm join 172.17.59.64:6443 --token ngiris.f82y1tx1xdzs02zt --discovery-token-ca-cert-hash sha256:422a1485a08f01743f140a7dfa68da5b02fe1ba75bad932ad015a155e8169391
3.4 workr节点加入k8s集群 (workr节点执行)
在workr节点中执行master节点生成出来的token命令即可
kubeadm join 172.17.59.64:6443 --token ngiris.f82y1tx1xdzs02zt --discovery-token-ca-cert-hash sha256:422a1485a08f01743f140a7dfa68da5b02fe1ba75bad932ad015a155e8169391
3.5 查看workr节点是否加入 (master节点执行)
# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-master Ready master 4h13m v1.18.6
k8s-node1 Ready <none> 5m11s v1.18.6
注:状态为Ready即可