Installation requirements, prepare the environment
#系统centos7.6
#硬件:2个CPU,2G内存
#可以访问外网,禁用swap
#提前安装docker环境
Prepare three servers
Server ip | name of server |
---|---|
192.168.106.102 | k8s-master |
192.168.106.103 | k8s-node01 |
192.168.106.104 | k8s-node02 |
1. Initialize the system environment, execute the following commands on all three servers
1.1 Turn off the firewall
systemctl stop firewalld
systemctl disable firewalld
1.2 Close selinux and close swap
sed -i 's/enforcing/disabled' /etc/selinux/config
setenforce 0
swapoff -a #临时关闭,永久关闭需要写入fatab
1.3 Modify the host name and write it into the host of the three servers
hostnamectl set-hostname K8S-master 102
hostnamectl set-hostname K8S-node01 103
hostnamectl set-hostname K8S-mast02 104
cat >> /etc/hosts << EOF
192.168.106.102 K8S-master
192.168.106.103 K8S-node01
192.168.106.104 K8S-mast02
1.4 Pass the bridged IPV4 traffic to the iptables chain:
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system #配置生效
1.5 Synchronize time
ntpdate time.windows.com
#如果时区不对执行下面命令,然后在同步
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
1.6 Configure Alibaba source and download the corresponding software package
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
#kubectl-1.18.0命令行管理工具,kubeadm-1.18.0是引导K8S集群,kubelet-1.18.0管理容器
systemctl enable kubelet
2.0 Deploy kubernetes master node (executed on the master node)
kubeadm init \
--apiserver-advertise-address=192.168.106.102 \ #指定master监听的地址
--image-repository registry.aliyuncs.com/google_containers \ #指定下载源
--kubernetes-version v1.18.0 \ #指定kubernetes版本
--service-cidr=10.96.0.0/12 #设置集群内部的网络
--pod-network-cidr=10.244.0.0/16 #设置pod的网络
The following commands are the commands to join the master and need to be copied
kubeadm join 192.168.106.102:6443 --token 7w0oxu.drdkjuzirow3dvj7 \
--discovery-token-ca-cert-hash sha256:e30452be8217affa2f11229e45cb2ed9dfa4424c82d5f354d9813ce789f58fdb`
```bash
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get node #查看版本
kubectl get pods -n kube-system #查看所有组件
2.1 Install Pod Network Plugin
wget http://120.78.77.38/file/kube-flannel.yaml #下载镜像,国外源,我已经下载到本地
kubectl apply -f kube-flannel.yaml #原始镜像有问题改成如下图所示的镜像名称
Start the mirror, after starting to view the mirror, a flannel mirror will be added. Support multi-host container network communication
docker pull lizhenliang/flannel:v0.11.0-amd64 #建议先下载镜像
kubectl apply -f kube-flannel.yaml
kubectl get pods -n kube-system
As shown in the figure, K8Spod starts normally
If there is a problem with the previous environment configuration, execute the following command to clean up the K8S environment
kubectl log kube-flannel-ds-sjs4p -n kube-system -f 启动有问题查看日志详细信息`
`kubectl delete -f kube-flannel.yaml #删除pod`
```bash
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
3. The k8s-node node joins the master node (two hosts are executed separately)
kubeadm join 192.168.106.102:6443 --token 7w0oxu.drdkjuzirow3dvj7 \
--discovery-token-ca-cert-hash sha256:e30452be8217affa2f11229e45cb2ed9dfa4424c82d5f354d9813ce789f58fdb
kubectl get nodes #master节点运行
So far, the K8S cluster deployment is complete
4. Master node installation management page dashboard
wget http://120.78.77.38/file/kubernetes-dashboard.yaml
kubectl apply -f kubernetes-dashboard.yaml
kubectl get pods -n kubernetes-dashboard
#下图位置需要修改
kubectl get pod -n kubernetes-dashboard -o wide #查看pod在哪个节点
https://192.168.106.104:30001 #需要用火狐浏览器打开访问
#创建token
kubectl create serviceaccount dashboard-admin -n kube-system
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret |awk '/dashboard-admin/{pirnt $1}')
After the creation is complete, copy the token to the page token and enter the web management page, you can see the three nodes on the page as shown in the figure
https://192.168.106.104:30001/#/node?namespace=default