1. 程式人生 > >k8s下叢集搭建

k8s下叢集搭建

一、系統環境centos7系統機器二臺:

    192.168.59.1: 用來安裝kubernetes master

    192.168.59.2: 用作kubernetes minion (minion1)

1.如果系統開啟了防火牆則按如下步驟關閉防火牆(所有機器)

# systemctl stop firewalld

# systemctl disable firewalld

2.關閉selinux

1

2

#setenforce 0

#sed -i '/^SELINUX=/cSELINUX=disabled' /etc/sysconfig/selinux

二、MASTER安裝配置

1. 安裝並配置Kubernetes master(yum 方式)

1

# yum -y install etcd kubernetes

配置etcd。確保列出的這些項都配置正確並且沒有被註釋掉,下面的配置都是如此 

1

2

3

4

5

6

#vim /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"

ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379"

配置kubernetes

1

2

3

4

5

6

7

8

vim /etc/kubernetes/apiserver

  

KUBE_API_ADDRESS="--address=0.0.0.0"

KUBE_API_PORT="--port=8080"

KUBELET_PORT="--kubelet_port=10250"

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=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"

KUBE_API_ARGS=""

2. 啟動etcd, kube-apiserver, kube-controller-manager and kube-scheduler服務

1

# for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do

systemctl restart $SERVICES

systemctl enable $SERVICES

systemctl status $SERVICES

done

 

3. 設定etcd網路

1

#etcdctl -endpoint="http://192.168.59.1:2379" set /coreos.com/network/config  '{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}'

 

檢視狀態:

# kubectl get nodes

# kubectl get cs

 

三、MINION安裝配置(每臺minion機器都按如下安裝配置)

 

1. 環境安裝和配置

1

# yum -y install flannel kubernetes

 

配置kubernetes連線的服務端IP

1

2

3

#vim /etc/kubernetes/config

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

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

 

配置kubernetes  

 

1

2

3

4

5

#vim /etc/kubernetes/kubelet

KUBELET_ADDRESS="--address=0.0.0.0"

KUBELET_PORT="--port=10250"

# change the hostname to this host’s IP address K

UBELET_HOSTNAME="--hostname_override=192.168.59.2"

KUBELET_API_SERVER="--api_servers=http://192.168.59.1:8080"

UBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"

KUBELET_ARGS=""

 

2. 準備啟動服務(如果本來機器上已經執行過docker的請看過來,沒有執行過的請忽略此步驟)

    執行ifconfig,檢視機器的網路配置情況(有docker0)

1

2

3

4

5

# ifconfig docker0

Link encap:Ethernet HWaddr 02:42:B2:75:2E:67 inet addr:172.17.0.1 Bcast:0.0.0.0 Mask:255.255.0.0 UP

BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0

errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0

RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

  warning:在執行過docker的機器上可以看到有docker0,這裡在啟動服務之前需要刪掉docker0配置,在命令列執行:sudo ip link delete docker0

3.配置flannel網路

1

2

3

#vim /etc/sysconfig/flanneld

FLANNEL_ETCD_ENDPOINTS="http://192.168.59.1:2379"

FLANNEL_ETCD_PREFIX="/coreos.com/network"

 

4. 啟動服務

1

# for SERVICES in flanneld kube-proxy kubelet docker; do

systemctl restart $SERVICES

systemctl enable $SERVICES

systemctl status $SERVICES

done

 

四、啟動一個測試示例

啟動一個Nginx副本示例:

# kubectl run nginx --image=nginx --replicas=3

pod正常執行後,建立Service:

# kubectl expose deployment nginx --port=88 --target-port=80 --type=NodePort

# kubectl get svc nginx

在Node節點訪問:

curl <CLUSTER-IP>:PORT