Kubernetes部署并初始化群集

环境规划

角色规划

主机名系统版本K8S版本角色IP地址
k8s-masterCentos71.18.5master192.168.3.5
k8s-node-1Centos71.18.5node192.168.3.6
k8s-node-2Centos71.18.5node192.168.3.7

网络规划

用途网络
service10.96.0.0/12
pod10.244.0.0/16

环境配置

以下的环境设置需要在所有服务器上操作。

repo设置

docker

[docker-ce]
name=Docker CE
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/
enabled=1
gpgcheck=0

kubernetes

[kubernetes]
name=Kubernetes Repo
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0

epel

yum -y install epel-release

查看repo源是否配置正确:

[root@k8s-master yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
docker-ce                                                                                                       | 3.5 kB  00:00:00     
kubernetes                                                                                                      | 1.4 kB  00:00:00     
(1/3): docker-ce/updateinfo                                                                                     |   55 B  00:00:00     
(2/3): docker-ce/primary_db                                                                                     |  45 kB  00:00:00     
(3/3): kubernetes/primary                                                                                       |  73 kB  00:00:00     
Loading mirror speeds from cached hostfile
 * base: ftp.yz.yamagata-u.ac.jp
 * epel: mirror.telkomuniversity.ac.id
 * extras: ftp.yz.yamagata-u.ac.jp
 * updates: ftp.yz.yamagata-u.ac.jp
kubernetes                                                                                                                     533/533
repo id                                          repo name                                                                       status
base/7/x86_64                                    CentOS-7 - Base                                                                 10,070
docker-ce                                        Docker CE                                                                           79
epel/x86_64                                      Extra Packages for Enterprise Linux 7 - x86_64                                  13,376
extras/7/x86_64                                  CentOS-7 - Extras                                                                  412
kubernetes                                       Kubernetes Repo                                                                    533
updates/7/x86_64                                 CentOS-7 - Updates                                                                 890
repolist: 25,360

hosts

192.168.3.5 k8s-master
192.168.3.6 k8s-node-1
192.168.3.7 k8s-node-2

防火墙

systemctl stop firewalld
systemctl disable firewalld
sed -i "s/enforcing/disabled/" /etc/selinux/config 

网络设置

访问外网

自行处理(可以访问到github、kubernetes等其他你懂的)

桥接设置

[root@k8s-master ~]# cat /proc/sys/net/bridge/bridge-nf-call-ip6tables 
1
[root@k8s-master ~]# cat /proc/sys/net/bridge/bridge-nf-call-iptables 
1

其他设置

关闭交换分区(swap)
时间同步(所有服务器时间都必须保持一致,可以使用ntp同步。)

安装部署

kubernetes的部署方式有很多,最原始的方式我就不尝试了,下面我使用kubeadm的方式来初始化部署一个kubernetes群集。

Master

安装组件和容器引擎

[root@k8s-master yum.repos.d]# yum -y install docker-ce kubelet kuberadm kubectl

初始化群集

[root@k8s-master ~]# kubeadm init --kubernetes-version=v1.18.5 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap

--kubernetes-version:指定要部署的k8s版本,不指定的话就是默认的最新稳定版本;
--pod-network-cidr:指定pod的网络CIDR;
--service-cidr:指定service的网络CIDR;
--ignore-preflight-errors:在预检中如果有错误可以忽略掉,比如忽略 IsPrivilegedUser,Swap等;
--apiserver-advertise-address:表示apiserver对外的地址是什么,默认是0.0.0.0;
--apiserver-bind-port:表示apiserver的端口是什么,默认是6443;

W0709 11:32:12.551573    2186 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[init] Using Kubernetes version: v1.18.5
[preflight] Running pre-flight checks
    [WARNING Firewalld]: firewalld is active, please ensure ports [6443 10250] are open or your cluster may not function correctly
    [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/
    [WARNING Hostname]: hostname "k8s-master" could not be reached
    [WARNING Hostname]: hostname "k8s-master": lookup k8s-master on 192.168.3.1:53: no such host
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[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.3.5]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.3.5 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.3.5 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
W0709 11:32:18.434222    2186 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
[control-plane] Creating static Pod manifest for "kube-scheduler"
W0709 11:32:18.435427    2186 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[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
[apiclient] All control plane components are healthy after 23.007757 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.18" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node k8s-master as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: wintvq.mug5ekr610i7i0b0
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

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

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 192.168.3.5:6443 --token wintvq.mug5ekr610i7i0b0 \
    --discovery-token-ca-cert-hash sha256:41d54381dfbebd943b03cd7368584414635eeab0f9cba375480f618a33bae157 

上述初始化成功之后,按照提示里面说的需要做一些操作。生产环境请使用普通用户执行下述命令,我这边事实验环境,所以就直接root了。

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

安装flannel组件

[root@k8s-master ~]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds-amd64 created
daemonset.apps/kube-flannel-ds-arm64 created
daemonset.apps/kube-flannel-ds-arm created
daemonset.apps/kube-flannel-ds-ppc64le created
daemonset.apps/kube-flannel-ds-s390x created

node

[root@k8s-node-1 yum.repos.d]# yum -y install docker-ce kubelet kuberadm kubectl
[root@k8s-node-2 yum.repos.d]# yum -y install docker-ce kubelet kuberadm kubectl

加入群集

登陆到node节点,执行join命令加入群集。

kubeadm join 192.168.3.5:6443 --token wintvq.mug5ekr610i7i0b0 \
        --discovery-token-ca-cert-hash sha256:41d54381dfbebd943b03cd7368584414635eeab0f9cba375480f618a33bae157 

测时群集

查看node状态

[root@k8s-master ~]# kubectl get nodes
NAME         STATUS   ROLES    AGE   VERSION
k8s-master   Ready    master   25h   v1.18.5
k8s-node-1   Ready    <none>   25h   v1.18.5
k8s-node-2   Ready    <none>   24h   v1.18.5

查看命名空间

[root@k8s-master ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   25h
kube-node-lease   Active   25h
kube-public       Active   25h
kube-system       Active   25h

查看指定命名空间详细信息

[root@k8s-master ~]# kubectl get pods -n kube-system -o wide
NAME                                 READY   STATUS    RESTARTS   AGE     IP            NODE         NOMINATED NODE   READINESS GATES
coredns-66bff467f8-jwrl5             1/1     Running   0          28m     10.244.0.3    k8s-master   <none>           <none>
coredns-66bff467f8-s4jbd             1/1     Running   0          28m     10.244.0.2    k8s-master   <none>           <none>
etcd-k8s-master                      1/1     Running   0          28m     192.168.3.5   k8s-master   <none>           <none>
kube-apiserver-k8s-master            1/1     Running   0          28m     192.168.3.5   k8s-master   <none>           <none>
kube-controller-manager-k8s-master   1/1     Running   0          28m     192.168.3.5   k8s-master   <none>           <none>
kube-flannel-ds-amd64-64j2m          1/1     Running   0          2m11s   192.168.3.6   k8s-node-1   <none>           <none>
kube-flannel-ds-amd64-sfqlj          1/1     Running   0          16m     192.168.3.5   k8s-master   <none>           <none>
kube-proxy-hdh9n                     1/1     Running   0          2m11s   192.168.3.6   k8s-node-1   <none>           <none>
kube-proxy-nx7nr                     1/1     Running   0          28m     192.168.3.5   k8s-master   <none>           <none>
kube-scheduler-k8s-master            1/1     Running   0          28m     192.168.3.5   k8s-master   <none>           <none>

查看组件状态

[root@k8s-master ~]# kubectl get cs
NAME                 STATUS    MESSAGE             ERROR
controller-manager   Healthy   ok                  
scheduler            Healthy   ok                  
etcd-0               Healthy   {"health":"true"}  

查看pods

[root@k8s-master ~]# kubectl get pods -n kube-system
NAME                                 READY   STATUS    RESTARTS   AGE
coredns-66bff467f8-jwrl5             0/1     Running   0          13m
coredns-66bff467f8-s4jbd             0/1     Running   0          13m
etcd-k8s-master                      1/1     Running   0          13m
kube-apiserver-k8s-master            1/1     Running   0          13m
kube-controller-manager-k8s-master   1/1     Running   0          13m
kube-flannel-ds-amd64-sfqlj          1/1     Running   0          2m10s
kube-proxy-nx7nr                     1/1     Running   0          13m
kube-scheduler-k8s-master            1/1     Running   0          13m

查看版本信息

[root@k8s-master ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.5", GitCommit:"e6503f8d8f769ace2f338794c914a96fc335df0f", GitTreeState:"clean", BuildDate:"2020-06-26T03:47:41Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.5", GitCommit:"e6503f8d8f769ace2f338794c914a96fc335df0f", GitTreeState:"clean", BuildDate:"2020-06-26T03:39:24Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}

文章最后更新时间 : 2020年07月10日 13:16:46

评论已关闭