创建k8s集群

准备

安装之前准备三台服务器,或者通过虚拟机启动,这里推荐使用vargent的方式启动虚拟机。
下面以Centos为例

安装Docker

对于docker的安装可以移步

安装前准备

1
sudo bash -c 'cat <<EOF > /etc/yum.repos.d/kubernetes.repo
2
[kubernetes]
3
name=Kubernetes
4
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
5
enabled=1
6
gpgcheck=1
7
repo_gpgcheck=1
8
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
9
EOF'
Copied!
国内的用户可以将goole换成国内的源,然后分别执行以下命令:
1
sudo setenforce 0
2
sudo yum install -y kubelet kubeadm kubectl
3
4
sudo bash -c 'cat <<EOF > /etc/sysctl.d/k8s.conf
5
net.bridge.bridge-nf-call-ip6tables = 1
6
net.bridge.bridge-nf-call-iptables = 1
7
net.ipv4.ip_forward=1
8
EOF'
9
sudo sysctl --system
10
11
sudo systemctl stop firewalld
12
sudo systemctl disable firewalld
13
# 很重要
14
sudo swapoff -a
15
16
systemctl enable docker.service
17
systemctl enable kubelet.service
Copied!

创建集群

在启动之前先检查一下kubeadm, kubectl, kubelet是否安装成功,并且docker是否启动(需要在三台服务器上都执行下面的命令)
1
[[email protected] ~]$ which kubeadm
2
/usr/bin/kubeadm
3
[[email protected] ~]$ which kubelet
4
/usr/bin/kubelet
5
[[email protected] ~]$ which kubectl
6
/usr/bin/kubectl
7
[[email protected] ~]$ sudo docker version
8
Client: Docker Engine - Community
9
Version: 20.10.7
10
API version: 1.41
11
Go version: go1.13.15
12
Git commit: f0df350
13
Built: Wed Jun 2 11:58:10 2021
14
OS/Arch: linux/amd64
15
Context: default
16
Experimental: true
17
18
Server: Docker Engine - Community
19
Engine:
20
Version: 20.10.7
21
API version: 1.41 (minimum version 1.12)
22
Go version: go1.13.15
23
Git commit: b0f5bc3
24
Built: Wed Jun 2 11:56:35 2021
25
OS/Arch: linux/amd64
26
Experimental: false
27
containerd:
28
Version: 1.4.6
29
GitCommit: d71fcd7d8303cbf684402823e425e9dd2e99285d
30
runc:
31
Version: 1.0.0-rc95
32
GitCommit: b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
33
docker-init:
34
Version: 0.19.0
35
GitCommit: de40ad0
Copied!
除了上面的操作,还需要检查三台服务器的k8s版本是否相同。
1
[[email protected] ~]# kubectl version
2
Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.2", GitCommit:"092fbfbf53427de67cac1e9fa54aaa09a28371d7", GitTreeState:"clean", BuildDate:"2021-06-16T12:59:11Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"linux/amd64"}
3
Server Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.2", GitCommit:"092fbfbf53427de67cac1e9fa54aaa09a28371d7", GitTreeState:"clean", BuildDate:"2021-06-16T12:53:14Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"linux/amd64"}
4
[[email protected] ~]# kubectl version
5
Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.2", GitCommit:"092fbfbf53427de67cac1e9fa54aaa09a28371d7", GitTreeState:"clean", BuildDate:"2021-06-16T12:59:11Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"linux/amd64"}
6
Server Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.2", GitCommit:"092fbfbf53427de67cac1e9fa54aaa09a28371d7", GitTreeState:"clean", BuildDate:"2021-06-16T12:53:14Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"linux/amd64"}
7
[[email protected] ~]# kubelet --version
8
Kubernetes v1.21.2
Copied!
然后将其中一台服务器作为master,执行初始化集群操作(只需要在master服务器执行下面的操作即可)
其中
  • --pod-network-cidr: 为使用的网段
  • --apiserver-advertise-address: 为master服务器的ip地址,需要注意的是三台服务器之间需要相互ping通才可以
1
[[email protected] ~]$ sudo kubeadm init --pod-network-cidr 172.100.0.0/16 --apiserver-advertise-address xxx.xxx.xxx.xxx
2
[init] Using Kubernetes version: v1.15.3
3
[preflight] Running pre-flight checks
4
[WARNING Service-Docker]: docker service is not enabled, please run 'systemctl enable docker.service'
5
[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/
6
[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 19.03.1. Latest validated version: 18.09
7
[WARNING Service-Kubelet]: kubelet service is not enabled, please run 'systemctl enable kubelet.service'
8
[preflight] Pulling images required for setting up a Kubernetes cluster
9
[preflight] This might take a minute or two, depending on the speed of your internet connection
10
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
11
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
12
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
13
[kubelet-start] Activating the kubelet service
14
[certs] Using certificateDir folder "/etc/kubernetes/pki"
15
[certs] Generating "ca" certificate and key
16
[certs] Generating "apiserver-kubelet-client" certificate and key
17
[certs] Generating "apiserver" certificate and key
18
[certs] apiserver serving cert is signed for DNS names [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.205.120]
19
[certs] Generating "front-proxy-ca" certificate and key
20
[certs] Generating "front-proxy-client" certificate and key
21
[certs] Generating "etcd/ca" certificate and key
22
[certs] Generating "etcd/server" certificate and key
23
[certs] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.205.120 127.0.0.1 ::1]
24
[certs] Generating "etcd/healthcheck-client" certificate and key
25
[certs] Generating "apiserver-etcd-client" certificate and key
26
[certs] Generating "etcd/peer" certificate and key
27
[certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.205.120 127.0.0.1 ::1]
28
[certs] Generating "sa" key and public key
29
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
30
[kubeconfig] Writing "admin.conf" kubeconfig file
31
[kubeconfig] Writing "kubelet.conf" kubeconfig file
32
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
33
[kubeconfig] Writing "scheduler.conf" kubeconfig file
34
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
35
[control-plane] Creating static Pod manifest for "kube-apiserver"
36
[control-plane] Creating static Pod manifest for "kube-controller-manager"
37
[control-plane] Creating static Pod manifest for "kube-scheduler"
38
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
39
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
40
[apiclient] All control plane components are healthy after 23.002991 seconds
41
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
42
[kubelet] Creating a ConfigMap "kubelet-config-1.15" in namespace kube-system with the configuration for the kubelets in the cluster
43
[upload-certs] Skipping phase. Please see --upload-certs
44
[mark-control-plane] Marking the node k8s-master as control-plane by adding the label "node-role.kubernetes.io/master=''"
45
[mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
46
[bootstrap-token] Using token: snipoh.vxfykjsi7e7rbtna
47
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
48
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
49
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
50
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
51
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
52
[addons] Applied essential addon: CoreDNS
53
[addons] Applied essential addon: kube-proxy
54
55
Your Kubernetes control-plane has initialized successfully!
56
57
To start using your cluster, you need to run the following as a regular user:
58
59
mkdir -p $HOME/.kube
60
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
61
sudo chown $(id -u):$(id -g) $HOME/.kube/config
62
63
You should now deploy a pod network to the cluster.
64
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
65
https://kubernetes.io/docs/concepts/cluster-administration/addons/
66
67
Then you can join any number of worker nodes by running the following on each as root:
68
69
kubeadm join xxx.xxx.xxx.xxx:6443 --token snipoh.vxfykjsi7e7rbtna \
70
--discovery-token-ca-cert-hash sha256:e202fbfa3eed1e1d6c646dd568285947d67e99b51e824c99aeb6f45080d284c1
Copied!
如果服务器最后的输出结果如上所示,那么代表集群已经初始化成功了。
需要保存好下面这个字符串,另外的服务器会通过它来加入集群
1
kubeadm join xxx.xxx.xxx.xxx:6443 --token snipoh.vxfykjsi7e7rbtna \
2
--discovery-token-ca-cert-hash sha256:e202fbfa3eed1e1d6c646dd568285947d67e99b51e824c99aeb6f45080d284c1
Copied!
还需要执行下面的命令:
1
mkdir -p $HOME/.kube
2
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
3
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Copied!
然后通过kubectl检查pod状态
1
[[email protected] ~]$ kubectl get pod --all-namespaces
2
NAMESPACE NAME READY STATUS RESTARTS AGE
3
kube-system coredns-5c98db65d4-f4kjf 0/1 Pending 0 58m
4
kube-system coredns-5c98db65d4-xqpwd 0/1 Pending 0 58m
5
kube-system etcd-k8s-master 1/1 Running 0 57m
6
kube-system kube-apiserver-k8s-master 1/1 Running 0 57m
7
kube-system kube-controller-manager-k8s-master 1/1 Running 0 57m
8
kube-system kube-proxy-9l9vr 1/1 Running 0 58m
9
kube-system kube-scheduler-k8s-master 1/1 Running 0 57m
Copied!
终端会输出类似这样的结果,其中两个仍处于pending状态,这是由于没有添加网络插件导致的,通过下面的命令即可添加网络插件
1
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
Copied!
命令执行后过一段时间再检查pod状态就可以发现全部运行了,如下
1
[[email protected] ~]$ kubectl get pod --all-namespaces
2
NAMESPACE NAME READY STATUS RESTARTS AGE
3
kube-system coredns-5c98db65d4-gpsvq 1/1 Running 0 7h31m
4
kube-system coredns-5c98db65d4-st4pf 1/1 Running 0 7h31m
5
kube-system etcd-k8s-master 1/1 Running 0 7h30m
6
kube-system kube-apiserver-k8s-master 1/1 Running 0 7h30m
7
kube-system kube-controller-manager-k8s-master 1/1 Running 0 7h30m
8
kube-system kube-proxy-kx5mv 1/1 Running 0 7h31m
9
kube-system kube-scheduler-k8s-master 1/1 Running 0 7h30m
10
kube-system weave-net-57dtf 2/2 Running 0 59s=
Copied!
再查看当前node状态,可以发现只有master节点在集群当中
1
[[email protected] ~]$ kubectl get nodes
2
NAME STATUS ROLES AGE VERSION
3
copy-of-vm-ee-centos76-v1.05 Ready control-plane,master 16h v1.21.2
Copied!
接下来在另外两台服务器执行下面的命令:
1
# 可以通过 --node-name 来自己指定添加到k8s集群的名称
2
# 这里的字符串为master节点初始化集群后的输出
3
[[email protected] ~]$ sudo kubeadm join xxx.xxx.xxx.xxx:6443 --token tte278.145ozal6u6e26ypm --discovery-token-ca-cert-hash sha256:cbb168e0665fe1b14e96a87c2da5dc1eeda04c70932ac1913d989753703277bb
4
[preflight] Running pre-flight checks
5
[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/
6
[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 19.03.1. Latest validated version: 18.09
7
[preflight] Reading configuration from the cluster...
8
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
9
[kubelet-start] Downloading configuration for the kubelet from the "kubelet-config-1.15" ConfigMap in the kube-system namespace
10
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
11
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
12
[kubelet-start] Activating the kubelet service
13
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
14
15
This node has joined the cluster:
16
* Certificate signing request was sent to apiserver and a response was received.
17
* The Kubelet was informed of the new secure connection details.
18
19
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
Copied!
两台服务器都执行成功后,在master节点查看node状态即可发现另外两个节点已经添加到集群中了
1
[[email protected] ~]$ kubectl get nodes
2
NAME STATUS ROLES AGE VERSION
3
copy-of-vm-ee-centos76-v1.05 Ready control-plane,master 16h v1.21.2
4
k8s-node1 Ready <none> 16h v1.21.2
5
k8s-node2 Ready <none> 16h v1.21.2
Copied!

新手推荐

GitHub - kubernetes/minikube: Run Kubernetes locally
GitHub
GitHub - fanux/sealos: 一条命令离线安装高可用kubernetes,3min装完,700M,100年证书,生产环境稳如老狗
GitHub
最近更新 6mo ago