k8s+docker+calico

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s+docker+calico相关的知识,希望对你有一定的参考价值。

实验环境:

enp0s3:192.168.112.149 (master)

enp0s3:192.168.112.42(node1)

enp0s3:192.168.112.249 (node2)

注意:以上操作系统环境都是 Ubuntu Xenial 16.04 (LTS)

一.安装Docker

注意:docker支持64位操作系统,docker 的内核3.10以上版本

1.更新包信息,确保APT使用https方法,并且已安装CA证书

sudo apt-get update

sudo apt-get install apt-transport-https ca-certificates

2.添加新的GPG密钥

sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

3.添加docker源

echo ‘deb https://apt.dockerproject.org/repo ubuntu-xenial main‘ >> /etc/apt/sources.list.d/docker.list

注意:这是Ubuntu Xenial 16.04 (LTS)的安装源,每个版本的源是不一样的具体见官方文档

4.更新源

sudo apt-get update

5.验证APT的存储库是否正确

apt-cache policy docker-engine

注意:出现以上这样的才正确

6.更新源

sudo apt-get update

7.安装镜像

sudo apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual

8.安装docker

sudo apt-get install docker-engine

9.启动docker守护进程

sudo service docker start

10.拉取hello-world镜像验证docker是否正确

sudo docker run hello-world

二.在每个节点都要操作

1.拉取镜像(注意:不是root用户及时sudo)

docker pull calico/node:v0.23.0

docker pull tristan129/pause-amd64:3.0

docker tag tristan129/pause-amd64:3.0 gcr.io/google_containers/pause-amd64:3.0

将附件里的kubernets_wangjun.tar 放到每个节点的家目录下并解压

cp master/calico-node.service /etc/systemd/

systemctl enable /etc/systemd/calico-node.service

cp master/calicoctl.0.23.0 master/kubectl.1.4.3 master/kubelet.1.4.3 /usr/bin/

ln -snf kubelet.1.4.3 kubelet

ln -snf kubectl.1.4.3 kubectl

ln -snf calicoctl.0.23.0 calicoctl

mkdir -p /etc/kubernetes/manifests

sudo mkdir -p /etc/kubernetes/{manifests,ssl}

编辑 /etc/profile

export MASTER_NODE_IP=‘填写master ip‘

export NODE_IP=‘填写node ip‘

三.TLS认证

.master节点上生成证书

openssl genrsa -out ca-key.pem 2048

openssl req -x509 -new -nodes -key ca-key.pem -days 1000 -out ca.pem -subj "/CN=kube-ca"

openssl genrsa -out apiserver-key.pem 2048

openssl req -new -key apiserver-key.pem -out apiserver.csr -subj "/CN=kube-apiserver" -config openssl.cnf      

这里的openssl.conf路径是在提供的tar包里的master路径下

openssl x509 -req -in apiserver.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out apiserver.pem -days 1000 -extensions v3_req -extfile openssl.cnf 这里的openssl.conf路径是在提供的tar包里的master路径下

cp *.pem /etc/kubernetes/ssl/

cp master/kubernetes-master.manifest /etc/kubernetes/manifests/

cp master/ calico-etcd.manifest /etc/kubernetes/ssl

启动服务

systemctl start calico-node

这时候calico并不会起来,因为连接不到etcd的6666端口

检查calcio服务状态

sudo systemctl status calico-node.service

可以看下日志 /var/log/syslog

Nov 22 15:56:08 master calicoctl[3045]: ERROR: Could not connect to etcd at 192.168.112.123:6666: Connection to etcd failed due to MaxRetryError("HTTPConnectionPool(host=‘192.168.112.123‘, port=6666): Max retries exceeded with url: /version (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x7f962f0e1510>: Failed to establish a new connection: [Errno 111] Connection refused‘,))",)

Nov 22 15:56:08 master systemd[1]: calico-node.service: Main process exited, code=exited, status=1/FAILURE

Nov 22 15:56:08 master systemd[1]: calico-node.service: Unit entered failed state.

Nov 22 15:56:08 master systemd[1]: calico-node.service: Failed with result ‘exit-code‘.

Nov 22 15:56:18 master systemd[1]: calico-node.service: Service hold-off time over, scheduling restart.

Nov 22 15:56:18 master systemd[1]: Stopped Calico per-node agent.

Nov 22 15:56:18 master systemd[1]: Started Calico per-node agent.

Nov 22 15:56:18 master calicoctl[3066]: ERROR: Could not connect to etcd at 192.168.112.123:6666: Connection to etcd failed due to MaxRetryError("HTTPConnectionPool(host=‘192.168.112.123‘, port=6666): Max retries exceeded with url: /version (Caused by NewConnectionError(‘<urllib3.connection.HTTPConnection object at 0x7f6d36d0e510>: Failed to establish a new connection: [Errno 111] Connection refused‘,))",)

Nov 22 15:56:18 master systemd[1]: calico-node.service: Main process exited, code=exited, status=1/FAILURE

Nov 22 15:56:18 master systemd[1]: calico-node.service: Unit entered failed state.

Nov 22 15:56:18 master systemd[1]: calico-node.service: Failed with result ‘exit-code‘.

cp master/kubeletserver /etc/systemd/

systemctl enable kubelet.service

systemctl start kubelet.service

此时你docker ps 应该看到 apiserver proxy scheduler controlle pause etcd (2个,一个被kubernetes集群使用另外一个备calico集群使用)

在node节点上

cd client_node/

生成私钥和证书请求文件

openssl genrsa -out worker-key.pem 2048

mv worker-key.pem /etc/kubernetes/ssl

chmod 600 /etc/kubernetes/ssl/worker-key.pem

chown root:root /etc/kubernetes/ssl/worker-key.pem

export WORKER_IP=这里填节点IP

openssl req -new -key worker-key.pem -out worker.csr -subj "/CN=worker-key" -config worker-openssl.cnf

需要将worker.csr 发送到maste节点上的进行签名

scp worker.csr ${MASTER_NODE_IP}/../master/

四.在maser节点上签名

export WORKER_IP=这里填节点IP

openssl x509 -req -in worker.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out worker.pem -days 365 -extensions v3_req -extfile worker-openssl.cnf/

scp apiserver-key.pem apiserver.pem ca.pem worker-key.pem worker.pem 拷贝到节点的/etc/kubernetes/ssl/

五.在node节点上

mkdir -p /etc/cni/net.d

mkdir -p /opt/cni/bin

cp client_node/10-calico.conf /etc/cni/net.d 注意替换${MASTER_NODE_IP}

cp client_node/cni_network.tar /opt/cni/bin 并解压

cp client_node/kubelet.service /etc/systemd/

cp client_node/network-environment /etc/ 注意替换${MASTER_NODE_IP} 和${NODE_IP}

export ETCD_AUTHORITY=${MASTER_NODE_IP}:6666 注意替换${MASTER_NODE_IP}

calico pool remove 192.168.0.0/16

calico pool add 172.26.0.0/16

calico pool show

systemctl enable /etc/systemd/kubelet.service

systemctl start systemd/kubelet

这个时候docker ps 应该看到 proxy 进程已经起来了

六 安装额外组件 dashboard及kube-dns在master 上操作

cd addon

kubectl create -f kubernetes-dashboard.yaml

kubectl create -f kubdns-rc.yaml

kubectl create -f kubdns-svc.yaml

完毕

在集群中任意节点上可以访问cluster ip 及 pod ip

calico status 可以看到各节点之间为bgp全互联关系,我们可以关闭全互联,作为三层交换机的RR client . 这样我们的容器可以访问到集群外的应用或者你访问使用snat

calico pool add 2.26.0.0/16 --nat-outgoing

这时候你的容器就可以访问外部应用了。

如果外部需要访问你的服务可以使用node port 或者loadblance的方式,详细内容见V2将更性使用BGP 反射器的结构及master的高可用。

以上是关于k8s+docker+calico的主要内容,如果未能解决你的问题,请参考以下文章

如何使用Calico实现跨主机Docker网络通信

云原生-K8skubeadm搭建k8s集群1.25版本完整教程docker网络插件calico中间层cri-docker

16.kubernetes笔记 CNI网络插件(二) Calico介绍

云主机上的k8s集群通信故障mtu

k8s之calico网络

k8s calico flannel cilium 网络性能测试