您好,登錄后才能下訂單哦!
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 -h292.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)
關于Kubernetes中怎么集成docker問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。