使用三台服务器(一主二从)搭建一套Kubernetes集群用于学习。
节点准备
- master:192.168.170.71
- node01:192.168.170.72
- node02: 192.168.170.73
环境设置
OS环境
* Centos7.5
EPEL源安装
yum install epel-release -y
yum clean all
时间同步
yum install chrony -y # 安装chrony
systemctl start chronyd && systemctl enable chronyd # 启动chronyd进程
timedatectl # 查看时间是否同步
修改主机名
hostnamectl set-hostname master # 192.168.170.71节点
hostnamectl set-hostname node01 # 192.168.170.72节点
hostnamectl set-hostname node01 # 192.168.170.73节点
关闭防火墙
systemctl stop firewalld && systemctl disable firewalld # 关闭防火墙并禁止开机自启动
systemctl status firewalld # 查看firewalld状态
关闭SElinux
setenforce 0 # 设置SElinux为Premissive
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config # 设置SElinux配置文件中为disabled
关闭SWAP
# setp1: 临时禁用swap
swapoff -a
# setp2: 修改配置文件,永久禁用,加注释
vi /etc/fstab
# /dev/mapper/centos-swap swap swap defaults 0 0
配置hosts解析(基于主机名称解析,每台都要配置)
cat <<EOF >> /etc/hosts
192.168.170.71 master
192.168.170.72 node01
192.168.170.73 node02
EOF
Docker部署
使用Docker作为Kubernetes编排的容器
Docker 阿里源:https://mirrors.aliyun.com/docker-ce
安装Docker
# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安装 Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# Step 4: 开启Docker服务
systemctl start docker && systemctl enable docker
配置Docker代理
# setp 1: 配置Docker代理拉取Kubernets运行所需要的镜像文件
vi /usr/lib/systemd/system/docker.service
# setp 2: 添加代理
Environment="HTTPS_PROXY=代理地址"
Environment="HTTP_PROXY=代理地址"
Environment="NO_PROXY=代理地址"
# setp 3: 重新载入daemon
systemctl daemon-reload
# setp 4: 检查Docker是否有代理配置
docker info
Kubernests安装
阿里云镜像:https://opsx.alibaba.com/mirror
搜索kubernets,点击后面的帮助
添加YUM源
cat <<EOF > /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
EOF
安装kubelet、kubectl、kubeadm
# setp1: 安装kubelet、kubeadm、kubectl
yum install -y kubelet kubeadm kubectl
# setp2: 启动
systemctl enable kubelet && systemctl start kubelet
PULL镜像
# setp1: pull镜像(每个node都要pull镜像)
kubeadm config images pull
初始化master
# setp1: 执行初始化(master节点执行)
kubeadm init --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12
# setp2:显示以下信息表示主节点初始化successfully
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.170.71:6443 --token xxyr6g.eenzpv5icronnuff \
--discovery-token-ca-cert-hash sha256:9f20cdfd78d902f1dc5e2ad0db97b584585a1641a16261ed3f0d0308681816d8
# setp3: (master)
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# setp4: 安装网络插件(master)
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
node节点加入master节点
# setp1: nodes节点加入主节点执行
kubeadm join 192.168.170.71:6443 --token xxyr6g.eenzpv5icronnuff \
--discovery-token-ca-cert-hash sha256:9f20cdfd78d902f1dc5e2ad0db97b584585a1641a16261ed3f0d0308681816d8