一键部署k8s,十分钟搞定

一.初始化

二.集群安装


一.初始化

我将繁琐的准备配置成了一个bash脚本,注意这里需要修改DNS解析下的nameserver,改为你的ip,在三个节点上执行脚本

#!/bin/bash


echo "1、修改DNS配置如有需要"
chattr -i /etc/resolv.conf
cat <>  /etc/resolv.conf 
nameserver 124.70.16.104
nameserver 121.36.26.129
nameserver 121.36.20.14
EOF


echo "清理一些无用的yum repos"
mkdir -p  /etc/yum.repos.d/back && mv /etc/yum.repos.d/ambari*   /etc/yum.repos.d/hdp*   /etc/yum.repos.d/back 

yum clean all

### 关闭swap分区
echo "swapoff ## 临时生效"
swapoff -a

## 永久生效可以编辑/etc/fstab文件 注释掉swap那行

echo "关闭防火墙"
systemctl stop firewalld
systemctl disable --now firewalld

echo "关闭sselinux"
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config



# 内核开启IPv4转发需要开启下面的模块
modprobe br_netfilter

cat > /etc/sysctl.d/k8s.conf < /etc/docker/daemon.json < /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
exclude=kube*
EOF



yum install -y kubelet-1.19.3 kubeadm-1.19.3 kubectl-1.19.3 --disableexcludes=kubernetes

sleep 1

kubeadm version

systemctl enable kubelet
systemctl daemon-reload
systemctl restart kubelet

二.集群安装

接下来在master节点

直接init

kubeadm init \
  --apiserver-advertise-address=master节点的阿里云公网ip \
  --image-repository registry.aliyuncs.com/google_containers

 或者init之前先把镜像拉取下来

kubeadm config image pull

如果显示镜像拉取失败,可以采取docker的方式获取镜像

docker pull gotok8s/kube-apiserver:v1.19.0
docker tag gotok8s/kube-apiserver:v1.19.0 k8s.gcr.io/kube-apiserver:v1.19.0

docker pull gotok8s/kube-controller-manager:v1.19.0
docker tag gotok8s/kube-controller-manager:v1.19.0 k8s.gcr.io/kube-controller-manager:v1.19.0

docker pull gotok8s/kube-scheduler:v1.19.0
docker tag gotok8s/kube-scheduler:v1.19.0 k8s.gcr.io/kube-scheduler:v1.19.0

docker pull gotok8s/kube-proxy:v1.19.0
docker tag gotok8s/kube-proxy:v1.19.0 k8s.gcr.io/kube-proxy:v1.19.0

docker pull gotok8s/pause:3.2
docker tag gotok8s/pause:3.2 k8s.gcr.io/pause:3.2


docker pull gotok8s/etcd:3.4.13-0
docker tag gotok8s/etcd:3.4.13-0 k8s.gcr.io/etcd:3.4.13-0

docker pull gotok8s/coredns:1.7.0
docker tag gotok8s/coredns:1.7.0 k8s.gcr.io/coredns:1.7.0

 init之后再主节点执行

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

安装网络插件:

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

init成功之后会有一行join命令,直接在其他的node上运行

kubeadm join 你的主节点ip:6443 \ 
	--token 8507uc.o0knircuri8etnw2 \
	--discovery-token-ca-cert-hash \
	sha256:acc37967fb5b0acf39d7598f8a439cc7dc88f439a3f4d0c9cae88e7901b9d3f

查看节点状态 

kubectl get nodes

一键部署k8s,十分钟搞定_第1张图片

刚刚加入时是noready状态,等几分钟就ready啦。这里就恭喜你部署完成啦。

你可能感兴趣的:(kubernetes,docker,容器)