1. 首页
  2. Kubernetes

kubeadm部署k8s1.18.5及节点扩容

 

 
目前生产部署Kubernetes集群主要有两种方式:
1、kubeadm
Kubeadm是一个K8s部署工具,提供kubeadm init和kubeadm join,用于快速部署Kubernetes集群。
官方地址:https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/
 
2、二进制包
从github下载发行版的二进制包,手动部署每个组件,组成Kubernetes集群。
Kubeadm降低部署门槛,但屏蔽了很多细节,遇到问题很难排查。如果想更容易可控,推荐使用二进制包部署Kubernetes集群,虽然手动部署麻烦点,期间可以学习很多工作原理,也利于后期维护。
 
 
一、本文通过kubeadn部署kubernetes
1、系统准备:
查看系统系统版本、配置网络阿里源等
CentOS Linux release 7.8.2003 (Core)
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
 
 
配置主机名
systemctl set-hostname k8s-master(不过本文的操作中我没改,Master节点主机名是node1)
vim /etc/hosts
172.20.10.6 k8s-master
 
关闭swap,注释swap分区
swapoff -a
vim /etc/fstab   注释swap设备
#/dev/mapper/centos-swap swap  swap   defaults  0 0
 
配置内核参数,将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl –system
 
2、安装常用包
yum install vim bash-completion net-tools gcc -y
 
 
 
3、安装docker-ce
yum install -y yum-utils device-mapper-persistent-data lvm2
 
yum-config-manager –add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
 
yum -y install docker-ce
安装时报软件包需要container-selinux >= 2:2.74
yum install container-selinux -y    
安装最新的contain-selinux,然后再安装docker-ce即可成功。
 
添加使用阿里云配置镜像加速器:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-‘EOF’
{
  “registry-mirrors”: [“https://ojr73abz.mirror.aliyuncs.com”]
}
EOF
 
systemctl daemon-reload
systemctl restart docker
 
4、安装kubectl、kubelet、kubeadm
添加阿里kubernetes源
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
 
yum -y install kubectl kubelet kubeadm
systemctl enable kubelet
查看版本:
kubeadm version; kubelet –version; kubectl version
 
 
5、初始化k8s集群
kubeadm init –kubernetes-version=1.18.5  \–apiserver-advertise-address=172.20.10.6  \–image-repository registry.aliyuncs.com/google_containers  \–service-cidr=10.10.0.0/16 –pod-network-cidr=10.122.0.0/16
 
pod的网段为: 10.122.0.0/16, api server地址就是master本机IP。
这一步很关键,由于kubeadm 默认从官网k8s.grc.io下载所需镜像,国内无法访问,因此需要通过–image-repository指定阿里云镜像仓库地址。
 
初始化成功会输出如下:
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
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 172.20.10.6:6443 –token yfmhxq.mvuu0aer2665nulz \
    –discovery-token-ca-cert-hash sha256:d959447af0814de5b955bce4b9c773e9074f5dcc6582b6400fd279d03d7cb047
 
记录生成的最后粗体部分内容,此内容需要在其它节点加入Kubernetes集群时执行。
根据提示创建kubectl:
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
 
执行下面命令,使kubectl可以自动补充
source <(kubectl completion bash)
 
#若需要,执行该命令会清空集群信息
#kubeadm reset
 
查看节点、pod:
kubectl get node ; kubectl get pod –all-namespaces
node节点为NotReady,因为corednspod没有启动,缺少网络pod
 
6、安装calico网络
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
稍等一会,查看node pod状态,此时集群状态正常
kubeadm部署k8s1.18.5及节点扩容
 
7、安装kubernetes-dashboard
官方部署dashboard的服务没使用nodeport,将yaml文件下载到本地,在service里添加nodeport
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml
vim recommended.yaml修改:(蓝色字体是新增的)
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
spec:
  type: NodePort
  ports:
    – port: 443
      targetPort: 8443
      nodePort: 30000    #The range of valid ports is 30000-32767
  selector:
    k8s-app: kubernetes-dashboard
 
kubectl create -f recommended.yaml
 
查看pod,sevice
kubectl get pod –all-namespaces
kubectl get svc -n kubernetes-dashboard
kubeadm部署k8s1.18.5及节点扩容
 
8、创建service account并绑定默认cluster-admin管理员集群角色:
kubectl create serviceaccount dashboard-admin -n kube-system
kubectl create clusterrolebinding dashboard-admin –clusterrole=cluster-admin –serviceaccount=kube-system:dashboard-admin
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk ‘/dashboard-admin/{print $1}’)
获取token并复制
浏览器访问:https://172.20.10.6:30000,输入token
kubeadm部署k8s1.18.5及节点扩容
 
二、集群节点扩容
1、添加node节点,配置环境:
配置主机名
kubectl set-hostname k8s-node
 
配置hosts文件,各个节点操作:
172.20.10.7     k8s-node
172.20.10.6     k8s-master
 
关闭防火墙和selinux
关闭swap
修改内核参数
(同步骤一相同)
 
2、设置yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager –add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
 
添加阿里kubernetes源:
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
 
3、安装docker-ce
注意安装以下两个服务版本要和master端一致,可以去master看下:
yum -y install  kubelet kubeadm    
systemctl start docker
systemctl enable docker
systemctl enable kubelet
 
4、将本node加入Master集群(从一初始化集群获得token)
kubeadm join 172.20.10.6:6443 –token yfmhxq.mvuu0aer2665nulz –discovery-token-ca-cert-hash sha256:d959447af0814de5b955bce4b9c773e9074f5dcc6582b6400fd279d03d7cb047
kubeadm部署k8s1.18.5及节点扩容
 
5、在master机器上查看node,状态为ready即可。
kubeadm部署k8s1.18.5及节点扩容
最后在浏览器界面查看到加入集群的k8s-node即可。
 

原创文章,作者:wz,如若转载,请注明出处:https://www.wzstyle.cn/619.html

发表评论

邮箱地址不会被公开。 必填项已用*标注