启动kubernetes服务
# systemctl daemon-reload# systemctl enable docker.service kubelet.service kube-proxy.service
# systemctl start docker.service kubelet.service kube-proxy.service
3.校验安装(在master主机操作,或可访问master主机8080端口的client api主机)
1) kubernetes常用命令
# kubectl get minions #查查看minion主机# kubectl get pods #查看pods清单
# kubectl get services 或 kubectl get services -o json #查看service清单
# kubectl get replicationControllers #查看replicationControllers清单
# for i in `kubectl get pod|tail -n +2|awk '{print $1}'`; do kubectl delete pod $i; done #删除所有pods
或者通过Server api for REST方式(推荐,及时性更高):
# curl -s -L :8080/api/v1beta1/version | Python -mjson.tool #查看kubernetes版本# curl -s -L :8080/api/v1beta1/pods | python -mjson.tool #查看pods清单
# curl -s -L :8080/api/v1beta1/replicationControllers | python -mjson.tool #查看replicationControllers清单
# curl -s -L :8080/api/v1beta1/minions | python -m json.tool #查查看minion主机
# curl -s -L :8080/api/v1beta1/services | python -m json.tool #查看service清单
注:在新版Kubernetes中,所有的操作命令都整合至kubectl,包括kubecfg、kubectl.sh、kubecfg.sh等
2)创建测试pod单元
# /home/kubermange/pods && cd /home/kubermange/pods
# vi apache-pod.json
{"id": "Fedoraapache",
"kind": "Pod",
"apiVersion": "v1beta1",
"desiredState": {
"manifest": {
"version": "v1beta1",
"id": "fedoraapache",
"containers": [{
"name": "fedoraapache",
"image": "fedora/apache",
"ports": [{
"containerPort": 80,
"hostPort": 8080
}]
}]
}
},
"labels": {
"name": "fedoraapache"
}
}
# kubectl create -f apache-pod.json
# kubectl get pod
NAME IMAGE(S) HOST LABELS STATUSfedoraapache fedora/apache 192.168.1.202/ name=fedoraapache Running
启动浏览器访问:8080/,对应的服务端口切记在iptables中已添加。效果图如下:
观察kubernetes在etcd中的数据存储结构
观察单个pods的数据存储结构,以json的格式存储。
二、实战操作
任务:通过Kubernetes创建一个LNMP架构的服务集群,以及观察其负载均衡,涉及镜像“yorko/webserver”已经push至registry.hub.docker.com,大家可以通过“docker pull yorko/webserver”下载。
# mkdir -p /home/kubermange/replication && mkdir -p /home/kubermange/service# cd /home/kubermange/replication
1. 创建一个replication,本例直接在replication模板中创建pod并复制,也可独立创建pod再通过replication来复制。
【replication/lnmp-replication.json】
{"id": "webserverController",
"kind": "ReplicationController",
"apiVersion": "v1beta1",
"labels": {"name": "webserver"},
"desiredState": {
"replicas": 2,
"replicaSelector": {"name": "webserver_pod"},
"podTemplate": {
"desiredState": {
"manifest": {
"version": "v1beta1",
"id": "webserver",
"volumes": [
{"name":"httpconf", "source":{"hostDir":{"path":"/etc/httpd/conf"}}},
{"name":"httpconfd", "source":{"hostDir":{"path":"/etc/httpd/conf.d"}}},
{"name":"httproot", "source":{"hostDir":{"path":"/data"}}}
],
"containers": [{
"name": "webserver",
"image": "yorko/webserver",
"command": ["/bin/sh", "-c", "/usr/bin/supervisord -c /etc/supervisord.conf"],
"volumeMounts": [
{"name":"httpconf", "mountPath":"/etc/httpd/conf"},
{"name":"httpconfd", "mountPath":"/etc/httpd/conf.d"},
{"name":"httproot", "mountPath":"/data"}
],
"cpu": 100,
"memory": 50000000,
"ports": [{
"containerPort": 80,
},{
"containerPort": 22,
}]
}]
}
},
"labels": {"name": "webserver_pod"},
},
}
}
执行创建命令
#kubectl create -f lnmp-replication.json
观察生成的pod副本清单: