k8s 1.15.1 集群搭建之kubeadm简易安装方法

运行如下脚本

#最小化安装没有yum-utils
yum -y install yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache
#安装docker
yum -y install docker-ce
#设置docker镜像加速
echo '{"registry-mirrors": ["http://hub-mirror.c.163.com"]}'>/etc/docker/daemon.json
#重启生效
systemctl start docker & systemctl enable docker
#禁用swap,否则kubelet无法正常使用
sed -i '/ swap / s/^/#/' /etc/fstab
swapoff -a
#关闭防火墙,各种端口访问简化设置
systemctl stop firewalld & systemctl disable firewalld
#配置K8S软件源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
exclude=kube*
EOF
#目前kubelet还不支持selinux
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
#安装kubelet kubeadmin kubectl
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable --now kubelet
#官方说是有些用户反映CentOS的BUG
cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system

#get latest packages
yum update

下载镜像

使用如下命令检查所需镜像。

kubeadm config images list

修改如下脚本的KUBE_VERSION为上面命令里面获取的版本号,保存并运行。

cat>pull_images.sh<<'EOF'
#!/bin/bash
KUBE_VERSION=v1.15.1
FLANNEL_VERSION=v0.11.0
KUBE_PAUSE_VERSION=3.1	
ETCD_VERSION=3.3.10
DNS_VERSION=1.3.1
prefix=registry.cn-hangzhou.aliyuncs.com/google_containers
images=(
        kube-proxy-amd64:${KUBE_VERSION}
        kube-scheduler-amd64:${KUBE_VERSION}
        kube-controller-manager-amd64:${KUBE_VERSION}
        kube-apiserver-amd64:${KUBE_VERSION}
        pause:${KUBE_PAUSE_VERSION}
        etcd-amd64:${ETCD_VERSION}
        coredns:${DNS_VERSION}
        )
for image in ${images[@]}
do
NEW_IMAGE=`echo ${image}|awk '{gsub(/-amd64/,"",$0);print}'`
echo ${NEW_IMAGE}
docker pull ${prefix}/${image}
docker tag ${prefix}/${image} k8s.gcr.io/${NEW_IMAGE}
docker rmi ${prefix}/${image}
done
#prefix仓库里面没有flannel,单独拉取
docker pull quay-mirror.qiniu.com/coreos/flannel:${FLANNEL_VERSION}-amd64
docker tag quay-mirror.qiniu.com/coreos/flannel:${FLANNEL_VERSION}-amd64 quay.io/coreos/flannel:${FLANNEL_VERSION}-amd64 
EOF
bash pull_images.sh
#不执行如下命令运行kubectl时会报错:The connection to the server localhost:8080 was refused
export KUBECONFIG=/etc/kubernetes/admin.conf
#简化kubectl命令输入
cat >> ~/.bashrc <<EOF
alias k='kubectl'
alias kk='kubectl -n kube-system'
export KUBECONFIG=/etc/kubernetes/admin.conf
EOF
source ~/.bashrc

至此模板结点已经配置完成!

生成node1

关机,在node0上右键选择复制,输入node1,勾选初使化所有网卡的MAC地址,点击继续,选择完全复制,点击继续,选择当前电脑虚拟状态,点击复制。参照node0配置好端口转发。

集群安装

在node0上执行如下初使化集群。

#设置主机名为node0
hostnamectl set-hostname node0
kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.15.1 --apiserver-advertise-address=10.4.0.5
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/2140ac876ef134e0ed5af15c65e414cf26827915/Documentation/kube-flannel.yml

单结点使用如下命令去除污点,使得master也成为工作结点

kubectl taint nodes --all node-role.kubernetes.io/master-

记录下如上命令最后的输出,用于其它结点加入集群。

kubeadm join 10.4.0.5:6443 --token sd7jv7.aq4cis0cfftrm0p1 \
    --discovery-token-ca-cert-hash sha256:17c6ccc29821224ac40834f28fda20bedafc505364fb1b7c3823e9dd8385f6a3

启动node1,在node0上执行如下命令将配置文件分发到node1,方便使用。

scp /etc/kubernetes/admin.conf root@10.4.0.6:/etc/kubernetes/admin.conf

node1上运行如下命令设置主机名

hostnamectl set-hostname node1

运行如上的kubeadm join加入集群,至此,2节点集群创建完成!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,***s实施的一些建议和步骤: 1. 确保环境准备: - 确保系统配置符合要求,如CentOS 7、k8s版本1.15.1、docker版本18.09.7。 - 确保有足够的机器数量,至少需要4台机器,包括k8s-master1、k8s-master2、k8s-worker1和k8s-worker2。 2. 安装和配置k8s集群: - 根据k8s版本和docker版本的对应关系,选择合适的k8s版本进行安装。 - 配置k8s集群的网络版本,确保与初始化集群yaml中配置的podSubnet对应。 - 部署负载均衡器,可以选择使用nginx或者haproxy,官方推荐使用haproxy。 - 使用keepalived实现负载均衡节点的高可用性,确保主节点宕机后备用节点能够提供服务。 3. 部署示例: - 在k8s-master1上执行以下命令,安装go和k8s相关工具: ```shell # 安装go wget https://dl.google.com/go/go1.14.4.linux-amd64.tar.gz tar -C /usr/local -xzf go1.14.4.linux-amd64.tar.gz export PATH=$PATH:/usr/local/go/bin echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc # 安装k8s相关工具 go get -u k8s.io/kubernetes cd $GOPATH/src/k8s.io/kubernetes make ``` - 在k8s-master1上执行以下命令,初始化k8s集群: ```shell kubeadm init --pod-network-cidr=<podSubnet> ``` - 在k8s-master2上执行以下命令,加入k8s集群: ```shell kubeadm join <k8s-master1-ip>:<k8s-master1-port> --token <token> --discovery-token-ca-cert-hash <hash> ``` - 在k8s-worker1和k8s-worker2上执行在k8s-master1上执行的加入集群命令。 4. 验证k8s集群: - 在任意一个节点上执行以下命令,确保所有节点都已加入集群: ```shell kubectl get nodes ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值