1、配置国内(阿里)源
[root@master01 ~]# 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
2、安装kubectl、kubelet、kubeadm
[root@master01 ~]# yum install kubectl kubelet kubeadm
[root@master01 ~]# systemctl enable kubelet
3、初始化k8s集群(主机)
[root@master01 tmp]# kubeadm init --kubernetes-version=1.18.1 --apiserver-advertise-address=192.168.100.206 --image-repository registry.aliyuncs.com/google_containers --service-cidr=10.10.0.0/16 --pod-network-cidr=10.122.0.0/16
注:api server地址就是master01的IP。
初始化成功后会显示如下信息:
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.100.206:6443 --token v2r5a4.veazy2xhzetpktfz \
--discovery-token-ca-cert-hash sha256:daded8514c8350f7c238204979039ff9884d5b595ca950ba8bbce80724fd65d4
可保存下来,留待下一步使用。
注:token是有24小时有效期的,过期需要重新生成:
[root@master01 ~]# # kubeadm token create --print-join-command
[root@master01 ~]# kubeadm token create --print-join-command
W0421 23:54:22.888946 20770 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
kubeadm join 192.168.100.206:6443 --token zu3xel.gg8z2z3kkmyoq1yq --discovery-token-ca-cert-hash sha256:02bc1434037929734dd43f5707469ed5805ec50c6978cd4d2b12e9b6c24a9889
4、初始化k8s集群(从机)
在node01上执行:
[root@node01 ~]# kubeadm join 192.168.100.206:6443 --token v2r5a4.veazy2xhzetpktfz --discovery-token-ca-cert-hash sha256:daded8514c8350f7c238204979039ff9884d5b595ca950ba8bbce80724fd65d4
5、配置k8s
[root@master01 ~]# mkdir -p $HOME/.kube
[root@master01 ~]# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@master01 ~]# chown $(id -u):$(id -g) $HOME/.kube/config
[root@master01 ~]# source <(kubectl completion bash)
如果一切正常,会得到如下结果:
root@master01 ~]# kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS
kube-system coredns-7ff77c879f-9lqhx 1/1 Running 2 2d2h
kube-system coredns-7ff77c879f-gxbvv 1/1 Running 2 2d2h
kube-system etcd-master01.huiyan.com 1/1 Running 2 2d2h
kube-system kube-apiserver-master01.huiyan.com 1/1 Running 2 2d2h
kube-system kube-controller-manager-master01.huiyan.com 1/1 Running 3 2d2h
kube-system kube-proxy-82cwf 1/1 Running 2 2d2h
kube-system kube-proxy-xs9wq 1/1 Running 2 2d2h
kube-system kube-scheduler-master01.huiyan.com 1/1 Running 2 2d2h
6、安装calico网络
[root@master01 ~]# kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
configmap/calico-config created
customresourcedefinition.apiextensions.k8s.io/bgpconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/bgppeers.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/blockaffinities.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/clusterinformations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/felixconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworksets.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/hostendpoints.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamblocks.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamconfigs.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamhandles.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ippools.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networksets.crd.projectcalico.org created
clusterrole.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrolebinding.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrole.rbac.authorization.k8s.io/calico-node created
clusterrolebinding.rbac.authorization.k8s.io/calico-node created
daemonset.apps/calico-node created
serviceaccount/calico-node created
deployment.apps/calico-kube-controllers created
serviceaccount/calico-kube-controllers created