Kubernetes中怎么集成docker-成都快上网建站

Kubernetes中怎么集成docker

Kubernetes中怎么集成docker,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联公司于2013年创立,先为渝北等服务建站,渝北等地企业,进行企业商务咨询服务。为渝北企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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 10.254.0.2 443/TCP

kubernetes-ro component=apiserver,provider=kubernetes 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问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


当前标题:Kubernetes中怎么集成docker
网站路径:http://kswjz.com/article/ipcdsh.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流