1. 程式人生 > >k8s1.4.3安裝實踐記錄(1)-etcd、docker、flannel安裝配置

k8s1.4.3安裝實踐記錄(1)-etcd、docker、flannel安裝配置

由於剛開始學習k8s,本次軟體的安裝,我們都採用最簡單的方式,能用yum 安裝的儘量採用yum安裝

1、ETCD安裝

ETCD官方文件:https://github.com/coreos/etcd/blob/master/Documentation/docs.md

1.1 檢查ETCD版本

[[email protected] ~]# yum list|grep etcd
etcd.x86_64                                2.3.7-4.el7                 @extras  
[[email protected] ~]# 

1.2 安裝ETCD

yum install etcd

1.3 修改ETCD配置

安裝好後,系統會自動生成etcd.service檔案(路徑為/usr/lib/systemd/system/),修改對應的配置

複製程式碼
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf
User=etcd
# set GOMAXPROCS to number of processors
ExecStart
=/bin/bash -c "GOMAXPROCS=$(nproc) /usr/bin/etcd \ --name=\"${ETCD_NAME}\" \ --data-dir=\"${ETCD_DATA_DIR}\" \ --listen-peer-urls=\"${ETCD_LISTEN_PEER_URLS}\" \ --advertise-client-urls=\"${ETCD_ADVERTISE_CLIENT_URLS}\" \ --initial-cluster-token=\"${ETCD_INITIAL_CLUSTER_TOKEN}\" \ --initial-cluster=\"${ETCD_INITIAL_CLUSTER}\" \
--initial-cluster-state=\"${ETCD_INITIAL_CLUSTER_STATE}\" \ --listen-client-urls=\"${ETCD_LISTEN_CLIENT_URLS}\"" Restart=on-failure LimitNOFILE=65536 [Install] WantedBy=multi-user.target
複製程式碼

 並配置其配置檔案

複製程式碼
ETCD_NAME=zwetcd_2
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"ETCD_LISTEN_PEER_URLS="http://192.168.37.131:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.37.131:2379,http://127.0.0.1:2379"
#[cluster]
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.37.131:2380"
# if you use different ETCD_NAME (e.g. test), set ETCD_INITIAL_CLUSTER value for this name, i.e. "test=http://..."
ETCD_INITIAL_CLUSTER="zwetcd_2=http://192.168.37.131:2380,zwetcd_1=http://192.168.37.130:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.37.131:2379"
複製程式碼

如果使用firewalld作為防火牆,則需要開放埠:

1 2 3 4 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

  問題:

1、本地連線報錯

[[email protected] system]# etcdctl ls /
Error: client: etcd cluster is unavailable or misconfigured
error #0: dial tcp 127.0.0.1:2379: getsockopt: connection refused
error #1: dial tcp 127.0.0.1:4001: getsockopt: connection refused

如果出現如上的錯誤,是因為ETCD_LISTEN_CLIENT_URLS引數沒有配置http://127.0.0.1:2379而導致的,不過已經配置了具體的IP,還需要配置本地鏈路,這個就有點奇怪了。

2、Docker安裝

2.1、檢查docker版本

yum list |grep docker

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 [[email protected] ~]# yum list|grep docker docker.x86_64                              1.10.3-46.el7.centos.14     @extras  docker-common.x86_64                       1.10.3-46.el7.centos.14     @extras  docker-selinux.x86_64                      1.10.3-46.el7.centos.14     @extras  cockpit-docker.x86_64                      0.114-2.el7.centos          extras   docker-devel.x86_64                        1.3.2-4.el7.centos          extras   docker-distribution.x86_64                 2.4.1-2.el7                 extras   docker-forward-journald.x86_64             1.10.3-44.el7.centos        extras   docker-latest.x86_64                       1.12.1-2.el7.centos         extras   docker-latest-logrotate.x86_64             1.12.1-2.el7.centos         extras   docker-latest-v1.10-migrator.x86_64        1.12.1-2.el7.centos         extras   docker-logrotate.x86_64                    1.10.3-46.el7.centos.14     extras   docker-lvm-plugin.x86_64                   1.10.3-46.el7.centos.14     extras   docker-novolume-plugin.x86_64              1.10.3-46.el7.centos.14     extras   docker-python.x86_64                       1.4.0-115.el7               extras   docker-registry.noarch                     0.6.8-8.el7                 extras   docker-registry.x86_64                     0.9.1-7.el7                 extras   docker-unit-test.x86_64                    1.10.3-46.el7.centos.14     extras   docker-v1.10-migrator.x86_64               1.10.3-46.el7.centos.14     extras   python-docker-py.noarch                    1.7.2-1.el7                 extras   [[email protected] ~]#

2.2 安裝docker

1 yum install docker -y

2.3 檢查docker安裝資訊

複製程式碼
[[email protected] ~]# docker version
Client:
 Version:         1.10.3
 API version:     1.22
 Package version: docker-common-1.10.3-46.el7.centos.14.x86_64
 Go version:      go1.6.3
 Git commit:      cb079f6-unsupported
 Built:           Fri Sep 16 13:24:25 2016
 OS/Arch:         linux/amd64
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
複製程式碼

3 flannel

3.1 檢查flannel版本

[[email protected] etcd]# yum list |grep flannel
flannel.x86_64                             0.5.3-9.el7                 @extras  

3.2 安裝flannel

yum install flannel 

3.3 修改service配置

檢視flannel的配置檔案(使用yum安裝會自動生成此檔案,如果下載的執行檔案則需要手動生成,在使用systemctl命令執行service 的時候會用到),可以看到flannel的service配置如下:

複製程式碼
[[email protected] etcd]# more /usr/lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld overlay address etcd agent
After=network.target
After=network-online.target
Wants=network-online.target
After=etcd.service
Before=docker.service

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/flanneld
EnvironmentFile=-/etc/sysconfig/docker-network
ExecStart=/usr/bin/flanneld -etcd-endpoints=${FLANNEL_ETCD} -etcd-prefix=${FLANNEL_ETCD_KEY} $FLANNEL_OPTIONS
ExecStartPost=/usr/libexec/flannel/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/docker
Restart=on-failure

[Install]
WantedBy=multi-user.target
RequiredBy=docker.service
複製程式碼

其中所有的引數都配置在/etc/sysconfig/flanneld檔案中,修改此檔案,初始檔案如下:

複製程式碼
# Flanneld configuration options  

# etcd url location.  Point this to the server where etcd runs
FLANNEL_ETCD="http://127.0.0.1:2379"

# etcd config key.  This is the configuration key that flannel queries
# For address range assignment
FLANNEL_ETCD_KEY="/atomic.io/network"

# Any additional options that you want to pass
FLANNEL_OPTIONS=""
複製程式碼

其中

  FLANNEL_ETCD:為ETCD的地址,

  FLANNEL_ETCD_KEY:為在etcd中配置的網路引數的key 

  FLANNEL_OPTIONS:為flannel的啟動引數,我在這裡加上了監聽的網絡卡

根據前面步驟中etcd的配置,我們修改配置檔案如下:

複製程式碼
# Flanneld configuration options  

# etcd url location.  Point this to the server where etcd runs
FLANNEL_ETCD="http://192.168.37.130:2379"

# etcd config key.  This is the configuration key that flannel queries
# For address range assignment
FLANNEL_ETCD_KEY="/flannel/network"

# Any additional options that you want to pass
FLANNEL_OPTIONS="--iface=eno16777736"
複製程式碼

3.4 啟動FLANNEL

可以使用service flanneld start 或者systemctl start flannel啟動flannel

3.5 修改docker網路

 因為docker需要使用flanneld的網路,因此需要修改docker的service檔案:

複製程式碼
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.com
After=network.target rhel-push-plugin.socket
Wants=docker-storage-setup.service

[Service]
Type=notify
NotifyAccess=all
#import flannel configuration 
EnvironmentFile=-/etc/sysconfig/flanneld
EnvironmentFile=-/run/flannel/subnet.env
EnvironmentFile=-/etc/sysconfig/docker
EnvironmentFile=-/etc/sysconfig/docker-storage
EnvironmentFile=-/etc/sysconfig/docker-network
Environment=GOTRACEBACK=crash
ExecStart=/usr/bin/docker-current daemon \
          --exec-opt native.cgroupdriver=systemd \
          $OPTIONS \
          $DOCKER_STORAGE_OPTIONS \
          $DOCKER_NETWORK_OPTIONS \
          $ADD_REGISTRY \
          $BLOCK_REGISTRY \
          $INSECURE_REGISTRY \
          --bip=${FLANNEL_SUBNET}
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
TimeoutStartSec=0
MountFlags=slave
Restart=on-abnormal

[Install]
WantedBy=multi-user.target
複製程式碼

在執行前增加配置檔案

EnvironmentFile=-/etc/sysconfig/flanneld

EnvironmentFile=-/run/flannel/subnet.env

執行命令增加引數 --bip=${FLANNEL_SUBNET}

重啟docker

systemctl daemon-reload
systemctl restart docker

3.6 問題

1、Failed to retrieve network config: 104: Not a directory (/flannel/network/config)

問題原因:在初次配置的時候,把flannel的配置檔案中的etcd-prefix-key配置成了/flannel/network/config,實際上應該是/flannel/network

注意:如上配置需要在叢集的所有機器上執行,完成後,上述安裝的各個系統的啟動順序應該是:

systemctl start etcd

systemctl start flannel