1、软件准备
1.1、Ubuntu系统安装
https://ubuntu.com/download#download
ubuntu系统需要设置用户,root默认为系统的账户不能被用户设置且每一次开机都是随机密码。所以要设置自定义用户并且设置拥有root权限
自定义用户设置拥有root权限有三种方法:
1、编辑/etc/passwd
sudo vim /etc/passwd
##找到用户test test:x:1000:1000::/home/test ##修改权限 test:x:0:0::/home/test
2、临时使用root用户
##修改密码
passwd
##登陆root
su root
3、编辑/etc/sudoers
vi /etc/sudoers ##新建用户授权root # User privilege specification root ALL=(ALL:ALL) ALL test ALL=(ALL:ALL) ALL 保存之后执行命令 su -
1.2、虚拟机安装
https://my.vmware.com/web/vmware/info/slug/desktop_end_user_computing/vmware_workstation_pro/14_0
2、设置系统
2.1、关闭防火墙
sudo ufw disable
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
2.2、关闭swap
##临时关闭 sudo swapoff -a ##永久关闭 vi /etc/fstab UUID=ecdeee3f-5b0e-44f7-af33-99e283247b70 / ext4 defaults 0 0 ##/swap.img none swap sw 0 0
##修改master的vi /etc/hosts 127.0.0.1 localhost 127.0.1.1 k8s-master 192.168.65.137 k8s-master 192.168.65.138 k8s-node-1 # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ##修改master的vi /etc/hostname k8s-master ##修改node-1的的vi /etc/hosts 127.0.0.1 localhost 127.0.1.1 k8s-node-1 192.168.65.137 k8s-master 192.168.65.138 k8s-node-1 # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ##修改node-1的vi /etc/hostname k8s-node-1
3、安装docker
sudo apt install docker.io
sudo systemctl enable docker
3.1 、修改docker的Cgroup Driver
cat << EOF > /etc/docker/daemon.json { "exec-opts": ["native.cgroupdriver=systemd"] } EOF
##默认
Cgroup Driver: cgroupfs
##修改后
Cgroup Driver: systemd
##查询docker信息
docker info
4、安装k8s
apt-get update && apt-get install -y apt-transport-https
##官网给的地址由于网络原因替换成aliyun地址 curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main EOF
apt-get update
apt-get install -y kubelet kubeadm kubectl
4.1、如果是主节点就执行初始化,如果是node 节点可以先不执行初始化
sudo kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.15.3 --pod-network-cidr=192.168.0.0/16 --apiserver-advertise-address=192.168.65.137
mkdir -p HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
4.2、此时的k8s基本安装了,但是kubectl get nodes时status显示NoReady,安装flannel
##任意一个执行 kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/bc79dd1505b0c8681ece4de4c0d86c5cd2643275/Documentation/kube-flannel.yml
4.3、重启一下
systemctl daemon-reload
systemctl restart kubelet
5、集群节点
重复第4步骤,安装一个子节点。
5.1、让node-1机器作为子节点
kubectl taint nodes --all node-role.kubernetes.io/master-
5.2、在master机器获取token,discovery-token-ca-cert-hash
kubeadm token create --print-join-command
这个token是init初始化生成的,有时效性。可以手动生成
生成token:
##生成
kubeadm token create
##查询
kubeadm token list
生成discovery-token-ca-cert-hash:
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
5.3、在node-1节点执行
kubeadm join 192.168.65.137:6443 --token 96ux2r.itzmtbmlfk7yegr1 --discovery-token-ca-cert-hash sha256:92ee062053b3516fbfcaa9ce3bf3380b454f0dfddbf769858700789a3e9a31c3
5.4、查询集群节点
kubectl get nodes
6、卸载k8s
kubeadm reset -f modprobe -r ipip lsmod rm -rf ~/.kube/ rm -rf /etc/kubernetes/ rm -rf /etc/systemd/system/kubelet.service.d rm -rf /etc/systemd/system/kubelet.service rm -rf /usr/bin/kube* rm -rf /etc/cni rm -rf /opt/cni rm -rf /var/lib/etcd rm -rf /var/etcd