快速安装及搭建k8s集群
快速安装及搭建k8s集群1、准备工作服务器环境主机名操作系统IP地址masterCentos7.4192.168.32.100node1Centos7.4192.168.32.101node2Centos7.4192.168.32.102关闭防火墙systemctl disable firewalldsystemctl stop firewalld修改主机名(三台服务器都针对自己的角色进行此操作)
快速安装及搭建k8s集群
1、准备工作
服务器环境
主机名 | 操作系统 | IP地址 |
---|---|---|
master | Centos7.4 | 192.168.32.100 |
node1 | Centos7.4 | 192.168.32.101 |
node2 | Centos7.4 | 192.168.32.102 |
关闭防火墙和swap
systemctl disable firewalld
systemctl stop firewalld
setenforce 0
swapoff -a #实时动态关闭
sed -i '/ swap / s/^/#/' /etc/fstab #禁止重启后自动开启
配置内核参数
cat > /etc/sysctl.d/k8s.conf << EOF
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
vm.swappiness=0
EOF
sysctl --system
修改主机名(三台服务器都针对自己的角色进行此操作)
hostnamectl set-hostname master //192.168.32.100
hostnamectl set-hostname node1 //192.168.32.101
hostnamectl set-hostname node2 //192.168.32.102
修改/etc/hosts文件
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.32.100 master
192.168.32.101 node1
192.168.32.102 node2
时间校对
[root@master ~]# yum -y install ntp*
[root@master ~]# ntpdate ntp1.aliyun.com
[root@master ~]# hwclock
2、master安装kubernetes
配置docker镜像
[root@master ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
配置kubernetes仓库
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[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@master yum.repos.d]# wget https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
[root@master yum.repos.d]# rpm --import rpm-package-key.gpg
[root@master yum.repos.d]# yum repolist
拷贝到node1和node2
[root@master yum.repos.d]# scp CentOS-Base.repo docker-ce.repo kubernetes.repo node1:/etc/yum.repos.d/
安装docker和kubelet
[root@master ~]# yum -y install docker-ce kubelet kubeadm kubectl
修改环境变量
[root@master ~]# vim /usr/lib/systemd/system/docker.service
添加2行:
Environment=“HTTPS_PROXY=http://www.ik8s.io:10080”
Environment=“NO_PROXY=127.0.0.0/8,172.0.0.0/16”
[root@master ~]# systemctl daemon-reload
启动docker并设置开机启动
[root@master ~]# systemctl start docker
[root@master ~]# systemctl enable docker
修改docker的cgroup驱动
vi /etc/docker/daemon.json
写入以下内容
{
"exec-opts":["native.cgroupdriver=systemd"]
}
重启docker
systemctl restart docker
systemctl status docker
查看输出是否是1
[root@master ~]# cat /proc/sys/net/bridge/bridge-nf-call-ip6tables
[root@master ~]# cat /proc/sys/net/bridge/bridge-nf-call-iptables
[root@master ~]# rpm -ql kubelet
/etc/kubernetes/manifests ----清单目录
/etc/sysconfig/kubelet ----配置文件
/etc/systemd/system/kubelet.service
/usr/bin/kubelet ----主程序
设置开机启动
[root@master ~]# systemctl enable kubelet
kubeadm初始化准备
在使用kubeadm init命令初始化时报错,pull镜像失败,因为镜像在国外,所以需要我们可以下载国内的镜像,然后再修改tag。
kubeadm config images list 命令查看需要哪些镜像**
下载镜像(其他的镜像修改后面的名字即可)
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.19.4
修改tag
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.18.3 k8s.gcr.io/kube-proxy:v1.19.4
[root@master ~]# vim /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="–fail-swap-on=false"
初始化
[root@master ~]# kubeadm init --kubernetes-version=v1.19.4 --pod-network-cidr=10.10.0.0/16 --service-cidr=10.20.0.0/16 --ignore-preflight-errors=Swap
保存好此命令(加集群)
kubeadm join 192.168.32.100:6443 --token m8zgnk.1qeouhrtgdc5l35n
–discovery-token-ca-cert-hash sha256:d5bb2b9b84e9b8a84cb826cb5cc806bb645eb415596ddaa3b7cde1f56ecabff0
netstat -ntlp 查看监听端口
[root@master ~]# mkdir -p $HOME/.kube
[root@master ~]# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@master ~]# sudo chown $(id -u): $(id -g) $HOME/.kube/config
查看组件是否健康
[root@master ~]# kubectl get cs
如果kubectl get cs时显示controller-manager和scheduler状态unhealthy,则修改kube-controller-manager.yaml和kube-scheduler.yaml,将port=0这一行注释掉,然后重启kubelet
systemctl restart kubelet
查看节点
[root@master ~]# kubectl get nodes
安装weave
[root@master ~]# kubectl apply -f “https://cloud.weave.works/k8s/net?k8s-version=1.19.4”
查看节点信息
[root@master ~]# kubectl get nodes
查看pod运行情况
[root@master ~]# kubectl get pods -n kube-system
复制配置文件到node1、node2
[root@master ~]# scp /usr/lib/systemd/system/docker.service node1:/usr/lib/systemd/system/docker.service
[root@master ~]# scp /etc/sysconfig/kubelet node1:/etc/sysconfig/
[root@master ~]# scp /usr/lib/systemd/system/docker.service node2:/usr/lib/systemd/system/docker.service
[root@master ~]# scp /etc/sysconfig/kubelet node2:/etc/sysconfig/
3、node1、node2部署
[root@node1 ~]# wget https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
[root@node1 ~]# rpm --import rpm-package-key.gpg
[root@node1 ~]# yum -y install docker-ce kubelet kubeadm
[root@node1 ~]# systemctl start docker
[root@node1 ~]# systemctl enable docker kubelet
[root@node1 ~]# docker info
添加集群
[root@node1 ~]# kubeadm join 192.168.32.100:6443 --token m8zgnk.1qeouhrtgdc5l35n
–discovery-token-ca-cert-hash sha256:d5bb2b9b84e9b8a84cb826cb5cc806bb645eb415596ddaa3b7cde1f56ecabff0
查看节点
[root@master ~]# kubectl get nodes
查看pod
[root@master ~]# kubectl get pods -n kube-system -o wide
更多推荐
所有评论(0)