二进制安装k8s之网络安装

一、安装网络插件flannel

所有的node节点都需要安装网络插件才能让所有的Pod加入到同一个局域网中。

yum install -y flannel
vim /usr/lib/systemd/system/flanneld.service
	[Unit]
	Description=Flanneld overlay address etcd agent
	After=network.target
	After=network-online.target
	Wants=network-online.target
	After=etcd.service
	Before=docker.service
	
	[Service]
	Type=notify
	EnvironmentFile=/etc/sysconfig/flanneld
	EnvironmentFile=-/etc/sysconfig/docker-network
	ExecStart=/usr/bin/flanneld-start \
	  -etcd-endpoints=${FLANNEL_ETCD_ENDPOINTS} \
	  -etcd-prefix=${FLANNEL_ETCD_PREFIX} \
	  $FLANNEL_OPTIONS
	
	ExecStartPost=/usr/libexec/flannel/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/docker
	Restart=on-failure
	
	[Install]
	WantedBy=multi-user.target
	WantedBy=docker.service
	
vim /etc/sysconfig/flanneld
	# Flanneld configuration options  
	
	# etcd url location.  Point this to the server where etcd runs
	FLANNEL_ETCD_ENDPOINTS="https://192.168.80.112:2379,https://192.168.80.130:2379,https://192.168.80.146:2379"
	
	# etcd config key.  This is the configuration key that flannel queries
	# For address range assignment
	FLANNEL_ETCD_PREFIX="/kube-centos/network"
	
	# Any additional options that you want to pass
	FLANNEL_OPTIONS="-etcd-cafile=/opt/etcd_ca/ca.pem -etcd-certfile=/opt/etcd_ca/server.pem -etcd-keyfile=/opt/etcd_ca/server-key.pem"

#在etcd库中为flannel使用的网络配置
etcdctl mkdir /kube-centos/network
etcdctl mk /kube-centos/network/config '{"Network":"172.30.0.0/16","SubnetLen":24,"Backend":{"Type":"hostgw"}}'

systemctl daemon-reload
systemctl enable flanneld
systemctl start flanneld
systemctl status flanneld

#让docker使用网络插件所配置的网络
#在/usr/lib/systemd/system/docker.service中的[service]添加下面一行
EnvironmentFile=/run/flannel/docker
systemctl daemon-reload
systemctl enable docker
systemctl start docker
systemctl status docker

试着创建pod进行 node节点和pod互ping  pod之间互ping  

二、安装coreDNS

为k8s集群安装CoreDNS插件用于实现域名解析的工作
在master节点上执行

mkdir /opt/coredns  && cd /opt/coredns/
wget https://raw.githubusercontent.com/coredns/deployment/master/kubernetes/deploy.sh
wget https://raw.githubusercontent.com/coredns/deployment/master/kubernetes/coredns.yaml.sed
chmod +x deploy.sh
#修改$DNS_DOMAIN、$DNS_SERVER_IP变量为实际值,并修改image后面的镜像。这里直接用deploy.sh脚本进行修改:
./deploy.sh -s -r 10.0.0.0/16 -i 10.0.0.2 -d cluster.local > coredns.yaml

[root@192-168-80-112 coredns]# kubectl create -f coredns.yaml

serviceaccount/coredns created
clusterrole.rbac.authorization.k8s.io/system:coredns created
clusterrolebinding.rbac.authorization.k8s.io/system:coredns created
configmap/coredns created
deployment.apps/coredns created
service/kube-dns created

[root@192-168-80-112 coredns]# kubectl get svc,pod -n kube-system
NAME               TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
service/kube-dns   ClusterIP   10.0.0.2     <none>        53/UDP,53/TCP,9153/TCP   19s

NAME                           READY   STATUS    RESTARTS   AGE
pod/coredns-55f46dd959-rdsf7   1/1     Running   0          19s
pod/coredns-55f46dd959-vwxcd   1/1     Running   0          19s

node节点中kubelet配置使用coredns
在/opt/kubernetes/cfg/kubelet文件添加如下三行
–cluster-dns=10.0.0.2
–cluster-domain=cluster.local.
–resolv-conf=/etc/resolv.conf \

验证coredns工作

kubectl run busybox --replicas=2 --labels="run=load-balancer-example" --image=busybox  --port=80 --command sleep 3600

[root@192-168-80-112 coredns]# kubectl exec -it  busybox-654f446c66-92s79 /bin/sh

发布了40 篇原创文章 · 获赞 2 · 访问量 2064

猜你喜欢

转载自blog.csdn.net/weixin_42155272/article/details/92633888