赞
踩
Kubernetes,也被称为K8s,是一个用于自动化部署、扩展和管理容器化应用程序的开源系统,是生产级别的容器编排系统.
环境介绍:3台全新的centos7.9系统、cpu2核、内存4G、磁盘20G
控制节点 k8s-master 192.168.145.2213
工作节点 k8s-node1 192.168.145.222
工作节点 k8s-node2 192.168.145.223
(注意:以下操作三台服务器均要完成,可使用发送键输入到所有会话功能,然后在master一端输入即可,工作节点会同步进行)
[root@k8s-master ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@k8s-master ~]# service firewalld stop
Redirecting to /bin/systemctl stop firewalld.service
[root@k8s-master ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
##重启
[root@k8s-master ~]# reboot
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.145.221 k8s-master
192.168.145.222 k8s-node1
192.168.145.223 k8s-node2
k8s-master:
[root@k8s-master ~]# ssh-keygen #一路回车,不输入密码
#把本地生成的密钥文件和私钥文件拷贝到远程主机
[root@k8s-master ~]# ssh-copy-id k8s-master
[root@k8s-master ~]# ssh-copy-id k8s-node1
[root@k8s-master ~]# ssh-copy-id k8s-node2
k8s-node1:
[root@k8s-node1 ~]# ssh-keygen
[root@k8s-node1 ~]# ssh-copy-id k8s-node1
[root@k8s-node1 ~]# ssh-copy-id k8s-node2
[root@k8s-node1 ~]# ssh-copy-id k8s-master
k8s-node2:
[root@k8s-node2 ~]# ssh-keygen
[root@k8s-node2 ~]# ssh-copy-id k8s-node2
[root@k8s-node2 ~]# ssh-copy-id k8s-node1
[root@k8s-node2 ~]# ssh-copy-id k8s-master
临时关闭:swapoff -a
永久关闭:注释swap挂载 vim /etc/fstab
为什么要注释swap分区?
因为机器内存不够的情况下,机器会使用swap交换分区,但swap分区的性能较低,k8s设计的时候为了提升性能,是默认不允许使用交换分区的。如果在k8s初始化失败,可能是因为swap分区没有关闭。
[root@k8s-master ~]# modprobe br_netfilter`
[root@k8s-master ~]# echo "modprobe br_netfilter" >> /etc/profile
[root@k8s-master ~]# cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
[root@k8s-master ~]# sysctl -p /etc/sysctl.d/k8s.conf
sysctl是在运行时配置内核参数
-p 从指定的文件加载系统参数,如不指定即从/etc/sysctl.conf中加载
[root@k8s-master ~]# yum install -y yum-utils
[root@k8s-master ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@k8s-master ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat ipvsadm conntrack ntpdate telnet ipvsadm
[root@k8s-master ~]# vim /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
#安装ntpdate命令
[root@k8s-master ~]# yum install ntpdate -y
#跟网络时间做同步
[root@k8s-master ~]# ntpdate cn.pool.ntp.org
#把时间同步做成计划任务
[root@k8s-master ~]# crontab -e
* */1 * * * /usr/sbin/ntpdate cn.pool.ntp.org
#重启crond服务
[root@k8s-master ~]#service crond restart
[root@k8s-master ~]# yum install docker-ce-20.10.6 -y
[root@k8s-master ~]# systemctl start docker && systemctl enable docker.service
[root@k8s-master ~]# vim /etc/docker/daemon.json
{
"registry-mirrors":["https://rsbud4vc.mirror.aliyuncs.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://hub-mirror.c.163.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
#修改docker文件驱动为systemd,默认为cgroupfs,kubelet默认使用systemd,两者必须一致才可以
[root@k8s-master ~]# systemctl daemon-reload && systemctl restart docker
[root@k8s-master ~]# systemctl status docker
[root@k8s-master ~]# yum install -y kubelet-1.20.6 kubeadm-1.20.6 kubectl-1.20.6
[root@k8s-master ~]# systemctl enable kubelet
每个软件包的作用:
#把初始化k8s集群需要的离线镜像包上传到k8s-master、node1、node2机器上(可以用scp给node节点传过去,提升速率)
[root@k8s-master ~]# docker load -i k8simage-1-20-6.tar.gz
[root@k8s-master ~]# scp k8simage-1-20-6.tar.gz k8s-node1:/root
[root@k8s-master ~]# scp k8simage-1-20-6.tar.gz k8s-node2:/root
使用kubeadm初始化k8s集群
[root@k8s-master~]# kubeadm config print init-defaults > kubeadm.yaml
kubeadm.yaml配置如下:
apiVersion: kubeadm.k8s.io/v1beta2 bootstrapTokens: - groups: - system:bootstrappers:kubeadm:default-node-token token: abcdef.0123456789abcdef ttl: 24h0m0s usages: - signing - authentication kind: InitConfiguration localAPIEndpoint: advertiseAddress: 192.168.145.221 #控制节点(master)的ip bindPort: 6443 nodeRegistration: criSocket: /var/run/dockershim.sock name: xianchaomaster1 #控制节点主机名 taints: - effect: NoSchedule key: node-role.kubernetes.io/master --- apiServer: timeoutForControlPlane: 4m0s apiVersion: kubeadm.k8s.io/v1beta2 certificatesDir: /etc/kubernetes/pki clusterName: kubernetes controllerManager: {} dns: type: CoreDNS etcd: local: dataDir: /var/lib/etcd imageRepository: registry.aliyuncs.com/google_containers kind: ClusterConfiguration kubernetesVersion: v1.20.6 networking: dnsDomain: cluster.local serviceSubnet: 10.96.0.0/12 podSubnet: 10.244.0.0/16 #指定pod网段, 需要新增加这个 scheduler: {} #追加如下几行 --- apiVersion: kubeproxy.config.k8s.io/v1alpha1 kind: KubeProxyConfiguration mode: ipvs --- apiVersion: kubelet.config.k8s.io/v1beta1 kind: KubeletConfiguration cgroupDriver: systemd
[root@k8s-master~]# kubeadm init --config=kubeadm.yaml --ignore-preflight-errors=SystemVerification
显示如下的结果,则说明安装完成:
相当于对kubectl进行授权,这样kubectl命令可以使用这个证书对k8s集群进行管理
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
[root@k8s-master ~]# kubeadm join 192.168.145.221:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:c7ccdd9cfd97afcf738bc47601d519c20d84a422738bc47601d519c20
master查看集群节点状况
[root@k8s-master ~]# kubectl get nodes
(下面状态都是NotReady状态,说明没有安装网络插件)
可以看到node1、node2的ROLES角色为空,就表示这个节点是工作节点。
#可以把node1和node2的ROLES变成work,按照如下方法:
[root@k8s-master ~]# kubectl label node k8s-node1 node-role.kubernetes.io/worker=worker
注:在线下载配置文件地址是: https://docs.projectcalico.org/manifests/calico.yaml
[root@k8s-master ~]# kubectl apply -f calico.yaml
再次查看集群状态,STATUS状态是Ready,说明k8s集群正常运行了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。