机器规划
k8s master: 172.26.237.103
k8s node1:172.26.237.105
k8s node2: 172.26.237.103
修改hosts文件
[root@k8s001 .ssh]# vi /etc/hosts
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
172.26.237.103 k8s001 k8s001
172.26.237.105 k8s002 k8s002
172.26.237.104 k8s003 k8s003
安装Docker
见[https://www.jianshu.com/writer#/notebooks/31084596/notes/36288645]
安装kubelet、kubeadm和kubectl
1、添加源
[root@k8s001 soft]# cat <<EOF > /etc/yum.repos.d/kubernetes.repo
> [kubernetes]
> name=Kubernetes
> baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
> enabled=1
> gpgcheck=1
> repo_gpgcheck=1
> gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
> exclude=kube*
> EOF
2、安装
[root@k8s001 soft]# yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
[root@k8s001 soft]# systemctl enable kubelet && systemctl start kubelet
3、修改网络配置
[root@k8s001 soft]# cat <<EOF > /etc/sysctl.d/k8s.conf
> net.bridge.bridge-nf-call-ip6tables = 1
> net.bridge.bridge-nf-call-iptables = 1
> EOF
[root@k8s001 soft]# sysctl --system
初始化Master
[root@k8s001 soft]# kubeadm config print init-defaults > kubeadm-init.yaml
修改ip和image仓库地址
advertiseAddress: 172.26.237.103
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
下载镜像
[root@k8s001 soft]# kubeadm config images pull --config kubeadm-init.yaml
下载镜像报错
failed to pull image "registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns:v1.8.0": output: Error response from daemon: pull access denied for registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns, repository does not exist or may require 'docker login'
, error: exit status 1
To see the stack trace of this error execute with --v=5 or higher
手动下载镜像
[root@k8s001 soft]# docker pull coredns/coredns
Using default tag: latest
latest: Pulling from coredns/coredns
查看kubeadm需要镜像,并修改名称
[root@k8s001 soft]# kubeadm config images list --config kubeadm-init.yaml
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.21.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.21.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.21.0
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.21.0
registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.4.1
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.13-0
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns:v1.8.0
[root@k8s001 soft]# docker tag coredns/coredns:latest registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns:v1.8.0
[root@k8s001 soft]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver v1.21.0 4d217480042e 4 weeks ago 126MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy v1.21.0 38ddd85fe90e 4 weeks ago 122MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler v1.21.0 62ad3129eca8 4 weeks ago 50.6MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager v1.21.0 09708983cc37 4 weeks ago 120MB
coredns/coredns latest 3885a5b7f138 2 months ago 43.5MB
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns v1.8.0 3885a5b7f138 2 months ago 43.5MB
registry.cn-hangzhou.aliyuncs.com/google_containers/pause 3.4.1 0f8457a4c2ec 3 months ago 683kB
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd 3.4.13-0 0369cf4303ff 8 months ago 253MB
然后删除多余镜像
[root@k8s001 soft]# docker rmi coredns/coredns:latest
Untagged: coredns/coredns:latest
Untagged: coredns/coredns@sha256:642ff9910da6ea9a8624b0234eef52af9ca75ecbec474c5507cb096bdfbae4e5
[root@k8s001 soft]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver v1.21.0 4d217480042e 4 weeks ago 126MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy v1.21.0 38ddd85fe90e 4 weeks ago 122MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager v1.21.0 09708983cc37 4 weeks ago 120MB
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler v1.21.0 62ad3129eca8 4 weeks ago 50.6MB
registry.cn-hangzhou.aliyuncs.com/google_containers/coredns/coredns v1.8.0 3885a5b7f138 2 months ago 43.5MB
registry.cn-hangzhou.aliyuncs.com/google_containers/pause 3.4.1 0f8457a4c2ec 3 months ago 683kB
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd 3.4.13-0 0369cf4303ff 8 months ago 253MB
执行初始化
[root@k8s001 soft]# kubeadm init --config kubeadm-init.yaml
[init] Using Kubernetes version: v1.21.0
[preflight] Running pre-flight checks
[addons] Applied essential addon: kube-proxy
Your Kubernetes control-plane has initialized successfully!
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 172.26.237.103:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:efdcd7f0265b9b2669b9590767cf95828c92d630127a155e46b5d90133900d3b
初始化环境,使当前用户可以使用kubectl
[root@k8s001 soft]# mkdir -p $HOME/.kube
[root@k8s001 soft]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@k8s001 soft]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
[root@k8s001 soft]# kubectl get node
NAME STATUS ROLES AGE VERSION
node NotReady control-plane,master 2m8s v1.21.0
配置网络
[root@k8s001 soft]# wget https://docs.projectcalico.org/v3.8/manifests/calico.yaml
[root@k8s001 soft]# cat kubeadm-init.yaml | grep serviceSubnet:
serviceSubnet: 10.96.0.0/12
#192.168.0.0/16修改为10.96.0.0/12
[root@k8s001 soft]# sed -i 's/192.168.0.0\/16/10.96.0.0\/12/g' calico.yaml
#初始化网络
[root@k8s001 soft]# kubectl apply -f calico.yaml
#再查看
[root@k8s001 soft]# kubectl get node
NAME STATUS ROLES AGE VERSION
node Ready control-plane,master 30m v1.21.0
添加node节点
[root@k8s002 ~]# kubeadm join 172.26.237.103:6443 --token abcdef.0123456789abcdef \
> --discovery-token-ca-cert-hash sha256:efdcd7f0265b9b2669b9590767cf95828c92d630127a155e46b5d90133900d3b
[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 -o yaml'
[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] Starting the kubelet
[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.
查看节点
[root@k8s001 soft]# kubectl get node
NAME STATUS ROLES AGE VERSION
k8s002 Ready <none> 2m22s v1.21.0
k8s003 Ready <none> 2m26s v1.21.0
node Ready control-plane,master 35m v1.21.0