欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CentOS7配置Kubernetes(K8S)集群

程序员文章站 2022-03-07 10:29:12
...

1.物理环境 

两台CentOS7机器,地址为192.168.0.51和192.168.0.61

1.1 机器信息

  1. 192.168.0.51 Master/etcd
  2. 192.168.0.61 Node

2. 部署主机

2.1 部署etcd

yum -y install etcd

yum安装的etcd默认配置文件在/etc/etcd/etcd.conf。编辑配置文件,更改为:

# [member]
ETCD_NAME=master
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
#ETCD_WAL_DIR="" #ETCD_SNAPSHOT_COUNT="10000" #ETCD_HEARTBEAT_INTERVAL="100" #ETCD_ELECTION_TIMEOUT="1000" #ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
#ETCD_MAX_SNAPSHOTS="5" #ETCD_MAX_WALS="5" #ETCD_CORS="" # #[cluster] #ETCD_INITIAL_ADVERTISE_PEER_URLS="http://localhost:2380" # if you use different ETCD_NAME (e.g. test), set ETCD_INITIAL_CLUSTER value for this name, i.e. "test=http://..." #ETCD_INITIAL_CLUSTER="default=http://localhost:2380" #ETCD_INITIAL_CLUSTER_STATE="new" #ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.0.51:2379,http://192.168.0.51:4001"
#ETCD_DISCOVERY="" #ETCD_DISCOVERY_SRV="" #ETCD_DISCOVERY_FALLBACK="proxy" #ETCD_DISCOVERY_PROXY="" 

启动并验证状态

[[email protected] ~]# vim /etc/etcd/etcd.conf
[[email protected] ~]# systemctl start etcd
[[email protected] ~]# etcdctl set testdir/testkey0 0 0
[[email protected] ~]# etcdctl get testdir/testkey0 0
[[email protected] ~]# etcdctl set testdir/testkey0 1 1
[[email protected] ~]# etcdctl get testdir/testkey0 1
[[email protected] ~]# etcdctl -C http://192.168.0.51:4001 cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://192.168.0.51:2379
cluster is healthy
[[email protected] ~]# etcdctl -C http://192.168.0.51:2379 cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://192.168.0.51:2379
cluster is healthy

2.2 部署master

安装kubernetes:

yum -y install kubernetes

安装kubernetes会安装docker,所以不单独再安装docker(如果安装docker版本过高会冲突,建议卸载原有docker) 启动Docker并开机启动

systemctl enable docker && systemctl start docker

在kubernetes master上需要运行以下组件:

    Kubernets API Server

    Kubernets Controller Manager

    Kubernets Scheduler

修改配置:

2.2.1 /etc/kubernetes/apiserver

### # kubernetes system config # # The following values are used to configure the kube-apiserver # # The address on the local server to listen to. KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0" # The port on the local server to listen on. KUBE_API_PORT="--port=8080" # Port minions listen on # KUBELET_PORT="--kubelet-port=10250" # Comma separated list of nodes in the etcd cluster KUBE_ETCD_SERVERS="--etcd-servers=http://192.168.0.51:2379" # Address range to use for services KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16" # default admission control policies #KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota" KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota" # Add your own! KUBE_API_ARGS="" 

这里去除ServiceAccount是为了跳过验证。

2.2.2 /etc/kubernetes/config

### # kubernetes system config # # The following values are used to configure various aspects of all # kubernetes services, including # # kube-apiserver.service # kube-controller-manager.service # kube-scheduler.service # kubelet.service # kube-proxy.service # logging to stderr means we get it in the systemd journal KUBE_LOGTOSTDERR="--logtostderr=true" # journal message level, 0 is debug KUBE_LOG_LEVEL="--v=0" # Should this cluster be allowed to run privileged docker containers KUBE_ALLOW_PRIV="--allow-privileged=false" # How the controller-manager, scheduler, and proxy find the apiserver KUBE_MASTER="--master=http://192.168.0.51:8080" 

设置开机启动并启动服务

[[email protected] kubernetes]# systemctl enable kube-apiserver.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-apiserver.service to /usr/lib/systemd/system/kube-apiserver.service.
[[email protected] kubernetes]# systemctl start kube-apiserver.service
[[email protected] kubernetes]# systemctl enable kube-controller-manager.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-controller-manager.service to /usr/lib/systemd/system/kube-controller-manager.service.
[[email protected] kubernetes]# systemctl start kube-controller-manager.service
[[email protected] kubernetes]# systemctl enable kube-scheduler.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-scheduler.service to /usr/lib/systemd/system/kube-scheduler.service.
[[email protected] kubernetes]# systemctl start kube-scheduler.service 

3. 部署节点

安装kubernetes

yum -y install kubernetes
systemctl enable docker && systemctl start docker

3.1 配置服务

在kubernetes node上需要运行以下组件:

    Kubelet

    Kubernets Proxy

修改配置:

3.1.1 /etc/kubernetes/config

### # kubernetes system config # # The following values are used to configure various aspects of all # kubernetes services, including # # kube-apiserver.service # kube-controller-manager.service # kube-scheduler.service # kubelet.service # kube-proxy.service # logging to stderr means we get it in the systemd journal KUBE_LOGTOSTDERR="--logtostderr=true" # journal message level, 0 is debug KUBE_LOG_LEVEL="--v=0" # Should this cluster be allowed to run privileged docker containers KUBE_ALLOW_PRIV="--allow-privileged=false" # How the controller-manager, scheduler, and proxy find the apiserver KUBE_MASTER="--master=http://192.168.0.51:8080" 

3.1.2 /etc/kubernetes/kubelet

### # kubernetes kubelet (minion) config # The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces) KUBELET_ADDRESS="--address=0.0.0.0" # The port for the info server to serve on # KUBELET_PORT="--port=10250" # You may leave this blank to use the actual hostname KUBELET_HOSTNAME="--hostname-override=192.168.0.61" # location of the api-server KUBELET_API_SERVER="--api-servers=http://192.168.0.51:8080" # pod infrastructure container KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest" # Add your own! KUBELET_ARGS="" 

设置开机启动并启动服务

[[email protected] kubernetes]# systemctl enable kubelet.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
[[email protected] kubernetes]# systemctl start kubelet.service
A dependency job for kubelet.service failed. See 'journalctl -xe' for details.
[[email protected] kubernetes]# systemctl enable kube-proxy.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kube-proxy.service to /usr/lib/systemd/system/kube-proxy.service.
[[email protected] kubernetes]# systemctl start kube-proxy.service 

这里发现kubelet未启动成功 查看日志

[[email protected] kubernetes]# journalctl -xe
-- -- Unit docker-storage-setup.service has begun starting up.
Jul 26 14:40:00 localhost.localdomain kernel: SELinux: initialized (dev overlay, type overlay), uses xattr
Jul 26 14:40:00 localhost.localdomain systemd[1]: docker-storage-setup.service: main process exited, code=exited, status=1/FAILURE
Jul 26 14:40:00 localhost.localdomain systemd[1]: Failed to start Docker Storage Setup.
-- Subject: Unit docker-storage-setup.service has failed -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit docker-storage-setup.service has failed. -- -- The result is failed.
Jul 26 14:40:00 localhost.localdomain systemd[1]: Unit docker-storage-setup.service entered failed state.
Jul 26 14:40:00 localhost.localdomain systemd[1]: docker-storage-setup.service failed.
Jul 26 14:40:00 localhost.localdomain systemd[1]: Starting Docker Application Container Engine...
-- Subject: Unit docker.service has begun start-up -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit docker.service has begun starting up.
Jul 26 14:40:00 localhost.localdomain container-storage-setup[22194]: ERROR: XFS filesystem at /var has ftype=0, cannot use overlay backend; consider different driver or separate vol
Jul 26 14:40:01 localhost.localdomain dockerd-current[22281]: time="2018-07-26T14:40:00.993907399+08:00" level=info msg="libcontainerd: new containerd process, pid: 22293"
Jul 26 14:40:01 localhost.localdomain systemd[1]: Started Session 170 of user root.
-- Subject: Unit session-170.scope has finished start-up -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit session-170.scope has finished starting up. -- -- The start-up result is done.
Jul 26 14:40:01 localhost.localdomain CROND[22302]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Jul 26 14:40:02 localhost.localdomain systemd[1]: Starting Session 170 of user root.
-- Subject: Unit session-170.scope has begun start-up -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit session-170.scope has begun starting up.
Jul 26 14:40:02 localhost.localdomain dockerd-current[22281]: time="2018-07-26T14:40:02.044621941+08:00" level=warning msg="overlay2: the backing xfs filesystem is formatted without
Jul 26 14:40:02 localhost.localdomain dockerd-current[22281]: Error starting daemon: SELinux is not supported with the overlay2 graph driver on this kernel. Either boot into a newer
Jul 26 14:40:02 localhost.localdomain systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
Jul 26 14:40:02 localhost.localdomain systemd[1]: Failed to start Docker Application Container Engine.

注意到Error starting daemon: SELinux is not supported with the overlay2 graph driver on this kernel. Either boot into a newer,根据报错信息找到是启动docker出错,原因则是当前kernel不支持overlay2,解决方案为关闭selinux或者使用新的kernel。这里我直接关闭selinux。

[[email protected] kubernetes]# vim /etc/sysconfig/docker # /etc/sysconfig/docker # Modify these options if you want to change the way the docker daemon runs
OPTIONS='--selinux-enabled=false --log-driver=journald --signature-verification=false' if [ -z "${DOCKER_CERT_PATH}" ]; then
 DOCKER_CERT_PATH=/etc/docker
fi 

然后重新启动kubelet

systemctl start kubelet.service

查看结果:

[[email protected] kubernetes]# kubectl -s http://192.168.0.51:8080/ get node
NAME STATUS AGE
192.168.0.61 Ready 12m

在master上可以直接查看

[[email protected] kubernetes]# kubectl get node NAME STATUS AGE
192.168.0.61 Ready 1m 

注意到这里的AGE不同,目前还不清楚原因,做个记录。

4. 部署Flannel

在所有节点(包括master和node)上安装flannel

yum -y install flannel

4.1 修改配置

所有节点修改/etc/sysconfig/flanneld

[[email protected] kubernetes]# vim /etc/sysconfig/flanneld # Flanneld configuration options # etcd url location. Point this to the server where etcd runs FLANNEL_ETCD_ENDPOINTS="http://192.168.0.51:2379" # etcd config key. This is the configuration key that flannel queries # For address range assignment FLANNEL_ETCD_PREFIX="/atomic.io/network" # Any additional options that you want to pass #FLANNEL_OPTIONS="" 

4.2 修改etc配置

Flannel使用Etcd进行配置,来保证多个Flannel实例之间的配置一致性,所以需要在etcd上进行如下配置:(‘/atomic.io/network/config’这个key与上文/etc/sysconfig/flannel中的配置项FLANNEL_ETCD_PREFIX是相对应的,错误的话启动就会出错)

etcdctl mk /atomic.io/network/config '{ "Network": "10.0.0.0/16" }'

4.3 启动

启动Flannel之后,需要依次重启docker、kubernete。 master:

systemctl enable flanneld.service systemctl start flanneld.service systemctl restart docker systemctl restart kube-apiserver.service systemctl restart kube-controller-manager.service systemctl restart kube-scheduler.service 

node:

systemctl enable flanneld.service systemctl start flanneld.service systemctl restart docker systemctl restart kubelet.service systemctl restart kube-proxy.service

本文转移开源中国-CentOS7配置Kubernetes(K8S)集群