CentOS7配置Kubernetes(K8S)集群

简介: 1.物理环境 两台CentOS7机器,地址为192.168.0.51和192.168.0.61 1.1 机器信息 192.168.0.51 Master/etcd 192.168.0.61 Node 2. 部署主机 2.1 部署etcd yum -y install etcd yum安装的etcd默认配置文件在/etc/etcd/etcd.conf。

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="" 

启动并验证状态

[root@localhost ~]# vim /etc/etcd/etcd.conf
[root@localhost ~]# systemctl start etcd
[root@localhost ~]# etcdctl set testdir/testkey0 0 0
[root@localhost ~]# etcdctl get testdir/testkey0 0
[root@localhost ~]# etcdctl set testdir/testkey0 1 1
[root@localhost ~]# etcdctl get testdir/testkey0 1
[root@localhost ~]# 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
[root@localhost ~]# 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" 

设置开机启动并启动服务

[root@localhost 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.
[root@localhost kubernetes]# systemctl start kube-apiserver.service
[root@localhost 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.
[root@localhost kubernetes]# systemctl start kube-controller-manager.service
[root@localhost 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.
[root@localhost 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="" 

设置开机启动并启动服务

[root@localhost kubernetes]# systemctl enable kubelet.service
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
[root@localhost kubernetes]# systemctl start kubelet.service
A dependency job for kubelet.service failed. See 'journalctl -xe' for details.
[root@localhost 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.
[root@localhost kubernetes]# systemctl start kube-proxy.service 

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

[root@localhost 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。

[root@localhost 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

查看结果:

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

在master上可以直接查看

[root@localhost 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

[root@localhost 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)集群
相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
相关文章
|
18天前
|
存储 运维 Kubernetes
Docker+Kubernetes/K8s+Jenkins视频资料【干货分享】
Docker+Kubernetes/K8s+Jenkins视频资料【干货分享】
Docker+Kubernetes/K8s+Jenkins视频资料【干货分享】
|
4天前
|
存储 Kubernetes 数据安全/隐私保护
|
7天前
|
Linux
CentOS 7 配置yum阿里源 (三步即可)
CentOS 7 配置yum阿里源 (三步即可)
|
13天前
|
Kubernetes API 调度
|
18天前
|
网络协议 Java 应用服务中间件
记录_centos7离线环境和虚拟机共享文件安装jdk和tomcat(配置环境变量)
记录_centos7离线环境和虚拟机共享文件安装jdk和tomcat(配置环境变量)
11 0
|
18天前
|
Kubernetes Ubuntu Linux
Kubernetes(K8S)集群管理Docker容器(部署篇)
Kubernetes(K8S)集群管理Docker容器(部署篇)
|
18天前
|
存储 Kubernetes Docker
Kubernetes(K8S)集群管理Docker容器(概念篇)
Kubernetes(K8S)集群管理Docker容器(概念篇)
|
19天前
|
Kubernetes Shell 网络安全
Shell脚本快速部署Kubernetes(K8S v1.1版本)集群系统
Shell脚本快速部署Kubernetes(K8S v1.1版本)集群系统
|
19天前
|
Kubernetes Ubuntu Docker
Kubernetes(K8S v1.1版本) 集群管理Docker容器之部署篇
Kubernetes(K8S v1.1版本) 集群管理Docker容器之部署篇
|
19天前
|
Kubernetes 监控 调度
Kubernetes(K8s)与虚拟GPU(vGPU)协同:实现GPU资源的高效管理与利用
本文探讨了如何使用Kubernetes和虚拟GPU(vGPU)实现异构GPU的协同调度。Kubernetes是一个容器编排平台,通过设备插件、资源规格、调度器扩展和节点标签实现GPU资源管理。vGPU技术允许物理GPU资源在多个虚拟机或容器中共享。文章详细介绍了vGPU的部署配置步骤,并提出了GPU资源调度、负载均衡和监控调优的方法。强调虚拟GPU的性能取决于硬件和驱动支持,合理配置能提供高性能计算环境。参考文献包括Kubernetes和NVIDIA官方文档及相关研究论文。