1.2、安装k8s-node1 和 k8s-node2节点虚拟机

        k8s-master节点的虚拟机环境弄好之后,这小节继续介绍k8s-node1 和 k8s-node2节点虚拟机环境安装。

节点主机名ip
主节点k8s-master172.31.0.10
节点1k8s-node1172.31.0.11
节点2k8s-node2172.31.0.12
  •  在D:\vagrant目录下新建centos_stream_9_node1文件夹,然后在文件夹中新建Vagrantfile文件。

centos_stream_9_node1节点的Vagrantfile文件内容如下:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "eurolinux-vagrant/centos-stream-9"
  config.vm.box_version = "9.0.45"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # 指定虚拟机网络ip为:172.31.0.11
  config.vm.network "private_network", ip: "172.31.0.11"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network", bridge: "Intel(R) Wi-Fi 6 AX200 160MHz"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Disable the default share of the current code directory. Doing this
  # provides improved isolation between the vagrant box and your host
  # by making sure your Vagrantfile isn't accessible to the vagrant box.
  # If you use this you may want to enable additional shared subfolders as
  # shown above.
  # config.vm.synced_folder ".", "/vagrant", disabled: true

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  config.vm.provision "shell", inline: <<-SHELL
  # 1、Docker安装
  # 1.1、卸载旧版本docker
  sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
  # 1.2、设置存储库
  sudo yum install -y yum-utils
  sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  # 1.3、安装 Docker Engine
  sudo yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  sudo systemctl enable docker
  sudo systemctl start docker
  # 1.4、禁用防火墙
  sudo systemctl stop firewalld
  sudo systemctl disable firewalld
  # 1.5、修改 SSH 配置
  sudo sed -i 's/^#*PasswordAuthentication .*/PasswordAuthentication yes/' /etc/ssh/sshd_config
  sudo sed -i 's/^#*PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config
  # 1.6、重启 SSH 服务
  sudo systemctl restart sshd.service
  # 1.7、修改 root 用户密码
  echo "root:1TdhblkFcdhx2a" | sudo chpasswd
  # 1.8、配置 Docker 镜像加速
  mkdir -p /etc/docker
  cat > /etc/docker/daemon.json <<EOF
  {
    "registry-mirrors": ["https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn"],
    "exec-opts": ["native.cgroupdriver=systemd"]
  }
EOF
  # 1.9、配置HTTP/HTTPS 网络代理 使用Docker的过程中,因为网络原因,通常需要使用 HTTP/HTTPS 代理来加速镜像拉取、构建和使用。
  # 为 dockerd 设置网络代理 "docker pull" 命令是由 dockerd 守护进程执行。而 dockerd 守护进程是由 systemd 管理。因此,如果需要在执行 "docker pull" 命令时使用 HTTP/HTTPS 代理,需要通过 systemd 配置。
  # 1.9.1、为 dockerd 创建配置文件夹。(mkdir -p /etc/systemd/system/docker.service.d)
  # 1.9.2、为 dockerd 创建 HTTP/HTTPS 网络代理的配置文件,文件路径是 /etc/systemd/system/docker.service.d/http-proxy.conf 。并在该文件中添加相关环境变量。
  sudo sh -c 'mkdir -p /etc/systemd/system/docker.service.d && \
    cat <<EOF > /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://172.31.0.1:7890/"
Environment="HTTPS_PROXY=http://172.31.0.1:7890/"
Environment="NO_PROXY=localhost,127.0.0.1,.example.com"
EOF'
  sudo systemctl daemon-reload
  sudo systemctl restart docker

  # 2、配置非root用户(Docker)执行docker命令时不使用sudo。
  # 2.1、创建名为 "Docker" 的用户
  sudo useradd Docker
  # 2.2、设置 "Docker" 用户的密码
  echo "Docker:1TdhblkFcdhx2a" | sudo chpasswd
  # 2.3、创建名为 "docker" 的组
  sudo groupadd docker
  # 2.4、将用户 "Docker" 添加到组 "docker"
  sudo gpasswd -a Docker docker
  # 2.5、重启docker
  sudo systemctl restart docker

  SHELL
end
  • 在D:\vagrant\centos_stream_9_node1目录这里点击一下,然后输入"cmd",在弹出的cmd命令框中输入:"vagrant up"命令,进行虚拟机创建。

  • 在D:\vagrant目录下新建centos_stream_9_node2文件夹,然后在文件夹中新建Vagrantfile文件。

centos_stream_9_node2节点的Vagrantfile文件内容如下:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "eurolinux-vagrant/centos-stream-9"
  config.vm.box_version = "9.0.45"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # 指定虚拟机网络ip为:172.31.0.12
  config.vm.network "private_network", ip: "172.31.0.12"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network", bridge: "Intel(R) Wi-Fi 6 AX200 160MHz"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Disable the default share of the current code directory. Doing this
  # provides improved isolation between the vagrant box and your host
  # by making sure your Vagrantfile isn't accessible to the vagrant box.
  # If you use this you may want to enable additional shared subfolders as
  # shown above.
  # config.vm.synced_folder ".", "/vagrant", disabled: true

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  config.vm.provision "shell", inline: <<-SHELL
  # 1、Docker安装
  # 1.1、卸载旧版本docker
  sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
  # 1.2、设置存储库
  sudo yum install -y yum-utils
  sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  # 1.3、安装 Docker Engine
  sudo yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  sudo systemctl enable docker
  sudo systemctl start docker
  # 1.4、禁用防火墙
  sudo systemctl stop firewalld
  sudo systemctl disable firewalld
  # 1.5、修改 SSH 配置
  sudo sed -i 's/^#*PasswordAuthentication .*/PasswordAuthentication yes/' /etc/ssh/sshd_config
  sudo sed -i 's/^#*PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config
  # 1.6、重启 SSH 服务
  sudo systemctl restart sshd.service
  # 1.7、修改 root 用户密码
  echo "root:1TdhblkFcdhx2a" | sudo chpasswd
  # 1.8、配置 Docker 镜像加速
  mkdir -p /etc/docker
  cat > /etc/docker/daemon.json <<EOF
  {
    "registry-mirrors": ["https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn"],
    "exec-opts": ["native.cgroupdriver=systemd"]
  }
EOF
  # 1.9、配置HTTP/HTTPS 网络代理 使用Docker的过程中,因为网络原因,通常需要使用 HTTP/HTTPS 代理来加速镜像拉取、构建和使用。
  # 为 dockerd 设置网络代理 "docker pull" 命令是由 dockerd 守护进程执行。而 dockerd 守护进程是由 systemd 管理。因此,如果需要在执行 "docker pull" 命令时使用 HTTP/HTTPS 代理,需要通过 systemd 配置。
  # 1.9.1、为 dockerd 创建配置文件夹。(mkdir -p /etc/systemd/system/docker.service.d)
  # 1.9.2、为 dockerd 创建 HTTP/HTTPS 网络代理的配置文件,文件路径是 /etc/systemd/system/docker.service.d/http-proxy.conf 。并在该文件中添加相关环境变量。
  sudo sh -c 'mkdir -p /etc/systemd/system/docker.service.d && \
    cat <<EOF > /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://172.31.0.1:7890/"
Environment="HTTPS_PROXY=http://172.31.0.1:7890/"
Environment="NO_PROXY=localhost,127.0.0.1,.example.com"
EOF'
  sudo systemctl daemon-reload
  sudo systemctl restart docker

  # 2、配置非root用户(Docker)执行docker命令时不使用sudo。
  # 2.1、创建名为 "Docker" 的用户
  sudo useradd Docker
  # 2.2、设置 "Docker" 用户的密码
  echo "Docker:1TdhblkFcdhx2a" | sudo chpasswd
  # 2.3、创建名为 "docker" 的组
  sudo groupadd docker
  # 2.4、将用户 "Docker" 添加到组 "docker"
  sudo gpasswd -a Docker docker
  # 2.5、重启docker
  sudo systemctl restart docker

  SHELL
end

  • 在D:\vagrant\centos_stream_9_node2目录这里点击一下,然后输入"cmd",在弹出的cmd命令框中输入:"vagrant up"命令,进行虚拟机创建。

  • 等待两个虚拟机创建完成

现在我们就创建好k8s-node1节点(172.31.0.11)和k8s-node2节点(172.31.0.12)的虚拟机了,它们两个虚拟机root账号的密码都是:1TdhblkFcdhx2a

  • 使用ssh工具MobaXterm连接k8s-node1节点(172.31.0.11)和k8s-node2节点(172.31.0.12)

现在,我们就配置好k8s-node1和k8s-node2节点的虚拟机,并在两台虚拟机中安装好Docker。

接下来,我们要在k8s-master上面 安装 kubeadm,然后用 kubeadm 安装k8s集群。

请看下一篇文章:

1.3、k8s-master上面安装 kubeadm-CSDN博客

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lvdapiaoliang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值