📣读完这篇文章里你能收获到
- K8S安装全过程
- 博主自己实操笔记带你跳过所有坑
- 感谢点赞+收藏,避免下次找不到~
博主实操系统:Centos7、Centos8
一、基本环境配置
1. 关闭selinux
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
2. 关闭swap分区或禁用swap文件
swapoff -a
# 注释掉关于swap分区的行
yes | cp /etc/fstab /etc/fstab_bak
cat /etc/fstab_bak |grep -v swap > /etc/fstab
3. 修改网卡配置
$ vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
$ sysctl -p
4. 启用内核模块
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
cut -f1 -d " " /proc/modules | grep -e ip_vs -e nf_conntrack_ipv4
vim /etc/sysconfig/modules/ipvs.modules
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
5. 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
6. 设置机器HostName
这里设置为maste,其他节点需要自行定义,如node1,node2....
hostnamectl set-hostname master
二、kubectl、kubeadm、kubelet的安装
1. 添加Kubernetes的yum源,此处使用alibaba的镜像源
vim /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
2. 安装kubelet、kubeadm、kubectl
yum -y install kubelet-1.19.5-0.x86_64 kubeadm-1.19.5-0.x86_64 kubectl-1.19.5-0.x86_64
3. 启动kubelet服务
systemctl enable kubelet
systemctl start kubelet
此时执行
systemctl status kubelet
查看服务状态,服务状态应为Error(255), 如果是其他错误可使用journalctl -xe查看错误信息。
三、Docker安装和配置
1. Docker安装
2. Docker配置
① 配置cgroup-driver为systemd
vim /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
② 查看kubeadm所需镜像
kubeadm config images list
返回结果:
k8s.gcr.io/kube-apiserver:v1.16.3 k8s.gcr.io/kube-controller-manager:v1.16.3 k8s.gcr.io/kube-scheduler:v1.16.3 k8s.gcr.io/kube-proxy:v1.16.3 k8s.gcr.io/pause:3.1 k8s.gcr.io/etcd:3.3.15-0 k8s.gcr.io/coredns:1.6.2
③ 预先拉取所需镜像
docker pull kubeimage/kube-apiserver-amd64:v1.16.3
docker pull kubeimage/kube-controller-manager-amd64:v1.16.3
docker pull kubeimage/kube-scheduler-amd64:v1.16.3
docker pull kubeimage/kube-proxy-amd64:v1.16.3
docker pull kubeimage/pause-amd64:3.1
docker pull kubeimage/etcd-amd64:3.3.15-0
docker pull coredns/coredns:1.6.2
④ 对预先拉取的镜像重新打tag
docker tag kubeimage/kube-apiserver-amd64:v1.16.3 k8s.gcr.io/kube-apiserver:v1.16.3
docker tag kubeimage/kube-controller-manager-amd64:v1.16.3 k8s.gcr.io/kube-controller-manager:v1.16.3
docker tag kubeimage/kube-scheduler-amd64:v1.16.3 k8s.gcr.io/kube-scheduler:v1.16.3
docker tag kubeimage/kube-proxy-amd64:v1.16.3 k8s.gcr.io/kube-proxy:v1.16.3
docker tag kubeimage/pause-amd64:3.1 k8s.gcr.io/pause:3.1
docker tag kubeimage/etcd-amd64:3.3.15-0 k8s.gcr.io/etcd:3.3.15-0
docker tag coredns/coredns:1.6.2 k8s.gcr.io/coredns:1.6.2
⑤ 删除多余的镜像
docker rmi kubeimage/kube-apiserver-amd64:v1.19.16
docker rmi kubeimage/kube-controller-manager-amd64:v1.19.16
docker rmi kubeimage/kube-scheduler-amd64:v1.19.16
docker rmi kubeimage/kube-proxy-amd64:v1.19.16
docker rmi kubeimage/pause-amd64:3.2
docker rmi bitnami/etcd:3.4.13
docker rmi coredns/coredns:1.7.0
四、Master节点的配置
以上步骤需要在node节点和master节点执行,当前步骤仅需在master节点执行。
1. Master节点的初始化
--pod-network-cidr=10.244.0.0/16 指定使用Calico网络
--apiserver-advertise-address=10.0.3.62 指向master节点IP,此处也可以使用hosts
kubeadm init --pod-network-cidr=10.244.0.0/16 \
--kubernetes-version=v1.19.16 \
--apiserver-advertise-address=10.0.3.62 --image-repository=registry.aliyuncs.com/google_containers --ignore-preflight-errors=all --v=6
以上输出
...
...
...
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 10.0.3.62:6443 --token kt58np.djd3youoqb0bnz4r \
--discovery-token-ca-cert-hash sha256:37a3924142dc6d57eac2714e539c174ee3b0cda723746ada2464ac9e8a2091ce
保存输出中的kubeadm join
部分内容,用于添加node节点,或者使用kubeadm token list
和kubeadm token create --print-join-command
查看
- 本句不需要执行
kubeadm join 10.0.3.62:6443 --token kt58np.djd3youoqb0bnz4r \
--discovery-token-ca-cert-hash sha256:37a3924142dc6d57eac2714e539c174ee3b0cda723746ada2464ac9e8a2091ce
2. 执行剩余的初始化步骤
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
五、Calico网络插件的配置
Calico的官方文档地址为: https://docs.projectcalico.org/v3.10/getting-started/kubernetes/
1. 安装Calico
kubectl apply -f https://docs.projectcalico.org/v3.10/manifests/calico.yaml
2. 监听安装进度
watch kubectl get pods --all-namespaces
- 出现以下内容时为安装成功
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-kube-controllers-6ff88bf6d4-tgtzb 1/1 Running 0 2m45s
kube-system calico-node-24h85 1/1 Running 0 2m43s
kube-system coredns-846jhw23g9-9af73 1/1 Running 0 4m5s
kube-system coredns-846jhw23g9-hmswk 1/1 Running 0 4m5s
kube-system etcd-jbaker-1 1/1 Running 0 6m22s
kube-system kube-apiserver-jbaker-1 1/1 Running 0 6m12s
kube-system kube-controller-manager-jbaker-1 1/1 Running 0 6m16s
kube-system kube-proxy-8fzp2 1/1 Running 0 5m16s
kube-system kube-scheduler-jbaker-1 1/1 Running 0 5m41s
3. 测试
kubectl get nodes -o wide
- 输出
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
kubernetes-master Ready master 4d12h v1.19.6 10.244.0.101 <none> CentOS Linux 8 (Core)
六、Node节点的初始化
1. 登录node节点,执行加入集群的命令,完成加入集群操作
kubeadm join 10.0.3.62:6443 --token kt58np.djd3youoqb0bnz4r \
--discovery-token-ca-cert-hash sha256:37a3924142dc6d57eac2714e539c174ee3b0cda723746ada2464ac9e8a2091ce
2. 在master节点上查看添加结果
kubectl get nodes -o wide
- 输出:
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
kubernetes-master Ready master 4d12h v1.16.3 10.244.0.101 <none> CentOS Linux 8 (Core) 3.10.0-1062.el7.x86_64 docker://19.3.4
kubernetes-node-1 Ready <none> 4d12h v1.16.3 10.244.0.102 <none> CentOS Linux 8 (Core) 3.10.0-1062.el7.x86_64 docker://19.3.4