01 先決條件
檢測系統(tǒng)環(huán)境是否支持虛擬化
# 安裝檢查工具
apt install cpu-checker
# 使用命令檢查是否支持kvm
kvm-ok
# 出現(xiàn)如下信息則表示支持kvm虛擬化技術(shù)
INFO: /dev/kvm exists
KVM acceleration can be used
如有提示,請根據(jù)提示開啟相應(yīng)的虛擬化功能。
02 安裝軟件
# 安裝必要的kvm軟件
apt install qemu-kvm libvirt-clients libvirt-daemon-system -y
# 啟動并設(shè)置開機(jī)自啟
systemctl restart libvirtd && systemctl enable libvirtd
03 安裝一臺虛擬機(jī)
virt-install --virt-type kvm \
--os-type=linux --os-variant rhel7 \
--name k8s-base --memory 4096 --vcpus 2 \
--disk /data/vir-disk/k8s-base.qcow2,format=qcow2,size=100 \
--cdrom /data/images/CentOS-7-x86_64-DVD-1810.iso \
--network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --debug
解釋:安裝一個(gè)2核4G的centos系統(tǒng),這里需要提前下好需要的鏡像。
Centos7.6下載地址: https://mirrors.tuna.tsinghua.edu.cn/centos-vault/7.6.1810/isos/x86_64/CentOS-7-x86_64-DVD-1810.iso
安裝系統(tǒng)
重要說明:安裝系統(tǒng)需要使用vnc viewer連接宿主機(jī)IP地址+5900端口進(jìn)行安裝,具體如下圖

vnc連接虛擬機(jī)安裝系統(tǒng)

vnc連接成功圖

更改網(wǎng)卡名稱
安裝時(shí)需要將網(wǎng)卡名稱進(jìn)行更改,在安裝界面按tab鍵,在quiet前面輸入
net.ifnames=0 bisodevname=0然后回車,其他步驟略過。
優(yōu)化系統(tǒng)
開啟終端控制
系統(tǒng)安裝完成后使用vnc連接后,在命令行輸入如下命令開啟虛擬機(jī)終端控制功能:
grubby --update-kernel=ALL --args="console=ttyS0,115200n8"
此時(shí)需要重啟系統(tǒng),方可生效,具體命令如下:
# 重啟虛擬機(jī)
virsh reboot k8s-base
# 使用過終端進(jìn)行連接,連接上后敲擊回車則會出現(xiàn)登錄提示,輸入賬戶密碼即可
virsh console k8s-base

終端連接提示
下載源優(yōu)化
# 優(yōu)化基礎(chǔ)下載源
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# 安裝epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# 安裝docker源
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/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=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
優(yōu)化系統(tǒng)參數(shù)
# 關(guān)閉swap
swapoff -a # 臨時(shí)
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
# 修改內(nèi)核參數(shù)并加載生效
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system # 生效
# 關(guān)閉防火墻
systemctl stop firewalld
systemctl disable firewalld
# 關(guān)閉Selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
setenforce 0 # 臨時(shí)
# 優(yōu)化環(huán)境變量PS1
echo 'export PS1="\[\e[32;1m\][\u@\h \W]#>\[\e[0m\]"' >> ~/.bashrc
source ~/.bashrc
下載軟件
# 時(shí)間同步
yum install ntpdate -y
ntpdate time.windows.com
# 安裝docker
yum -y install docker-ce
# 啟動docker并設(shè)置開機(jī)啟動
systemctl enable docker && systemctl start docker
# 優(yōu)化docker下載鏡像加速器
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
systemctl restart docker
docker info
# 下載kubelet、kubeadm、kubectl
yum install -y kubelet-1.21.0 kubeadm-1.21.0 kubectl-1.21.0
# 設(shè)置kubelet開機(jī)自啟
systemctl enable kubelet
kvm基礎(chǔ)常用命令
virsh list # 默認(rèn)只顯示開啟狀態(tài)下的虛擬機(jī)
virsh list --all # 顯示所有虛擬機(jī)
virsh start VMname # 啟動
virsh shutdown VMname # 關(guān)機(jī)
virsh reboot VMname # 重啟
virsh destroy VMname # 強(qiáng)制關(guān)機(jī),相當(dāng)于拔電源
virsh dumpxml VMname >/tmp/VM_name.xml # 備份某個(gè)虛擬機(jī)的配置文件
virsh undefine VMname # 刪除虛擬機(jī)
virsh defined /dir/vm_backup.xml # 恢復(fù)備份過的虛擬機(jī)
virsh edit VMnamevi # 修改虛擬機(jī)配置文件
virsh domrename old_VMname new_VMname # 重命名虛擬機(jī)名稱,需要關(guān)機(jī)狀態(tài)
virsh suspend VMname # 掛起虛擬機(jī)
virsh vncdisplay VMname # 顯示虛擬機(jī)短端口,vnc可以使用短端口號連接虛擬機(jī)
virsh autostart VMname # 設(shè)置虛擬機(jī)開機(jī)自啟
virsh autostart --disable VMname # 取消虛擬機(jī)開機(jī)自啟
ls /etc/libvirt/qemu/autostart # 查看開機(jī)自啟的虛擬機(jī)