1. k8s集群环境搭建之master节点

准备好机器

安装etcd

[Unit]
Description=Etcd Server
After=network.target

[Service]
Type=simple
# etcd 数据存储目录
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf
ExecStart=/usr/bin/etcd

[Install]
WantedBy=multi-user.target
  • 创建目录/var/lib/etcd/,否则会启动失败
  • 启动etcd
systemctl enabled etcd.service
systemctl start etcd.service

若启动不成功,可通过systemctl status etcd查看

安装kube-apiserver kube-controller-manager kube-scheduler

  • 下载k8s二进制文件
    链接: https://pan.baidu.com/s/15n7ackiDQ4hxwYkHB5--uA
    提取码: 97e9
  • 复制kube-apiserver kube-controller-manager kube-scheduler文件到/usr/bin中
  • 创建kube-apiserver.service
    vi /usr/lib/systemd/system/kube-apiserver.service
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=etcd.service
Wants=etcd.service

[Service]
EnvironmentFile=/etc/kubernetes/apiserver
ExecStart=/usr/bin/kube-apiserver $KUBE_API_ARGS
Restart=on-failure
Type=notify
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
  • 新增文件/etc/kubernetes/apiserver,该文件中包括了kube-apiserver的全部启动参数(具体参数含义参kubernetes权威指南54页)
KUBE_API_ARGS="--storage-backend=etcd3 --etcd-servers=http://127.0.0.1:2379 --insecure-bind-address=0.0.0.0   --insecure-port=8080 --service-cluster-ip-range=169.169.0.0/16 --service-node-port-range=1-65535 --admission_control=NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,ResourceQuota --logtostderr=false --log-dir=/var/log/kubernetes --v=2"
  • 启动apiserver
systemctl daemon-reload
systemctl enable kube-apiserver.service
systemctl start kube-apiserver.service
systemctl status kube-apiserver.service

若启动不成功,通过curl http://{config中的etcd ip}:2379测试etcd连接是否正常

  • 创建文件/usr/lib/systemd/system/kube-controller-manager.service
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=kube-apiserver.service
Requires=kube-apiserver.service

[Service]
EnvironmentFile=/etc/kubernetes/controller-manager
ExecStart=/usr/bin/kube-controller-manager $KUBE_CONTROLLER_MANAGER_ARGS
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
  • 创建配置文件/etc/kubernetes/controller-manager
KUBE_CONTROLLER_MANAGER_ARGS="--master=http://10.254.0.53:8080 --logtostderr=false --log-dir=/var/log/kubernetes --v=2"
  • 启动controller-manager
systemctl daemon-reload
systemctl enable kube-controller-manager
systemctl start kube-controller-manager
systemctl status kube-controller-manager
  • 创建文件/usr/lib/systemd/system/kube-scheduler.service
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=kube-apiserver.service
Requires=kube-apiserver.service

[Service]
EnvironmentFile=/etc/kubernetes/scheduler
ExecStart=/usr/bin/kube-scheduler $KUBE_SCHEDULER_ARGS
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
  • 创建配置文件/etc/kubernetes/scheduler
KUBE_SCHEDULER_ARGS="--master=http://10.254.0.53:8080 --logtostderr=false --log-dir=/var/log/kubernetes --v=2"
  • 启动kube-scheduler服务,并设置自启动
systemctl daemon-reload
systemctl enable 
systemctl start kube-scheduler
systemctl status kube-scheduler

systemctl start kube-scheduler实际表示执行某个二进制文件,若systemctl也可通过/usr/bin/kube-apiserver --storage-backend=etcd3 --etcd-servers=http://127.0.0.1:2379 --insecure-bind-address=0.0.0.0 --insecure-port=8080 --service-cluster-ip-range=169.169.0.0/16 --service-node-port-range=1-655执行,以便查找问题