安装k8s实践(多主机未加密)

1、下载需要的安装包。

# wget  https://github.com/etcd-io/etcd/releases/download/v3.3.13/etcd-v3.3.13-linux-amd64.tar.gz
# wget https://dl.k8s.io/v1.14.3/kubernetes.tar.gz

2、安装etcd

# tar -xf etcd-v3.3.13-linux-amd64.tar.gz -C /usr/local/
# cd /usr/local/etcd-v3.3.13-linux-amd64/

# ln -sv /usr/local/etcd-v3.3.13-linux-amd64/etcd /usr/bin/
# ln -sv /usr/local/etcd-v3.3.13-linux-amd64/etcdctl /usr/bin/

测试etcd命令行:
# etcdctl --version
etcdctl version: 3.3.13
API version: 2

创建etcd启动
# vim /usr/lib/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target

[Service]
Type=notify
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf
#ExecStart=/usr/bin/etcd --config-file=/etc/etcd/etcd.conf
ExecStart=/usr/bin/etcd
Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

# mkdir -pv /var/lib/etcd/

启动etcd:
# systemctl daemon-reload
# systemctl start etcd
# systemctl status etcd
● etcd.service - Etcd Server
   Loaded: loaded (/usr/lib/systemd/system/etcd.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2019-06-23 11:44:32 CST; 5s ago
 Main PID: 22055 (etcd)
   CGroup: /system.slice/etcd.service
           └─22055 /usr/bin/etcd

查看etcd状态
# etcdctl cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://localhost:2379
cluster is healthy

3、创建kube-apiserver、kube-controller-manager、kube-scheduler、kubectl

创建运行程序软连接:
ln -sv /usr/local/kubernetes/server/bin/kube-apiserver /usr/bin/
ln -sv /usr/local/kubernetes/server/bin/kube-controller-manager /usr/bin/
ln -sv /usr/local/kubernetes/server/bin/kube-scheduler /usr/bin/
ln -sv /usr/local/kubernetes/server/bin/kubectl /usr/bin/
安装kube-apiserver:
创建kube-apiserver启动文件
 vim /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_ARGSS
Restart=on-failure
RestartSec=5
Type=notify
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

创建kube-apiserver配置文件
# mkdir -pv /etc/kubernetes

# vim /etc/kubernetes/apiserver
KUBE_API_ARGSS="--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/kube-apiserve \
--v=2"
安装kube-controller-manager
创建kube-controller-manager启动文件:
# vim /usr/lib/systemd/system/kube-controller-manager.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_ARGSS
Restart=on-failure
Type=notify
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

创建kube-controller-manager配置文件
# vim /etc/kubernetes/controller-manager
KUBE_CONTROLLER_MANAGER_ARGS="--master=http://192.168.131.130:8080 \
--logtostderr=false \
--log-dir=/var/log/kube-controller-manager  \
--v=2"
安装kube-scheduler:
创建kube-scheduler启动文件:
vim /usr/lib/systemd/system/kube-scheduler.service

[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=kube-apiserver.service
Wants=kube-apiserver.service

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

[Install]
WantedBy=multi-user.target

创建kube-scheduler配置文件
# vim /etc/kubernetes/kube-scheduler
KUBE_SCHEDULER_ARGS="--master=http://192.168.131.130:8080 \
  --logtostderr=false \
  --log-dir=/var/log/kube-scheduler \
  --v=2"

启动相关服务并验证:
启动k8s master相关服务:
# systemctl start kube-apiserver.service kube-controller-manager.service kube-scheduler.service 

验证服务:
#  systemctl status kube-apiserver.service kube-controller-manager.service kube-scheduler.service 
● kube-apiserver.service - Kubernetes API Server
   Loaded: loaded (/usr/lib/systemd/system/kube-apiserver.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2019-06-24 20:16:56 CST; 3h 9min ago
kube-controller-manager.service - Kubernetes Controller Manager
   Loaded: loaded (/usr/lib/systemd/system/kube-controller-manager.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2019-06-23 21:04:38 CST; 1 day 2h ago
● kube-scheduler.service - Kubernetes Scheduler
   Loaded: loaded (/usr/lib/systemd/system/kube-scheduler.service; disabled; vendor preset: disabled)
   Active: active (running) since Sun 2019-06-23 21:06:21 CST; 1 day 2h ago
     Docs: https://github.com/GoogleCloudPlatform/kubernetes

# kubectl get cs
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok                  
etcd-0               Healthy   {"health":"true"}   
controller-manager   Healthy   ok                  

4、安装node(kubelet、proxy)

安装docker-ce:
安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
添加软件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
更新并安装 Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
开启Docker服务
sudo service docker start

# mkdir /etc/docker/

创建docker配置文件:
# cat > /etc/docker/daemon.json <<EOF 
{
"registry-mirrors": ["https://fgl80ig9.mirror.aliyuncs.com","http://04be47cf.m.daocloud.io"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
启动docker服务:
systemctl enable docker && systemctl start docker

安装bubelet、proxy服务准备

# tar -xf kubernetes-server-linux-amd64.tar.tar -C /usr/local/

# mkdir -pv /var/lib/kubelet
# mkdir -pv /etc/kubernetes/
# mkdir -pv /var/log/kubelet
# ln -sv /usr/local/kubernetes/server/bin/kubectl /usr/bin/
# ln -sv /usr/local/kubernetes/server/bin/kubelet /usr/bin/
# ln -sv /usr/local/kubernetes/server/bin/kube-proxy /usr/bin/
安装kubelet:
创建kubelet配置文件
# vim /etc/kubernetes/kubelet

# 日志目录
KUBE_LOG_DIR="--log-dir=/var/log/kubelet" \
# 启用日志标准错误
KUBE_LOGTOSTDERR="--logtostderr=false"
# 日志级别
KUBE_LOG_LEVEL="--v=2"
# Kubelet服务IP地址
NODE_ADDRESS="--address=192.168.131.132"
# Kubelet服务端口
NODE_PORT="--port=10250"
# 自定义节点名称
NODE_HOSTNAME="--hostname-override=192.168.131.132"
# kubeconfig路径,指定连接API服务器
KUBELET_KUBECONFIG="--kubeconfig=/etc/kubernetes/kubelet.kubeconfig"
# 允许容器请求特权模式,默认false
KUBE_ALLOW_PRIV="--allow-privileged=false"
#KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"
# DNS信息
KUBELET_DNS_IP="--cluster-dns=10.254.0.2"
KUBELET_DNS_DOMAIN="--cluster-domain=cluster.local"
# 禁用使用Swap
KUBELET_SWAP="--fail-swap-on=false"
# 修改cgroup
KUBELET_CGROUP_ARGS="--cgroup-driver=systemd"

创建配置文件:

# vim /etc/kubernetes/kubelet.kubeconfig
apiVersion: v1
kind: Config
clusters:
  - cluster:
      server: http://192.168.131.130:8080
    name: local
contexts:
  - context:
      cluster: local
    name: local
current-context: local

创建kubelet启动文件
# vim /usr/lib/systemd/system/kubelet.service
[Unit]
Description=Kubernetes Kubelet
After=docker.service
Requires=docker.service
[Service]
WorkingDirectory=/var/lib/kubelet
EnvironmentFile=-/etc/kubernetes/kubelet
ExecStart=/usr/bin/kubelet \
${KUBE_LOG_DIR}
${KUBE_LOGTOSTDERR} \
${KUBE_LOG_LEVEL} \
${NODE_ADDRESS} \
${NODE_PORT} \
${NODE_HOSTNAME} \
${KUBELET_KUBECONFIG} \
${KUBE_ALLOW_PRIV} \
${KUBELET_DNS_IP} \
${KUBELET_DNS_DOMAIN} \
${KUBELET_SWAP}\
${KUBELET_CGROUP_ARGS}
Restart=on-failure
KillMode=process
[Install]
WantedBy=multi-user.target

启动kubelet
# systemctl daemon-reload 
# systemctl start kubelet
# systemctl status kubelet
● kubelet.service - Kubernetes Kubelet
   Loaded: loaded (/usr/lib/systemd/system/kubelet.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2019-06-24 22:13:50 CST; 3s ago
 Main PID: 42041 (kubelet)
    Tasks: 13
   Memory: 23.6M
   CGroup: /system.slice/kubelet.service
   
master查看node状态:   
# kubectl get nodes
NAME              STATUS   ROLES    AGE    VERSION
192.168.131.132   Ready    <none>   4m2s   v1.14.3
安装和配置 kube-proxy


创建 kube-proxy kubeconfig 文件
# vim /etc/kubernetes/config
KUBE_LOGTOSTDERR="--logtostderr=false"
KUBE_LOG_LEVEL="--v=2"
KUBE_MASTER="--master=http://192.168.131.130:8080"
KUBE_LOG_DIR="--log-dir=/var/log/kube-proxy"

# vim /etc/kubernetes/proxy
###
# kubernetes proxy config
# default config should be adequate
# Add your own!
KUBE_PROXY_ARGS=""

配置kube-proxy启动文件
# vim /usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Kube-Proxy Server
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=network.target

[Service]
EnvironmentFile=-/etc/kubernetes/config
EnvironmentFile=-/etc/kubernetes/proxy
ExecStart=/usr/bin/kube-proxy \
            ${KUBE_LOGTOSTDERR} \
            ${KUBE_LOG_LEVEL} \
            ${KUBE_MASTER} \
			${KUBE_LOG_DIR}
Restart=on-failure
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target

启动kube-proxy:
# systemctl daemon-reload \
# systemctl start kube-proxy
# systemctl status kube-proxy
● kube-proxy.service - Kubernetes Kube-Proxy Server
   Loaded: loaded (/usr/lib/systemd/system/kube-proxy.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2019-06-24 22:57:55 CST; 1s ago
     Docs: https://github.com/GoogleCloudPlatform/kubernetes
 Main PID: 44017 (kube-proxy)
 

发表评论

电子邮件地址不会被公开。 必填项已用*标注