一. 安装docker
1.安装依赖包
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
2.设置阿里云镜像源
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
使用docker脚本一键安装
curl -fsSL "https://get.docker.com/" | sh
systemctl enable --now docker
3.安装Docker-CE
Install Docker CE
Set up the repository
Install required packages.
yum install yum-utils device-mapper-persistent-data lvm2
Add Docker repository.
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
Install Docker CE.
yum update && yum install docker-ce-18.06.2.ce
Create /etc/docker directory.
mkdir /etc/docker
Setup daemon.
cat > /etc/docker/daemon.json <
Restart Docker
systemctl daemon-reload
systemctl restart docker
安装k8s
在Docker安装完成的基础上,完成一下步骤
使用阿里云镜像源
cat < /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
EOF
关闭SElinux
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
安装kubelet kubeadm kubectl
yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable --now kubelet #
开机启动kubelet
centos7用户还需要设置路由:
yum install -y bridge-utils.x86_64
modprobe br_netfilter # 加载br_netfilter模块,使用lsmod查看开启的模块
cat < /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system # 重新加载所有配置文件
systemctl disable --now firewalld # 关闭防火墙
k8s要求关闭swap (qxl)
swapoff -a && sysctl -w vm.swappiness=0 # 关闭swap
sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab # 取消开机挂载swap
国外服务器
kubeadm config images pull #拉取集群所需镜像,这个需要翻墙
国内服务器
kubeadm config images list #列出所需镜像,根据所需镜像安装
(不是一定是下面的,根据实际情况来选择版本)
建议节点中也进行此操作安装
根据所需镜像名字先拉取国内资源
docker pull mirrorgooglecontainers/kube-apiserver:v1.14.2
docker pull mirrorgooglecontainers/kube-controller-manager:v1.14.2
docker pull mirrorgooglecontainers/kube-scheduler:v1.14.2
docker pull mirrorgooglecontainers/kube-proxy:v1.14.2
docker pull mirrorgooglecontainers/pause:3.1
docker pull mirrorgooglecontainers/etcd:3.3.10
docker pull coredns/coredns:1.3.1 # 这个在mirrorgooglecontainers中没有
# 修改镜像tag
docker tag mirrorgooglecontainers/kube-apiserver:v1.14.2 k8s.gcr.io/kube-apiserver:v1.14.2
docker tag mirrorgooglecontainers/kube-controller-manager:v1.14.2 k8s.gcr.io/kube-controller-manager:v1.14.2
docker tag mirrorgooglecontainers/kube-scheduler:v1.14.2 k8s.gcr.io/kube-scheduler:v1.14.2
docker tag mirrorgooglecontainers/kube-proxy:v1.14.2 k8s.gcr.io/kube-proxy:v1.14.2
docker tag mirrorgooglecontainers/pause:3.1 k8s.gcr.io/pause:3.1
docker tag mirrorgooglecontainers/etcd:3.3.10 k8s.gcr.io/etcd:3.3.10
docker tag coredns/coredns:1.3.1 k8s.gcr.io/coredns:1.3.1
# 把所需的镜像下载好,init的时候就不会再拉镜像,由于无法连接google镜像库导致出错
# 删除原来的镜像
docker rmi mirrorgooglecontainers/kube-apiserver:v1.14.2
docker rmi mirrorgooglecontainers/kube-controller-manager:v1.14.2
docker rmi mirrorgooglecontainers/kube-scheduler:v1.14.2
docker rmi mirrorgooglecontainers/kube-proxy:v1.14.2
docker rmi mirrorgooglecontainers/pause:3.1
docker rmi mirrorgooglecontainers/etcd:3.3.10
docker rmi coredns/coredns:1.3.1
cat >> kubeadm-config.yaml <
初始化k8s
kubeadm reset --force #重置集群,初始化出错可以使用
#k8s初始化
#对hostname命名也有规范,报hostname不规范修改/etc/hostname,然后reboot就行了
kubeadm init --config=kubeadm-config.yaml --experimental-upload-certs
初始化成功会生成token
#每个机器创建的master以下部分都不同,需要自己保存好,用于添加k8s节点
kubeadm join ip:8443 --token xxx \
--discovery-token-ca-cert-hash sha256:xxxx
普通用户设置权限
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
安装网络calico、flannel
# use calico cni
kubectl apply -f https://docs.projectcalico.org/v3.7/manifests/canal.yaml
# use standard flannel
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.11.0/Documentation/kube-flannel.yml
node加入集群中
- node需要安装docker和kubectl\kubecadm\kubelet,且版本与master的版本一致。
#主节点初始化生成的token,在节点中使用
kubeadm join master_ip:8443 --token xxx \
--discovery-token-ca-cert-hash sha256:xxxx
# 加入成功后查看master的nodes数目,稍等节点进行初始化,然后从notready到ready的状态。
```