操作环境为VMware 虚拟机

部署前最好 yum update 更新一下系统的服务(时间较长)

#1、能访问外网

#2、关闭防火墙;
systemctl stop firewalld && systemctl disable firewalld

#3、关闭SELinux;
setenforce 0

#4、关闭Swap交换分区;
swapoff -a

#5、master与node之间ssh免密登录;

#设置hostname
hostnamectl set-hostname m1

#配置hosts文件
192.168.18.136 m1
192.168.18.137 n1
192.168.18.138 n2


#在本机(主节点)自动以rsa算法生成公私钥对
ssh-keygen -t rsa -f /root/.ssh/id_rsa -P "" 

#配置公钥到其他节点,输入对方密码即可完成免密访问
ssh-copy-id n1 
ssh-copy-id n2


#6、同步NTP时间;(这里只是简单同步公网时间,没有搭建自己的ntp服务器)
timedatectl set-timezone Asia/Shanghai (这里是设置亚洲上海上海)
systemctl restart chronyd

#7、配置kubernetes源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
        https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
EOF



##############     以上步骤每台服务器都需要操作   ###############


#8、master上yum安装etcd kubernetes-master
#安装kubernetes时也可以不用区分master或node,直接使用 yum -y install kubernetes 会同时安装master和node包
yum -y install etcd kubernetes-master  #这个不会安装docker



#9、修改etcd配置文件
vim  /etc/etcd/etcd.conf


ETCD_NAME=myetcd                                                                                                                                                                  
ETCD_DATA_DIR="/var/lib/etcd/etcd3"

ETCD_LISTEN_PEER_URLS="http://192.168.20.73:2380"

ETCD_LISTEN_CLIENT_URLS="http://127.0.0.1:2379,http://192.168.20.73:2379"

ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.20.73:2380"

ETCD_INITIAL_CLUSTER="myetcd=http://192.168.20.73:2380"

ETCD_INITIAL_CLUSTER_STATE="new"

ETCD_INITIAL_CLUSTER_TOKEN="etcd-test"

ETCD_ADVERTISE_CLIENT_URLS="http://192.168.20.73:2379"

#10、启动服务
systemctl start etcd
systemcet enable etcd 

# 检查etcd cluster状态
etcdctl cluster-health


#11、生成密钥,k8s大部分访问都需要证书认证:
openssl genrsa -out /etc/kubernetes/serviceaccount.key 2048


#12、配置master服务

#kube-apiserver配置文件
[root@master ~]# cat /etc/kubernetes/config
KUBE_LOGTOSTDERR="--logtostderr=true"
KUBE_LOG_LEVEL="--v=0"
KUBE_ALLOW_PRIV="--allow-privileged=false"
KUBE_MASTER="--master=http://192.168.20.73:8080"

[root@master ~]# cat /etc/kubernetes/apiserver
## kubernetes system config
KUBE_API_ADDRESS="--address=0.0.0.0"
KUBE_API_PORT="--port=8080"
KUBELET_PORT="--kubelet-port=10250"
KUBE_ETCD_SERVERS="--etcd-servers=http://192.168.20.73:2379"
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"
KUBE_API_ARGS="--service_account_key_file=/etc/kubernetes/serviceaccount.key"
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota"
KUBE_API_ARGS=""


#controller-manager配置文件
[root@master ~]# cat /etc/kubernetes/controller-manager
KUBE_CONTROLLER_MANAGER_ARGS="--service_account_private_key_file=/etc/kubernetes/serviceaccount.key"

#scheduler配置文件
[root@master ~]# cat /etc/kubernetes/scheduler
KUBE_SCHEDULER_ARGS=""


#在etcd服务上首先需要添加网络,即在master上
[root@master ~]# etcdctl set /k8s/network/config '{"Network": "10.255.0.0/16"}'
{"Network": "10.255.0.0/16"}
[root@master ~]# etcdctl get /k8s/network/config
{"Network": "10.255.0.0/16"}

#此配置是将数据写入到etcd数据目录中的,不会生成实际目录


#13、配置node节点

#配置node1节点,网络控件采用flannel方式
#yum 安装kubernetes-node 和 flannel
yum -y install kubernetes-node flannel

#安装成功后,修改配置文件
[root@node1 ~]# grep -v '^#' /etc/sysconfig/flanneld
FLANNEL_ETCD_ENDPOINTS="http://192.168.20.73:2379"
FLANNEL_ETCD_PREFIX="/k8s/network"
FLANNEL_OPTIONS="--iface=ens192"
# 网卡信息用ip a命令获取

#配置node1
[root@node1 ~]# ls /etc/kubernetes/
config  kubelet  proxy

[root@node1 ~]# cat /etc/kubernetes/config
# kubernetes system config
KUBE_LOGTOSTDERR="--logtostderr=true"
KUBE_LOG_LEVEL="--v=0"
KUBE_ALLOW_PRIV="--allow-privileged=false"
KUBE_MASTER="--master=http://192.168.20.73:8080"

[root@node1 ~]# cat /etc/kubernetes/kubelet
KUBELET_ADDRESS="--address=192.168.20.74"
KUBELET_PORT="--port=10250"
KUBELET_HOSTNAME="--hostname-override=192.168.20.74"
KUBELET_API_SERVER="--api-servers=http://192.168.20.73:8080"
#KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=192.168.200.10/source/pause-amd64:3.1"
KUBELET_ARGS=""

[root@node1 ~]# cat /etc/kubernetes/proxy
KUBE_PROXY_ARGS="0.0.0.0"

##备注 kubelet配置文件,默认image下载方式是外网下载,可以修改为自己的harbor地址



#14、按顺序启动master 并加入开机启动
systemctl enable kube-apiserver&& systemctl start kube-apiserver
systemctl enable kube-controller-manager&& systemctl start kube-controller-manager
systemctl enable kube-scheduler&& systemctl start kube-scheduler

#启动node上的服务
systemctl enable docker && systemctl start docker
systemctl enable flanneld && systemctl start flanneld 
systemctl enable kubelet && systemctl start kubelet 
systemctl enable kube-proxy && systemctl start kube-proxy




附:

systemctl set-default multi-user.target  //设置成命令模式

systemctl set-default graphical.target  //设置成图形模式

参考资料:

https://blog.csdn.net/xtss999/article/details/105061136

https://www.cnblogs.com/fuhai0815/p/9687183.html

https://www.cnblogs.com/heian99/p/12182018.html

 

 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐