暗中观察

CentOS 安装kubernetes集群(二进制)
1. 安装Docker等(所有节点)1.1 创建k8s所需要目录# sudo yum -y install wge...
扫描右侧二维码阅读全文
06
2019/01

CentOS 安装kubernetes集群(二进制)

hign.png
soft-env.png
server-role.png

1. 安装Docker等(所有节点)

1.1 创建k8s所需要目录

# sudo yum -y install wget unzip tree vim
# sudo mkdir -p /root/k8s/k8s-cert
# sudo mkdir -p /root/k8s/etcd-cert && cd /root/k8s/etcd-cert
# sudo mkdir -p /opt/etcd/{cfg,bin,ssl}
# sudo mkdir -p /opt/kubernetes/{cfg,bin,ssl}

2. 安装Etcd(三个节点)

2.1、自签证书

2.1.1 下载 cfssl证书工具

# wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
# wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
# wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
# chmod +x cfssl_linux-amd64 cfssljson_linux-amd64 cfssl-certinfo_linux-amd64
# mv cfssl_linux-amd64 /usr/local/bin/cfssl
# mv cfssljson_linux-amd64 /usr/local/bin/cfssljson
# mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo

2.1.2 创建ca证书(ca-config.json , ca-csr.json , server-csr.json)

# cat > ca-config.json <<EOF
{
 "signing": {
 "default": {
 "expiry": "87600h"
 },
 "profiles": {
 "www": {
 "expiry": "87600h",
 "usages": [
 "signing",
 "key encipherment",
 "server auth",
 "client auth"
 ]
 }
 }
 }
}
EOF
cat > ca-csr.json <<EOF
{
 "CN": "etcd CA",
 "key": {
 "algo": "rsa",
 "size": 2048
 },
 "names": [
 {
 "C": "CN",
 "L": "Beijing",
 "ST": "Beijing"
 }
 ]
}
EOF
cat > server-csr.json <<EOF
{
 "CN": "etcd",
 "hosts": [
 "192.168.2.73",
 "192.168.2.75",
 "192.168.2.76"
 ],
 "key": {
 "algo": "rsa",
 "size": 2048
 },
 "names": [
 {
 "C": "CN",
 "L": "BeiJing",
 "ST": "BeiJing"
 }
 ]
}
EOF

2.1.3 创建etcd证书

# cfssl gencert -initca ca-csr.json | cfssljson -bare ca -
# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=www server-csr.json | cfssljson -bare server

查看证书:

[root@k8s-master01 etcd-cert]# ll *.pem
-rw-------. 1 root root 1675 Jan  7 23:10 ca-key.pem
-rw-r--r--. 1 root root 1265 Jan  7 23:10 ca.pem
-rw-------. 1 root root 1675 Jan  7 23:11 server-key.pem
-rw-r--r--. 1 root root 1338 Jan  7 23:11 server.pem

2.2、部署

2.2.1 拷贝证书到/opt/etcd/ssl

# cp -r ./*.pem /opt/etcd/ssl/

2.2.2 安装etcd文件

# yum -y install wget unzip tree
# wget https://github.com/etcd-io/etcd/releases/download/v3.3.10/etcd-v3.3.10-linux-amd64.tar.gz
# tar zxvf etcd-v3.3.10-linux-amd64.tar.gz
# mv etcd-v3.3.10-linux-amd64/* /opt/etcd/bin/

2.2.3 创建etcd配置文件和启动配置的脚本etcd.sh

# echo '
#!/bin/bash
# example: ./etcd.sh etcd01 192.168.1.10 etcd02=https://192.168.1.11:2380,etcd03=https://192.168.1.12:2380
ETCD_NAME=$1
ETCD_IP=$2
ETCD_CLUSTER=$3
WORK_DIR=/opt/etcd
cat <<EOF >$WORK_DIR/cfg/etcd
#[Member]
ETCD_NAME="${ETCD_NAME}"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://${ETCD_IP}:2380"
ETCD_LISTEN_CLIENT_URLS="https://${ETCD_IP}:2379"
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://${ETCD_IP}:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://${ETCD_IP}:2379"
ETCD_INITIAL_CLUSTER="${ETCD_NAME}=https://${ETCD_IP}:2380,${ETCD_CLUSTER}"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF
cat <<EOF >/usr/lib/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
EnvironmentFile=${WORK_DIR}/cfg/etcd
ExecStart=${WORK_DIR}/bin/etcd \
--name=\${ETCD_NAME} \
--data-dir=\${ETCD_DATA_DIR} \
--listen-peer-urls=\${ETCD_LISTEN_PEER_URLS} \
--listen-client-urls=\${ETCD_LISTEN_CLIENT_URLS},http://127.0.0.1:2379 \
--advertise-client-urls=\${ETCD_ADVERTISE_CLIENT_URLS} \
--initial-advertise-peer-urls=\${ETCD_INITIAL_ADVERTISE_PEER_URLS} \
--initial-cluster=\${ETCD_INITIAL_CLUSTER} \
--initial-cluster-token=\${ETCD_INITIAL_CLUSTER_TOKEN} \
--initial-cluster-state=new \
--cert-file=${WORK_DIR}/ssl/server.pem \
--key-file=${WORK_DIR}/ssl/server-key.pem \
--peer-cert-file=${WORK_DIR}/ssl/server.pem \
--peer-key-file=${WORK_DIR}/ssl/server-key.pem \
--trusted-ca-file=${WORK_DIR}/ssl/ca.pem \
--peer-trusted-ca-file=${WORK_DIR}/ssl/ca.pem
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable etcd
systemctl restart etcd
'>etcd.sh

2.2.4 拷贝etcd的证书和二进制文件到各节点

# scp -r /opt/etcd root@192.168.2.75:/opt/
# scp -r /opt/etcd root@192.168.2.76:/opt/

2.2.5 各节点执行etcd.sh脚本

在192.168.2.73 执行:

# sh -x etcd.sh etcd01 192.168.2.73 etcd02=https://192.168.2.75:2380,etcd03=https://192.168.2.76:2380 

在192.168.2.75 执行:

# sh -x etcd.sh etcd02 192.168.2.75 etcd01=https://192.168.2.73:2380,etcd03=https://192.168.2.76:2380 

在192.168.2.76 执行:

# sh -x etcd.sh etcd03 192.168.2.76 etcd01=https://192.168.2.73:2380,etcd02=https://192.168.2.75:2380 

2.2.6 验证etcd状态

# cd /opt/etcd/ssl/ && /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.2.73:2379,https://192.168.2.75:2379,https://192.168.2.76:2379"  cluster-health

etcd-healthy.png

3. 安装Flannel (所有节点)

flanneld.png

3.1 预定义子网

3.1.1 创建写入

# cd /opt/etcd/ssl/ && /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.2.73:2379,https://192.168.2.75:2379,https://192.168.2.76:2379"  set /coreos.com/network/config '{ "Network": "172.17.0.0/16", "Backend": {"Type": "vxlan"}}'

3.1.2 验证是否成功

# /opt/etcd/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.2.73:2379,https://192.168.2.75:2379,https://192.168.2.76:2379"  get /coreos.com/network/config

flanneld-config.png

3.2 部署(每个node节点)

3.2.1 下载安装二进制包

# wget https://github.com/coreos/flannel/releases/download/v0.10.0/flannel-v0.10.0-linux-amd64.tar.gz
# tar zxvf flannel-v0.10.0-linux-amd64.tar.gz
# mv flanneld mk-docker-opts.sh /opt/kubernetes/bin/

3.2.2 创建flannel配置文件和启动配置的脚本flannel.sh

# echo '
#!/bin/bash
ETCD_ENDPOINTS=${1:-"http://127.0.0.1:2379"}
cat <<EOF >/opt/kubernetes/cfg/flanneld
FLANNEL_OPTIONS="--etcd-endpoints=${ETCD_ENDPOINTS} \
-etcd-cafile=/opt/etcd/ssl/ca.pem \
-etcd-certfile=/opt/etcd/ssl/server.pem \
-etcd-keyfile=/opt/etcd/ssl/server-key.pem"
EOF
cat <<EOF >/usr/lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld overlay address etcd agent
After=network-online.target network.target
Before=docker.service
[Service]
Type=notify
EnvironmentFile=/opt/kubernetes/cfg/flanneld
ExecStart=/opt/kubernetes/bin/flanneld --ip-masq \$FLANNEL_OPTIONS
ExecStartPost=/opt/kubernetes/bin/mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/subnet.env
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
cat <<EOF >/usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
EnvironmentFile=/run/flannel/subnet.env
ExecStart=/usr/bin/dockerd \$DOCKER_NETWORK_OPTIONS
ExecReload=/bin/kill -s HUP \$MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable flanneld
systemctl restart flanneld
systemctl restart docker
'>flannel.sh

3.2.3 各节点执行flannel.sh脚本

# sh -x flannel.sh https://192.168.2.73:2379,https://192.168.2.75:2379,https://192.168.2.76:2379

3.2.4 验证是否flannel是否生效

确保docker0与flannel.1在同一网段。
测试不同节点互通,在当前节点访问另一个Node节点docker0 IP:
如果能通说明Flannel部署成功。如果不通检查下日志:journalctl -u flannel

flanneld-test1.png

flanneld-test2.png

4. 安装k8s-master (两个节点)

4.1 生成证书

4.1.1 生成ca证书

# cd /root/k8s/k8s-cert

# cat > ca-config.json <<EOF
{
 "signing": {
 "default": {
 "expiry": "87600h"
 },
 "profiles": {
 "kubernetes": {
 "expiry": "87600h",
 "usages": [
 "signing",
 "key encipherment",
 "server auth",
 "client auth"
 ]
 }
 }
 }
}
EOF
cat > ca-csr.json <<EOF
{
 "CN": "kubernetes",
 "key": {
 "algo": "rsa",
 "size": 2048
 },
 "names": [
 {
 "C": "CN",
 "L": "Beijing",
 "ST": "Beijing",
 "O": "k8s",
 "OU": "System"
 }
 ]
}
EOF

# cfssl gencert -initca ca-csr.json | cfssljson -bare ca -

4.1.2 生成apiserver证书

# cat > server-csr.json <<EOF
{
 "CN": "kubernetes",
 "hosts": [
 "10.0.0.1",
 "127.0.0.1",
 "192.168.2.70",
 "192.168.2.73",
 "192.168.2.74",
 "192.168.2.75",
 "192.168.2.76",
 "kubernetes",
 "kubernetes.default",
 "kubernetes.default.svc",
 "kubernetes.default.svc.cluster",
 "kubernetes.default.svc.cluster.local"
 ],
 "key": {
 "algo": "rsa",
 "size": 2048
 },
 "names": [
 {
 "C": "CN",
 "L": "BeiJing",
 "ST": "BeiJing",
 "O": "k8s",
 "OU": "System"
 }
 ]
}
EOF

注:以上192.168.2.70 为后续用到的高可用vip
192.168.2.73和192.168.2.74两个节点为master
192.168.2.75和192.168.2.76两个为nginx,keepalived节点

# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server

4.1.3 生成node节点的证书

# cat > kube-proxy-csr.json <<EOF
{
 "CN": "system:kube-proxy",
 "hosts": [],
 "key": {
 "algo": "rsa",
 "size": 2048
 },
 "names": [
 {
 "C": "CN",
 "L": "BeiJing",
 "ST": "BeiJing",
 "O": "k8s",
 "OU": "System"
 }
 ]
}
EOF

# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy

# cp ca*pem server*pem /opt/kubernetes/ssl/

4.2 部署apiserver组件

4.2.1 安装k8s

# wget https://dl.k8s.io/v1.12.4/kubernetes-server-linux-amd64.tar.gz
# tar zxvf kubernetes-server-linux-amd64.tar.gz
# cd /root/k8s/k8s-cert/kubernetes/server/bin/
# cp kube-apiserver kube-scheduler kube-controller-manager kubectl /opt/kubernetes/bin

4.2.2 拷贝node节点需要的

# scp /root/k8s/k8s-cert/kubernetes/server/bin/{kubelet,kube-proxy} root@192.168.2.75:/opt/kubernetes/bin/
# scp /root/k8s/k8s-cert/kubernetes/server/bin/{kubelet,kube-proxy} root@192.168.2.76:/opt/kubernetes/bin/

4.2.3 创建token

# BOOTSTRAP_TOKEN=$(head -c 16 /dev/urandom | od -An -t x | tr -d ' ')
# cat > /opt/kubernetes/cfg/token.csv <<EOF
${BOOTSTRAP_TOKEN},kubelet-bootstrap,10001,"system:kubelet-bootstrap"
EOF

注: # 第一列:随机字符串,自己可生成 第二列:用户名 第三列:UID 第四列:用户组

4.2.4 创建apiserver.sh 脚本

# cd /root/k8s/k8s-cert/
# echo '
#!/bin/bash
MASTER_ADDRESS=$1
ETCD_SERVERS=$2
cat <<EOF >/opt/kubernetes/cfg/kube-apiserver
KUBE_APISERVER_OPTS="--logtostderr=true \\
--v=4 \\
--etcd-servers=${ETCD_SERVERS} \\
--bind-address=${MASTER_ADDRESS} \\
--secure-port=6443 \\
--advertise-address=${MASTER_ADDRESS} \\
--allow-privileged=true \\
--service-cluster-ip-range=10.0.0.0/24 \\
--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,ResourceQuota,NodeRestriction \\
--authorization-mode=RBAC,Node \\
--kubelet-https=true \\
--enable-bootstrap-token-auth \\
--token-auth-file=/opt/kubernetes/cfg/token.csv \\
--service-node-port-range=30000-50000 \\
--tls-cert-file=/opt/kubernetes/ssl/server.pem \\
--tls-private-key-file=/opt/kubernetes/ssl/server-key.pem \\
--client-ca-file=/opt/kubernetes/ssl/ca.pem \\
--service-account-key-file=/opt/kubernetes/ssl/ca-key.pem \\
--etcd-cafile=/opt/etcd/ssl/ca.pem \\
--etcd-certfile=/opt/etcd/ssl/server.pem \\
--etcd-keyfile=/opt/etcd/ssl/server-key.pem"
EOF
cat <<EOF >/usr/lib/systemd/system/kube-apiserver.service
[Unit]
Description=Kubernetes API Server
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-apiserver
ExecStart=/opt/kubernetes/bin/kube-apiserver \$KUBE_APISERVER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable kube-apiserver
systemctl restart kube-apiserver
'>apiserver.sh

Master01执行apiserver.sh脚本:写入配置文件和启动脚本,启动kube-apiserver

# sh -x apiserver.sh 192.168.2.73 https://192.168.2.73:2379,https://192.168.2.75:2379,https://192.168.2.76:2379

参数说明:
--logtostderr 启用日志
---v 日志等级
--etcd-servers etcd集群地址
--bind-address 监听地址
--secure-port https安全端口
--advertise-address 集群通告地址
--allow-privileged 启用授权
--service-cluster-ip-range Service虚拟IP地址段
--enable-admission-plugins 准入控制模块
--authorization-mode 认证授权,启用RBAC授权和节点自管理
--enable-bootstrap-token-auth 启用TLS bootstrap功能
--token-auth-file token文件 --service-node-port-range Service Node类型默认分配端口范围

4.3 部署controller-manager组件

4.3.1 创建脚本并执行

# echo '
#!/bin/bash
MASTER_ADDRESS=$1
cat <<EOF >/opt/kubernetes/cfg/kube-controller-manager
KUBE_CONTROLLER_MANAGER_OPTS="--logtostderr=true \\
--v=4 \\
--master=${MASTER_ADDRESS}:8080 \\
--leader-elect=true \\
--address=127.0.0.1 \\
--service-cluster-ip-range=10.0.0.0/24 \\
--cluster-name=kubernetes \\
--cluster-signing-cert-file=/opt/kubernetes/ssl/ca.pem \\
--cluster-signing-key-file=/opt/kubernetes/ssl/ca-key.pem \\
--root-ca-file=/opt/kubernetes/ssl/ca.pem \\
--service-account-private-key-file=/opt/kubernetes/ssl/ca-key.pem \\
--experimental-cluster-signing-duration=87600h0m0s"
EOF
cat <<EOF >/usr/lib/systemd/system/kube-controller-manager.service
[Unit]
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-controller-manager
ExecStart=/opt/kubernetes/bin/kube-controller-manager \$KUBE_CONTROLLER_MANAGER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable kube-controller-manager
systemctl restart kube-controller-manager
'>kube-controller-manager.sh

执行kube-controller-manager.sh脚本:写入配置文件和启动脚本并启动

# sh -x kube-controller-manager.sh 127.0.0.1

4.4 部署schedule组件

4.4.1 创建脚本并执行

# cd /root/k8s/k8s-cert/
#echo '
#!/bin/bash
MASTER_ADDRESS=$1
cat <<EOF >/opt/kubernetes/cfg/kube-scheduler
KUBE_SCHEDULER_OPTS="--logtostderr=true \\
--v=4 \\
--master=${MASTER_ADDRESS}:8080 \\
--leader-elect"
EOF
cat <<EOF >/usr/lib/systemd/system/kube-scheduler.service
[Unit]
Description=Kubernetes Scheduler
Documentation=https://github.com/kubernetes/kubernetes
[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-scheduler
ExecStart=/opt/kubernetes/bin/kube-scheduler \$KUBE_SCHEDULER_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable kube-scheduler
systemctl restart kube-scheduler
'>scheduler.sh

执行scheduler.sh脚本:写入配置文件和启动脚本并启动scheduler.sh

# sh -x scheduler.sh 127.0.0.1

参数说明:
--master 连接本地apiserver
--leader-elect 当该组件启动多个时,自动选举(HA)

4.5 设置环境变量并验证

# export PATH=$PATH:/opt/kubernetes/bin/
# echo 'PATH=$PATH:/opt/kubernetes/bin/' >>/etc/profile

验证:(如下,就说明master三个组件都工作正常!)
k8s-cs-status.png

5. 安装k8s-node (两个节点)

Master apiserver启用TLS认证后,Node节点kubelet组件想要加入集群,必须使用CA签发的有效证书才能与
apiserver通信,当Node节点很多时,签署证书是一件很繁琐的事情,因此有了TLS Bootstrapping机制,kubelet
会以一个低权限用户自动向apiserver申请证书,kubelet的证书由apiserver动态签署

node.png

5.1 将kubelet-bootstrap用户绑定到系统集群角色

# kubectl create clusterrolebinding kubelet-bootstrap  --clusterrole=system:node-bootstrapper  --user=kubelet-bootstrap

5.2 创建kubeconfig文件

5.2.1 创建kubeconfig.sh脚本,并执行

创建kubelet bootstrapping kubeconfig 和kube-proxy kubeconfig文件

#  mkdir -p /root/k8s/kubeconfig && cd /root/k8s/kubeconfig
# echo '
# 创建 TLS Bootstrapping Token
#BOOTSTRAP_TOKEN需要和上面启动api-server的token一致
BOOTSTRAP_TOKEN=7e0d286b854495f2843dd78b56986225
cat > token.csv <<EOF
${BOOTSTRAP_TOKEN},kubelet-bootstrap,10001,"system:kubelet-bootstrap"
EOF
#----------------------
APISERVER=$1
SSL_DIR=$2
# 创建kubelet bootstrapping kubeconfig
export KUBE_APISERVER="https://$APISERVER"
# 设置集群参数
kubectl config set-cluster kubernetes \
 --certificate-authority=$SSL_DIR/ca.pem \
 --embed-certs=true \
 --server=${KUBE_APISERVER} \
 --kubeconfig=bootstrap.kubeconfig
# 设置客户端认证参数
kubectl config set-credentials kubelet-bootstrap \
 --token=${BOOTSTRAP_TOKEN} \
 --kubeconfig=bootstrap.kubeconfig
# 设置上下文参数
kubectl config set-context default \
 --cluster=kubernetes \
 --user=kubelet-bootstrap \
 --kubeconfig=bootstrap.kubeconfig
# 设置默认上下文
kubectl config use-context default --kubeconfig=bootstrap.kubeconfig
#----------------------
# 创建kube-proxy kubeconfig文件
kubectl config set-cluster kubernetes \
 --certificate-authority=$SSL_DIR/ca.pem \
 --embed-certs=true \
 --server=${KUBE_APISERVER} \
 --kubeconfig=kube-proxy.kubeconfig
kubectl config set-credentials kube-proxy \
 --client-certificate=$SSL_DIR/kube-proxy.pem \
 --client-key=$SSL_DIR/kube-proxy-key.pem \
 --embed-certs=true \
 --kubeconfig=kube-proxy.kubeconfig
kubectl config set-context default \
 --cluster=kubernetes \
 --user=kube-proxy \
 --kubeconfig=kube-proxy.kubeconfig
kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
'>kubeconfig.sh

执行kubeconfig.sh脚本:创建kubelet bootstrap kubeconfig 和kube-proxy kubeconfig文件

# sh -x kubeconfig.sh 192.168.2.73:6443 /root/k8s/k8s-cert/

5.2.2 拷贝kubeconfig文件到node节点

拷贝到 bootstrap.kubeconfig和kube-proxy.kubeconfig 到两个Node节点/opt/kubernetes/cfg/目录下

# scp bootstrap.kubeconfig kube-proxy.kubeconfig root@192.168.2.75:/opt/kubernetes/cfg/
# scp bootstrap.kubeconfig kube-proxy.kubeconfig root@192.168.2.76:/opt/kubernetes/cfg

5.3 部署kubelet组件

5.3.1 创建kubelet.sh脚本

# echo '
#!/bin/bash
NODE_ADDRESS=$1
DNS_SERVER_IP=${2:-"10.0.0.2"}
cat <<EOF >/opt/kubernetes/cfg/kubelet
KUBELET_OPTS="--logtostderr=true \\
--v=4 \\
--hostname-override=${NODE_ADDRESS} \\
--kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig \\
--bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig \\
--config=/opt/kubernetes/cfg/kubelet.config \\
--cert-dir=/opt/kubernetes/ssl \\
--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"
EOF
cat <<EOF >/opt/kubernetes/cfg/kubelet.config
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: ${NODE_ADDRESS}
port: 10250
readOnlyPort: 10255
cgroupDriver: cgroupfs
clusterDNS:
- ${DNS_SERVER_IP}
clusterDomain: cluster.local.
failSwapOn: false
authentication:
  anonymous:
    enabled: true
EOF
cat <<EOF >/usr/lib/systemd/system/kubelet.service
[Unit]
Description=Kubernetes Kubelet
After=docker.service
Requires=docker.service
[Service]
EnvironmentFile=/opt/kubernetes/cfg/kubelet
ExecStart=/opt/kubernetes/bin/kubelet \$KUBELET_OPTS
Restart=on-failure
KillMode=process
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable kubelet
systemctl restart kubelet
'>kubelet.sh

执行kubelet.sh脚本:写入配置文件和启动脚本,启动 kubelet(此时node将向master进行授权申请,需要去master进行同意授权才能加入到node集群)

# sh -x kubelet.sh 192.168.2.75 #在node01执行
# sh -x kubelet.sh 192.168.2.76 #在node02执行

参数说明: /opt/kubernetes/cfg/kubelet
--hostname-override 在集群中显示的主机名
--kubeconfig 指定kubeconfig文件位置,会自动生成
--bootstrap-kubeconfig 指定刚才生成的bootstrap.kubeconfig文件
--cert-dir 颁发证书存放位置
--pod-infra-container-image 管理Pod网络的镜像

5.3.2 在Master审批Node加入集群(master节点执行)

# kubectl get csr,node 
# kubectl certificate approve NAME
# kubectl get csr,node

如果加入成功后,则效果如下:
node-join.png

5.4 部署kube-proxy组件并运行

# echo '
#!/bin/bash
NODE_ADDRESS=$1
cat <<EOF >/opt/kubernetes/cfg/kube-proxy
KUBE_PROXY_OPTS="--logtostderr=true \\
--v=4 \\
--hostname-override=${NODE_ADDRESS} \\
--cluster-cidr=10.0.0.0/24 \\
--proxy-mode=ipvs \\
--masquerade-all=true \
--kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig"
EOF
cat <<EOF >/usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Proxy
After=network.target
[Service]
EnvironmentFile=-/opt/kubernetes/cfg/kube-proxy
ExecStart=/opt/kubernetes/bin/kube-proxy \$KUBE_PROXY_OPTS
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable kube-proxy
systemctl restart kube-proxy
'>proxy.sh

执行proxy.sh脚本: 写入配置文件和启动脚本,启动 kube-proxy

# sh -x proxy.sh 192.168.2.75 #在node-01执行
# sh -x proxy.sh 192.168.2.76 #在node-02执行

5.5 kubectl logs pod-name 无法查看日志,需要执行以下命令授权

# kubectl create clusterrolebinding cluster-system-anonymous --clusterrole=cluster-admin --user=system:anonymous

6. 测试安装

6.1 创建一个示例

# kubectl run nginx --image=nginx --replicas=3
# kubectl expose deployment nginx --port=88 --target-port=80 --type=NodePort

如下图效果:

k8s-nginx-demo.png

6.2 访问服务
最终访问: http://192.168.2.75:44664或者http://192.168.2.76:44664

k8s-nginx-demo1.png

7. master高可用

7.1 安装nginx (两个node节点操作相同)

# rpm -Uvh  http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# yum install -y nginx
# vi /etc/nginx/nginx.conf 

加入以下内容:

nginx-stream.png

# systemctl restart nginx
# systemctl enable nginx

7.2 安装keepalived

# yum install -y keepalived
# vi /etc/keepalived/

keepalived-conf.png

创建nginx_check文件,并赋权:

# vi /etc/keepalived/nginx_check.sh

keepalived-nginx-check.png

# chmod +x /etc/keepalived/nginx_check.sh

7.3 配置vip

如上面keepalived.conf配置文件所示:
高可用ip: 192.168.2.70

7.3.1 node节点修改kubeconfig 信息

执行5.2.1 脚本,重新生成bootstrap.kubeconfig 和kube-proxy.kubeconfig

# sh -x kubeconfig.sh 192.168.2.70:88 /root/k8s/k8s-cert/

生成之后scp到两个node节点,然后重启node节点的服务

# systemctl restart kubelet.service
# systemctl restart kube-proxy.service

7.3.2 查看状态是否正确

# kubectl get pods,svc

8. 安装dashboard UI (任一master节点)

文档:
https://github.com/kubernetes/dashboard/releases

8.1 非源码安装

# kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml

8.2 源码安装 (推荐)

# cp /root/k8s/k8s-cert/kubernetes/kubernetes-src.tar.gz /root/k8s/
# cd /root/k8s/ && tar -zxvf  kubernetes-src.tar.gz
# cd /root/k8s/kubernetes-src/cluster/addons/dashboard

# kubectl create -f dashboard-configmap.yaml
# kubectl create -f dashboard-rbac.yaml
# kubectl create -f dashboard-secret.yaml

修改为国内镜像:
registry.cn-shanghai.aliyuncs.com/qubit/kubernetes-dashboard-amd64:v1.10.1

# vi dashboard-controller.yaml

k8s-dashboard.png

# kubectl create -f dashboard-controller.yaml
# kubectl get pods -n kube-system

修改service的type为NodePort:

# vi dashboard-service.yaml

k8s-dashboard-service.png

# kubectl create -f dashboard-service.yaml
# kubectl get svc,nodes,pods -n kube-system

k8s-dashboard-ip.png

访问(火狐浏览器):
https://192.168.2.75:35151
https://192.168.2.76:35151

k8s-dashboard-page.png

8.3 使用令牌登录 (推荐)

# kubectl create -f k8s-admin.yaml

使用一下两个命令查看token:

# kubectl get secret -n kube-system
# kubectl describe secret dashboard-admin-token-hfzg -n kube-system

k8s-dashboard-token.png

复制token,登录,出现如下界面,搭建成功!

k8s-dashboard-success.png

最后修改:2019 年 04 月 04 日 01 : 35 AM
如果觉得我的文章对你有用,请随意赞赏

发表评论