虚拟机部署k8s集群
程序员文章站
2022-03-01 17:24:20
...
一、搭建虚拟机环境
环境要求
- 操作系统 CentOS 7.4
- 内存 2G 【至少】
- CPU 2核【至少】
- 硬盘 20G 【至少】
设置环境
- 配置yum源
- 关闭防火墙
- 关闭Swap
配置yum源(替换文件/etc/yum.repos.d/CentOS-Base.repo)
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
关闭防火墙
systemctl stop firewalld & systemctl disable firewalld
关闭Swap
执行swapoff -a可临时关闭,但系统重启后恢复
编辑/etc/fstab,注释掉包含swap的那一行即可,重启后可永久关闭
关闭SeLinux
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
安装Docker
- 添加仓库
yum -y install yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache
- 安装Docker
安装最新版Docker
yum install docker-ce -y
启动Docker服务并**开机启动
systemctl start docker & systemctl enable docker
二、安装Kubernetes
配置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
或者
官方(CentOs):
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
exclude=kube*
EOF
安装K8S组件
安装kubelet、kubeadm、kubectl:
yum install -y kubelet kubeadm kubectl
启动kubelet
systemctl enable kubelet && systemctl start kubelet
使用kubeadm工具初始化K8S集群(下载K8S的Docker镜像,需要*,否则需自己提前准备镜像)
三、创建集群
修改网桥设置
官方:
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
或者
vi /etc/sysctl.conf
追加:net.bridge.bridge-nf-call-iptables = 1
sudo sysctl -p
配置kubelet的cgroup drive
查看docker cgroup driver:
docker info | grep -i cgroup
查看kubenet cgroup
cat /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
若不一样,则执行:
sed -i "s/cgroup-driver=systemd/cgroup-driver=cgroupfs/g" /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
systemctl daemon-reload
创建集群
kubeadm init --pod-network-cidr=10.244.0.0/16 --kubernetes-version=v1.12.1 --apiserver-advertise-address=192.168.80.11(本机ip地址)
初始化成功后,注意执行界面上的提示信息
主节点上执行:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
网络方式:Canal(需要与kubeadm init 的–pod-network-cidr参数对应)
kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/canal/rbac.yaml
kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/canal/canal.yaml
从节点上执行:
kubeadm join...
验证集群是否成功
主节点上执行:
查看所有节点:kubectl get nodes
查看所有pod:kubectl get pods -n kube-system
四、搭建K8S Dashboard
- 创建dashbord
kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
- 使用NodePort方式暴露dashbord
kubectl edit -n kube-system svc kubernetes-dashboard
将type: ClusterIP 改为type: NodePort
- 访问
查看kubernetes-dashboard节点
kubectl get pods -n kube-system -o wide | grep kubernetes-dashboard
查看kubernetes-dashboard端口
kubectl get svc --all-namespaces | grep kubernetes-dashboard
访问:https://192.168.80.26:30665 (192.168.80.26为节点ip,30665为端口)
- 设置权限
wget https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
vi https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
将RoleBinding 改为 ClusterRoleBinding并且修改roleRef中的kind为ClusterRole,name修改为cluster-admin
重新部署dashboard: kubectl apply -f kubernetes-dashboard.yaml
- 查看pod信息
kubectl describe pods kubernetes-dashboard-767dc7d4d-dmpxx -n kube-system -o wide
- 查看日志
kubectl logs kubernetes-dashboard-767dc7d4d-dmpxx -n kube-system
- 开启代理访问:
kubectl proxy --address=192.168.80.26 --disable-filter=true
上一篇: CentOS7.8分区方案
下一篇: Centos7 minimal初始化