我在github上準備好了相關文件的下載:https://github.com/anakin/k8s-install
最近在看有關k8s的一些知識,想手動搭建一套環境來體驗一下,本文詳細記錄一下搭建的過程。
環境
三臺Ubuntu系統的虛擬機,其中一臺作為master,另外兩臺作為worker節點
步驟1:安裝虛擬機
我的筆記本是一臺Macbook Pro,首選安裝一個虛擬機軟件Vmware Fusion,過程略。
1. 下載好Ubuntu鏡像,我選擇的是`19.04`版本。
2. 打開Fusion,選擇New,然後選擇“Install from disk or image”,continue
3. 設置好用戶名和密碼,中間還可以修改使用的硬盤空間等等,這個過程就不詳述了。
4. 安裝好之後,進入系統,找到“terminal”,安裝net-tools和ssh server
sudo apt install net-tools ssh
步驟2:安裝docker
1. 更新包列表:
sudo apt update
2. 讓apt支持https方式
sudo apt install apt-transport-https ca-certificates curl software-properties-common
3. 添加GPG密鑰
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
4. 添加docker源
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
5. 再次更新apt
sudo apt update
6. 確保從Docker repo安裝而不是默認的Ubuntu repo
apt-cache policy docker-ce
7. 開始安裝docker
sudo apt install docker-ce
8. 檢查一下是否已經在運行
sudo systemctl status docker
步驟3:系統設置
1. 關閉防火牆
ufw disable
2. 設置系統轉發參數
cat < /etc/sysctl.d/k8s.conf net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.conf.all.rp_filter = 0 EOF sysctl -p /etc/sysctl.d/k8s.conf
3. 安裝ntp服務,同步時鐘
sudo apt install ntp
步驟4:複製虛擬機
使用fusion的複製功能,複製出另外兩臺虛擬機
關掉剛裝好的虛擬機,選擇Fusion菜單Virtual Machine下面的Create Full Clone,Fusion就會clone出一個一模一樣的虛擬機出來,是不是很方便
步驟5:設置免登陸和hosts文件
1. 打開三臺虛擬機,進入terminal,執行
ifconfig
找到各自的ip地址
2. 打開Mac的terminal,輸入下面的命令生成ssh公鑰
ssh-keygen -t rsa
3. 將公鑰分別拷貝到三臺虛擬機上
scp .ssh/id_rsa.pub [email protected]:
user是虛擬機上的用戶名,xxx代表各自的ip地址
4. 在虛擬機上將公鑰追加到.ssh/authorized_keys文件,並修改權限
cat id_rsa.pub >> .ssh/authorized_keys chmod 600 authorized_keys
5. 分別修改每臺虛擬機上的hosts文件,用vim編輯器打開/etc/hosts,添加三臺虛擬機的hosts信息,下面是我自己的host信息:
192.168.32.131 server01 192.168.32.132 server02 192.168.32.133 server03
ok,到現在為止,基本的安裝環境應該是準備好了。