關於什麼是Kubernetes請看另外一篇內容:http://www.cnblogs.com/boshen-hzb/p/6482734.htmlhtml
1、環境搭建node
master安裝的組件有:mysql
dockerredis
etcd 能夠理解爲是k8s的數據庫,存儲全部節點、pods、網絡信息sql
kube-proxy 提供service服務的基礎組件docker
kubelet 管理k8s節點的組件,由於這臺master同時也是nodes,因此也要安裝數據庫
kube-apiserver k8s提供API的接口,是整個k8s的核心centos
kube-controller-manager 管理分配資源的組件api
kube-scheduler 調度資源的組件服務器
flanneld 整個k8s的網絡組件
nodes安裝的組件有:
docker
kube-proxy
kubelet
flanneld
因爲是單機安裝,因此只須要安裝master節點就能夠。
2、安裝步驟
一、關閉防火牆
systemctl stop firewalld.service
systemctl disable firewalld.service
二、更新yum源
cat <<EOF> /etc/yum.repos.d/virt7-docker-common-release.repo [virt7-docker-common-release] name=virt7-docker-common-release baseurl=http://cbs.centos.org/repos/virt7-docker-common-release/x86_64/os/ gpgcheck=0 EOF
三、執行yum -y update
四、執行安裝命令
yum install -y etcd kubernetes flannel
若是系統已經存在安裝好的docker引擎,上面的命令會報錯,解決方法就是:先刪除掉docker,由於上面的命令當中,會自動安裝docker
五、配置etcd服務器
/etc/etcd/etcd.conf
# [member] ETCD_NAME=default ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_CLIENT_URLS="http://localhost:2379,http://10.111.131.51:2379" ETCD_ADVERTISE_CLIENT_URLS="http://10.111.131.51:2379"
啓動服務 systemctl start etcd systemctl enable etcd
檢查etcd cluster狀態
[root@localhost ~]# etcdctl cluster-health
member eb1f405cbdb8358 is healthy: got healthy result from http://localhost:2379
cluster is healthy
[root@localhost ~]#
檢查etcd集羣成員列表,這裏只有一臺
[root@localhost ~]# etcdctl member list
eb1f405cbdb8358: name=default peerURLs=http://10.111.131.51:2380 clientURLs=http://localhost:2379 isLeader=true
[root@localhost ~]
配置防火牆 firewall-cmd --zone=public --add-port=2379/tcp --permanent firewall-cmd --zone=public --add-port=2380/tcp --permanent firewall-cmd --reload firewall-cmd --list-all
六、配置master服務器
1) 配置kube-apiserver配置文件
/etc/kubernetes/config
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://10.111.131.51:8080"
/etc/kubernetes/apiserver
KUBE_API_ADDRESS="--insecure-bind-address=127.0.0.1" # 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://10.111.131.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" # Add your own! KUBE_API_ARGS=""
2) 配置kube-controller-manager配置文件
# The following values are used to configure the kubernetes controller-manager # defaults from config and apiserver should be adequate # Add your own! KUBE_CONTROLLER_MANAGER_ARGS=""
3) 配置kube-scheduler配置文件
/etc/kubernetes/scheduler
### # kubernetes scheduler config # default config should be adequate # Add your own! KUBE_SCHEDULER_ARGS="--address=0.0.0.0"
4) 啓動服務
service kube-apiserver restart
service kube-controller-manager restart
service kube-scheduler restart
如下是將本機看成node節點的配置
5)配置etcd
[root@localhost ~]# etcdctl set /k8s/network/config '{"Network": "10.111.131.0/24"}' {"Network": "10.111.131.0/24"} [root@localhost ~]#
6)配置node的network,本實例採用flannel方式來配置,如需其餘方式,請參考Kubernetes官網。
先用ifconfig查找到本機網絡10.111.131.51對應的網卡:
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.111.131.51 netmask 255.255.255.0 broadcast 10.111.131.255 inet6 fe80::20c:29ff:fedf:f83b prefixlen 64 scopeid 0x20<link> ether 00:0c:29:df:f8:3b txqueuelen 1000 (Ethernet) RX packets 161627 bytes 208913594 (199.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 113194 bytes 72446146 (69.0 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
打開/etc/sysconfig/flanneld,進行如下配置
# Flanneld configuration options # etcd url location. Point this to the server where etcd runs FLANNEL_ETCD_ENDPOINTS="http://10.111.131.51:2379" # etcd config key. This is the configuration key that flannel queries # For address range assignment FLANNEL_ETCD_PREFIX="/k8s/network" # Any additional options that you want to pass FLANNEL_OPTIONS="eno16777736"
7)配置node的kube-proxy
/etc/kubernetes/config (注意:本機是master也是node,由於前面已經配過,因此這裏不用再配)
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://10.111.131.51:8080"
/etc/kubernetes/proxy
進行如下配置
config should be adequate # Add your own! KUBE_PROXY_ARGS="--bind=address=0.0.0.0"
8) 配置node的kubelet
在/etc/hosts下加入:10.111.131.51 k8s-master
/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=127.0.0.1" # 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=k8s-master" # location of the api-server KUBELET_API_SERVER="--api-servers=http://10.111.131.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=""
9) 啓動node服務
service flanneld start
service kube-proxy start
service kubelet start
10)至此,整個Kubernetes單機版環境搭建完,下面建立一個deployment進行測試
mysql-deployment.yaml
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: mysql-test spec: replicas: 1 template: metadata: labels: app: redis spec: containers: - name: mysql-test image: 72.16.101.192/common/mysql:5.6 resources: requests: cpu: 100m memory: 100Mi ports: - containerPort: 3306
在機器上執行如下命令:
[root@localhost ~]# kubectl create -f mysql-deployment.yaml deployment "mysql-test" created [root@localhost ~]#
查看剛纔建立的deployment信息。
[root@localhost ~]# kubectl get deploy NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE mysql-test 1 0 0 0 1m [root@localhost ~]#
出現上面的信息,表示Kubernetes安裝成功。