1. 程式人生 > >kubernetes叢集搭建實戰

kubernetes叢集搭建實戰

1       條件準備:

         IP:172.16.20.17  Master Centos7  核心3.10 記憶體8G 硬碟150G

         IP:172.16.20.18  Node Centos7  核心3.10 記憶體8G 硬碟50G

         IP:172.16.20.19  Node Centos7  核心3.10 記憶體8G 硬碟50G

2       KubernetesMaster配置

         關閉防火牆、SELinux

         systemctlstop firewalld

         systemctldisable firewalld

         setenforce0

2.1  CentOS yum代理與yum源設定

# cd /etc/

# nano yum.conf

在檔案中增加

proxy=http://172.16.20.20:3128

然後

# cd /etc/yum.repos.d

# mv CentOS-Base.repo CentOS-Base.repo.bak

# vim CentOS-Base.repo  寫入如下內容

# CentOS-Base.repo

#

# The mirror system uses the connecting IP address of the client andthe

# update status of each mirror to pick mirrors that are updated toand

# geographically close to the client.  You should use this for CentOS updates

# unless you are manually picking other mirrors.

#

# If the mirrorlist= does not work for you, as a fall back you cantry the

# remarked out baseurl= line instead.

#

#

[base]

name=CentOS-$releasever - Base - 163.com

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os

baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/

gpgcheck=1

gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

#released updates

[updates]

name=CentOS-$releasever - Updates - 163.com

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates

baseurl=http://mirrors.163.com/centos/$releasever/updates/$basearch/

gpgcheck=1

gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful

[extras]

name=CentOS-$releasever - Extras - 163.com

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras

baseurl=http://mirrors.163.com/centos/$releasever/extras/$basearch/

gpgcheck=1

gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages

[centosplus]

name=CentOS-$releasever - Plus - 163.com

baseurl=http://mirrors.163.com/centos/$releasever/centosplus/$basearch/

gpgcheck=1

enabled=0

gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

          然後

# yum clean all

# yum repolist 檢視163源

# yum makecache

2.2  安裝docker、etcd、Kubernetes-master服務

yum install -y etcd docker kubernetes-master

配置docker代理,下載基礎映象

在/etc/sysconfig/docker檔案中增加代理設定

http_proxy=http://172.16.20.20:3128

重啟docker服務

# systemctl restart docker

2.3  搭建私有倉庫

下載倉庫映象:

# dockerpull docker.io/registry

vim /etc/docker/daemon.json

寫入

{"insecure-registries":["172.16.20.17:5000"]

}

vim /etc/sysconfig/docker

註釋代理

#http_proxy=http://172.16.20.20:3128

systemctl restart docker

docker run –d –p 5000:5000 docker.io/registry

驗證倉庫

docker tag docker.io/registry 172.16.20.17:5000/registry:latest

docker push 172.16.20.17:5000/registry:latest

push成功則部署倉庫成功。

將基礎映象上傳至倉庫(基礎映象需要自己先拿到master虛擬機器)

docker push 172.16.20.17:5000/alpine:tomcat       

docker push 172.16.20.17:5000/alpine:vpn

docker push 172.16.20.17:5000/phantomjs:tomcat

docker push 172.16.20.17:5000/rhel7/pod-infrastructure:latest

然後打標籤:

docker tag 172.16.20.17:5000/alpine:tomcat alpine:tomcat

。。。依次類推

2.4  配置etcd服務

a)  修改etcd配置檔案/etc/etcd/etcd.conf

ETCD_NAME=default

ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379

ETCD_ADVERTISE_CLIENT_URLS=http://localhost:2379

b)        更新etcd服務檔案,重啟服務

# systemctl restart etcd

2.5      在etcd裡定義flannel網路地址,供節點使用

master中執行:

etcdctl mk /coreos.com/network/config'{"Network":"172.17.0.0/16"}'

2.6      修改apiserver配置檔案

a)        修改apiserver配置檔案

vim /etc/kubernetes/apiserver

KUBE_API_ADDRESS="--address=0.0.0.0"

KUBE_ETCD_SERVERS="--etcd_servers=http://127.0.0.1:2379"

KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"

KUBE_ADMISSION_CONTROL="--admission_control=NamespaceAutoProvision,LimitRanger,ResourceQuota"

KUBE_API_ARGS="--service-node-port-range=80-35535"

2.7      啟動Master服務,建立啟動檔案,關閉檔案

Master上啟動kube-apiserver kube-controller-manager kube-scheduler服務

建立Master服務啟動檔案start-k8s-master.sh

for SERVICES in etcd kube-apiserverkube-controller-manager kube-scheduler;

 do

    systemctl restart $SERVICES

    systemctl enable $SERVICES

    systemctl status $SERVICES

done

加執行許可權:chmod +x start-k8s-master.sh

啟動master

./start-k8s-master.sh

3       Kubernetes node配置

關閉防火牆、SELinux

systemctl stop firewalld

systemctl disable firewalld

setenforce 0

3.1  安裝docker、flannel、kubernetes-node服務

yum install -y flannel docker kubernetes-node

3.2      在hosts新增k8s-master及k8s-slave*地址

在hosts檔案新增配置,/etc/hosts

172.16.20.17 k8s-master

172.16.20.18 slave-1

172.16.20.19 slave-2

3.3      配置flanneld

修改flanneld配置nano /etc/sysconfig/flanneld

FLANNEL_ETCD=http://k8s-master:2379

FLANNEL_ETCD_KEY="/coreos.com/network"

FLANNEL_OPTIONS="--logtostderr=false--log_dir=/var/log/flannel/ --iface=ens3"

3.4      配置kube-proxy

修改Kube-proxy 預設配置

nano /etc/kubernetes/config

KUBE_MASTER="--master=http://k8s-master:8080"

3.5      配置kubelet

修改各節點的kubelet配置

nano /etc/kubernetes/kubelet

節點1

KUBELET_ADDRESS="--address=0.0.0.0"

KUBELET_PORT="--port=10250"

# change the hostname to this host’s IP address

KUBELET_HOSTNAME="--hostname_override=k8s-slave"

KUBELET_API_SERVER="--api_servers=http://k8s-master:8080"

KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=rhel7/pod-infrastructure:latest"

KUBELET_ARGS=""

節點2

KUBELET_ADDRESS="--address=0.0.0.0"

KUBELET_PORT="--port=10250"

# change the hostname to this host’s IP address

KUBELET_HOSTNAME="--hostname_override=k8s-slave-2"

KUBELET_API_SERVER="--api_servers=http://k8s-master:8080"

KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=rhel7/pod-infrastructure:latest"

KUBELET_ARGS=""

3.6      修改docker啟動引數

修改/usr/lib/systemd/system/docker.service

使docker服務啟動時應用flannel網路

新增

EnvironmentFile=/run/flannel/subnet.env

更改儲存方式為overlay,提高讀寫效能#可選操作

修改/etc/sysconfig/docker-storage

DOCKER_STORAGE_OPTIONS="--storage-driver=overlay"

3.7      啟動Node服務,建立啟動檔案,關閉檔案

Slvae上啟動kube-proxy, kubelet, flannelddocker服務

建立Node服務啟動檔案start-node.sh

$ for SERVICES in kube-proxy kubelet flanneld docker;

 do

    systemctl restart $SERVICES

    systemctl enable $SERVICES

    systemctl status $SERVICES

done