接上文,master节点的搭建。现在master节点已经ready,我们需要部署两台node节点,组建一个k8s的集群。大致的步骤和搭建master差不多,但是更为简单,不过这里是Ubuntu系统,在搭建的时候遇到了很多的挫折。更多的是由于机器和命令不同引起的。

1. 部署前的准备工作

和搭建mater一样,这里在正式搭建node之前,也有很多的步骤

  • 第一件事永远是检查更换源
sudo su     #输入密码
cd /etc/apt  #切换到apt源文件
mv  sources.list sources.list_bak  #备份源文件
vim sources.list  #新建一个,然后将下面的内容copy进去
# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse

这里我有些包还是找不到所以在后面又加了清华的源:

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted universe multiverse

然后依次执行apt updateapt upgrade,等待资源的下载完成

  • 设置系统主机名以及 Host 文件的相互解析,具体步骤和上一篇博客对应部分相同
  • 安装依赖包
# yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git

这是Centos下的安装命令,这里我使用命令

apt-get install -y conntrack ntpdate ntp ipvsadm ipset jq iptables 
apt-get install -y sysstat vimnet-tools git

这里libseccomp并没有安装成功,找不到包,我就先跳过了,具体影响现在还未知

  • 设置防火墙为 Iptables 并设置空规则
systemctl stop firewalld && systemctl disable firewalld
# yum -y install iptables-services && systemctl start iptables && systemctl enable iptables&& iptables -F && service iptables save
# 以上是Centos下的命令
apt-get install iptables-services
# 这里我没有成功,没有和Centos对应起来
  • 关闭 SELINUX和swap
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
  • 重复了Centos对应步骤下的log模块修改
mkdir /var/log/journal # 持久化保存日志的目录
mkdir /etc/systemd/journald.conf.d
cat > /etc/systemd/journald.conf.d/99-prophet.conf <<EOF
[Journal]
# 持久化保存到磁盘
Storage=persistent
# 压缩历史日志
Compress=yes
SyncIntervalSec=5m
RateLimitInterval=30s
RateLimitBurst=1000
# 最大占用空间 10G
SystemMaxUse=10G
# 单日志文件最大 200M
SystemMaxFileSize=200M
# 日志保存时间 2 周
MaxRetentionSec=2week
# 不将日志转发到 syslog
ForwardToSyslog=no
EOF
systemctl restart systemd-journald

2.安装必备组件

  • kube-proxy开启ipvs的前置条件:
modprobe br_netfilter
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules &&
lsmod | grep -e ip_vs -e nf_conntrack_ipv4
# 以上是Centos下的对应步骤,但是因为前面iptables-services没有成功,这里也没有成功,但是modprobe br_netfilter成功
  • 安装Docker软件:
apt-get -y autoremove docker docker-engine docker.io docker-ce
apt-get update -y
apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
apt-get vim net-tools                                                                                                     
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 -y
# docker源加速
mkdir -p /etc/docker
echo '{"registry-mirrors":["https://vds6zmad.mirror.aliyuncs.com"]}' > /etc/docker/daemon.json
vim /etc/docker/daemon.json
# 内容如下:
{  "registry-mirrors": ["https://ozcouv1b.mirror.aliyuncs.com"] }

重启docker服务:

sudo systemctl daemon-reload
sudo systemctl restart docker
# 启动和自启动
systemctl start docker
systemctl enable docker

apt-get update && apt-get install -y apt-transport-https curl
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -

# 配置kubernetes阿里源
tee /etc/apt/sources.list.d/kubernetes.list <<-'EOF'
deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main
EOF

apt-get update

apt-get update可能会遇到如下错误:

The following signatures couldn't be verified because the public key is not
 available: NO_PUBKEY 6A030B21BA07F4FB 

这是签名认证失败,请记住上面的NO_PUBKEY后八位,添加认证:

gpg --keyserver keyserver.ubuntu.com --recv-keys XXXXXXXX

接着运行如下命令,看到ok。说明成功,之后进行安装:

gpg --export --armor XXXXXXXX | sudo apt-key add 

再次apt-get update

3. 安装k8s

sudo apt-get install kubeadm=1.15.1-00
sudo apt-get install kubectl=1.15.1-00
sudo apt-get install kubelet=1.15.1-00
systemctl enable kubelet.service

4.加入到master中

kubeadm join 10.1.21.152:6443 --token v4qcjk.7n2zrkt5hsik4pi6     --discovery-token-ca-cert-hash sha256:c2417c2d64be399e5115a473408f8ac8609507d7dccc95b9fec9970d36b99c84 

这里注意,token是8小时过期的,我第一次加入失败,因为master是前一天搭建的,第二天再拿那个token就不对了,可以重新生成:

kubeadm token create

如果你找不到–discovery-token-ca-cert-hash的值,可以使用以下命令生成:

openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'

5.在master上验证是否成功

在这里插入图片描述
运行5个nginx,并查看pod状态:
在这里插入图片描述

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐