目录
主机名称 | ip地址 | 主要软件 |
master | 192.168.32.11 | docker/kubeadm/kubelet/kubectl/flannel |
node1 | 192.168.32.12 | docker/kubeadm/kubelet/kubectl/flannel |
node2 | 192.168.32.13 | docker/kubeadm/kubelet/kubectl/flannel |
一、部署docker步骤
1、master节点上操作
(1)安装依赖包
[root@master ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
(2)设置阿里云镜像源
[root@master ~]# cd /etc/yum.repos.d/
[root@master yum.repos.d]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
(3)安装Docker-CE社区版
[root@master yum.repos.d]# yum install -y docker-ce
[root@master yum.repos.d]# systemctl start docker.service
[root@master yum.repos.d]# systemctl enable docker.service
(4)设置镜像加速
[root@master yum.repos.d]# mkdir -p /etc/docker
[root@master yum.repos.d]# cd /etc/docker/
[root@master docker]# vim daemon.json
{
"registry-mirrors": ["https://cvzmdwsp.mirror.aliyuncs.com"]
}
(5)开启路由转发
[root@master docker]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
[root@master docker]# sysctl -p
net.ipv4.ip_forward = 1
(6)重启docker以及开机自启动docker
[root@master docker]# systemctl daemon-reload
[root@master docker]# systemctl restart docker.service
[root@master docker]# systemctl status docker.service
(7)查看版本
[root@master docker]# docker --version
Docker version 20.10.12, build e91ed57
2、node1节点上操作
(1)安装依赖包
[root@node1 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
(2)设置阿里云镜像源
[root@node1 ~]# cd /etc/yum.repos.d/
[root@node1 yum.repos.d]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
(3)安装Docker-CE社区版
[root@node1 yum.repos.d]# yum install -y docker-ce
[root@node1 yum.repos.d]# systemctl start docker.service
[root@node1 yum.repos.d]# systemctl enable docker.service
(4)设置镜像加速
[root@node1 yum.repos.d]# mkdir -p /etc/docker
[root@node1 yum.repos.d]# cd /etc/docker/
[root@node1 docker]# vim daemon.json
{
"registry-mirrors": ["https://cvzmdwsp.mirror.aliyuncs.com"]
}
(5)开启路由转发
[root@node1 docker]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
[root@node1 docker]# sysctl -p
net.ipv4.ip_forward = 1
(6)重启docker以及开机自启动docker
[root@node1 docker]# systemctl daemon-reload
[root@node1 docker]# systemctl restart docker.service
[root@node1 docker]# systemctl status docker.service
(7)查看版本
[root@node1 docker]# docker --version
Docker version 20.10.12, build e91ed57
3、node2节点上操作
(1)安装依赖包
[root@node2 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
(2)设置阿里云镜像源
[root@node2 ~]# cd /etc/yum.repos.d/
[root@node2 yum.repos.d]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
(3)安装Docker-CE社区版
[root@node2 yum.repos.d]# yum install -y docker-ce
[root@node2 yum.repos.d]# systemctl start docker.service
[root@node2 yum.repos.d]# systemctl enable docker.service
(4)设置镜像加速
[root@node2 yum.repos.d]# mkdir -p /etc/docker
[root@node2 yum.repos.d]# cd /etc/docker/
[root@node2 docker]# vim daemon.json
{
"registry-mirrors": ["https://cvzmdwsp.mirror.aliyuncs.com"]
}
(5)开启路由转发
[root@node2 docker]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
[root@node2 docker]# sysctl -p
net.ipv4.ip_forward = 1
(6)重启docker以及开机自启动docker
[root@node2 docker]# systemctl daemon-reload
[root@node2 docker]# systemctl restart docker.service
[root@node2 docker]# systemctl status docker.service
(7)查看版本
[root@node2 docker]# docker --version
Docker version 20.10.12, build e91ed57
二、安装kubeadm、kubelet和kubectl
1、指定安装源
(1)master节点上操作
[root@master ~]# cd /etc/yum.repos.d/
[root@master yum.repos.d]# vim kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
(2)node1节点上操作
[root@node1 ~]# cd /etc/yum.repos.d/
[root@node1 yum.repos.d]# vim kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
(3)node2节点上操作
[root@node2 ~]# cd /etc/yum.repos.d/
[root@node2 yum.repos.d]# vim kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
2、本地映射/etc/hosts
(1)master节点上操作
[root@master ~]# vim /etc/hosts
192.168.32.11 master
192.168.32.12 node1
192.168.32.13 node2
(2)node1节点上操作
[root@node1 ~]# vim /etc/hosts
192.168.32.11 master
192.168.32.12 node1
192.168.32.13 node2
(3)node2节点上操作
[root@node2 ~]# vim /etc/hosts
192.168.32.11 master
192.168.32.12 node1
192.168.32.13 node2
3、指定安装版本
(1)master节点上操作
[root@master ~]# yum install -y kubelet-1.15.0 kubeadm-1.15.0 kubectl-1.15.0
[root@master ~]# rpm -qa | grep kube
(2)node1节点上操作
[root@node1 ~]# yum install -y kubelet-1.15.0 kubeadm-1.15.0 kubectl-1.15.0
[root@node1 ~]# rpm -qa | grep kube
(3)node2节点上操作
[root@node2 ~]# yum install -y kubelet-1.15.0 kubeadm-1.15.0 kubectl-1.15.0
[root@node2 ~]# rpm -qa | grep kube
4、设置开机自启动
(1)master节点上操作
[root@master ~]# systemctl enable kubelet.service
(2)node1节点上操作
[root@node1 ~]# systemctl enable kubelet.service
(3)node2节点上操作
[root@node2 ~]# systemctl enable kubelet.service
5、关闭swap分区
(1)master节点上操作
①临时关闭
[root@master ~]# swapoff -a
②永久关闭
[root@master ~]# sed -i 's/.*swap.*/#&/' /etc/fstab
(2)node1节点上操作
①临时关闭
[root@node1 ~]# swapoff -a
②永久关闭
[root@node1 ~]# sed -i 's/.*swap.*/#&/' /etc/fstab
(3)node2节点上操作
①临时关闭
[root@node2 ~]# swapoff -a
②永久关闭
[root@node2 ~]# sed -i 's/.*swap.*/#&/' /etc/fstab
6、将桥接的流量传递到iptables的链
(1)master节点上操作
[root@master ~]# cd /etc/sysctl.d/
[root@master sysctl.d]# vim k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
[root@master sysctl.d]# sysctl --system
(2)node1节点上操作
[root@node1 ~]# cd /etc/sysctl.d/
[root@node1 sysctl.d]# vim k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
[root@node1 sysctl.d]# sysctl --system
(3)node2节点上操作
[root@node2 ~]# cd /etc/sysctl.d/
[root@node2 sysctl.d]# vim k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
[root@node2 sysctl.d]# sysctl --system
7、执行初始化(master节点上操作)
[root@master ~]# mkdir k8s
[root@master ~]# cd k8s/
[root@master k8s]# kubeadm init \
> --apiserver-advertise-address=192.168.32.11 \
> --image-repository registry.aliyuncs.com/google_containers \
> --kubernetes-version v1.15.0 \
> --service-cidr=10.1.0.0/16 \
> --pod-network-cidr=10.244.0.0/16
8、使用kubectl工具
[root@master k8s]# mkdir -p $HOME/.kube
[root@master k8s]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@master k8s]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
9、安装pod网络插件(flannel)
[root@master k8s]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml
10、查看组件状态 && 查看节点状态
[root@master k8s]# kubectl get cs
[root@master k8s]# kubectl get nodes
11、docker拉取flannel镜像
(1)node1节点上操作
[root@node1 ~]# docker pull lizhenliang/flannel:v0.11.0-amd64
(2)node2节点上操作
[root@node2 ~]# docker pull lizhenliang/flannel:v0.11.0-amd64
12、添加节点
(1)node1节点上操作
[root@node1 ~]# kubeadm join 192.168.32.11:6443 --token eoxbk7.42l72x67rrmdg4lw --discovery-token-ca-cert-hash sha256:734a6eee9e432751ada6e7821f33325a1546cf42ed5e7fdaa7a093a7b5d76c36
(2)node2节点上操作
[root@node2 ~]# kubeadm join 192.168.32.11:6443 --token eoxbk7.42l72x67rrmdg4lw --discovery-token-ca-cert-hash sha256:734a6eee9e432751ada6e7821f33325a1546cf42ed5e7fdaa7a093a7b5d76c36
13、master节点查看node状态 “三个ready”
[root@master k8s]# kubectl get nodes
[root@master k8s]# kubectl get pods --all-namespaces
14、打node标签
[root@master k8s]# kubectl label node node1 node-role.kubernetes.io/node=node
node/node1 labeled
[root@master k8s]# kubectl label node node2 node-role.kubernetes.io/node=node
node/node2 labeled
15、查看名称空间
[root@master k8s]# kubectl get namespaces
16、查看pod名称空间
[root@master k8s]# kubectl get pod -n kube-system
17、查看服务发现的资源
[root@master k8s]# kubectl get service