文章目录
1. 准备环境
注意:
- 集群所有的机器之间要能相互ping通
- 可以访问外网,需要拉取镜像
# 关闭防火墙 并禁用
systemctl stop firewalld
systemctl disable firewalld
# 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
# 关闭swap分区
swapoff -a # 临时
vim /etc/fstab # 永久
# 配置主机名和ip映射
vim /etc/hosts
192.168.2.191 k8s-master
192.168.2.190 k8s-node1
192.168.2.189 k8s-node2
# 将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
2. 所有节点安装Docker/kubeadm/ kubelet
2.1 安装docker
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce-18.06.1.ce-3.el7
systemctl enable docker && systemctl start docker
docker --version
Docker version 19.03.11, build 42e35e61f3
2.2 添加阿里云yum源
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=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
2.3 安装kubeadm,kubelet和kubectl
# 在master节点执行
yum install -y kubelet kubeadm kubectl
systemctl enable kubelet
# 查看kubeadm版本信息
kubeadm version
# node节点执行
yum install -y kubelet kubeadm
systemctl enable kubelet
3. 部署Kubernetes Master
kubeadm init \
--apiserver-advertise-address=192.168.2.191 \ # 自己本机的ip
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.18.3 \ # k8s版本
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
# kubeadm init后会在 /etc 路径下生成配置文件和证书文件
[root@graph1 etc]# tree kubernetes/
kubernetes/
├── admin.conf
├── controller-manager.conf
├── kubelet.conf
├── manifests
│ ├── etcd.yaml
│ ├── kube-apiserver.yaml
│ ├── kube-controller-manager.yaml
│ └── kube-scheduler.yaml
├── pki
│ ├── apiserver.crt
│ ├── apiserver-etcd-client.crt
│ ├── apiserver-etcd-client.key
│ ├── apiserver.key
│ ├── apiserver-kubelet-client.crt
│ ├── apiserver-kubelet-client.key
│ ├── ca.crt
│ ├── ca.key
│ ├── etcd
│ │ ├── ca.crt
│ │ ├── ca.key
│ │ ├── healthcheck-client.crt
│ │ ├── healthcheck-client.key
│ │ ├── peer.crt
│ │ ├── peer.key
│ │ ├── server.crt
│ │ └── server.key
│ ├── front-proxy-ca.crt
│ ├── front-proxy-ca.key
│ ├── front-proxy-client.crt
│ ├── front-proxy-client.key
│ ├── sa.key
│ └── sa.pub
└── scheduler.conf
4. 使用kubectl工具
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# 查看节点状态
kubectl get nodes
5. 安装Pod网络插件
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
6. 将node节点加入到k8s集群
kubeadm join 192.168.2.191:6443 --token wbekm9.d7m4p57rro7xuwzw \
--discovery-token-ca-cert-hash sha256:69b5ee8eed42058d0993aceeb89320a9bf090bfae3f67aa1f858a32dd2dce1a7