Centos7.2搭建kubernates集群

一酸纲、準(zhǔn)備環(huán)境

1.1 準(zhǔn)備機(jī)器

準(zhǔn)備三臺(tái)CentOS7.2機(jī)器蚓土,一臺(tái)作為master節(jié)點(diǎn)庞溜,其他作為node節(jié)點(diǎn)

修改主機(jī)名稱

hostnamectl set-hostname k8s-mst
Role          IP                   Hostname

Master        192.168.0.87         k8s-cns1-mst

Node          192.168.0.88         k8s-cns1-nod1

Node          192.168.0.89         k8s-cns1-nod2

修改master節(jié)點(diǎn)/etc/hosts,添加以下內(nèi)容(不設(shè)置的話丛晦,在master上kubectl相關(guān)命令無(wú)法操作對(duì)應(yīng)主機(jī)上對(duì)象)

192.168.0.87     k8s-cns1-mst

192.168.0.88     k8s-cns1-nod1

192.168.0.89     k8s-cns1-nod2

為了避免和Docker的iptables產(chǎn)生沖突,關(guān)閉Node節(jié)點(diǎn)上的防火墻

systemctl stop firewalld

systemctl disable firewalld

了讓各個(gè)節(jié)點(diǎn)的時(shí)間保持一致提陶,所有節(jié)點(diǎn)安裝NTP

yum -y install ntp

systemctl start ntpd

systemctl enable ntpd

1.2 安裝docker(此處安裝的是docker-ce版本)

[root@k8s-cns1-nod2 home]# cat installdocker.sh

#!/bin/bash

yum install -y yum-utils device-mapper-persistent-data lvm2

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

yum-config-manager --enable docker-ce-edge

yum-config-manager --enable docker-ce-testing

yum makecache fast

yum install -y docker-ce

運(yùn)行Docker Daemon

systemctl start docker

二烫沙、源碼編譯

2.1 準(zhǔn)備golang環(huán)境

參考https://golang.org/doc/install,下載對(duì)應(yīng)版本并解壓到/usr/local隙笆,例如

tar -C /usr/local -xzf go1.8.3.linux-amd64.tar.gz

安裝git斧吐,下載kubenates源碼并切換到需要分支

yum install git

go get -d k8s.io/kubernetes

cd /root/go/src/k8s.io/kubernetes

git checkout release-1.6.3      //使用release-1.6.3版本

make

編譯成功后又固,可執(zhí)行文件在

/root/go/src/k8s.io/kubernetes/_output/bin

三、Master配置

3.1 安裝ectd(可選煤率,如已有etcd集群略過(guò))

3.1.1 軟件安裝

yum -y install etcd

3.1.2 修改etcd配置/etc/etcd/etcd.conf

ETCD_NAME=default

ETCD_DATA_DIR="/var/lib/etcd/default.etcd"

ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"

3.1.3 運(yùn)行etcd

systemctl enable etcd

systemctl start etcd

3.1.4 配置etcd子網(wǎng)

etcdctl mk /atomic.io/network/config '{"Network":"172.17.0.0/16"}'

3.2 kubernates配置

3.2.1 復(fù)制二進(jìn)制

將位于/root/go/src/k8s.io/kubernetes/_output/bin/目錄下的kube-apiserver仰冠、kube-controller-manager、kube-scheduler蝶糯、kubectl復(fù)制到Master節(jié)點(diǎn)的/usr/bin/目錄下

3.2.2 創(chuàng)建service配置腳本(shell)

[root@k8s-cns1-mst home]# cat configmaster.sh 
#!/bin/bash
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

MASTER_ADDRESS=${1:-"192.168.0.87"}
ETCD_SERVERS=${2:-"http://192.168.0.87:2379"}
SERVICE_CLUSTER_IP_RANGE=${3:-"10.254.0.0/16"}
ADMISSION_CONTROL=${4:-"NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"}

cat <<EOF >/etc/kubernetes/config
# --logtostderr=true: log to standard error instead of files
KUBE_LOGTOSTDERR="--logtostderr=false"

# --v=0: log level for V logs
KUBE_LOG_LEVEL="--v=0"

# --allow-privileged=false: If true, allow privileged containers.
KUBE_ALLOW_PRIV="--allow-privileged=true"

# How the controller-manager, scheduler, and proxy find the apiserver
KUBE_MASTER="--master=${MASTER_ADDRESS}:8080"
EOF

cat <<EOF >/etc/kubernetes/apiserver
# --insecure-bind-address=127.0.0.1: The IP address on which to serve the --insecure-port.
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"

# --insecure-port=8080: The port on which to serve unsecured, unauthenticated access.
KUBE_API_PORT="--insecure-port=8080"

# --kubelet-port=10250: Kubelet port
NODE_PORT="--kubelet-port=10250"

# --etcd-servers=[]: List of etcd servers to watch (http://ip:port), 
# comma separated. Mutually exclusive with -etcd-config
KUBE_ETCD_SERVERS="--etcd-servers=${ETCD_SERVERS}"

# --advertise-address=<nil>: The IP address on which to advertise 
# the apiserver to members of the cluster.
KUBE_ADVERTISE_ADDR="--advertise-address=${MASTER_ADDRESS}"

# --service-cluster-ip-range=<nil>: A CIDR notation IP range from which to assign service cluster IPs. 
# This must not overlap with any IP ranges assigned to nodes for pods.
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=${SERVICE_CLUSTER_IP_RANGE}"

# --admission-control="AlwaysAdmit": Ordered list of plug-ins 
# to do admission control of resources into cluster. 
# Comma-delimited list of: 
#   LimitRanger, AlwaysDeny, SecurityContextDeny, NamespaceExists, 
#   NamespaceLifecycle, NamespaceAutoProvision,
#   AlwaysAdmit, ServiceAccount, ResourceQuota, DefaultStorageClass
KUBE_ADMISSION_CONTROL="--admission-control=${ADMISSION_CONTROL}"

# Add your own!
KUBE_API_ARGS="--log-dir=/var/log/kubenates/"
EOF

KUBE_APISERVER_OPTS="   \${KUBE_LOGTOSTDERR}         \\
                        \${KUBE_LOG_LEVEL}           \\
                        \${KUBE_ETCD_SERVERS}        \\
                        \${KUBE_API_ADDRESS}         \\
                        \${KUBE_API_PORT}            \\
                        \${NODE_PORT}                \\
                        \${KUBE_ADVERTISE_ADDR}      \\
                        \${KUBE_ALLOW_PRIV}          \\
                        \${KUBE_SERVICE_ADDRESSES}   \\
                        \${KUBE_ADMISSION_CONTROL}   \\
                        \${KUBE_API_ARGS}"


cat <<EOF >/usr/lib/systemd/system/kube-apiserver.service
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes
After=network.target
After=etcd.service

[Service]
EnvironmentFile=-/etc/kubernetes/config
EnvironmentFile=-/etc/kubernetes/apiserver
ExecStart=/usr/bin/kube-apiserver ${KUBE_APISERVER_OPTS}
Restart=on-failure
Type=notify
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF >/etc/kubernetes/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=""
EOF

KUBE_CONTROLLER_MANAGER_OPTS="  \${KUBE_LOGTOSTDERR} \\
                                \${KUBE_LOG_LEVEL}   \\
                                \${KUBE_MASTER}      \\
                                \${KUBE_CONTROLLER_MANAGER_ARGS}"

cat <<EOF >/usr/lib/systemd/system/kube-controller-manager.service
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes

[Service]
EnvironmentFile=-/etc/kubernetes/config
EnvironmentFile=-/etc/kubernetes/controller-manager
ExecStart=/usr/bin/kube-controller-manager ${KUBE_CONTROLLER_MANAGER_OPTS}
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF >/etc/kubernetes/scheduler
###
# kubernetes scheduler config

# Add your own!
KUBE_SCHEDULER_ARGS=""
EOF

KUBE_SCHEDULER_OPTS="   \${KUBE_LOGTOSTDERR}     \\
                        \${KUBE_LOG_LEVEL}       \\
                        \${KUBE_MASTER}          \\
                        \${KUBE_SCHEDULER_ARGS}"

cat <<EOF >/usr/lib/systemd/system/kube-scheduler.service
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes

[Service]
EnvironmentFile=-/etc/kubernetes/config
EnvironmentFile=-/etc/kubernetes/scheduler
ExecStart=/usr/bin/kube-scheduler ${KUBE_SCHEDULER_OPTS}
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload

3.2.3 創(chuàng)建service啟動(dòng)腳本

for svc in kube-apiserver kube-controller-manager kube-scheduler; do 
    systemctl restart $svc
    systemctl enable $svc
    systemctl status $svc 
done

四洋只、Node配置

4.1 安裝flanneld

4.1.1 安裝軟件

yum -y install flannel

4.1.2 修改flannel的配置文件/etc/sysconfig/flanneld

FLANNEL_ETCD="http://192.168.0.87:2379"
FLANNEL_ETCD_KEY="/atomic.io/network"

4.1.3 運(yùn)行flannel

systemctl restart flanneld
systemctl enable flanneld
systemctl status flanneld

4.1.4 上傳網(wǎng)絡(luò)配置
創(chuàng)建一個(gè)config.json文件,內(nèi)容如下:

{
"Network": "172.17.0.0/16",
"SubnetLen": 24,
"Backend": {
     "Type": "vxlan",
     "VNI": 7890
     }
 }

將配置上傳到etcd服務(wù)器上

curl -L http://192.168.0.87:2379/v2/keys/atomic.io/network/config -XPUT --data-urlencode value@config.json

查看etcd分配的子網(wǎng)信息

[root@k8s-sz-0002 ~]# cat /run/flannel/subnet.env 
FLANNEL_NETWORK=172.17.0.0/16
FLANNEL_SUBNET=172.17.79.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=false

4.2 kubernates配置

4.2.1 復(fù)制二進(jìn)制

將位于/root/go/src/k8s.io/kubernetes/_output/bin/目錄下的kube-proxy昼捍、kubelet 復(fù)制到Node節(jié)點(diǎn)的/usr/bin/目錄下

4.2.2 創(chuàng)建service配置腳本

[root@k8s-cns1-nod1 home]# cat configslave.sh 
#!/bin/bash
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

MASTER_ADDRESS=${1:-"192.168.0.87"}
NODE_HOSTNAME=${2:-"k8s-cns1-nod1"}

cat <<EOF >/etc/kubernetes/config
# --logtostderr=true: log to standard error instead of files
KUBE_LOGTOSTDERR="--logtostderr=true"

# --v=0: log level for V logs
KUBE_LOG_LEVEL="--v=0"

# --allow-privileged=false: If true, allow privileged containers.
KUBE_ALLOW_PRIV="--allow-privileged=false"

# How the controller-manager, scheduler, and proxy find the apiserver
KUBE_MASTER="--master=${MASTER_ADDRESS}:8080"
EOF

cat <<EOF >/etc/kubernetes/proxy
###
# kubernetes proxy config

# default config should be adequate

# Add your own!
KUBE_PROXY_ARGS="--log-dir=/var/log/kubenates/"
EOF

KUBE_PROXY_OPTS="   \${KUBE_LOGTOSTDERR} \\
                    \${KUBE_LOG_LEVEL}   \\
                    \${KUBE_MASTER}    \\
                    \${KUBE_PROXY_ARGS}"

cat <<EOF >/usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Proxy
After=network.target

[Service]
EnvironmentFile=-/etc/kubernetes/config
EnvironmentFile=-/etc/kubernetes/kube-proxy
ExecStart=/usr/bin/kube-proxy ${KUBE_PROXY_OPTS}
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

cat <<EOF >/etc/kubernetes/kubelet
# --address=0.0.0.0: The IP address for the Kubelet to serve on (set to 0.0.0.0 for all interfaces)
KUBELET__ADDRESS="--address=0.0.0.0"

# --port=10250: The port for the Kubelet to serve on. Note that "kubectl logs" will not work if you set this flag.
KUBELET_PORT="--port=10250"

# --hostname-override="": If non-empty, will use this string as identification instead of the actual hostname.
KUBELET_HOSTNAME="--hostname-override=${NODE_HOSTNAME}"

# --api-servers=[]: List of Kubernetes API servers for publishing events, 
# and reading pods and services. (ip:port), comma separated.
KUBELET_API_SERVER="--api-servers=http://${MASTER_ADDRESS}: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=""
EOF

KUBE_PROXY_OPTS="   \${KUBE_LOGTOSTDERR}     \\
                    \${KUBE_LOG_LEVEL}       \\
                    \${KUBELET__ADDRESS}         \\
                    \${KUBELET_PORT}            \\
                    \${KUBELET_HOSTNAME}        \\
                    \${KUBELET_API_SERVER}   \\
                    \${KUBE_ALLOW_PRIV}      \\
                    \${KUBELET_POD_INFRA_CONTAINER}\\
                    \${KUBELET_ARGS}"

cat <<EOF >/usr/lib/systemd/system/kubelet.service
[Unit]
Description=Kubernetes Kubelet
After=docker.service
Requires=docker.service

[Service]
WorkingDirectory=/var/lib/kubelet
EnvironmentFile=-/etc/kubernetes/config
EnvironmentFile=-/etc/kubernetes/kubelet
ExecStart=/usr/bin/kubelet ${KUBE_PROXY_OPTS}
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload

4.2.3 創(chuàng)建service啟動(dòng)腳本

# cat /home/startslave.sh 
mkdir -p /etc/kubernetes/
mkdir -p /var/logs/kubernetes/
mkdir -p /var/lib/kubelet

source /run/flannel/subnet.env

sed -i "s|--bip=.*|--bip=${FLANNEL_SUBNET} --ip-masq=true --mtu=1472|" /usr/lib/systemd/system/docker.service

systemctl daemon-reload

for svc in docker kubelet kube-proxy; do 
    systemctl restart $svc
    systemctl enable $svc
    systemctl status $svc 
done

必須注意识虚,/usr/lib/systemd/system/docker.service是docker 的配置文件,因?yàn)閚ode和etcd之間是有租約時(shí)間的妒茬,如果node長(zhǎng)時(shí)間不在線的話担锤,etcd會(huì)認(rèn)為租約超期,清除子網(wǎng)信息乍钻,當(dāng)node起來(lái)后肛循,flannel重新獲取新的子網(wǎng),所以docker容器網(wǎng)段(--bip=)建議每次都從新的flannel獲取银择,保證一致

編輯/etc/rc.local多糠,將腳本執(zhí)行加入開(kāi)機(jī)啟動(dòng)項(xiàng),保證node重啟后自動(dòng)執(zhí)行腳本

驗(yàn)證環(huán)境配置

在Master節(jié)點(diǎn)運(yùn)行命令kubectl get nodes浩考,輸出信息如下:

[root@k8s-cns1-mst home]# kubectl get node
NAME            STATUS    AGE       VERSION
k8s-cns1-nod1   Ready     1d        v1.6.4
k8s-cns1-nod2   Ready     1d        v1.6.4

常見(jiàn)問(wèn)題

1夹孔、使用etcdctl時(shí)連接本地報(bào)錯(cuò)

/usr/lib # 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

error #0: dial tcp 127.0.0.1:2379: getsockopt: connection refused
error #1: dial tcp 127.0.0.1:4001: getsockopt: connection refused

這是因?yàn)镋TCD_LISTEN_CLIENT_URLS參數(shù)沒(méi)有配置http://127.0.0.1:2379而導(dǎo)致,使用etcdctl時(shí)加上endpoints選項(xiàng)

/usr/lib # etcdctl --endpoints=192.168.0.87:2379 ls /
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末析孽,一起剝皮案震驚了整個(gè)濱河市搭伤,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌袜瞬,老刑警劉巖闷畸,帶你破解...
    沈念sama閱讀 218,858評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異吞滞,居然都是意外死亡佑菩,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)裁赠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)殿漠,“玉大人,你說(shuō)我怎么就攤上這事佩捞〗驶希” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,282評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵一忱,是天一觀的道長(zhǎng)莲蜘。 經(jīng)常有香客問(wèn)我谭确,道長(zhǎng),這世上最難降的妖魔是什么票渠? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,842評(píng)論 1 295
  • 正文 為了忘掉前任逐哈,我火速辦了婚禮,結(jié)果婚禮上问顷,老公的妹妹穿的比我還像新娘昂秃。我一直安慰自己,他們只是感情好杜窄,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布肠骆。 她就那樣靜靜地躺著,像睡著了一般塞耕。 火紅的嫁衣襯著肌膚如雪蚀腿。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,679評(píng)論 1 305
  • 那天扫外,我揣著相機(jī)與錄音莉钙,去河邊找鬼。 笑死畏浆,一個(gè)胖子當(dāng)著我的面吹牛胆胰,可吹牛的內(nèi)容都是我干的狞贱。 我是一名探鬼主播刻获,決...
    沈念sama閱讀 40,406評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼瞎嬉!你這毒婦竟也來(lái)了蝎毡?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,311評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤氧枣,失蹤者是張志新(化名)和其女友劉穎沐兵,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體便监,經(jīng)...
    沈念sama閱讀 45,767評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡扎谎,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了烧董。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片毁靶。...
    茶點(diǎn)故事閱讀 40,090評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖逊移,靈堂內(nèi)的尸體忽然破棺而出预吆,到底是詐尸還是另有隱情,我是刑警寧澤胳泉,帶...
    沈念sama閱讀 35,785評(píng)論 5 346
  • 正文 年R本政府宣布拐叉,位于F島的核電站岩遗,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏凤瘦。R本人自食惡果不足惜宿礁,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望廷粒。 院中可真熱鬧窘拯,春花似錦、人聲如沸坝茎。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,988評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)嗤放。三九已至思喊,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間次酌,已是汗流浹背恨课。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,101評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留岳服,地道東北人剂公。 一個(gè)月前我還...
    沈念sama閱讀 48,298評(píng)論 3 372
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像吊宋,于是被迫代替她去往敵國(guó)和親纲辽。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容