为躲避病毒在家无聊,于是搭了个k8s玩下,准备分三到四篇来介绍:
1、Kubernetes集群的搭建;
2、应用布属到K8s:点击查看《将应用发布到k8s(.Net Core与Go)》
3、从外部访问应用:重点介绍Ingress的方式来访问,点此查看《通过hostNetwork、NodePort、Ingress 访问K8s应用》。
布属说明
买了4台云服务器,一台作为master,三台作为nodes,系统采用CentOS,为节省成本只买了 1核2GB最低配的服务器,但在后面安装master主机的时候有最低要求,CPU必需大于 2 个以上,不得不又做升级。
1、各服务器的时间同步
yum install -y chrony
systemctl enable chrony.service
2、主机名称解析(配置内容每台服务器同步)
vi /etc/hosts
192.168.1.26 master.hixiu.com master
192.168.1.85 node01.hixiu.com node01
192.168.1.100 node02.hixiu.com node02
192.168.1.117 node03.hixiu.com node03
3、关闭防火墙
systemctl status firewalld
systemctl stop firewalld
systemctl status iptables
systemctl stop iptables
4、禁用SELinux
vim /etc/selinux/config
SELINUX=disabled
#确认
getenforce
5、关闭swap
swapoff -a
vim /etc/fstab
6、启用ipvs(非必须)
方式参考:https://www.cnblogs.com/ccbyk-90/p/11795903.html
docker是必需安装的,采用阿里云的镜像:http://mirrors.aliyun.com/docker-ce/linux/centos/
配置yum的repo
cd /etc/yum.repos.d/
wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#注:先检查是否已安装老的版本,需先行卸载老版本。
yum list installed | grep docker
yum remove -y docker-xxxxxx
#安装
yum install -y docker
启动docker
systemctl start docker
设为开机自启动
systemctl enable docker
配置yum仓库,内容来源:https://mirrors.aliyun.com/kubernetes/
cd /etc/yum.repos.d/
vim k8s.repo
#粘贴以下内容
[kubernetes]
name=Kubernetes Repository
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
检查一下是否生效
yum list all | grep "^kube"
开始安装:kubeadm kubectl kubelet
yum -y install kubeadm kubectl kubelet
检查一下安装的几个工具的内容
rpm -ql kubelet
rpm -ql kubeadm
rpm -ql kubectl
正式安装kubernetes,列出需要安装的镜像,先把镜像pull下来,这样在安装的时候能快点
kubeadm config images list
kubeadm config images pull
安装之前可以先测试下命令,版本号可能过命令 kubeadm version来查看
kubeadm init --kubernetes-version="v1.17.2" --pod-network-cidr="10.244.0.0/16" --dry-run --ignore-preflight-errors=Swap
--dry-run只是测试,通过后再执行
kubeadm init --kubernetes-version="v1.17.2" --pod-network-cidr="10.244.0.0/16" --ignore-preflight-errors=Swap
出现以下信息说明安装成功
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.1.26:6443 --token bt7gqk.cv1k071goaip44mq \
--discovery-token-ca-cert-hash sha256:dd6802dfb5d62aa9df059ba1b226ca914b64176a93e6fc3ce5627712027c9f92
配置的最后也列出限Nodes的加入方式,这个需要提前copy出来,避免丢失。
Nodes的安装安装完:kubeadm kubectl kubelet之后,执行以下命令即可将服务器加入进来,服务器都加入进来后,集群就搭建完了。
[root@k8s-0001 findo]# kubeadm join 192.168.1.26:6443 --token bt7gqk.cv1k071goaip44mq \
--discovery-token-ca-cert-hash sha256:dd6802dfb5d62aa9df059ba1b226ca914b64176a93e6fc3ce5627712027c9f92
#查看集群中的nodes
[root@k8s-0001 findo]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8s-0001 Ready master 4d2h v1.17.2
k8s-0002 Ready 4d2h v1.17.2
k8s-0003 Ready 4d2h v1.17.2
k8s-0004 Ready 4d2h v1.17.2
下一步准备在集群中布属应用。