K8s入门篇---centos7下的k8s搭建部署

k8s 同时被 2 个专栏收录
1 篇文章 0 订阅
4 篇文章 0 订阅

前言:相信很多的小伙伴想学习k8s,但是都被第一步难住了,那就是k8s的搭建。看了很多的blog,都是要求翻墙,相信痛点不用我讲,大家也都深有感触,那么笔者就不翻墙的环境下,提供一套k8s集群搭建的方案及脚本。(PS:心急的小伙伴,可以直接跳到附录,拿脚本直接一键搭建)

环境要求:centos7系统

1、关闭防火墙及关闭selinux

systemctl disable firewalld
systemctl stop firewalld
setenforce 0

2、配置阿里源

cat >> /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
EOF

3、安装kubernetes

yum install -y docker kubelet-1.11.0-0 kubeadm-1.11.0-0 kubectl-1.11.0-0  kubernetes-cni

4、开启服务

systemctl enable docker && systemctl start docker
systemctl enable kubelet && systemctl start kubelet

5、docker镜像加速

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://43jugwwr.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

6、下载镜像

vi my.sh

新建一个sh文件,内容如下(下载k8s相关镜像,下载后将镜像名改为k8s.gcr.io/开头的名字,以便kubeadm识别使用)

#!/bin/bash
images=(kube-proxy-amd64:v1.11.0 kube-scheduler-amd64:v1.11.0 kube-controller-manager-amd64:v1.11.0 kube-apiserver-amd64:v1.11.0
etcd-amd64:3.2.18 pause-amd64:3.1 kubernetes-dashboard-amd64:v1.8.3 k8s-dns-sidecar-amd64:1.14.8 k8s-dns-kube-dns-amd64:1.14.8
k8s-dns-dnsmasq-nanny-amd64:1.14.8 coredns:1.1.3)
for imageName in ${images[@]} ; do
  docker pull keveon/$imageName
  docker tag keveon/$imageName k8s.gcr.io/$imageName
  docker rmi keveon/$imageName
done
docker tag  k8s.gcr.io/pause-amd64:3.1 k8s.gcr.io/pause:3.1

给权限,并运行

chmod 777 my.sh
./my.sh

7、设置内核参数和禁用swap等

sysctl net.bridge.bridge-nf-call-iptables=1
sysctl net.bridge.bridge-nf-call-ip6tables=1
sudo swapoff -a
rm -rf /var/lib/etcd/

8、初始化master

 kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.11.0

9、执行初始化cluster

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

10、cluster机器安装网络(这里我们选择flannel方案,希望使用其他网络支持的小伙伴可以另行选择)

kubectl create -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

 

至此k8s的master节点创建ok了。worker节点请执行1-7的步骤,然后执行8步骤输出的加入集群命令(如下图)即可。

可通过执行kubectl get nodes命令,查看当前集群node及节点状态。

 

 

附:

按如下脚本创建sh文件,然后赋执行权限,执行即可。(worker节点加入master管理命令不要忘记噢)

1、k8s_master.sh

#!/bin/bash
#关闭防火墙及关闭selinux
systemctl disable firewalld
systemctl stop firewalld
setenforce 0

#配置阿里源
cat >> /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
EOF

#安装kubernetes
yum install -y docker kubelet-1.11.0-0 kubeadm-1.11.0-0 kubectl-1.11.0-0  kubernetes-cni

#开启服务
systemctl enable docker && systemctl start docker
systemctl enable kubelet && systemctl start kubelet

#docker加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://43jugwwr.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

#下载镜像
images=(kube-proxy-amd64:v1.11.0 kube-scheduler-amd64:v1.11.0 kube-controller-manager-amd64:v1.11.0 kube-apiserver-amd64:v1.11.0
etcd-amd64:3.2.18 pause-amd64:3.1 kubernetes-dashboard-amd64:v1.8.3 k8s-dns-sidecar-amd64:1.14.8 k8s-dns-kube-dns-amd64:1.14.8
k8s-dns-dnsmasq-nanny-amd64:1.14.8 coredns:1.1.3)
for imageName in ${images[@]} ; do
  docker pull keveon/$imageName
  docker tag keveon/$imageName k8s.gcr.io/$imageName
  docker rmi keveon/$imageName
done
docker tag  k8s.gcr.io/pause-amd64:3.1 k8s.gcr.io/pause:3.1


#设置内核参数
sysctl net.bridge.bridge-nf-call-iptables=1
sysctl net.bridge.bridge-nf-call-ip6tables=1

#禁用swap(虚拟内存)和移除etcd
sudo swapoff -a
yum remove -y etcd
rm -rf /var/lib/etcd/

#初始化master
kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.11.0
 
#执行初始化cluster
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

#cluster机器安装网络
kubectl create -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml










 

 

2、k8s_worker.sh

#!/bin/bash
#关闭防火墙及关闭selinux
systemctl disable firewalld
systemctl stop firewalld
setenforce 0

#配置阿里源
cat >> /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
EOF

#安装kubernetes
yum install -y docker kubelet-1.11.0-0 kubeadm-1.11.0-0 kubectl-1.11.0-0  kubernetes-cni

#开启服务
systemctl enable docker && systemctl start docker
systemctl enable kubelet && systemctl start kubelet

#docker加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://43jugwwr.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

#下载镜像
images=(kube-proxy-amd64:v1.11.0 kube-scheduler-amd64:v1.11.0 kube-controller-manager-amd64:v1.11.0 kube-apiserver-amd64:v1.11.0
etcd-amd64:3.2.18 pause-amd64:3.1 kubernetes-dashboard-amd64:v1.8.3 k8s-dns-sidecar-amd64:1.14.8 k8s-dns-kube-dns-amd64:1.14.8
k8s-dns-dnsmasq-nanny-amd64:1.14.8 coredns:1.1.3)
for imageName in ${images[@]} ; do
  docker pull keveon/$imageName
  docker tag keveon/$imageName k8s.gcr.io/$imageName
  docker rmi keveon/$imageName
done
docker tag  k8s.gcr.io/pause-amd64:3.1 k8s.gcr.io/pause:3.1


#设置内核参数
sysctl net.bridge.bridge-nf-call-iptables=1
sysctl net.bridge.bridge-nf-call-ip6tables=1

#禁用swap(虚拟内存)和移除etcd
sudo swapoff -a
yum remove -y etcd
rm -rf /var/lib/etcd/





 

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值