• 幕客老师召集小伙伴
  • 运维高手36项修炼
  • python自动化运维项目实战
  • nginx从入门到实战
  • 阿里云与Centos7实战

kubernetes最小集群搭建

 

kubernetes最小集群搭建

 

1. 环境

  1. Virtual Box\KVM\云主机

  2. CentOS 7

  3. 虚拟机网络设置

  • 新建虚拟网络A(在主机网络管理菜单),定义网段192.168.56.1/24

  • 修改主机名:hostnamectl set-hostname k8s_master --static

主机名 IP 配置
Master 49.232.173.53 4CPU,8G
node1 81.70.50.88 4CPU,8G
node2 49.232.207.236 4CPU,8G

IP:82.157.16.138、82.157.13.37、82.157.12.111

 

 

2. 执行环境(无特殊说明,全部都要这样做)

  1. 三台主机互通

  2. master ssh passwdless 访问其它两台主机

  3. 打开内核转发

添加net.ipv4.ip_forward 到/etc/sysctl.conf

  1. 修改Docker配置

修改/usr/lib/systemd/system/docker.service

service段增加 ExecStartPost=/sbin/iptables -I FORWARD -s 0.0.0.0/0 -j ACCEPT

修改/etc/sysconfig/docker

OPTIONS="--insecure-registry gcr.io --selinux-enabled=false"

  1. 禁用防火墙
  systemctl stop firewalld
  systemctl disable firewalld
  1. 关闭SElinux

修改/etc/selinux/config

修改SELINUX=disabled

  1. 修改Docker配置,增加镜像(vi /etc/docker/daemon.json)
  {
      "registry-mirrors":["http://hub-mirror.c.163.com"]
  }
  1. 设置时区与时间同步
yum -y install ntp
timedatectl  set-timezone Asia/Shanghai

   10.配置kubernetes源

配置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

 

 

 

 

3. 节点功能规划

Master 部分增加了kube-proxy,第一版忘记加了

主机名 运行的服务
Master docker
etcd
kube-apiserver
flanneld
kube-proxy
kube-controller-manager
kube-scheduler
yum -y install etcd kubernetes-master 
node1 docker
flanneld
kubelet
kube-proxy
yum -y install kubernetes-node flannel
node2 docker
flanneld
kubelet
kube-proxy
yum -y install kubernetes-node flannel

4. Master设置​

9、修改etcd配置文件
vim  /etc/etcd/etcd.conf
(1)替换192.168.20.73ip信息
(2)备份默认配置文件


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

ETCD_LISTEN_PEER_URLS="http://172.21.64.4:2380"

ETCD_LISTEN_CLIENT_URLS="http://www.iaskjob.com:2379,http://172.21.64.4:2379"

ETCD_INITIAL_ADVERTISE_PEER_URLS="http://172.21.64.4:2380"

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

ETCD_INITIAL_CLUSTER_STATE="new"

ETCD_INITIAL_CLUSTER_TOKEN="etcd-test"

ETCD_ADVERTISE_CLIENT_URLS="http://172.21.64.4:2379"

#10、启动服务
systemctl start etcd
systemctl 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://172.21.64.4: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://172.21.64.4: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_MASTER="--master=http://172.21.64.4:8080"
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数据目录中的,不会生成实际目录

 5. 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 status kube-apiserver
systemctl enable kube-controller-manager&& systemctl start kube-controller-manager && systemctl status kube-controller-manager
systemctl enable kube-scheduler&& systemctl start kube-scheduler && systemctl status kube-scheduler

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

5、验证 

在master节点上执行

kubectl get nodes -o wide

NAME          STATUS    AGE       EXTERNAL-IP

172.21.64.2   Ready     40s       <none>

172.21.64.7   Ready     37s       <none>

如果在node节点上执行,需要参考如下配置:

https://www.codeleading.com/article/35215200739/

 

案例:

创建nginx的deployment应用、service

 

 

补充命令:

kubectl describe node 172.21.96.16

kubectl get pods

kubectl describe pod nginx-deployment-2947857529-35gr9

//批量删除Terminating状态容器

kubectl get pods -n default | grep Terminating | awk '{print $1}' | xargs kubectl delete pod -n default --force --grace-period=0

配置阿里云官方镜像加速:

https://developer.aliyun.com/article/29941

//缺失镜像pause-amd64.3.0解决方法,gcr.io被墙解决办法

https://blog.csdn.net/eagleuniversityeye/article/details/102851997

 

https://blog.csdn.net/textdemo123/article/details/97263146

 

kubernetes最小集群搭建

Pingbacks已打开。

引用地址

暂无评论

发表评论