(一)、环境
IP地址 | 系统 | 功能 |
---|---|---|
192.168.4.21 | CentOS7.4 | Master |
192.168.4.20 | CentOS7.4 | node1 |
192.168.4.19 | CentOS7.4 | node2 |
(二)、基础环境安装配置(每一台服务器都要执行)
1、关闭防火墙
[root@DEV004021 ~]# systemctl stop firewalld
[root@DEV004021 ~]# systemctl disable firewalld
2、创建/etc/sysctl.d/k8s.conf 文件
[root@DEV004021 ~]# vim /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
3、把以上配置修改的使其生效。
[root@DEV004021 ~]#modprobe br_netfilter
[root@DEV004021 ~]#sysctl -p /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
4、关闭虚拟内存
[root@DEV004021 ~]#sudo sed -i '/swap/ s/^/#/' /etc/fstab
[root@DEV004021 ~]#sudo swapoff -a
5、安装docker
5.1、删除旧版本的docker
[root@DEV004021 ~]# sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
5.2、安装必要的工具
[root@DEV004021 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
5.3、添加yum源的相关软件信息并更新缓存
[root@DEV004021 ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
5.4、更新并安装docker
[root@DEV004021 ~]# yum makecache fast
[root@DEV004021 ~]# yum install docker-ce -y
5.5、配置镜像加速
[root@localhost ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["http://hub-mirror.c.163.com"]
}
5.6、设置docker服务并做自启动
systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
systemctl start docker
6、安装kubelet、kubeadm、kubectl
[root@DEV004021 ~]# 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
[root@DEV004021 ~]# yum install -y kubelet kubeadm kubectl
systemctl enable --now kubelet
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
(三)、构建Kubernetes集群
1、初始化Master节点(只在master节点执行)。
[root@DEV004021 ~]# kubeadm init \
--apiserver-advertise-address=192.168.4.21 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.15.2 \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
####--pod-network-cidr :后续安装 flannel 的前提条件,且值为 10.244.0.0/16。--image-repository :指定镜像仓库这里是阿里云的仓库
2、查看输出日志如下,出现初始化成功了。
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
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.4.21:6443 --token dmzz6x.t864anv0btkyxjwi \
--discovery-token-ca-cert-hash sha256:2a8bbdd54dcc01435be1a3b443d33d0ce932c8d81c6d9ae8b3c248325977ceb1
3、依次执行如下命令:
[root@DEV004021 ~]# mkdir -p $HOME/.kube
[root@DEV004021 ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@DEV004021 ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
4、部署Pod Network到集群中
[root@otrs004021 ~]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/62e44c867a2846fefb68bd5f178daf4da3095ccb/Documentation/kube-flannel.yml
podsecuritypolicy.extensions/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.extensions/kube-flannel-ds-amd64 created
daemonset.extensions/kube-flannel-ds-arm64 created
daemonset.extensions/kube-flannel-ds-arm created
daemonset.extensions/kube-flannel-ds-ppc64le created
daemonset.extensions/kube-flannel-ds-s390x created
5、至此master节点初始化完毕,查看集群相关信息。
######查看集群相关信息
[root@otrs004021 ~]# kubectl cluster-info
Kubernetes master is running at https://192.168.4.21:6443
KubeDNS is running at https://192.168.4.21:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
#####查看节点相关信息
[root@otrs004021 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
otrs004097 Ready master 6m27s v1.15.2
#############查看pods信息
[root@otrs004021 ~]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-bccdc95cf-f5wtc 1/1 Running 0 6m32s
kube-system coredns-bccdc95cf-lnp2j 1/1 Running 0 6m32s
kube-system etcd-otrs004097 1/1 Running 0 5m56s
kube-system kube-apiserver-otrs004097 1/1 Running 0 5m38s
kube-system kube-controller-manager-otrs004097 1/1 Running 0 5m40s
kube-system kube-flannel-ds-amd64-xqdcf 1/1 Running 0 2m10s
kube-system kube-proxy-2lz96 1/1 Running 0 6m33s
kube-system kube-scheduler-otrs004097 1/1 Running 0 5m45s
###################初始化出现问题,使用如下命令进行重置
[root@DEV004021 ~]# kubeadm reset
[root@DEV004021 ~]# rm -rf /var/lib/cni/
[root@DEV004021 ~]# rm -f $HOME/.kube/config
(四)、添加kubernetes其他节点,有两种方法。
方法一、使用master节点初始化的token加入
[root@DEV004021 ~]# kubeadm join 192.168.4.21:6443 --token dmzz6x.t864anv0btkyxjwi \
--discovery-token-ca-cert-hash sha256:2a8bbdd54dcc01435be1a3b443d33d0ce932c8d81c6d9ae8b3c248325977ceb1
方法二、重新生成token来加入
[root@otrs004021 ~]# kubeadm token generate
3o7wop.z2kxzhy7p0zwnb3v
[root@otrs004021 ~]# kubeadm token create 3o7wop.z2kxzhy7p0zwnb3v --print-join-command --ttl=24h
kubeadm join 192.168.4.21:6443 --token 3o7wop.z2kxzhy7p0zwnb3v --discovery-token-ca-cert-hash sha256:2a8bbdd54dcc01435be1a3b443d33d0ce932c8d81c6d9ae8b3c248325977ceb1
2、在其他节点依次执行如下命令即可加入K8S
[root@DEV004021 ~]# kubeadm join 192.168.4.21:6443 --token 3o7wop.z2kxzhy7p0zwnb3v --discovery-token-ca-cert-hash sha256:2a8bbdd54dcc01435be1a3b443d33d0ce932c8d81c6d9ae8b3c248325977ceb1
[root@DEV004021 yum.repos.d]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
dev004019 Ready <none> 3d v1.15.2
dev004020 Ready <none> 3d v1.15.2
dev004021 Ready master 3d v1.15.2
至此,1个Master+2 nodes的K8S集群创建成功