前提:
1、操作系统Centos 8
2、通过阿里云相关镜像源安装
以下均为root用户下操作
操作步骤:
1、修改主机名
hostnamectl set-hostname hanyu
2、关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
3、安装docker
安装必要的工具
yum install -y yum-utils device-mapper-persistent-data lvm2
配置官方docker源或者aliyun docker源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
或者
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
查询docker版本(否则yum install默认安装最新的版本)
yum list docker-ce --showduplicates | sort -r
安装指定版本docker
yum -y install docker-ce-18.09.3-3.el7
解决方法:
wget https://download.docker.com/linux/centos/7/x86_64/edge/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
yum install -y containerd.io-1.2.6-3.3.el7.x86_64.rpm
启动docker
systemctl enable docker && systemctl start docker
查询docker服务状态
systemctl status docker
查看docker版本
docker version
4、安装kubelet、kubeadm 和 kubectl
配置kubernetes.repo的源,由于官方源国内无法访问,这里使用阿里云yum源
[root@hanyu1 ~]# 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
在所有节点上安装指定版本 kubelet、kubeadm 和 kubectl
yum list kubectl --showduplicates | sort -r
yum list kubeadm --showduplicates | sort -r
yum list kubelet --showduplicates | sort -r
yum install -y kubectl-1.18.2.-0 kubeadm-1.18.2-0 kubelet-1.18.2-0
启动kubelet服务
systemctl enable kubelet && systemctl start kubelet
5、关闭swap分区
swapoff -a
6、初始化master节点
kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.18.2 --pod-network-cidr=10.244.0.0/16 --token-ttl 0 --ignore-preflight-errors=Swap
按照kubeadm init成功后打印提示,继续操作:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown (id -g) $HOME/.kube/config
kubectl get nodes查询到节点处于NotReady状态,是因为网络插件还未就位,也就是这里要求运行的
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
如果要暂时忽略让节点Ready,将如下文件的--network-plugin=cni字段去掉, (该文件是在kubeadm init或者kubeadm join过程中生成的), 修改完后重启kubelet即可(systemctl restart kubelet)
[root@hanyu ~]# cat /var/lib/kubelet/kubeadm-flags.env
KUBELET_KUBEADM_ARGS="--cgroup-driver=cgroupfs --network-plugin=cni --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.2"
此处安装flannel后,node ready
7、join node节点
[root@hanyu2 ~]# kubeadm join 10.0.104.125:6443 --token 9o697n.syq87aguxtdmcn76 --discovery-token-ca-cert-hash sha256:294e07d4f7f09e4c99ef96a8563546e002672b8535ee7bc339e423d5cf38aa4c
[preflight] Running pre-flight checks
[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.16" ConfigMap in the kube-system namespace
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Activating the kubelet service
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
This node has joined the cluster:
Certificate signing request was sent to apiserver and a response was received.
The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
查询所有node及pod状态
kubectl get node -o wid
kubectl get pod --all-namespaces -o wide
增加kubectl命令自动补全功能
source <(kubectl completion bash)
默认master节点不会调度pod,去掉此限制
kubectl taint node hostname node-role.kubernetes.io/master:NoSchedule-