1. 程式人生 > >Kubernetes整合docker的部署

Kubernetes整合docker的部署

K8s 是一個容器叢集管理系統,提供資源排程、均衡容災、服務註冊、動態擴縮容等功能。

Kubernetes元件介紹:

etcd:儲存Kubernetes資訊、儲存各種業務容器資訊等,儲存flannel網路配置資訊,供各節點協調。就是kubernetes的資料庫,

flannel :用於多容器之間跨主機網路通訊

kube-apiserver:k8s的管理介面

kube-controller-manager:執行Kubernetes服務

kube-scheduler:容器的啟動、遷移、擴容縮減排程

kubelet:對容器的實際操作者

kube-proxy :對容器網路反向代理

準備:

1.關閉centos7的防火牆firewalld

$ systemctl stop firewalld

$ systemctl disable firewalld

2.安裝NTP並確保啟用和執行

$ yum -y install ntp

$ systemctl start ntpd

$ systemctl enable ntpd

配置Kubernetes Master:

在master上執行以下步驟

1.通過yum安裝etcd和Kubernetes:

$ yum -y install etcd kubernetes

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"

ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379"

3.配置Kubernetes介面服務,/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=""

4.啟動並啟用etcd、kube-apiserver、kube- controllermanager和kube-scheduler:

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

systemctl restart $SERVICES

systemctl enable $SERVICES

systemctl status $SERVICES

done

5.在etcd中定義flannel配置

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

6.檢查節點狀態,沒有任何資訊因為還沒啟動

$ kubectl get nodes

NAME LABELS STATUS

配置Kubernetes Minions節點:

以下步驟在minion1、minion2和minion3上執行

1.安裝flannel和Kubernetes

$ yum -y install flannel kubernetes

2.配置用於flannel服務的etcd伺服器。更新/etc/sysconfig/flanneld中的以下行連線到master伺服器:

FLANNEL_ETCD="http://192.168.204.30:2379"

3.在/etc/kubernetes/config中配置Kubernetes的主服務介面

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

4.配置/etc/kubernetes/kubelet

minion1:

KUBELET_ADDRESS="--address=0.0.0.0"

KUBELET_PORT="--port=10250"

# change the hostname to this host’s IP address

KUBELET_HOSTNAME="--hostname_override=192.168.204.31"

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

KUBELET_ARGS=""

minion2:

KUBELET_ADDRESS="--address=0.0.0.0"

KUBELET_PORT="--port=10250"

# change the hostname to this host’s IP address

KUBELET_HOSTNAME="--hostname_override=192.168.204.32"

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

KUBELET_ARGS=""

minion3:

KUBELET_ADDRESS="--address=0.0.0.0"

KUBELET_PORT="--port=10250"

# change the hostname to this host’s IP address

KUBELET_HOSTNAME="--hostname_override=192.168.204.33"

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

KUBELET_ARGS=""

5.啟動kube-proxy、kubelet、docker和flanneld服務:

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

systemctl restart $SERVICES

systemctl enable $SERVICES

systemctl status $SERVICES

done

6.檢查每個minion上有docker0和flannel0。在flannel0上可以獲得不同範圍的IP地址

minion1:

$ ip a | grep flannel | grep inet

inet 172.17.59.0/16 scope global flannel0

minion2:

$ ip a | grep flannel | grep inet

inet 172.17.19.0/16 scope global flannel0

minion3:

$ ip a | grep flannel | grep inet

inet 172.17.80.0/16 scope global flannel0

7.檢查節點狀態

$ kubectl get nodes

NAME STATUS AGE

192.168.204.31 Ready 8m

192.168.204.32 Ready 7m

192.168.204.33 Ready 7m

Kubernetes叢集已經配置並運行了。開始配置 pods

配置 pods(Containers)

1.在Kubernetes master中定義一個yaml檔案,使用kubectl命令根據定義建立pods。建立一個mysql.yaml檔案:

$ mkdir pods

$ cd pods

$ vim mysql.yaml

2.新增以下幾行:

apiVersion: v1

kind: Pod

metadata:

name: mysql

labels:

name: mysql

spec:

containers:

- resources:

limits :

cpu: 1

image: mysql

name: mysql

env:

- name: MYSQL_ROOT_PASSWORD

# change this

value: yourpassword

ports:

- containerPort: 3306

name: mysql

3.建立pod

$ kubectl create -f mysql.yaml

4.驗證pod的建立和執行:

$ kubectl get pods

POD IP CONTAINER(S) IMAGE(S) HOST LABELS STATUS CREATED

mysql 172.17.38.2 mysql mysql 192.168.204.32/192.168.204.32 name=mysql Running 3 hours

Kubernetes在192.168.204.32建立了一個Docker容器。現在需要建立一個服務,讓其他pods訪問已知埠和主機上的mysql資料庫。

建立服務

1.在192.168.204.32裡面有一個MySQL pod,定義一個 mysql-service.yaml

apiVersion: v1

kind: Service

metadata:

labels:

name: mysql

name: mysql

spec:

externalIPs:

- 192.168.204.32

ports:

# 這個埠是該服務的埠

- port: 3306

# label keys and values that must match in order to receive traffic for this service

selector:

name: mysql

2.執行服務

$ kubectl create -f mysql-service.yaml

報異常:Error from server (BadRequest): error when creating "mysql.yaml": Pod in version "v1" cannot be handled as a Pod: [pos 177]: json: expect char '"' but got char '1'

3. 檢視服務,10.254.x.x是定義在/etc/kubernetes/apiserver的 Kubernetes內部IP地址,所以定義了連線到外部的網路ip:

$ kubectl get services

NAME LABELS SELECTOR IP PORT(S)

kubernetes component=apiserver,provider=kubernetes <none> 10.254.0.2 443/TCP

kubernetes-ro component=apiserver,provider=kubernetes <none> 10.254.0.1 80/TCP

mysql name=mysql name=mysql 10.254.13.156 3306/TCP

                                                    192.168.204.32

4.外部連線資料庫

$ mysql -uroot -p -h192.168.204.32

MySQL [(none)]> show variables like '%version%';

+-------------------------+------------------------------+

| Variable_name | Value |

+-------------------------+------------------------------+

| innodb_version | 5.6.24 |

| protocol_version | 10 |

| slave_type_conversions | |

| version | 5.6.24 |

| version_comment | MySQL Community Server (GPL) |

| version_compile_machine | x86_64 |

| version_compile_os | Linux |

+-------------------------+------------------------------+

7 rows in set (0.01 sec)