注:由于Kubernetes对于支持RHEL7及以上的版本,故本次采用Fedora 22来安装。
本次使用2台主机,2台主机名分别为fed1和fed2。第一台主机,fed1,将作为kubernetes的主节点。这台主机将运行API服务和controller。剩下的主机fed2将作为minions并运行kubelet,proxy和docker。
Hosts:
fed1=10.x.x.241
fed2=10.x.x.240
Versions:
# cat /etc/redhat-release
Fedora release 22(Rawhide)
#rpm -q etcd kubernetes
etcd-0.4.5-11.fc22.x86_64
kubernetes-0.0.0.8.gitc78206d.fc22.x86_64
1.启动所有主机上的copr repo
# yum install -y dnf dnf-plugins-core
# dnf copr enable walters/atomic-next
# yum repolist walters-atomic-next/x86_64
2.在所有主机上(fed1,fed2)上安装kubernetes
# yum install -y kubernets
3.选择一个主机explore这些包
# rpm -qi kubernetes
# rpm -qc kubernetes
# rpm -ql kubernetes
# rpm -ql etcd
# rpm -qi etcd
4.配置fed1
输出etcd和kube的主节点变量,以便服务知道到哪里去。
# export KUBE_ETCD_SERVERS=10.x.x.241
# export KUBE_MASTER=10.x.x.241
这些是服务文件:apiserver,etcd和controller。他们已不再是分发的包。
先做一个备份,然后看看有什么:
# cp /usr/lib/systemd/system/kubernetes-apiserver.service{,.orig}
# cp /usr/lib/systemd/system/kubernetes-controller-manager.service{,.orig}
# cp /usr/lib/systemd/system/etcd.service{,.orig}
# cat /usr/lib/systemd/system/kubernetes-apiserver.service
[Unit]
Description=Kubernetes API Server
[Service]
ExecStart=/usr/bin/kubernetes-apiserver --logtostderr=true -etcd_servers=http://localhost:4001 -address=127.0.0.1 -port=8080 -machines=10.x.x.240
[Install]
WantedBy=multi-user.target
# cat /usr/lib/systemd/system/kubernetes-controller-manager.service
[Unit]
Description=Kubernetes Controller Manager
[Service]
ExecStart=/usr/bin/kubernetes-controller-manager --logtostderr=true --etcd_servers=$KUBE_ETC_SERVERS --master=$KUBE_MASTER
[Install]
WantedBy=multi-user.target
# cat /usr/lib/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target
[Service]
Type=simple
# etc logs to the journal directly, suppress double logging
StandardOutput=null
WorkingDirectory=/var/lib/etcd
ExecStart=/usr/bin/etcd
[Install]
WantedBy=multi-user.target
在fed1上启动相应的服务:
# systemctl daemon-reload
# systemctl restart etcd
# systemctl status etcd
# systemctl enable etcd
# systemctl restart kubernetes-apiserver.service
# systemctl status kubernetes-apiserver.service
# systemctl enable kubernetes-apiserver.service
# systemctl restart kubernetes-controller-manager
# systemctl status kubernetes-controller-manager
# systemctl enable kubernetes-controller-manager
在主节点(fed1)上测试etcd,并确保其能够工作:
curl -L http://127.0.0.1:4001/v2/keys/mykey -XPUT -d value="this is awesome"
curl -L http://127.0.0.1:4001/v2/keys/mykey
curl -L http://127.0.0.1:4001/version
为主节点(fed1)上的etcd和kubernetes API服务开放端口:
# iptables -I INPUT -p tcp --dport 4001 -j ACCEPT
# iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
查看这些服务运行在的端口:
# netstat -tulnp
5.配置fed2
这些是服务文件,他们已不再是分发的包。
先做一个备份,然后看看有什么:
# cp /usr/lib/systemd/system/kubernetes-kubelet.service{,.orig}
# cp /usr/lib/systemd/system/kubernetes-proxy.service{,.orig}
# cat /usr/lib/systemd/system/kubernetes-kubelet.service
[Unit]
Description=Kubernetes Kubelet
[Service]
ExecStart=/usr/bin/kubernetes-kubelet --logtostderr=true -etcd_servers=http://10.x.x.241:4001 -address=10.x.x.240 -hostname_override=10.x.x.240
[Install]
WantedBy=multi-user.target
# cat /usr/lib/systemd/system/kubernetes-proxy.service
[Unit]
Description=Kubernetes Proxy
[Service]
ExecStart=/usr/bin/kubernetes-proxy --logtostderr=true -etcd_servers=http://10.x.x.241:4001
[Install]
WantedBy=multi-user.target
在fed2上启用相应的服务:
# systemctl daemon-reload
# systemctl enable kubernetes-proxy.service
# systemctl restart kubernetes-proxy.service
# systemctl status kubernetes-proxy.service
# systemctl enable kubernetes-kubelet.service
# systemctl restart kubernetes-kubelet.service
# systemctl status kubernetes-kubelet.service
# systemctl restart docker
# systemctl status docker
# systemctl enable docker
查看这些服务在哪些端口运行:
# netstat -tulnp
为从节点(fed2)上的etcd和kubernetes API服务开放端口:
# iptables -I INPUT -p tcp --dport 10250 -j ACCEPT
现在2个服务器都设置好了,可以开始一个示例应用了。在这样的情况下,来部署一个web服务器在fed2上。通过在fed1上的root目录下,制作一个叫apache.json的文件来开始部署。文件如下:
# cat apache.json
{
"id": "apache",
"desiredState": {
"manifest": {
"version": "v1beta1",
"id": "apache-1",
"containers": [{
"name": "master",
"image": "fedora/apache",
"ports": [{
"containerPort": 80,
"hostPort": 80
}]
}]
}
},
"labels": {
"name": "apache"
}
}
这个json文件描述应用环境的属性。例如,他给出了一个"id","name","ports"和"image"。由于Fedora/Apache镜像确实在我们的环境中还不存在,它会自动pull下来作为部署过程的一部分。
现在通过apache.json文件来部署fedora/apache镜像。
# /usr/bin/kubernetes-kubecfg -c apache.json create pods
可以用这些命令监控的操作进度:
在fed1上:
# journalctl -f -xn -u kubernetes-apiserver -u etcd -u kubernetes-kubelet -u docker
在fed2上:
# journalctl -f -xn -u kubernetes-kubelet.service -u kubernetes-proxy -u docker
当成功的时候,会出现如下的结果:
# /usr/bin/kubernetes-kubecfg -c apache.json create pods
I0730 15:13:48.535653 27880 request.go:220] Waiting for completion of /operations/8
I0730 15:14:08.538052 27880 request.go:220] Waiting for completion of /operations/8
I0730 15:14:28.539936 27880 request.go:220] Waiting for completion of /operations/8
I0730 15:14:48.542192 27880 request.go:220] Waiting for completion of /operations/8
I0730 15:15:08.543649 27880 request.go:220] Waiting for completion of /operations/8
I0730 15:15:28.545475 27880 request.go:220] Waiting for completion of /operations/8
I0730 15:15:48.547008 27880 request.go:220] Waiting for completion of /operations/8
I0730 15:16:08.548512 27880 request.go:220] Waiting for completion of /operations/8
Name Image(s) Host Labels
---------- ---------- ---------- ----------
apache fedora/apache / name=apache
在pod部署完成之后,也可以列出pod:
# /usr/bin/kubernetes-kubecfg list pods
Name Image(s) Host Labels
---------- ---------- ---------- ----------
apache fedora/apache 10.x.x.240/ name=apache
redis-master-2 dockerfile/redis 10.x.x.240/ name=redis-master
也可以得到更多关于pod的信息:
# /usr/bin/kubernetes-kubecfg -json get pods/apache
最后,在fed2上,检查服务是否可用:
# docker images | grep fedora
fedora/apache latest 6927a389deb6 10 weeks ago 450.6 MB
# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d5871fc9af31 fedora/apache:latest /run-apache.sh 9 minutes ago Up 9 minutes k8s--master--apache--8d060183
# curl http://localhost
Apache
最后,删除容器:
/usr/bin/kubernetes-kubecfg -h http://127.0.0.1:8080 delete /pods/apache
Linux下搭建HA高可用集群:http://www.linuxdiyf.com/linux/12513.html
Linux下LVS搭建负载均衡集群:http://www.linuxdiyf.com/linux/12508.html
CentOS 6.5高可用集群LVS+Keepalived(DR模式):http://www.linuxdiyf.com/linux/11464.html
Linux系统高可用集群软件之HeartBeat:http://www.linuxdiyf.com/linux/10348.html
Linux系统高可用集群软件之Keepalived:http://www.linuxdiyf.com/linux/10347.html