一、安装docker-ce
sudo apt-get update sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common # step 2: 安装GPG证书 curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - # Step 3: 写入软件源信息 sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" # Step 4: 更新并安装Docker-CE sudo apt-get -y update sudo apt-get -y install docker-ce
二、将docker加入docker群组,使用普通用户也可以使用docker
sudo groupadd docker #添加docker用户组
sudo gpasswd -a $USER docker #将登陆用户加入到docker用户组中
newgrp docker #更新用户组
三、配置Ubuntu 的kubectl、kubeadm、kubelet
apt-get update && apt-get install -y apt-transport-https curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - cat </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=1.22.5-00 kubeadm=1.22.5-00 kubectl=1.22.5-00
如果安装过其他版本,先删除软件及其配置文件
apt-get --purge remove kubelet apt-get --purge remove kubectl apt-get --purge remove kubeadm
四、查看对应的k8s安装组件版本,在阿里云下载对应镜像
kubeadm config images list
五、设置配置文件
1.
sudo vim /etc/docker/daemon.json
在文件中添加代码段
{
"exec-opts": ["native.cgroupdriver=systemd"] #Kubernetes 推荐使用 systemd 来代替 cgroupfs
}
2.
vim /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
添加
Environment="KUBELET_SYSTEM_PODS_ARGS=--pod-manifest-path=/etc/kubernetes/manifests --allow-privileged=true --fail-swap-on=false"
3.
sudo vim /etc/fstab #swap那行 前面加#
保存配置
systemctl daemon-reload systemctl restart docker systemctl status docker systemctl restart kubectl systemctl status kubectl
六、初始化k8s master节点
sudo kubeadm init --kubernetes-version=v1.22.5 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.96.0.0/16
出现下面提示就成功了,然后按提示操作
Your Kubernetes control-plane has initialized successfully! To start using your cluster, you need to run the following as a regular user: mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config Alternatively, if you are the root user, you can run: export KUBEConFIG=/etc/kubernetes/admin.conf 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/ Then you can join any number of worker nodes by running the following on each as root: kubeadm join 10.3.172.47:6443 --token eul8eh.jhok8q7mj6mgijun
当初始化失败,重新初始化时,需要清理环境
kubeadm reset sudo rm -rf /etc/cni/net.d sudo rm -rf $HOME/.kube/config sudo rm -rf /etc/kubernetes/
七、查看节点状态
scarlettzhao@scarlettzhao-master:~$ kubectl get nodes NAME STATUS ROLES AGE VERSION scarlettzhao-master NotReady control-plane,master 100s v1.22.2
节点是notready,检查pod
scarlettzhao@scarlettzhao-master:~$ kubectl get pod --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE kube-system coredns-78fcd69978-8ps67 0/1 Pending 0 122m kube-system coredns-78fcd69978-ng7ln 0/1 Pending 0 122m kube-system etcd-scarlettzhao-master 1/1 Running 2 (4m32s ago) 122m kube-system kube-apiserver-scarlettzhao-master 1/1 Running 2 (4m31s ago) 122m kube-system kube-controller-manager-scarlettzhao-master 1/1 Running 2 (4m32s ago) 122m kube-system kube-flannel-ds-ntchz 0/1 Init:1/2 0 25s kube-system kube-proxy-9mh9h 1/1 Running 1 (4m32s ago) 122m kube-system kube-scheduler-scarlettzhao-master 1/1 Running 2 (4m32s ago) 122m
coredns 状态pending,未运行,因为缺少网络组件
八、配置网络
1.Install the Tigera Calico operator and custom resource definitions. kubectl create -f https://docs.projectcalico.org/manifests/tigera-operator.yaml 2.Install Calico by creating the necessary custom resource. For more information on configuration options available in this manifest, see the installation reference. kubectl create -f https://docs.projectcalico.org/manifests/custom-resources.yaml # Note: Before creating this manifest, read its contents and make sure its settings are correct for your environment. For example, you may need to change the default IP pool CIDR to match your pod network CIDR. 3.Confirm that all of the pods are running with the following command. watch kubectl get pods -n calico-system #Wait until each pod has the STATUS of Running. # Note: The Tigera operator installs resources in the calico-system namespace. Other install methods may use the kube-system namespace instead. 4.Remove the taints on the master so that you can schedule pods on it. kubectl taint nodes --all node-role.kubernetes.io/master- 5.kubectl get nodes -o wide



