基于Centos7构建Kubernetes平台
一、实验环境
3台centos7的主机:
master 192.168.111.131部署etcd,kube-apiserver,kube-controller-manager,kube-scheduler 4个应用。
node01 192.168.111.130 部署docker,kubelet, kube-proxy 3个应用
node02 192.168.111.129 部署docker,kubelet, kube-proxy 3个应用
二、实验步骤
1. 修改主机名和ip,三个主机网卡均设置为桥接模式,能够联网。
2. 分别修改/etc/hosts文件:
Master配置
#vim /etc/hosts
650) this.width=650;" src="https://s3.51cto.com/oss/201711/08/dfb247d8c8f9030af158b80bc925744c.png" title="图片1.png" alt="dfb247d8c8f9030af158b80bc925744c.png" />Node01
#scp root@192.168.111.128:/etc/hosts /etc/
Node02
#scp root@192.168.111.128:/etc/hosts /etc/
3. 三台均设置防火墙的默认区域为trusted。
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/ee14913cda9e6136612838b5ea8cd43a.png" title="图片2.png" alt="ee14913cda9e6136612838b5ea8cd43a.png" />
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/edc7c7a738f8d14ccddc6e9e3e6d68ff.png" title="图片3.png" alt="edc7c7a738f8d14ccddc6e9e3e6d68ff.png" />
650) this.width=650;" src="https://s3.51cto.com/oss/201711/08/6d0452b50002cb23ba7c252ebcfb2efd.png" title="图片4.png" alt="6d0452b50002cb23ba7c252ebcfb2efd.png" />
4. 三台均关闭selinux.
650) this.width=650;" src="https://s4.51cto.com/oss/201711/08/16ba9914cf30d9bd2c4c230190772897.png" title="图片11.png" alt="16ba9914cf30d9bd2c4c230190772897.png" />
5. 安装docker,并启动。因为master之后还需要做registry仓库,所以也需要安装docker.
#yum install -y docker
650) this.width=650;" src="https://s1.51cto.com/oss/201711/08/0161d2899f51f7d83b2aa3e03cd32c43.png" title="图片5.png" alt="0161d2899f51f7d83b2aa3e03cd32c43.png" />6. 开启路由转发功能:
650) this.width=650;" src="https://s1.51cto.com/oss/201711/08/2b64e2e20511279cdccdb0763a6348b0.png" title="图片6.png" alt="2b64e2e20511279cdccdb0763a6348b0.png" />
7、实现node01和node02之间容器互通。
1)node01和node02新建网桥,并固定网桥ip。
node01上操作:
删除网桥docker0,新建网桥kbr0:
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/a57508bd2557d3e92e3bb233c65e6abc.png" title="图片7.png" alt="a57508bd2557d3e92e3bb233c65e6abc.png" />
新建网桥的配置文件:
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/e1bf0987bc4280d5f3681817a27b24ed.png" title="图片8.png" alt="e1bf0987bc4280d5f3681817a27b24ed.png" />
新建路由文件:route-eth0,eth0为node1上的网卡名。
650) this.width=650;" src="https://s4.51cto.com/oss/201711/08/045e9c72261ffc3bc8be0738f3567640.png" title="图片9.png" alt="045e9c72261ffc3bc8be0738f3567640.png" />
修改docker配置文件,添加-b=kbr0参数
650) this.width=650;" src="https://s2.51cto.com/oss/201711/08/7536fc560d9c7651dae4de5e517a8dfc.png" title="图片10.png" alt="7536fc560d9c7651dae4de5e517a8dfc.png" />reboot重启系统使网桥设置生效。
2)对node02做同样的设置:
新建网桥:
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/902c7272873851e9f4947cf44ee7c250.png" title="图片12.png" alt="902c7272873851e9f4947cf44ee7c250.png" />
新建网桥配置文件:
#scp root@192.168.111.129:/etc/sysconfig/network-scripts/ifcfg-kbr0 /etc/sysconfig/network-scripts/
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/e444a1d8153fb2960b172cb94376b220.png" title="图片13.png" alt="e444a1d8153fb2960b172cb94376b220.png" />
创建路由文件:
650) this.width=650;" src="https://s1.51cto.com/oss/201711/08/04dcf0b497c6e0963068bf46571a89ec.png" title="图片14.png" alt="04dcf0b497c6e0963068bf46571a89ec.png" />
修改docker 配置文件:
#vim /etc/sysconfig/docker
650) this.width=650;" src="https://s1.51cto.com/oss/201711/08/3fda716a39723b9a3f371e9db6b7e850.png" title="图片15.png" alt="3fda716a39723b9a3f371e9db6b7e850.png" />重启系统reboot.
3)系统启动之后,查看网卡信息和路由信息。
#ifconfig
650) this.width=650;" src="https://s2.51cto.com/oss/201711/08/423107baf316d4c890712cb519425e44.png" title="图片16.png" alt="423107baf316d4c890712cb519425e44.png" />650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/f89d381183e623b7c2973c4aaab0e1e5.png" title="图片17.png" alt="f89d381183e623b7c2973c4aaab0e1e5.png" />4)验证两个宿主机间容器的互通性:
node01上运行一个容器:
650) this.width=650;" src="https://s4.51cto.com/oss/201711/08/b4515d46dbd4bbab91735bbb122548bc.png" title="图片18.png" alt="b4515d46dbd4bbab91735bbb122548bc.png" />node02上运行一个容器:
650) this.width=650;" src="https://s1.51cto.com/oss/201711/08/03624400d8d502816adc9f556ff6ec59.png" title="图片19.png" alt="03624400d8d502816adc9f556ff6ec59.png" />容器间互ping:
#docker run -it docker.io/centos:centos6
# yum install -y iputils 进入容器安装
650) this.width=650;" src="https://s4.51cto.com/oss/201711/08/7a48343bf4af234450f5292c0918faa1.png" title="图片20.png" alt="7a48343bf4af234450f5292c0918faa1.png" />
#dcoker run -it docker.io/centos:centos6
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/190ef4aa5c21c3b94e4b092357a3bf16.png" title="图片21.png" alt="190ef4aa5c21c3b94e4b092357a3bf16.png" />
master上部署应用:
1、安装etcd.
650) this.width=650;" src="https://s1.51cto.com/oss/201711/08/b2ec6adccb7b54d00444701bfb8bec39.png" title="图片22.png" alt="b2ec6adccb7b54d00444701bfb8bec39.png" />
查看etcd配置文件:
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/3547be473cfe7605b646fa6b7fb7cca0.png" title="图片23.png" alt="3547be473cfe7605b646fa6b7fb7cca0.png" />
2、修改配置文件:
650) this.width=650;" src="https://s1.51cto.com/oss/201711/08/117b4cc2efc38c06753eb82eef6843fb.png" title="图片24.png" alt="117b4cc2efc38c06753eb82eef6843fb.png" />3、启动etcd服务:
650) this.width=650;" src="https://s3.51cto.com/oss/201711/08/44aafb9685afc5b045e900dbfdc2beb9.png" title="图片25.png" alt="44aafb9685afc5b045e900dbfdc2beb9.png" />查看服务端口号:
650) this.width=650;" src="https://s3.51cto.com/oss/201711/08/7cbbdbf3aa69eb516490d5e9c12b22d7.png" title="图片26.png" alt="7cbbdbf3aa69eb516490d5e9c12b22d7.png" />4、部署k8s-master组件(apiserver+controller-manager+scheduler)
安装软件:
#yum install -y kubernetes-master
650) this.width=650;" src="https://s1.51cto.com/oss/201711/08/65bdc1f6c20f9fb86af9c59439d7d0c9.png" title="图片27.png" alt="65bdc1f6c20f9fb86af9c59439d7d0c9.png" />
查看配置文件:
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/9f6e50472f9b1cb019cff62546d2b6a0.png" title="图片28.png" alt="9f6e50472f9b1cb019cff62546d2b6a0.png" />
5、修改配置文件:
#vim /etc/kubernetes/config
650) this.width=650;" src="https://s2.51cto.com/oss/201711/08/f8160a12218c224b2557aa95d513e933.png" title="图片29.png" alt="f8160a12218c224b2557aa95d513e933.png" />解释:
KUBE_LOGTOSTDERR 日志设置
KUBE_LOG_KEVEL 日志级别设置
KUBE_ALLOW_PRIV 是否允许运行特权容器
KUBE_MASTER 主节点的地址,主要为replication controller和scheduler及kubelet可以顺利找到apiserver
#vim /etc/kubernetes/apiserver
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/f34a7f7536585e4053b5260459eaaa22.png" title="图片30.png" alt="f34a7f7536585e4053b5260459eaaa22.png" />KUBE_API_ADDRESS 监听的接口,如果配置为127.0.0.1则只监听localhost,配置为0.0.0.0会监听所有接口,这里配置为0.0.0.0。
KUBE_API_PORT="--port=8080" apiserver的监听端口,默认8080,不用修改。
KUBELET_PORT="--kubelet_port=10250" kubelet监听的端口,默认10250,无需修改
KUBE_ETCD_SERVERS //指定etcd节点的地址
KUBE_SERVICE_ADDRESSES 这个是设置今后运行Service所在的ip网段
KUBE_API_ARGS=”--secure-port=0” 默认是要求https安全通信,”--secure-port=0”则不要求https安全通信
注:这里需要注意原来KUBE_ADMISSION_CONTROL默认包含的要删掉,不然启动API server的时候会报错
6、启动服务:
650) this.width=650;" src="https://s3.51cto.com/oss/201711/08/5b8dd14a28f21aef29433d5e5d1f4eb8.png" title="图片31.png" alt="5b8dd14a28f21aef29433d5e5d1f4eb8.png" />
查看服务端口:
650) this.width=650;" src="https://s2.51cto.com/oss/201711/08/63d2ba2c998cbfa73ba87301f2fe8d17.png" title="图片32.png" alt="63d2ba2c998cbfa73ba87301f2fe8d17.png" />部署node主机:
1、安装kubernetes-node软件:
#yum install -y kubernetes-node
650) this.width=650;" src="https://s4.51cto.com/oss/201711/08/cc37a364afc6bac7462120e9047dd1c4.png" title="图片33.png" alt="cc37a364afc6bac7462120e9047dd1c4.png" />
2、修改配置文件:
#vim /etc/kubernetes/config
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/960aba70e03939223e9a4bdb51eb7102.png" title="图片34.png" alt="960aba70e03939223e9a4bdb51eb7102.png" />
#vim /etc/kubernetes/kubelet
650) this.width=650;" src="https://s1.51cto.com/oss/201711/08/4617ee2516c2d346177fcef4e549200c.png" title="图片35.png" alt="4617ee2516c2d346177fcef4e549200c.png" />配置项KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"作用k8s创建pod的时候需要起一个基本容器,所以node节点要能连网。也可以自己搭建一个私有仓库,将pod-infrastructure:latest镜像上传到私有仓库中,修改此配置文件的地址就可以。
3、启动服务:
650) this.width=650;" src="https://s2.51cto.com/oss/201711/08/941a130a6a594b6f7fdf00339db79b30.png" title="图片36.png" alt="941a130a6a594b6f7fdf00339db79b30.png" />
4、在node02主机上重复上面的操作。
5、在master上检查node 状态:
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/fd645fb0fefffb94c2f567d49b8fd16a.png" title="图片37.png" alt="fd645fb0fefffb94c2f567d49b8fd16a.png" />
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/383c21067ee0922d81304ac0976cfe63.png" title="图片38.png" alt="383c21067ee0922d81304ac0976cfe63.png" /> 建私有仓库:
Kubernetes管理容器如果是第一次操作,可能会有一定的等待时间,这是因为第一次下载images需要一段时间。如果本地没有docker registry,要确保节点能访问互联网,所以我们可以搭建一个私有仓库,由私有仓库提供所需要的镜像,
本实验环境中用kubernetes同时作为registry。
1、导入镜像:
650) this.width=650;" src="https://s3.51cto.com/oss/201711/08/0fb4f66daebd207bc5de445289b9af7a.png" title="图片39.png" alt="0fb4f66daebd207bc5de445289b9af7a.png" />2、基于私有仓库镜像运行容器
650) this.width=650;" src="https://s2.51cto.com/oss/201711/08/66b1d36919b589641fa594d6540523f4.png" title="图片40.png" alt="66b1d36919b589641fa594d6540523f4.png" />3、本地访问私有仓库:
650) this.width=650;" src="https://s4.51cto.com/oss/201711/08/a66732ae83ba48d1a6dd2df0d8093eb5.png" title="图片41.png" alt="a66732ae83ba48d1a6dd2df0d8093eb5.png" />
4、上传镜像到私有仓库。
导入之后用到的镜像到本地
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/2a4a51981a656c6eea61a75e6a1fb21e.png" title="图片42.png" alt="2a4a51981a656c6eea61a75e6a1fb21e.png" />给基础镜像打个标签:
650) this.width=650;" src="https://s4.51cto.com/oss/201711/08/5e927423385cf9e78bd916e03e296181.png" title="图片43.png" alt="5e927423385cf9e78bd916e03e296181.png" />
在三台主机上均修改docker 的配置文件,指定私有仓库的url.
650) this.width=650;" src="https://s2.51cto.com/oss/201711/08/b43a7c89dfa67d28876f2235f5e6816d.png" title="图片44.png" alt="b43a7c89dfa67d28876f2235f5e6816d.png" />重启docker服务:
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/906ca0c0e995e10ebce97bf663f4f4c2.png" title="图片45.png" alt="906ca0c0e995e10ebce97bf663f4f4c2.png" />
上传镜像到私有仓库并查看:
650) this.width=650;" src="https://s1.51cto.com/oss/201711/08/022afb8d27f67af86fe6c5812ecce871.png" title="图片46.png" alt="022afb8d27f67af86fe6c5812ecce871.png" />测试,下载刚才上传的镜像:
650) this.width=650;" src="https://s4.51cto.com/oss/201711/08/deb2b7a58019d98c7c9829ccfd2f45b2.png" title="图片47.png" alt="deb2b7a58019d98c7c9829ccfd2f45b2.png" />650) this.width=650;" src="https://s3.51cto.com/oss/201711/08/bac7fe1875675269eebb0b1d7392dea1.png" title="图片48.png" alt="bac7fe1875675269eebb0b1d7392dea1.png" />部署web应用
1、为了部署pod的时候,下载镜像速度快点,把node节点主机配置文件中的pod镜像下载地址修改为本地私有仓库地址:
#vim /etc/kubernetes/kubelet
650) this.width=650;" src="https://s2.51cto.com/oss/201711/08/7c79026c79bda0eb2df7014d320e461c.png" title="图片49.png" alt="7c79026c79bda0eb2df7014d320e461c.png" />#systemctl restart kubelet.service
2、创建apache-rc.yaml文件,根据文件内容创建rc和pod。
650) this.width=650;" src="https://s3.51cto.com/oss/201711/08/859e50cb0790c2a919b501df31549f2f.png" title="图片50.png" alt="859e50cb0790c2a919b501df31549f2f.png" />
注意:必须按照此格式编写否则会报错
#kubectl create -f apache-rc.yaml
error: error validating "apache-rc.yaml": error validating data: found invalid field spec for v1.ReplicationControllerSpec; if you choose to ignore these errors, turn validation off with --validate=false
650) this.width=650;" src="https://s1.51cto.com/oss/201711/08/8922902afa67688ccce834f574d00643.png" title="图片51.png" alt="8922902afa67688ccce834f574d00643.png" />
查看pods状态:
650) this.width=650;" src="https://s1.51cto.com/oss/201711/08/80f19fdbe81b01aec08306d69d9afd9d.png" title="图片52.png" alt="80f19fdbe81b01aec08306d69d9afd9d.png" />
查看pods详细状态:
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/18d8cc6694b9bee4754eac30b4d554f5.png" title="图片53.png" alt="18d8cc6694b9bee4754eac30b4d554f5.png" />附:删除pod
kubectl delete pod podName
由于设置了两份副本,所以删除pod的时候,k8s会迅速起另外一个一模一样的pod以保持副本数量为2不变。
要彻底删除pod,只能删除创建它的replication controller
查看replication controller
kubectl get rc
删除replication controller
kubectl delete rc rcName
删除rc之后,其创建的pod会一并删除
3、部署节点内部可访问的apache service
Server的type有ClusterIP和 NodePort之分,缺省是ClusterIp,这种类型的service只能在集群内部访问
创建配置文件:
#vim apache-service-clusterip.yaml
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/918b925883281d36b5503a062c820c8a.png" title="图片54.png" alt="918b925883281d36b5503a062c820c8a.png" />
创建service:
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/b2555de5ee8b225ff258c6d9ada2a1eb.png" title="图片55.png" alt="b2555de5ee8b225ff258c6d9ada2a1eb.png" />
查看service状态:
650) this.width=650;" src="https://s5.51cto.com/oss/201711/08/928c44f47dc3015f42d1969c5bf498d4.png" title="图片56.png" alt="928c44f47dc3015f42d1969c5bf498d4.png" />
验证apache服务:(在node节点执行)
650) this.width=650;" src="https://s2.51cto.com/oss/201711/08/d8f57ac4a6601bb5d896b09638e3d5dc.png" title="图片57.png" alt="d8f57ac4a6601bb5d896b09638e3d5dc.png" />
4、部署外部可访问的apache service。
创建nodeport类型的service . 这种类型的Service在集群外部是可以访问
#vim apache-nodeport-service.yaml
650) this.width=650;" src="https://s2.51cto.com/oss/201711/08/ac10bb24a5849013e3c00de39e5cb1af.png" title="图片58.png" alt="ac10bb24a5849013e3c00de39e5cb1af.png" />
创建service:
650) this.width=650;" src="https://s2.51cto.com/oss/201711/08/716e73c2122df42822b97b9579994383.png" title="图片59.png" alt="716e73c2122df42822b97b9579994383.png" />
查看service:
650) this.width=650;" src="https://s2.51cto.com/oss/201711/08/c81450e7452d2a37414e9c5cdc5abf7c.png" title="图片60.png" alt="c81450e7452d2a37414e9c5cdc5abf7c.png" />验证service的可访问性:
外部访问:
650) this.width=650;" src="https://s1.51cto.com/oss/201711/08/a4786eb847c076cea79de58f367bad67.png" title="图片61.png" alt="a4786eb847c076cea79de58f367bad67.png" />650) this.width=650;" src="https://s4.51cto.com/oss/201711/08/dcfad9ef4b9b479aecf6eac2a7d02d79.png" title="图片62.png" alt="dcfad9ef4b9b479aecf6eac2a7d02d79.png" />
内部访问:
650) this.width=650;" src="https://s1.51cto.com/oss/201711/08/92c7ae5dd5b86a6754d72cb94d5ad06d.png" title="图片63.png" alt="92c7ae5dd5b86a6754d72cb94d5ad06d.png" />到此为止kubernetes 集群搭建完毕!!!
本文出自 “IT大本营” 博客,谢绝转载!
基于 ?CentOS 7 搭建kubernetes集群
原文地址:http://itdby.blog.51cto.com/13153042/1980018