第一步:准备
1. 安装包:
kubeadm-1.6.0-0.alpha.0.2074.a092d8e0f95f52.x86_64.rpm
kubernetes-cni-0.3.0.1-0.07a8a2.x86_64.rpm
kubelet-1.5.1-0.x86_64.rpm
kubectl-1.5.1-0.x86_64.rpm
etcd-v2.2.1-linux-amd64.tar.gz
2. 上传
在三台主机分别上传以上安装包。
第二步:安装 k8s
# yum localinstall *.rpm -y
返回信息:
第三步:安装 tmux
# yum install tmux
返回信息:
第四步:安装配置 etcd
1. 删除默认的 k8s 配置文件
# rm -rf /etc/kubernetes/
2. 解压 etcd 压缩包
# tar zxvf etcd-v2.2.1-linux-amd64.tar.gz# mv etcd-v2.2.1-linux-amd64 etcd# cd etcd
3. 执行 tmux 进入后台,执行 etcd 配置脚本
1)在 192.168.6.128 执行
./etcd --name infra0 --initial-advertise-peer-urls http://192.168.6.128:2380 \ --listen-peer-urls http://192.168.6.128:2380 \ --listen-client-urls http://192.168.6.128:2379,http://127.0.0.1:2379 \ --advertise-client-urls http://192.168.6.128:2379 \ --initial-cluster-token etcd-cluster- \ --initial-cluster infra0=http://192.168.6.128:2380,infra1=http://192.168.6.129:2380,infra2=http://192.168.6.130:2380 \ --initial-cluster-state new
返回信息:
-- :: -- ::34.058350 I | etcdmain: Git SHA: 75f8282 -- ::34.058358 I | etcdmain: Go Version: go1.5.1 -- ::34.058364 I | etcdmain: Go OS/Arch: linux/amd64 -- ::, total number of available CPUs -- ::34.058385 W | etcdmain: no data-dir provided, using default data-dir ./infra0.etcd -- ::34.058499 I | etcdmain: listening for peers on http://192.168.6.128:2380 -- ::34.058545 I | etcdmain: listening for client requests on http://127.0.0.1:2379 -- ::34.058575 I | etcdmain: listening for client requests on http://192.168.6.128:2379 -- ::34.060783 I | etcdserver: name = infra0 -- ::34.060805 I | etcdserver: data dir = infra0.etcd -- ::34.060813 I | etcdserver: member dir = infra0.etcd/member -- ::34.060820 I | etcdserver: heartbeat = 100ms -- ::34.060826 I | etcdserver: election = 1000ms -- :: -- ::34.060851 I | etcdserver: advertise client URLs = http://192.168.6.128:2379 -- ::34.060862 I | etcdserver: initial advertise peer URLs = http://192.168.6.128:2380 -- ::34.060882 I | etcdserver: initial cluster = infra0=http://192.168.6.128:2380,infra1=http://192.168.6.129:2380,infra2=http://192.168.6.130:2380 -- ::34.102470 I | etcdserver: starting member 205e265f32ab88c4 in cluster 24d40765d97551b1 -- :: -- ::, commit: , applied: , lastindex: , lastterm: ] -- :: -- ::, cluster version: to_be_decided] -- ::34.162442 E | etcdmain: failed to notify systemd for readiness: No socket -- ::34.162485 E | etcdmain: forgot to set Type=notify in systemd service file? -- ::34.168847 N | etcdserver: added local member 205e265f32ab88c4 [http://192.168.6.128:2380] to cluster 24d40765d97551b1 -- ::34.169066 N | etcdserver: added member 277ba44edc63d7cd [http://192.168.6.130:2380] to cluster 24d40765d97551b1 -- ::34.169745 N | etcdserver: added member c1d267ff57ac5bcc [http://192.168.6.129:2380] to cluster 24d40765d97551b1 -- ::: getsockopt: connection refused) -- ::: getsockopt: connection refused) -- ::: getsockopt: connection refused) -- ::: getsockopt: connection refused) -- :: -- :: -- :: -- ::, index: ] sent vote request to 277ba44edc63d7cd at term -- ::, index: ] sent vote request to c1d267ff57ac5bcc at term -- ::: getsockopt: connection refused) -- ::: getsockopt: connection refused) -- :: -- :: -- :: -- ::, index: ] sent vote request to 277ba44edc63d7cd at term -- ::, index: ] sent vote request to c1d267ff57ac5bcc at term -- :: -- :: -- :: -- ::, index: ] sent vote request to c1d267ff57ac5bcc at term -- ::, index: ] sent vote request to 277ba44edc63d7cd at term -- :: -- :: -- :: -- ::, index: ] sent vote request to 277ba44edc63d7cd at term -- ::, index: ] sent vote request to c1d267ff57ac5bcc at term -- :: -- :: -- :: -- ::, index: ] sent vote request to 277ba44edc63d7cd at term -- ::, index: ] sent vote request to c1d267ff57ac5bcc at term -- ::41.168876 E | etcdserver: publish error: etcdserver: request timed out -- :: -- :: -- :: -- ::, index: ] sent vote request to 277ba44edc63d7cd at term -- ::, index: ] sent vote request to c1d267ff57ac5bcc at term -- :: -- :: -- :: -- ::, index: ] sent vote request to 277ba44edc63d7cd at term -- ::, index: ] sent vote request to c1d267ff57ac5bcc at term -- :: -- :: -- :: -- ::, index: ] sent vote request to 277ba44edc63d7cd at term -- ::, index: ] sent vote request to c1d267ff57ac5bcc at term -- :: -- :: -- :: -- ::, index: ] sent vote request to c1d267ff57ac5bcc at term -- ::, index: ] sent vote request to 277ba44edc63d7cd at term -- :: -- :: -- :: -- ::, index: ] sent vote request to 277ba44edc63d7cd at term -- ::, index: ] sent vote request to c1d267ff57ac5bcc at term -- ::47.855884 I | rafthttp: the connection with c1d267ff57ac5bcc became active -- ::] ignored a MsgVote message with lower term ] -- :: -- :: -- :: -- ::, index: ] sent vote request to 277ba44edc63d7cd at term -- ::, index: ] sent vote request to c1d267ff57ac5bcc at term -- :: -- ::] has received votes and vote rejections -- :: -- :: -- ::48.164621 W | etcdserver: failed to reach the peerURL(http://192.168.6.130:2380) of member 277ba44edc63d7cd (Get http://192.168.6.130:2380/version: dial tcp 192.168.6.130:2380: getsockopt: connection refused) -- ::48.164660 W | etcdserver: cannot get the version of member 277ba44edc63d7cd (Get http://192.168.6.130:2380/version: dial tcp 192.168.6.130:2380: getsockopt: connection refused) -- ::48.167418 I | etcdserver: setting up the initial cluster version to 2.1 -- ::48.169673 E | etcdserver: publish error: etcdserver: request timed out, possibly due to connection lost -- ::48.173202 N | etcdserver: set the initial cluster version to 2.1 -- ::48.176115 I | etcdserver: published {Name:infra0 ClientURLs:[http://192.168.6.128:2379]} to cluster 24d40765d97551b1 -- ::53.169422 W | etcdserver: failed to reach the peerURL(http://192.168.6.130:2380) of member 277ba44edc63d7cd (Get http://192.168.6.130:2380/version: dial tcp 192.168.6.130:2380: getsockopt: connection refused) -- ::53.169475 W | etcdserver: cannot get the version of member 277ba44edc63d7cd (Get http://192.168.6.130:2380/version: dial tcp 192.168.6.130:2380: getsockopt: connection refused) -- ::58.177751 W | etcdserver: failed to reach the peerURL(http://192.168.6.130:2380) of member 277ba44edc63d7cd (Get http://192.168.6.130:2380/version: dial tcp 192.168.6.130:2380: getsockopt: connection refused) -- ::58.177823 W | etcdserver: cannot get the version of member 277ba44edc63d7cd (Get http://192.168.6.130:2380/version: dial tcp 192.168.6.130:2380: getsockopt: connection refused) -- ::01.438424 I | rafthttp: the connection with 277ba44edc63d7cd became active -- ::03.191553 I | etcdserver: updating the cluster version from 2.1 to 2.2 -- ::03.197963 N | etcdserver: updated the cluster version from 2.1 to 2.2
2)在 192.168.6.129 上执行:
./etcd --name infra1 --initial-advertise-peer-urls http://192.168.6.129:2380 \ --listen-peer-urls http://192.168.6.129:2380 \ --listen-client-urls http://192.168.6.129:2379,http://127.0.0.1:2379 \ --advertise-client-urls http://192.168.6.129:2379 \ --initial-cluster-token etcd-cluster- \ --initial-cluster infra0=http://192.168.6.128:2380,infra1=http://192.168.6.129:2380,infra2=http://192.168.6.130:2380 \ --initial-cluster-state new
返回信息:
-- :: -- ::47.824299 I | etcdmain: Git SHA: 75f8282 -- ::47.824318 I | etcdmain: Go Version: go1.5.1 -- ::47.824332 I | etcdmain: Go OS/Arch: linux/amd64 -- ::, total number of available CPUs -- ::47.824376 W | etcdmain: no data-dir provided, using default data-dir ./infra1.etcd -- ::47.824605 I | etcdmain: listening for peers on http://192.168.6.129:2380 -- ::47.824832 I | etcdmain: listening for client requests on http://127.0.0.1:2379 -- ::47.824919 I | etcdmain: listening for client requests on http://192.168.6.129:2379 -- ::47.830245 I | etcdserver: name = infra1 -- ::47.830298 I | etcdserver: data dir = infra1.etcd -- ::47.830316 I | etcdserver: member dir = infra1.etcd/member -- ::47.830368 I | etcdserver: heartbeat = 100ms -- ::47.830388 I | etcdserver: election = 1000ms -- :: -- ::47.830435 I | etcdserver: advertise client URLs = http://192.168.6.129:2379 -- ::47.830477 I | etcdserver: initial advertise peer URLs = http://192.168.6.129:2380 -- ::47.830530 I | etcdserver: initial cluster = infra0=http://192.168.6.128:2380,infra1=http://192.168.6.129:2380,infra2=http://192.168.6.130:2380 -- ::47.836392 I | etcdserver: starting member c1d267ff57ac5bcc in cluster 24d40765d97551b1 -- :: -- ::, commit: , applied: , lastindex: , lastterm: ] -- :: -- ::, cluster version: to_be_decided] -- ::47.858946 E | etcdmain: failed to notify systemd for readiness: No socket -- ::47.858966 E | etcdmain: forgot to set Type=notify in systemd service file? -- ::47.862192 I | rafthttp: the connection with 205e265f32ab88c4 became active -- ::47.862912 N | etcdserver: added member 205e265f32ab88c4 [http://192.168.6.128:2380] to cluster 24d40765d97551b1 -- ::47.863026 N | etcdserver: added member 277ba44edc63d7cd [http://192.168.6.130:2380] to cluster 24d40765d97551b1 -- ::47.863120 N | etcdserver: added local member c1d267ff57ac5bcc [http://192.168.6.129:2380] to cluster 24d40765d97551b1 -- ::: getsockopt: connection refused) -- ::: getsockopt: connection refused) -- :: -- :: -- :: -- ::, index: ] sent vote request to 277ba44edc63d7cd at term -- ::, index: ] sent vote request to 205e265f32ab88c4 at term -- ::: getsockopt: connection refused) -- ::] received a MsgVote message with higher term ] -- :: -- ::, index: , vote: ] voted , index: ] at term -- :: -- ::48.181204 I | etcdserver: published {Name:infra1 ClientURLs:[http://192.168.6.129:2379]} to cluster 24d40765d97551b1 -- ::48.184526 N | etcdserver: set the initial cluster version to 2.1 -- ::01.448599 I | rafthttp: the connection with 277ba44edc63d7cd became active -- ::03.207866 N | etcdserver: updated the cluster version from 2.1 to 2.2
3)在 192.168.6.130 上执行:
./etcd --name infra2 --initial-advertise-peer-urls http://192.168.6.130:2380 \ --listen-peer-urls http://192.168.6.130:2380 \ --listen-client-urls http://192.168.6.130:2379,http://127.0.0.1:2379 \ --advertise-client-urls http://192.168.6.130:2379 \ --initial-cluster-token etcd-cluster- \ --initial-cluster infra0=http://192.168.6.128:2380,infra1=http://192.168.6.129:2380,infra2=http://192.168.6.130:2380 \ --initial-cluster-state new
返回信息:
-- :: -- ::01.402651 I | etcdmain: Git SHA: 75f8282 -- ::01.402677 I | etcdmain: Go Version: go1.5.1 -- ::01.402691 I | etcdmain: Go OS/Arch: linux/amd64 -- ::, total number of available CPUs -- ::01.402736 W | etcdmain: no data-dir provided, using default data-dir ./infra2.etcd -- ::01.402972 I | etcdmain: listening for peers on http://192.168.6.130:2380 -- ::01.403070 I | etcdmain: listening for client requests on http://127.0.0.1:2379 -- ::01.403145 I | etcdmain: listening for client requests on http://192.168.6.130:2379 -- ::01.408318 I | etcdserver: name = infra2 -- ::01.408354 I | etcdserver: data dir = infra2.etcd -- ::01.408364 I | etcdserver: member dir = infra2.etcd/member -- ::01.408372 I | etcdserver: heartbeat = 100ms -- ::01.408379 I | etcdserver: election = 1000ms -- :: -- ::01.408422 I | etcdserver: advertise client URLs = http://192.168.6.130:2379 -- ::01.408440 I | etcdserver: initial advertise peer URLs = http://192.168.6.130:2380 -- ::01.408479 I | etcdserver: initial cluster = infra0=http://192.168.6.128:2380,infra1=http://192.168.6.129:2380,infra2=http://192.168.6.130:2380 -- ::01.421508 I | etcdserver: starting member 277ba44edc63d7cd in cluster 24d40765d97551b1 -- :: -- ::, commit: , applied: , lastindex: , lastterm: ] -- :: -- ::, cluster version: to_be_decided] -- ::01.448691 E | etcdmain: failed to notify systemd for readiness: No socket -- ::01.448707 E | etcdmain: forgot to set Type=notify in systemd service file? -- ::01.451690 I | rafthttp: the connection with 205e265f32ab88c4 became active -- ::01.454571 N | etcdserver: added member 205e265f32ab88c4 [http://192.168.6.128:2380] to cluster 24d40765d97551b1 -- ::01.454801 N | etcdserver: added local member 277ba44edc63d7cd [http://192.168.6.130:2380] to cluster 24d40765d97551b1 -- ::01.454895 N | etcdserver: added member c1d267ff57ac5bcc [http://192.168.6.129:2380] to cluster 24d40765d97551b1 -- ::01.456262 I | rafthttp: the connection with c1d267ff57ac5bcc became active -- ::] received a MsgHeartbeat message with higher term ] -- :: -- :: -- ::01.502371 N | etcdserver: set the initial cluster version to 2.1 -- ::01.506410 I | etcdserver: published {Name:infra2 ClientURLs:[http://192.168.6.130:2379]} to cluster 24d40765d97551b1 -- ::03.214718 N | etcdserver: updated the cluster version from 2.1 to 2.2
4. 退出 tmux 后台模式
Ctrl + b 再按 d 键
附:tmux 帮助
1)查看存在的 session
# tmux ls
返回信息:
2)删除某个 session
# tmux kill-session -t $session_name
3)进入某个 session
# tmux attach -t $session_name
第五步:在初始化 k8s
1. 为 192.168.6.128 网卡增加 IP
# ip addr add 192.168.6.132 dev eno16777736
查看网卡:
2. 初始化
1)执行初始化命令
# kubeadm init --api-advertise-addresses=192.168.6.131 --external-etcd-endpoints=http://192.168.6.128:2379,http://192.168.6.129:2379,http://192.168.6.130:2379 --use-kubernetes-version v1.5.1
返回信息:
Flag --external-etcd-endpoints has been deprecated, this flag will be removed when componentconfig exists [kubeadm] WARNING: kubeadm is in alpha, please do not use it for production clusters. [preflight] Running pre-flight checks [preflight] WARNING: kubelet service is not enabled, please run 'systemctl enable kubelet.service' [preflight] Starting the kubelet service [init] Using Kubernetes version: v1.5.1 [tokens] Generated token: "e2e92d.d96382dcac25aa22" [certificates] Generated Certificate Authority key and certificate. [certificates] Generated API Server key and certificate [certificates] Generated Service Account signing keys [certificates] Created keys and certificates in "/etc/kubernetes/pki" [kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/kubelet.conf" [kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/admin.conf" [apiclient] Created API client, waiting for the control plane to become ready [apiclient] All control plane components are healthy after 22.501928 seconds [apiclient] Waiting for at least one node to register and become ready [apiclient] First node is ready after 0.509735 seconds [apiclient] Creating a test deployment [apiclient] Test deployment succeeded [token-discovery] Created the kube-discovery deployment, waiting for it to become ready [token-discovery] kube-discovery is ready after 3.503953 seconds [addons] Created essential addon: kube-proxy [addons] Created essential addon: kube-dns Your Kubernetes master has initialized successfully! You should now deploy a pod network to the cluster. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: http://kubernetes.io/docs/admin/addons/ You can now join any number of machines by running the following on each node: kubeadm join --token=e2e92d.d96382dcac25aa22 192.168.6.131
其中,切记:kubeadm join --token=e2e92d.d96382dcac25aa22 192.168.6.131
2)添加 kubelet 服务
# systemctl enable kubelet.service
返回信息:
3)查看 nodes
# kubectl get nodes
返回信息:
4)查看 pods
# kubectl get pods --all-namespaces
返回信息:
3. 配置 192.168.6.129 的 k8s
1)停止 k8s 服务
# systemctl stop kubelet
2)将 192.168.6.128 上的 /etc/kubernetes 目录复制到 192.168.6.129 上
3)重启 k8s 服务
# systemctl start kubelet
3)查看 nodes
# kubectl get nodes
返回信息:
4)查看 pods
# kubectl get pods --all-namespaces
返回信息:
4. 在 192.168.6.130 上启动 k8s 服务,并加入集群
1)执行上述切记的脚本:
# kubeadm join --token=e2e92d.d96382dcac25aa22 192.168.6.131
返回信息:
2)查看 nodes
# kubectl get nodes
返回信息:
第六步:添加 calico
1. 修改 calico.yaml
找到 etcd_endpoints 字段,修改成应的 IP:
etcd_endpoints: "http://192.168.6.128:2379,http://192.168.6.129:2379,http://192.168.6.130:2379"
2. 在主节点(192.168.6.128)添加 calico.yaml
# kubectl apply -f calico.yaml
返回信息:
3. 在主节点(192.168.6.128)添加 busybox.yaml
# kubectl create -f busybox.yaml
返回信息:
4. 查看 pods
# kubectl get pods --all-namespaces
返回信息:
第七步:kubuctl 其他配置
1. 目前 deploy/kube-dns 只有主节点上有,需进行扩容 kube-dns
1)执行命令
# kubectl scale deploy/kube-dns --replicas= -n kube-system
返回信息:
2)查看 pods
# kubectl get pods --all-namespaces
返回信息:
2. 目前 deploy/kube-discovery 只有主节点上有,需进行扩容 kube-discovery
1)执行命令
# kubectl scale deploy/kube-discovery --replicas= -n kube-system
返回信息:
2)查看 pods
# kubectl get pods --all-namespaces
返回信息:
3. 给另一个主节点打主节点标签
1)在 192.168.6.129 执行
# kubectl label node 192-168-6-129.master kubeadm.alpha.kubernetes.io/role=master
返回信息:
2)重启 192.168.6.129 上的 docker 服务
# systemctl restart docker
3)查看 nodes
# kubectl get nodes
返回信息:
4. 设置主节点可调节度
即设置应用也能在主节点上运行。
# kubectl taint nodes --all dedicated-
返回信息: