搭建K8s集群

Posted 辣酱9拌饭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建K8s集群相关的知识,希望对你有一定的参考价值。

资源准备

准备3台虚拟机

先安装docker

再安装k8s

安装版本

  • Docker 18.09.0
  • calico:v3.22.1
  • kubeadm-1.23.5
  • kubelet-1.23.5
  • kubectl-1.23.5
  • k8s.gcr.io/kube-apiserver:v1.23.5
  • k8s.gcr.io/kube-controller-manager:v1.23.5
  • k8s.gcr.io/kube-scheduler:v1.23.5
  • k8s.gcr.io/kube-proxy:v1.23.5
  • k8s.gcr.io/pause:3.6
  • k8s.gcr.io/etcd:3.5.1-0
  • k8s.gcr.io/coredns/coredns:v1.8.6

环境搭建

1.创建 Vagrantfile 文件

备注:10.13.11.21 修改成当前所属网段

boxes = [

:name => "master-kubeadm-k8s",
:eth1 => "10.13.11.21",
:mem => "2048",
:cpu => "2",
:sshport => 22230
,

:name => "worker01-kubeadm-k8s",
:eth1 => "10.13.11.22",
:mem => "2048",
:cpu => "2",
:sshport => 22231
,

:name => "worker02-kubeadm-k8s",
:eth1 => "10.13.11.23",
:mem => "2048",
:cpu => "2",
:sshport => 22232

]
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
boxes.each do |opts|
config.vm.define opts[:name] do |config|
config.vm.hostname = opts[:name]
config.vm.network :public_network, ip: opts[:eth1]
config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: "true"
config.vm.network "forwarded_port", guest: 22, host: opts[:sshport]
config.vm.provider "vmware_fusion" do |v|
v.vmx["memsize"] = opts[:mem]
v.vmx["numvcpus"] = opts[:cpu]
end
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", opts[:mem]]
v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
v.customize ["modifyvm", :id, "--name", opts[:name]]
end
end
end
end

2.启动,并进入到三台虚拟机中

#[启动虚拟机]
vagrant up
#[进入manager-node]
vagrant ssh master-kubeadm-k8s
#[进入worker01-node]
vagrant ssh worker01-kubeadm-k8s
#[进入worker02-node]
vagrant ssh worker02-kubeadm-k8s

3.修改成可以密码登陆root账户(3台机器同时操作)

#[进入root账户]
sudo -i
#[设置root账户可以密码登陆]
vi /etc/ssh/sshd_config
#修改 PasswordAuthentication yes
PasswordAuthentication yes
#[修改密码]
passwd
#[重启sshd服务]
systemctl restart sshd

4.更新并安装依赖(3台机器都要操作)

yum -y update
yum install -y conntrack ipvsadm ipset jq sysstat curl iptables libseccomp

安装Docker(3台机器都要操作)

#01 卸载之前安装的docker
sudo yum remove docker docker latest docker-latest-logrotate \\
docker-logrotate docker-engine docker-client docker-client-latest docker-common
#02 安装必要依赖
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
#03 添加软件源信息
sudo yum-config-manager \\
--add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#04 更新yum缓存
sudo yum makecache fast
#05 安装docker [指定安装docker版本]
udo yum install -y docker-ce-18.09.0 docker-ce-cli-18.09.0 containerd.io
#06 `启动docker并设置开机启动
sudo systemctl start docker && sudo systemctl enable docker
#07 测试docker安装是否成功
sudo docker run hello-world

修改hosts文件

1.设置master的hostname

sudo hostnamectl set-hostname m

2.设置worker01的hostname

sudo hostnamectl set-hostname w1

3.设置worker02的hostname

sudo hostnamectl set-hostname w

4.修改hosts文件(3台机器同时操作)

vi /etc/hosts

10.13.11.21 m
10.13.11.22 w1
10.13.11.23 w2

5.测试是否ping通

ping m
ping w1
ping w2

系统基础前提配置(3台机器都要操作)

#01 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
#02 关闭selinux
setenforce 0
sed -i s/^SELINUX=enforcing$/SELINUX=permissive/ /etc/selinux/config
#03 关闭swap
swapoff -a
sed -i /swap/s/^\\(.*\\)$/#\\1/g /etc/fstab
#04 配置iptables的ACCEPT规则
iptables -F && iptables -X && iptables \\
-F -t nat && iptables -X -t nat && iptables -P FORWARD ACCEPT
#05 设置系统参数
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system

安装 kubeadm, kubelet and kubectl(3台机器都要操作)

1.配置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

2.安装kubeadm&kubelet&kubectl

yum install -y kubeadm-1.23.5-0 kubelet-1.23.5-0 kubectl-1.23.5-0

3.docker和k8s设置同一个cgroup

#文件没内容的话,就新建;有的话,就加上这一句,注意文件的格式[逗号]
vi /etc/docker/daemon.json


"exec-opts": ["native.cgroupdriver=systemd"]


#【`重启docker,一定要执行`】
systemctl restart docker

#【`找不到内容没关系`】
sed -i "s/cgroup-driver=systemd/cgroup-driver=cgroupfs/g" /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

#【`重启kubelet,一定要执行`】
systemctl enable kubelet && systemctl start kubelet

proxy/pause/scheduler等国内镜像(3台机器都要操作)

1.查看kubeadm使用的镜像

kubeadm config images list
#############################得到结果######################
k8s.gcr.io/kube-apiserver:v1.23.5
k8s.gcr.io/kube-controller-manager:v1.23.5
k8s.gcr.io/kube-scheduler:v1.23.5
k8s.gcr.io/kube-proxy:v1.23.5
k8s.gcr.io/pause:3.6
k8s.gcr.io/etcd:3.5.1-0
k8s.gcr.io/coredns/coredns:v1.8.6
#############################得到结果######################

解决国外镜像不能访问的问题

#创建kubeadm.sh脚本,用于拉取镜像/打tag/删除原有镜像
vi kubeadm.sh

#!/bin/bash
set -e
KUBE_VERSION=v1.23.5
KUBE_PAUSE_VERSION=3.6
ETCD_VERSION=3.5.1-0
CORE_DNS_VERSION=v1.8.6
GCR_URL=k8s.gcr.io
ALIYUN_URL=registry.cn-hangzhou.aliyuncs.com/google_containers
images=(kube-proxy:$KUBE_VERSION
kube-scheduler:$KUBE_VERSION
kube-controller-manager:$KUBE_VERSION
kube-apiserver:$KUBE_VERSION
pause:$KUBE_PAUSE_VERSION
etcd:$ETCD_VERSION
coredns:$CORE_DNS_VERSION)
for imageName in $images[@] ; do
docker pull $ALIYUN_URL/$imageName
docker tag $ALIYUN_URL/$imageName $GCR_URL/$imageName
docker rmi $ALIYUN_URL/$imageName
done

#运行脚本和查看镜像
K8S之001: 概述集群搭建

K8S之001: 概述集群搭建

两地三中心,搭建一套k8s集群还是各区域搭建各自k8s集群?

minio笔记3--基于k8s搭建minio集群

Linux企业运维——K8s高可用集群架构搭建详解

Linux企业运维——K8s高可用集群架构搭建详解