前言:K8S組件概念
etcd - A highly available key-value store for shared configuration and service discovery.
flannel - An etcd backed network fabric for containers.
kube-apiserver - Provides the API for Kubernetes orchestration.
kube-controller-manager - Enforces Kubernetes services.
kube-scheduler - Schedules containers on hosts.
kubelet - Processes a container manifest so the containers are launched according to how they are described.
kube-proxy - Provides network proxy services.
第一部分:節(jié)點(diǎn)規(guī)劃及準(zhǔn)備
- 搭建4臺(tái)服務(wù)器,最小化安裝Centos 7 x64系統(tǒng),以下為架構(gòu)示意圖而账,對(duì)應(yīng)地址及域名為
d181.contoso.com/172.16.3.181
d182.contoso.com/172.16.3.182
d183.contoso.com/172.16.3.183
d184.contoso.com/172.16.3.184
網(wǎng)關(guān)及DNS服務(wù)器為172.16.3.1
- 關(guān)閉防火墻
所有節(jié)點(diǎn)上
$ systemctl stop firewalld
$ systemctl disable firewalld
第二部分:安裝master
以下操作在d181節(jié)點(diǎn)
- 安裝軟件包胰坟,以下命令會(huì)自動(dòng)安裝docker
$ yum -y install etcd kubernetes
- 配置etcd
$ 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"
- 配置api server
$ vim /etc/kubernetes/apiserver
KUBE_API_ADDRESS="--insecure-bind-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"
# 注意去掉了ServiceAccount防止報(bào)錯(cuò)
KUBE_ADMISSION_CONTROL="--admission_control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"
KUBE_API_ARGS=""
- 啟動(dòng)服務(wù)
$ for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do
systemctl restart $SERVICES
systemctl enable $SERVICES
systemctl status $SERVICES
done
- 定義flannel網(wǎng)絡(luò)
$ etcdctl mk /atomic.io/network/config '{"Network":"172.27.0.0/16"}'
- 測試
$ kubectl get nodes
結(jié)果應(yīng)該什么都沒有,因?yàn)檫€未啟動(dòng)節(jié)點(diǎn)服務(wù)
第三部分:安裝nodes
以下操作在其他節(jié)點(diǎn)
- 安裝軟件包
$ yum -y install flannel kubernetes
- 配置與etcd連接
vim /etc/sysconfig/flanneld
FLANNEL_ETCD_ENDPOINTS="http://d181.contoso.com:2379"
- 配置與apiserver連接
vim /etc/kubernetes/config
KUBE_MASTER="--master=http://d181.contoso.com:8080"
- d182配置kubelet服務(wù)
vim /etc/kubernetes/kubelet
KUBELET_ADDRESS="--address=0.0.0.0"
KUBELET_PORT="--port=10250"
# change the hostname to this host’s IP address
KUBELET_HOSTNAME="--hostname_override=d182.contoso.com"
KUBELET_API_SERVER="--api_servers=http://d181.contoso.com:8080"
#KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"
KUBELET_ARGS="--pod-infra-container-image=kubernetes/pause --cluster-domain=contoso.com --cluster-dns=172.16.3.1"
上句如果采用本地鏡像有庫則為
KUBELET_ARGS="--pod-infra-container-image=docker.contoso.com:5000/pause-amd64.3.0 --cluster-domain=contoso.com --cluster-dns=172.16.3.1"
- d183配置kubelet服務(wù)
vim /etc/kubernetes/kubelet
KUBELET_ADDRESS="--address=0.0.0.0"
KUBELET_PORT="--port=10250"
# change the hostname to this host’s IP address
KUBELET_HOSTNAME="--hostname_override=d183.contoso.com"
KUBELET_API_SERVER="--api_servers=http://d181.contoso.com:8080"
#KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"
KUBELET_ARGS="--pod-infra-container-image=kubernetes/pause --cluster-domain=contoso.com --cluster-dns=172.16.3.1"
- d184配置kubelet服務(wù)
vim /etc/kubernetes/kubelet
KUBELET_ADDRESS="--address=0.0.0.0"
KUBELET_PORT="--port=10250"
# change the hostname to this host’s IP address
KUBELET_HOSTNAME="--hostname_override=d184.contoso.com"
KUBELET_API_SERVER="--api_servers=http://d181.contoso.com:8080"
#KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"
KUBELET_ARGS="--pod-infra-container-image=kubernetes/pause --cluster-domain=contoso.com --cluster-dns=172.16.3.1"
- 啟動(dòng)服務(wù)
$ for SERVICES in kube-proxy kubelet docker flanneld; do
systemctl restart $SERVICES
systemctl enable $SERVICES
systemctl status $SERVICES
done
- 在每個(gè)節(jié)點(diǎn)檢查
$ ip a | grep flannel | grep inet
會(huì)發(fā)現(xiàn)每個(gè)節(jié)點(diǎn)都有新的網(wǎng)絡(luò)接口docker0和flannel0
d181節(jié)點(diǎn)檢查應(yīng)可看到其他節(jié)點(diǎn)已經(jīng)啟動(dòng)
$ kubectl get nodes
第四部分:創(chuàng)建pods
以下操作在d181節(jié)點(diǎn)
- 編寫mysql配置文件
$ mkdir pods
$ cd pods
$ vim mysql.yaml
apiVersion: v1
kind: Pod
metadata:
name: mysql-pod
labels:
name: mysql-pod
spec:
containers:
- resources:
limits :
cpu: 1
image: mysql
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: 1a.genius
ports:
- containerPort: 3306
name: mysql
- 創(chuàng)建pods
$ kubectl create -f mysql.yaml
- 測試
查看pod在哪個(gè)node上創(chuàng)建
$ kubectl get pods
$ kubectl get pod mysql -o wide
$ kubectl describe pod mysql
在上面查找到的節(jié)點(diǎn)上運(yùn)行
docker ps -a
注:發(fā)現(xiàn)有兩個(gè)2個(gè)容器泞辐,一個(gè)是mysql笔横,一個(gè)是pause,pause是Netowrk Container, 每啟動(dòng)一個(gè)Pod都會(huì)附加啟動(dòng)這樣一個(gè)容器咐吼,它的作用就只是簡單的等待吹缔,設(shè)置Pod的網(wǎng)絡(luò)
獲得pod更詳細(xì)的信息
kubectl get pod mysql -o yaml
kubectl get pod mysql -o json
手動(dòng)停止容器測試
docker stop $(docker ps -a -q)
注:發(fā)現(xiàn)k8s會(huì)自動(dòng)重新生成新容器,但如果關(guān)閉pod所在node再重啟該node锯茄,在d181上再查詢則發(fā)現(xiàn)pod會(huì)被銷毀厢塘,且不會(huì)自動(dòng)在其它node上創(chuàng)建新的pod
如何刪除pod
kubectl delete pod mysql-pod
第五部分:創(chuàng)建RC
注:創(chuàng)建rc時(shí)會(huì)自動(dòng)創(chuàng)建pod,所以運(yùn)行該命令前應(yīng)刪除上一步創(chuàng)建的pod
意義:Replication Controller可確保任何時(shí)候Kubernetes集群中有指定數(shù)量的pod副本
在d181節(jié)點(diǎn)運(yùn)行
- 編寫mysql rc文件
$ vim mysql-controller.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: mysql-controller
labels:
name: mysql-controller
spec:
replicas: 2
selector:
name: mysql-pod
template:
metadata:
labels:
name: mysql-pod
spec:
containers:
- name: mysql
image: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: 1a.genius
ports:
- containerPort: 3306
name: mysql
- 創(chuàng)建rc
kubectl create -f mysql-controller.yaml
- 查詢r(jià)c
kubectl get rc
kubectl get rc mysql-controller
kubectl describe rc mysql-controller
- 查詢pods
kubectl get pods
kubectl get pods -o wide
注:可見到在兩個(gè)node上創(chuàng)建了pod
- 擴(kuò)大副本數(shù)量
kubectl scale rc mysql-controller --replicas=3
kubectl get pods -o wide
- 減小副本數(shù)量
kubectl scale rc mysql-controller --replicas=1
kubectl get pods -o wide
- 刪除測試
刪除容器測試
docker rm <容器ID>
注:刪除某個(gè)容器肌幽,會(huì)馬上自動(dòng)啟動(dòng)新的容器
刪除rc不影響pod
kubectl delete rc mysql-controller
刪除rc及其下所有的pod
kubectl delete -f mysql-controller.yaml
第六部分:創(chuàng)建services(master節(jié)點(diǎn))
注:創(chuàng)建service前最好已創(chuàng)建好pod
每個(gè)Pod都會(huì)分配一個(gè)單獨(dú)的IP地址晚碾,但這個(gè)IP地址會(huì)隨著Pod的銷毀而消失。Service解決通過一個(gè)IP訪問一組pod
在d181節(jié)點(diǎn)運(yùn)行
- 編寫mysql service文件
$ vim mysql-service.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql-service
labels:
name: mysql-service
spec:
externalIPs:
- 172.16.3.182
ports:
- port: 3306
selector:
name: mysql-pod
注:externalIP無所謂是哪一個(gè)地址喂急,但一定是集群中實(shí)實(shí)在在的地址
- 創(chuàng)建service
$ kubectl create -f mysql-service.yaml
- 查詢
kubectl get services
kubectl get endpoints
注:mysql-service顯示兩個(gè)地址格嘁,一個(gè)地址是10.254.*是內(nèi)部地址,另一個(gè)是外部地址
- 刪除服務(wù)
kubectl delete -f mysql-service.yaml
- 客戶端測試
mysql -uroot -p -h172.16.3.182
MySQL [(none)]> show variables like '%version%';