K8S再入门
对于K8S的安装,我们先尝试使用Kubeadm方式进行安装
首先准备2-3台服务器,要做到彼此内网互通,其次设置hostname,不能为localhost
所以第一步相关的过程如下
#1. 关闭防火墙: 如果是云服务器,需要设置安全组策略放行端口
systemctl stop firewalld systemctl disable firewalld #2.设置hostname hostnamectl set-hostname k8s-01 echo “127.0.0.1 $(hostname)” >> /etc/hosts #3.关闭selinux 和 swap sed -i ‘s/enforcing/disabled/’ /etc/selinux/config setenforce 0 swapoff -a sed -ri ‘s/.*swap.*/#&/’ /etc/fstab #4.设置桥接路由 # 修改 /etc/sysctl.conf # 如果有配置,则修改 sed -i “s#^net.ipv4.ip_forward.*#net.ipv4.ip_forward=1#g” /etc/sysctl.conf sed -i “s#^net.bridge.bridge-nf-call-ip6tables.*#net.bridge.bridge-nf-call-ip6tables=1#g” /etc/sysctl.conf sed -i “s#^net.bridge.bridge-nf-call-iptables.*#net.bridge.bridge-nf-call-iptables=1#g” /etc/sysctl.conf sed -i “s#^net.ipv6.conf.all.disable_ipv6.*#net.ipv6.conf.all.disable_ipv6=1#g” /etc/sysctl.conf sed -i “s#^net.ipv6.conf.default.disable_ipv6.*#net.ipv6.conf.default.disable_ipv6=1#g” /etc/sysctl.conf sed -i “s#^net.ipv6.conf.lo.disable_ipv6.*#net.ipv6.conf.lo.disable_ipv6=1#g” /etc/sysctl.conf sed -i “s#^net.ipv6.conf.all.forwarding.*#net.ipv6.conf.all.forwarding=1#g” /etc/sysctl.conf # 可能没有,追加 echo “net.ipv4.ip_forward = 1” >> /etc/sysctl.conf echo “net.bridge.bridge-nf-call-ip6tables = 1” >> /etc/sysctl.conf echo “net.bridge.bridge-nf-call-iptables = 1” >> /etc/sysctl.conf echo “net.ipv6.conf.all.disable_ipv6 = 1” >> /etc/sysctl.conf echo “net.ipv6.conf.default.disable_ipv6 = 1” >> /etc/sysctl.conf echo “net.ipv6.conf.lo.disable_ipv6 = 1” >> /etc/sysctl.conf echo “net.ipv6.conf.all.forwarding = 1” >> /etc/sysctl.conf |
对于宿主机相关的环境配置完成后,需要安装CRI,即Docker
sudo yum remove docker*
sudo yum install -y yum-utils sudo yum-config-manager –add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum install -y docker-ce-3:19.03.9-3.el7.x86_64 docker-ce-cli-3:19.03.9-3.el7.x86_64 containerd.io #启动服务 systemctl start docker systemctl enable docker #配置加速 sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-‘EOF’ { “registry-mirrors”: [“https://82m9ar63.mirror.aliyuncs.com”] } EOF sudo systemctl daemon-reload sudo systemctl restart docker |
需要注意的是,设置的registry-mirrors中需要有对应的kube-apiserver/proxy等镜像
安装kubelet,也就是所有镜像的执行者
#设置K8S的yum源
cat <<EOF > /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 EOF # 卸载旧版本 yum remove -y kubelet kubeadm kubectl # 查看可以安装的版本 yum list kubelet –showduplicates | sort -r # 安装kubelet、kubeadm、kubectl 指定版本 yum install -y kubelet-1.21.0 kubeadm-1.21.0 kubectl-1.21.0 # 开机启动kubelet systemctl enable kubelet && systemctl start kubelet |
之后利用kubeadm 进行相关的init
# 书写一个shell脚本
#!/bin/bash images=( kube-apiserver:v1.21.0 kube-proxy:v1.21.0 kube-controller-manager:v1.21.0 kube-scheduler:v1.21.0 coredns:v1.8.0 etcd:3.4.13-0 pause:3.4.1 ) for imageName in ${images[@]} ; do docker pull registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/$imageName done chmod +x images.sh && ./images.sh #对coredns进行打标签 docker tag registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/coredns:v1.8.0 registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/coredns/coredns:v1.8.0 #执行kubeadm init指令 kubeadm init \ –apiserver-advertise-address=10.170.11.8 \ –image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \ –kubernetes-version v1.21.0 \ –service-cidr=10.96.0.0/16 \ –pod-network-cidr=192.168.0.0/16 |
上面需要注意,对于cidr来说
无论是service-cidr还是pod-network-cidr,都需要注意和本级的子网范围不要有冲突
之后init完成后,可以将init后的日志打印看下
日志中其实提醒了我们
#给kubectl设置了权限
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config |
’
如果是admin用户,可以设置对应的环境变量
export KUBECONFIG=/etc/kubernetes/admin.conf
安装一个pod网络
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
这样就可以检查本机相关的Pod状态了
顺便将worker节点进行加入集群
kubeadm join 172.24.80.222:6443 –token nz9azl.9bl27pyr4exy2wz4 \
–discovery-token-ca-cert-hash sha256:4bdc81a83b80f6bdd30bb56225f9013006a45ed423f131ac256ffe16bae73a20 #即使失效了也可以再创建 kubeadm token create –print-join-command kubeadm token create –ttl 0 –print-join-command |
之后进行Pod的测试
Kubectl get pods
设置ipvs模式
kubectl edit cm kube-proxy -n kube-system\
将其中如下位置修改为
为了应用相关的mode
将Pod删掉重启即可
kubectl delete pod kube-proxy-pqgnt -n kube-system