本文主要参考。
物理环境和网络
理论上,Kubernetes将服务放在两台主机上。第一台主机,centos-master,是 Kubernetes 主节点,主要运行kube-apiserver, kube-controller-manager, 和 kube-scheduler,以及 etcd。另一台主机,centos-minion,是工作节点,主要运行 kubelet, proxy, cadvisor 和 docker。
本文简单处理,物理机只有1台,分别配置 host 如下。
1 2
| 192.168.2.202 centos-master 192.168.2.202 centos-minion
|
centos-master 操作系统是 Centos 7。
yum 源配置
增加 Kubernetes 的 yum 源,sudo vi /etc/yum.repos.d/virt7-docker-common-release.repo
,
1 2 3 4
| [virt7-docker-common-release] name=virt7-docker-common-release baseurl=http://cbs.centos.org/repos/virt7-docker-common-release/x86_64/os/ gpgcheck=0
|
安装 Kubernetes
注意:Kubernetes 目前尚不能支持 Docker 的最新版,如果本机已经安装 Docker,建议先删除 Docker。
由于已经配置 yum 源,安装就比较简单,
节点配置
在所有主机上,做如下配置,vi /etc/kubernetes/config
,
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| KUBE_LOGTOSTDERR="--logtostderr=true" KUBE_LOG_LEVEL="--v=0" KUBE_ALLOW_PRIV="--allow-privileged=false" KUBE_MASTER="--master=http://centos-master:8080" KUBE_ETCD_SERVERS="--etcd-servers=http://centos-master:2379"
|
注意,上述的 KUBE_ETCD_SERVERS 参数要根据 etcd 的实际情况来配置,etcd 的配置请在 /etc/etcd/etcd.conf
。
在主节点和工作节点上禁用防火墙,因为 docker 并不擅长防火墙规则管理,
1 2
| systemctl disable iptables-services firewalld systemctl stop iptables-services firewalld
|
主节点配置和启动
在主节点上配置如下,vi /etc/kubernetes/apiserver
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| KUBE_API_ADDRESS="--address=0.0.0.0" KUBE_API_PORT="--port=8080" KUBE_MASTER="--master=http://centos-master:8080" KUBELET_PORT="--kubelet-port=10250" KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16" KUBE_API_ARGS=""
|
然后用以下命令启动,
1 2 3 4 5
| for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do systemctl restart $SERVICES systemctl enable $SERVICES systemctl status $SERVICES done
|
将以上脚本保存在脚本start_kubernetes.sh
中。以下是运行输出,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| [root@Centos-L410 kubernetes]# ./start_kubernetes.sh ● etcd.service - Etcd Server Loaded: loaded (/usr/lib/systemd/system/etcd.service; enabled; vendor preset: disabled) Active: active (running) since 日 2016-02-28 18:04:49 CST; 188ms ago Main PID: 24669 (etcd) CGroup: /system.slice/etcd.service └─24669 /usr/bin/etcd 2月 28 18:04:49 Centos-L410 etcd[24669]: election = 1000ms 2月 28 18:04:49 Centos-L410 etcd[24669]: snapshot count = 10000 2月 28 18:04:49 Centos-L410 etcd[24669]: advertise client URLs = http://centos-master:2379 2月 28 18:04:49 Centos-L410 etcd[24669]: restarting member ce2a822cea30bfca in cluster 7e27652122e8b2ae at comm... 8613 2月 28 18:04:49 Centos-L410 etcd[24669]: ce2a822cea30bfca became follower at term 7 2月 28 18:04:49 Centos-L410 etcd[24669]: newRaft ce2a822cea30bfca [peers: [], term: 7, commit: 8613, applied: 0...m: 7] 2月 28 18:04:49 Centos-L410 etcd[24669]: starting server... [version: 2.2.2, cluster version: to_be_decided] 2月 28 18:04:49 Centos-L410 systemd[1]: Started Etcd Server. 2月 28 18:04:49 Centos-L410 etcd[24669]: added local member ce2a822cea30bfca [http://localhost:2380 http://loca...8b2ae 2月 28 18:04:49 Centos-L410 etcd[24669]: set the initial cluster version to 2.2 Hint: Some lines were ellipsized, use -l to show in full. ● kube-apiserver.service - Kubernetes API Server Loaded: loaded (/usr/lib/systemd/system/kube-apiserver.service; enabled; vendor preset: disabled) Active: active (running) since 日 2016-02-28 18:04:52 CST; 152ms ago Docs: https://github.com/GoogleCloudPlatform/kubernetes Main PID: 24726 (kube-apiserver) CGroup: /system.slice/kube-apiserver.service └─24726 /usr/bin/kube-apiserver 2月 28 18:04:50 Centos-L410 systemd[1]: Starting Kubernetes API Server... 2月 28 18:04:52 Centos-L410 systemd[1]: Started Kubernetes API Server. ● kube-controller-manager.service - Kubernetes Controller Manager Loaded: loaded (/usr/lib/systemd/system/kube-controller-manager.service; enabled; vendor preset: disabled) Active: active (running) since 日 2016-02-28 18:04:52 CST; 207ms ago Docs: https://github.com/GoogleCloudPlatform/kubernetes Main PID: 24777 (kube-controller) CGroup: /system.slice/kube-controller-manager.service └─24777 /usr/bin/kube-controller-manager 2月 28 18:04:52 Centos-L410 systemd[1]: Started Kubernetes Controller Manager. 2月 28 18:04:52 Centos-L410 systemd[1]: Starting Kubernetes Controller Manager... 2月 28 18:04:52 Centos-L410 kube-controller-manager[24777]: I0228 18:04:52.714705 24777 plugins.go:71] No cloud...ed. 2月 28 18:04:52 Centos-L410 kube-controller-manager[24777]: I0228 18:04:52.722761 24777 nodecontroller.go:133] ...er. 2月 28 18:04:52 Centos-L410 kube-controller-manager[24777]: E0228 18:04:52.722944 24777 controllermanager.go:27...er. Hint: Some lines were ellipsized, use -l to show in full. ● kube-scheduler.service - Kubernetes Scheduler Plugin Loaded: loaded (/usr/lib/systemd/system/kube-scheduler.service; enabled; vendor preset: disabled) Active: active (running) since 日 2016-02-28 18:04:52 CST; 187ms ago Docs: https://github.com/GoogleCloudPlatform/kubernetes Main PID: 24829 (kube-scheduler) CGroup: /system.slice/kube-scheduler.service └─24829 /usr/bin/kube-scheduler 2月 28 18:04:52 Centos-L410 systemd[1]: Started Kubernetes Scheduler Plugin. 2月 28 18:04:52 Centos-L410 systemd[1]: Starting Kubernetes Scheduler Plugin...
|
工作节点配置和启动
在工作节点上配置如下,vi /etc/kubernetes/kubelet
,
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| KUBELET_ADDRESS="--address=0.0.0.0" KUBELET_PORT="--port=10250" KUBELET_HOSTNAME="--hostname-override=centos-minion" KUBELET_API_SERVER="--api-servers=http://centos-master:8080" KUBELET_ARGS=""
|
然后用以下命令启动,
1 2 3 4 5
| for SERVICES in kube-proxy kubelet docker; do systemctl restart $SERVICES systemctl enable $SERVICES systemctl status $SERVICES done
|
将以上脚本保存在脚本start_kubernetes_work.sh
中。以下是运行输出,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| [root@Centos-L410 kubernetes] ● kube-proxy.service - Kubernetes Kube-Proxy Server Loaded: loaded (/usr/lib/systemd/system/kube-proxy.service; enabled; vendor preset: disabled) Active: active (running) since 日 2016-02-28 18:05:15 CST; 307ms ago Docs: https://github.com/GoogleCloudPlatform/kubernetes Main PID: 24880 (kube-proxy) CGroup: /system.slice/kube-proxy.service ├─24880 /usr/bin/kube-proxy --logtostderr=true --v=0 --master=http://centos-master:8080 └─24939 iptables -w -C KUBE-PORTALS-HOST -t nat -m comment --comment default/kubernetes:https -p tcp -m tc... 2月 28 18:05:15 Centos-L410 systemd[1]: Started Kubernetes Kube-Proxy Server. 2月 28 18:05:15 Centos-L410 systemd[1]: Starting Kubernetes Kube-Proxy Server... 2月 28 18:05:15 Centos-L410 kube-proxy[24880]: E0228 18:05:15.932316 24880 server.go:324] Not trying iptables...found 2月 28 18:05:15 Centos-L410 kube-proxy[24880]: E0228 18:05:15.966946 24880 proxier.go:193] Error removing pur...ctory 2月 28 18:05:15 Centos-L410 kube-proxy[24880]: Try `iptables -h' or 'iptables --help' for more information. 2月 28 18:05:15 Centos-L410 kube-proxy[24880]: E0228 18:05:15.970419 24880 proxier.go:197] Error removing pur...ctory 2月 28 18:05:15 Centos-L410 kube-proxy[24880]: Try `iptables -h' or 'iptables --help' for more information. Hint: Some lines were ellipsized, use -l to show in full. ● kubelet.service - Kubernetes Kubelet Server Loaded: loaded (/usr/lib/systemd/system/kubelet.service; enabled; vendor preset: disabled) Active: active (running) since 日 2016-02-28 18:05:17 CST; 282ms ago Docs: https://github.com/GoogleCloudPlatform/kubernetes Main PID: 25036 (kubelet) CGroup: /system.slice/kubelet.service └─25036 /usr/bin/kubelet --logtostderr=true --v=0 --api-servers=http://centos-master:8080 --address=0.0.0.... 2月 28 18:05:17 Centos-L410 systemd[1]: Started Kubernetes Kubelet Server. 2月 28 18:05:17 Centos-L410 systemd[1]: Starting Kubernetes Kubelet Server... ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled) Active: active (running) since 日 2016-02-28 18:05:18 CST; 245ms ago Docs: http://docs.docker.com Main PID: 25120 (docker) CGroup: /system.slice/docker.service └─25120 /usr/bin/docker daemon 2月 28 18:05:18 Centos-L410 docker[25120]: time="2016-02-28T18:05:18.069063505+08:00" level=info msg="Option De...idge" 2月 28 18:05:18 Centos-L410 docker[25120]: time="2016-02-28T18:05:18.069114767+08:00" level=info msg="Option De...idge" 2月 28 18:05:18 Centos-L410 docker[25120]: time="2016-02-28T18:05:18.081358591+08:00" level=warning msg="Runnin...us 1" 2月 28 18:05:18 Centos-L410 docker[25120]: time="2016-02-28T18:05:18.089480121+08:00" level=info msg="Firewalld...alse" 2月 28 18:05:18 Centos-L410 docker[25120]: time="2016-02-28T18:05:18.386325219+08:00" level=info msg="Loading c...art." 2月 28 18:05:18 Centos-L410 docker[25120]: .......... 2月 28 18:05:18 Centos-L410 docker[25120]: time="2016-02-28T18:05:18.391606225+08:00" level=info msg="Loading c...one." 2月 28 18:05:18 Centos-L410 docker[25120]: time="2016-02-28T18:05:18.391633742+08:00" level=info msg="Daemon ha...tion" 2月 28 18:05:18 Centos-L410 docker[25120]: time="2016-02-28T18:05:18.391658744+08:00" level=info msg="Docker da...entos 2月 28 18:05:18 Centos-L410 systemd[1]: Started Docker Application Container Engine. Hint: Some lines were ellipsized, use -l to show in full.
|
检查 Kubernetes 运行
运行以下命令可以查到工作节点,
1 2 3 4 5
| [root@Centos-L410 kubernetes] NAME LABELS STATUS AGE centos-minion kubernetes.io/hostname=centos-minion Ready 50s [root@Centos-L410 kubernetes] Kubernetes master is running at http://localhost:8080
|
常用脚本
常用的 Kubernetes 的脚本,总共6个,分别是对主节点和工作节点的启动、停止和获取状态,如下,
1 2 3 4 5 6 7 8
| #!/bin/bash for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do systemctl restart $SERVICES systemctl enable $SERVICES systemctl status $SERVICES done
|
1 2 3 4 5 6 7 8
| #!/bin/bash for SERVICES in kube-proxy kubelet docker; do systemctl restart $SERVICES systemctl enable $SERVICES systemctl status $SERVICES done
|
1 2 3 4 5 6
| #!/bin/bash for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do systemctl status $SERVICES done
|
1 2 3 4 5 6
| #!/bin/bash for SERVICES in kube-proxy kubelet docker; do systemctl status $SERVICES done
|
1 2 3 4 5 6
| #!/bin/bash for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do systemctl stop $SERVICES done
|
1 2 3 4 5 6
| #!/bin/bash for SERVICES in kube-proxy kubelet docker; do systemctl stop $SERVICES done
|
启动一个 mysql
新建一个 mysql-pod.yaml 文件,用于描述 pod,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| apiVersion: v1 kind: Pod metadata: name: mysql labels: name: mysql spec: containers: - resources: limits : cpu: 0.5 image: docker.io/mysql name: mysql env: - name: MYSQL_ROOT_PASSWORD value: "111111" ports: - containerPort: 3306 hostPort: 3326 name: mysql volumeMounts: - name: mysql-persistent-storage mountPath: /var/lib/mysql volumes: - name: mysql-persistent-storage hostPath: path: /home/arnes/kubernetes/mysql-kube-data
|
然后启动这个 pod,检查状态,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| [root@Centos-L410 kubernetes]# kubectl create -f mysql-pod.yaml pod "mysql" created [root@Centos-L410 kubernetes]# kubectl get pods NAME READY STATUS RESTARTS AGE mysql 0/1 Pending 0 31s [root@Centos-L410 kubernetes]# kubectl get pods mysql NAME READY STATUS RESTARTS AGE mysql 1/1 Running 0 8s [root@Centos-L410 kubernetes]# kubectl describe pod mysql Name: mysql Namespace: default Image(s): docker.io/mysql Node: centos-minion/192.168.2.202 Start Time: Sun, 28 Feb 2016 22:21:48 +0800 Labels: name=mysql Status: Running Reason: Message: IP: 172.17.1.26 Replication Controllers: <none> Containers: mysql: Container ID: docker://4ea08f19a85e71084bef939524f9adffd733944744fdb31dcfd25ba2ccb087fb Image: docker.io/mysql Image ID: docker://082e0ea24cc00eced7b39d88f46e508ab83298f79e008b1e5d7fb0c928ff692b QoS Tier: cpu: Guaranteed memory: BestEffort Limits: cpu: 500m Requests: cpu: 500m State: Running Started: Sun, 28 Feb 2016 22:21:55 +0800 Ready: True Restart Count: 0 Environment Variables: MYSQL_ROOT_PASSWORD: 111111 Conditions: Type Status Ready True Volumes: mysql-persistent-storage: Type: HostPath (bare host directory volume) Path: /home/arnes/kubernetes/mysql-kube-data Events: FirstSeen LastSeen Count From SubobjectPath Reason Message ───────── ──────── ───── ──── ───────────── ────── ─────── 4m 4m 1 {scheduler } Scheduled Successfully assigned mysql to centos-minion 4m 4m 1 {kubelet centos-minion} implicitly required container POD Pulled Container image "registry.access.redhat.com/rhel7/pod-infrastructure:latest" already present on machine 4m 4m 1 {kubelet centos-minion} implicitly required container POD Created Created with docker id 84660e3722fa 4m 4m 1 {kubelet centos-minion} implicitly required container POD Started Started with docker id 84660e3722fa 4m 4m 1 {kubelet centos-minion} spec.containers{mysql} Pulled Container image "docker.io/mysql" already present on machine 4m 4m 1 {kubelet centos-minion} spec.containers{mysql} Created Created with docker id 4ea08f19a85e 4m 4m 1 {kubelet centos-minion} spec.containers{mysql} Started Started with docker id 4ea08f19a85e
|