node节点单独安装

Posted effortsing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了node节点单独安装相关的知识,希望对你有一定的参考价值。

node节点单独安装

# 注意:本实验先在master上安装完必要组件后,就开始在另一台上单独安装node组件,以后扩容node节点就这样做。

# 本实验 master节点不安装kubelet组件,最后通过kubectl get no 只得到一个node2这一个节点,但是node的功能是齐全的,所以是node节点单独安装

# 注意:准备了三台,这三台都做了环境准备,但是k8s集群只是使用了 master、node2这两个节点,如果把node3扩容进来,随时都可以,

# 注意:本实验 node2 没有安装etcd,之前安装的etcd给剔除了

实验架构
master: 192.168.0.91   etcd、kubectl工具、kube-apiserver、kube-controller-manager、kube-scheduler 

node2: 192.168.0.92    docker、kubectl工具、kubelet组件、cni、kube-proxy、flannel、coredns

node3:192.168.0.931、环境配置

如下操作在所有节点操作

配置hosts解析
[[email protected]-10-1-1-8 k8s]# hostnamectl set-hostname master
[[email protected]-10-1-1-68 ~]# hostnamectl set-hostname node2
[[email protected]-10-1-1-111 ~]# hostnamectl set-hostname node3

cat >>/etc/hosts<<EOF
192.168.0.91 master
192.168.0.92 node2
192.168.0.93 node3
EOF

禁用selinux
sed -i s/SELINUX=permissive/SELINUX=disabled/ /etc/sysconfig/selinux

关闭swap
注释/etc/fstab文件里swap相关的行

所有节点都重启

开启forward
iptables -P FORWARD ACCEPT


配置转发相关参数
cat >> /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
vm.swappiness=0
EOF
sysctl --system


加载ipvs相关内核模块
如果重新开机,需要重新加载
modprobe ip_vs
modprobe ip_vs_rr
modprobe ip_vs_wrr
modprobe ip_vs_sh
modprobe nf_conntrack_ipv4
lsmod | grep ip_vs


2、安装CFSSL证书生成工具

只在master节点操作

mkdir -pv /server/software/k8s
cd /server/software/k8s

通过网盘下载cfssl工具

链接:https://pan.baidu.com/s/1PGVlADPfCMhYEfYlMngDHQ 
提取码:itrj 


链接:https://pan.baidu.com/s/1KsDKbbzwO82WegqPAlonyg 
提取码:n8ce 


链接:https://pan.baidu.com/s/1dM8cJ38XAO_n6S-KKHZlqw 
提取码:5n6m 



安装cfssl工具

只要把安装包改下名字,移动到usr/local/bin/下,加上授权即可

mv cfssl-certinfo_linux-amd64 /usr/local/bin/cfssl-certinfo
mv cfssl_linux-amd64 /usr/local/bin/cfssl
mv cfssljson_linux-amd64 /usr/local/bin/cfssljson
chmod +x /usr/local/bin/cfssl*




3、创建CA配置文件:生成其他组件ca证书时需要用到(除了根证书)

只在master节点操作

mkdir -p $HOME/ssl && cd $HOME/ssl

cat >ca-config.json<<EOF
{
  "signing": {
    "default": {
      "expiry": "87600h"
    },
    "profiles": {
      "kubernetes": {
        "usages": [
            "signing",
            "key encipherment",
            "server auth",
            "client auth"
        ],
        "expiry": "87600h"
      }
    }
  }
}
EOF




4、生成 ca 根证书和私钥: 生成其他组件ca证书时需要用到

只在master节点操作

cd $HOME/ssl

cat >ca-csr.json<<EOF
{
  "CN": "kubernetes",
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      "O": "k8s",
      "OU": "System"
    }
  ],
  "ca": {
     "expiry": "87600h"
  }
}
EOF


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

查看生成的证书和私钥

ca-key.pem  ca.pem

把根证书和私钥复制到一个目录里面

mkdir -p /etc/kubernetes/cert/
cp ca*.pem /etc/kubernetes/cert/



5、安装、配置、启动etcd

只在master节点上操作

5.1、生成etcd的ca证书和私钥

cd $HOME/ssl

cat >etcd-csr.json<<EOF
{
    "CN": "etcd",
    "hosts": [
      "127.0.0.1",
      "192.168.0.91",
      "192.168.0.92",
      "192.168.0.93"
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "ST": "BeiJing",
            "L": "BeiJing",
            "O": "etcd",
            "OU": "Etcd Security"
        }
    ]
}
EOF

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

查看生成的证书和私钥

etcd-key.pem  etcd.pem   

把etcd证书复制到一个目录里面

mkdir -p /etc/etcd/cert/
cp etcd*.pem /etc/etcd/cert/


5.2、安装etcd

mkdir -p /server/software/k8s
mkdir -p /opt/k8s/bin
cd /server/software/k8s
wget https://github.com/coreos/etcd/releases/download/v3.2.18/etcd-v3.2.18-linux-amd64.tar.gz
tar -xf etcd-v3.2.18-linux-amd64.tar.gz
mv etcd-v3.2.18-linux-amd64/etcd* /opt/k8s/bin
chmod +x /opt/k8s/bin/*
ln -s /opt/k8s/bin/etcd /usr/bin/etcd
etcd --version


5.3 配置etcd启动脚本

注意:经过多次尝试,没有 --force-new-claster 就无法添加成功,这句话意思是强制生成新的节点

cat >> /etc/profile << EOF
export ETCD_NAME=$(hostname)
export INTERNAL_IP=$(hostname -i | awk ‘{print $NF}‘)
export ECTD_CLUSTER=‘master=https://192.168.0.91:2380‘
EOF

source /etc/profile

mkdir -p /data/etcd

cat > /etc/systemd/system/etcd.service <<EOF
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
Documentation=https://github.com/coreos

[Service]
Type=notify
WorkingDirectory=/data/etcd
EnvironmentFile=-/etc/etcd/etcd.conf
ExecStart=/opt/k8s/bin/etcd   --name $ETCD_NAME   --cert-file=/etc/etcd/cert/etcd.pem   --key-file=/etc/etcd/cert/etcd-key.pem   --peer-cert-file=/etc/etcd/cert/etcd.pem   --peer-key-file=/etc/etcd/cert/etcd-key.pem   --trusted-ca-file=/etc/kubernetes/cert/ca.pem   --peer-trusted-ca-file=/etc/kubernetes/cert/ca.pem   --initial-advertise-peer-urls https://${INTERNAL_IP}:2380   --listen-peer-urls https://${INTERNAL_IP}:2380   --listen-client-urls https://${INTERNAL_IP}:2379,http://127.0.0.1:2379   --advertise-client-urls https://${INTERNAL_IP}:2379   --initial-cluster-token my-etcd-token   --initial-cluster $ECTD_CLUSTER   --initial-cluster-state new   --force-new-claster   --data-dir=/data/etcd
Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF



5.4、启动etctd、设置开机启动

systemctl daemon-reload      #一定要执行,否则报错
systemctl start etcd
systemctl status etcd
systemctl enable etcd
systemctl stop etcd


5.5、查看单个etcd集群状态

[[email protected] ~]# etcdctl cluster-health
member 42f7141ed6110de1 is healthy: got healthy result from https://192.168.0.91:2379
cluster is healthy



6、分发二进制组件

# 提前分发k8s所有组件二进制文件、顺便安装kubectl工具

# 安装包解压后包括 kubectl 工具,所以不需要单独使用kubernetes-server-client-amd64.tar.gz 安装包分发 kubectl 工具


下载、解压安装包

cd /server/software/k8s

下载链接:https://pan.baidu.com/s/1DXahqP8nXWP1aw5pIunJrw 
提取码:9xfp

tar -xf kubernetes-server-linux-amd64.tar.gz



分发所有组件二进制文件,

# 后面配置 kube-apiserver 等各个组件启动文件里面需要带上各自二进制文件路径

mkdir -p /usr/local/kubernetes/bin
cd /server/software/k8s/kubernetes/server/bin
cp kube-apiserver kube-controller-manager kube-scheduler kube-proxy kubelet kubectl /usr/local/kubernetes/bin    #这一步很关键



安装kubectl工具

# 后面创建 admin kubeconfig等配置文件时候需要用到;

# admin.conf = ~/.kube/config,因为是复制过来的 ;

# kubectl作用:当kubelet组件通过bootstrap token 认证后,kubectl默认从 ~/.kube/config 文件读取 kube-apiserver 地址、证书、用户名等信息;

cp /usr/local/kubernetes/bin/kubectl /usr/local/bin/kubectl



查看 kubectl 版本,

# 出现下面的情况就是正确的。did you specify the right host or port? 这个报错忽略,因为还没有安装kubelet服务

kubectl version

[[email protected] bin]# kubectl version
Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.0", GitCommit:"91e7b4fd31fcd3d5f436da26c980becec37ceefe", GitTreeState:"clean", BuildDate:"2018-06-27T20:17:28Z", GoVersion:"go1.10.2", Compiler:"gc", Platform:"linux/amd64"}
The connection to the server localhost:8080 was refused - did you specify the right host or port?
 
cd $HOME




7、生成admin的ca证书和私钥   

# kubectl 作为集群的管理工具,需要被授予最高权限。这里创建具有最高权限的 admin 证书、admin kubeconfig

# 注意:后面只有apiserver和kubelet这两个服务启动参数会用到admin的ca证书;kubectl工具和kubelet服务不是一回事

cd $HOME/ssl

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

生成 admin ca
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin


查看生成的admin ca

ls admin*.pem




8、配置 kube-apiserver ca

# 10.96.0.1 是 kube-apiserver 指定的 service-cluster-ip-range 网段的第一个IP

cd $HOME/ssl

cat >kube-apiserver-csr.json<<EOF
{
    "CN": "kube-apiserver",
    "hosts": [
      "127.0.0.1",
      "192.168.0.91",
      "10.96.0.1",
      "kubernetes",
      "kubernetes.default",
      "kubernetes.default.svc",
      "kubernetes.default.svc.cluster",
      "kubernetes.default.svc.cluster.local"
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "ST": "BeiJing",
            "L": "BeiJing",
            "O": "k8s",
            "OU": "System"
        }
    ]
}
EOF

生成 kube-apiserver ca

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

查看生成的kube-apiserver ca

ls kube-apiserver*.pem




9、配置 kube-controller-manager ca

cd $HOME/ssl

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

生成 kube-controller-manager ca

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

查看生成的kube-controller-manager ca

ls kube-controller-manager*.pem






10、配置 kube-scheduler ca

cd $HOME/ssl

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

生成 kube-scheduler ca

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

查看生成的kube-scheduler ca

ls kube-scheduler*.pem




11、配置 kube-proxy ca

# 注意:只是node节点需要用到kube-proxy ca

cd $HOME/ssl

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


生成 kube-proxy ca

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

查看生成的kube-proxy ca

ls kube-proxy*.pem



12、复制所有的ca 证书到一个目录里面,方便管理

cd $HOME/ssl
mkdir -p /etc/kubernetes/pki
cp ca*.pem admin*.pem kube-proxy*.pem kube-scheduler*.pem kube-controller-manager*.pem kube-apiserver*.pem /etc/kubernetes/pki
cp etcd.pem etcd-key.pem /etc/kubernetes/pki/etcd/




13、开启 bootstrap token 认证 ,kubelet TLS Boostrap机制 

# kube-apiserver、kubelet启动文件需要用到token,

# token中包含kubelet-bootstrap用户

# 后面kubelet组件启动参数中需要使用 kubelet-bootstrap.conf 文件向 kube-apiserver 发送 CSR 请求,

# 请求通过后,kubectl才会从 ~/.kube/config 文件读取 kube-apiserver 地址、证书、用户名等信息



静态获取token

# 还有一种是用 kubeadm 动态获取token,kubeadm token create,这样可以使用 TLS bootstrap 机制自动生成 client 和 server 证书,过期后自动轮转。

# 本实验采用静态获取,一天后过期

export BOOTSTRAP_TOKEN=$(head -c 16 /dev/urandom | od -An -t x | tr -d ‘ ‘)



创建token.csv文件

# 只有kube-apiserver 启动文件中需要用到

cat > /etc/kubernetes/token.csv <<EOF
${BOOTSTRAP_TOKEN},kubelet-bootstrap,10001,"system:kubelet-bootstrap"
EOF



创建kubelet-bootstrap.conf

# 只有kubelet启动文件需要用到

cd /etc/kubernetes

export KUBE_APISERVER="https://192.168.0.91:6443"

kubectl config set-cluster kubernetes   --certificate-authority=/etc/kubernetes/pki/ca.pem   --embed-certs=true   --server=${KUBE_APISERVER}   --kubeconfig=kubelet-bootstrap.conf

kubectl config set-credentials kubelet-bootstrap   --token=${BOOTSTRAP_TOKEN}   --kubeconfig=kubelet-bootstrap.conf

kubectl config set-context default   --cluster=kubernetes   --user=kubelet-bootstrap   --kubeconfig=kubelet-bootstrap.conf

kubectl config use-context default --kubeconfig=kubelet-bootstrap.conf



给kubelet-bootstrap用户授权

# 创建一个 clusterrolebinding,将 bootstrap token 文件中的 kubelet-bootstrap 用户赋予 system:node-bootstrapper cluster 角色

# 默认情况下,bootstrap这个 user 和 group 没有创建 CSR 的权限,kubelet 会启动失败,所以要给kubelet-bootstrap角色授权

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

# 如果没有授权会出现下面错误:

[[email protected] kubernetes]# journalctl -u kubelet |tail
failed to run Kubelet: cannot create certificate signing request: certificatesigningrequests.certificates.k8s.io is forbidden: User "kubelet-bootstrap" cannot create certificatesigningrequests.certificates.k8s.io at the cluster scope



14、创建 admin kubeconfig 

# 只有kubelet服务启动参数需要用到admin kubeconfig,
# admin.conf = ~/.kube/config,因为复制过来的 ;kubectl 默认从 ~/.kube/config 文件读取 kube-apiserver 地址、证书、用户名等信息,

cd /etc/kubernetes

export KUBE_APISERVER="https://192.168.0.91:6443"

设置集群参数

kubectl config set-cluster kubernetes   --certificate-authority=/etc/kubernetes/pki/ca.pem   --embed-certs=true   --server=${KUBE_APISERVER}   --kubeconfig=admin.conf

设置客户端认证参数

kubectl config set-credentials admin   --client-certificate=/etc/kubernetes/pki/admin.pem   --client-key=/etc/kubernetes/pki/admin-key.pem   --embed-certs=true   --kubeconfig=admin.conf

设置上下文参数

kubectl config set-context default   --cluster=kubernetes   --user=admin   --kubeconfig=admin.conf

设置默认上下文

kubectl config use-context default --kubeconfig=admin.conf



15、创建 kube-controller-manager kubeconfig

cd /etc/kubernetes

export KUBE_APISERVER="https://192.168.0.91:6443"

kubectl config set-cluster kubernetes   --certificate-authority=/etc/kubernetes/pki/ca.pem   --embed-certs=true   --server=${KUBE_APISERVER}   --kubeconfig=kube-controller-manager.conf

kubectl config set-credentials kube-controller-manager   --client-certificate=/etc/kubernetes/pki/kube-controller-manager.pem   --client-key=/etc/kubernetes/pki/kube-controller-manager-key.pem   --embed-certs=true   --kubeconfig=kube-controller-manager.conf

kubectl config set-context default   --cluster=kubernetes   --user=kube-controller-manager   --kubeconfig=kube-controller-manager.conf

kubectl config use-context default --kubeconfig=kube-controller-manager.conf




16、创建 kube-scheduler kubeconfig

cd /etc/kubernetes

export KUBE_APISERVER="https://192.168.0.91:6443"

kubectl config set-cluster kubernetes   --certificate-authority=/etc/kubernetes/pki/ca.pem   --embed-certs=true   --server=${KUBE_APISERVER}   --kubeconfig=kube-scheduler.conf

kubectl config set-credentials kube-scheduler   --client-certificate=/etc/kubernetes/pki/kube-scheduler.pem   --client-key=/etc/kubernetes/pki/kube-scheduler-key.pem   --embed-certs=true   --kubeconfig=kube-scheduler.conf

kubectl config set-context default   --cluster=kubernetes   --user=kube-scheduler   --kubeconfig=kube-scheduler.conf

kubectl config use-context default --kubeconfig=kube-scheduler.conf




17、创建 kube-proxy kubeconfig

# 注意:只是node节点需要用到kube-proxy kubeconfig

cd /etc/kubernetes

export KUBE_APISERVER="https://192.168.0.91:6443"

kubectl config set-cluster kubernetes   --certificate-authority=/etc/kubernetes/pki/ca.pem   --embed-certs=true   --server=${KUBE_APISERVER}   --kubeconfig=kube-proxy.conf

kubectl config set-credentials kube-proxy   --client-certificate=/etc/kubernetes/pki/kube-proxy.pem   --client-key=/etc/kubernetes/pki/kube-proxy-key.pem   --embed-certs=true   --kubeconfig=kube-proxy.conf

kubectl config set-context default   --cluster=kubernetes   --user=kube-proxy   --kubeconfig=kube-proxy.conf

kubectl config use-context default --kubeconfig=kube-proxy.conf

cd $HOME




18、配置启动kube-apiserver

复制 etcd ca

mkdir -pv /etc/kubernetes/pki/etcd
cd /etc/etcd/ssl
cp etcd.pem ca-key.pem ca.pem /etc/kubernetes/pki/etcd



生成 service account key

cd /etc/kubernetes/pki/
openssl genrsa -out /etc/kubernetes/pki/sa.key 2048
openssl rsa -in /etc/kubernetes/pki/sa.key -pubout -out /etc/kubernetes/pki/sa.pub
ls /etc/kubernetes/pki/sa.*
cd $HOME



配置启动文件

cat >/etc/systemd/system/kube-apiserver.service<<EOF
[Unit]
Description=Kubernetes API Service
Documentation=https://github.com/kubernetes/kubernetes
After=network.target

[Service]
EnvironmentFile=-/etc/kubernetes/config
EnvironmentFile=-/etc/kubernetes/apiserver
ExecStart=/usr/local/kubernetes/bin/kube-apiserver         $KUBE_LOGTOSTDERR         $KUBE_LOG_LEVEL         $KUBE_ETCD_ARGS         $KUBE_API_ADDRESS         $KUBE_SERVICE_ADDRESSES         $KUBE_ADMISSION_CONTROL         $KUBE_APISERVER_ARGS
Restart=on-failure
Type=notify
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF



配置参数变量文件

# 下面 kube-apiserver、kube-controller-manager、kube-scheduler、kube-proxy这些服务 都需要用到,这里只配置一次,以后重复利用,后面的也写了变量文件,只是为了知道怎么回事

cat >/etc/kubernetes/config<<EOF
KUBE_LOGTOSTDERR="--logtostderr=true"
KUBE_LOG_LEVEL="--v=2"
EOF



配置apiserver

# 注意参数--token-auth-file=/etc/kubernetes/token.csv 表示在 apiserver 中静态配置bootstrap token,和后面开启 bootstrap token 认证步骤相呼应,不是动态的,所以有过期时间,

# 后面kubelet组件启动参数中需要使用 kubelet-bootstrap.conf 文件向 kube-apiserver 发送 CSR 请求,--bootstrap-kubeconfig 文件里面包含token和apiserver里面的token是一样的,

cat >/etc/kubernetes/apiserver<<EOF
KUBE_API_ADDRESS="--advertise-address=192.168.0.91"
KUBE_ETCD_ARGS="--etcd-servers=https://192.168.0.91:2379 --etcd-cafile=/etc/kubernetes/pki/ca.pem --etcd-certfile=/etc/kubernetes/pki/etcd/etcd.pem --etcd-keyfile=/etc/kubernetes/pki/etcd/etcd-key.pem"
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.96.0.0/12"
KUBE_ADMISSION_CONTROL="--enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota"
KUBE_APISERVER_ARGS="--allow-privileged=true --authorization-mode=Node,RBAC --enable-bootstrap-token-auth=true --token-auth-file=/etc/kubernetes/token.csv --service-node-port-range=30000-32767 --tls-cert-file=/etc/kubernetes/pki/kube-apiserver.pem --tls-private-key-file=/etc/kubernetes/pki/kube-apiserver-key.pem --client-ca-file=/etc/kubernetes/pki/ca.pem --service-account-key-file=/etc/kubernetes/pki/sa.pub --enable-swagger-ui=true --secure-port=6443 --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname --anonymous-auth=false --kubelet-client-certificate=/etc/kubernetes/pki/admin.pem --kubelet-client-key=/etc/kubernetes/pki/admin-key.pem"
EOF



启动

systemctl daemon-reload
systemctl enable kube-apiserver
systemctl start kube-apiserver
systemctl status kube-apiserver



通过浏览器访问测试

# 401 报错正常,是因为没有权限,不影响,以后解决

curl https://192.168.0.91:6443/swaggerapi

[[email protected] ~]# curl https://192.168.0.91:6443/swaggerapi
{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {
    
  },
  "status": "Failure",
  "message": "Unauthorized",
  "reason": "Unauthorized",
  "code": 401
}You have new mail in /var/spool/mail/root





18、配置启动kube-controller-manager

配置启动文件

cat >/etc/systemd/system/kube-controller-manager.service<<EOF
Description=Kubernetes Controller Manager
Documentation=https://github.com/kubernetes/kubernetes
After=network.target

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

[Install]
WantedBy=multi-user.target
EOF



配置参数变量文件

# 配置kube-apiserver启动文件时已经配置过参数变量文件,这里就不需要再做,写在这里只是为了知道下面配置文件里的参数怎么回事

cat >/etc/kubernetes/config<<EOF
KUBE_LOGTOSTDERR="--logtostderr=true"
KUBE_LOG_LEVEL="--v=2"
EOF



配置controller-manager文件

cat >/etc/kubernetes/controller-manager<<EOF
KUBECONFIG="--kubeconfig=/etc/kubernetes/kube-controller-manager.conf"
KUBE_CONTROLLER_MANAGER_ARGS="--address=127.0.0.1 --cluster-cidr=10.244.0.0/16 --cluster-name=kubernetes --cluster-signing-cert-file=/etc/kubernetes/pki/ca.pem --cluster-signing-key-file=/etc/kubernetes/pki/ca-key.pem --service-account-private-key-file=/etc/kubernetes/pki/sa.key --root-ca-file=/etc/kubernetes/pki/ca.pem --leader-elect=true --use-service-account-credentials=true --node-monitor-grace-period=10s --pod-eviction-timeout=10s --allocate-node-cidrs=true --controllers=*,bootstrapsigner,tokencleaner"
EOF



启动
systemctl daemon-reload
systemctl enable kube-controller-manager
systemctl start kube-controller-manager
systemctl status kube-controller-manager




19、配置启动kube-scheduler

配置启动文件

cat >/etc/systemd/system/kube-scheduler.service<<EOF
[Unit]
Description=Kubernetes Scheduler Plugin
Documentation=https://github.com/kubernetes/kubernetes

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

[Install]
WantedBy=multi-user.target
EOF



配置参数变量文件

# 配置kube-apiserver启动文件时已经配置过参数变量文件,这里就不需要再做,写在这里只是为了知道下面配置文件里的参数怎么回事

cat >/etc/kubernetes/config<<EOF
KUBE_LOGTOSTDERR="--logtostderr=true"
KUBE_LOG_LEVEL="--v=2"
EOF



配置scheduler文件

cat >/etc/kubernetes/scheduler<<EOF
KUBECONFIG="--kubeconfig=/etc/kubernetes/kube-scheduler.conf"
KUBE_SCHEDULER_ARGS="--leader-elect=true --address=127.0.0.1"
EOF



启动

systemctl daemon-reload
systemctl enable kube-scheduler
systemctl start kube-scheduler
systemctl status kube-scheduler




# master 节点安装到此结束,下面开始单独安装node节点




20、单独配置node2 相关组件


20.1、安装docker

# 注意:docker和flannel是一体的,哪个节点上需要安装flannel,哪个节点上就需要安装docker

# docker 和kubelet组件、node状态有关系, 之前停掉docker后,kubelet组件会自动停掉;node节点会变成 NotReady 状态

# 但是停掉docker后,flanel、coredns 的pod 让然是 running 状态

# v1.11.0版本推荐使用docker v17.03, v1.11,v1.12,v1.13, 也可以使用,再高版本的docker可能无法正常使用。测试发现17.09无法正常使用,不能使用资源限制(内存CPU)



卸载自带docker

yum remove -y docker-ce docker-ce-selinux container-selinux



下载Docker镜像

下载链接:https://pan.baidu.com/s/1whfkq5wDODIew5_eqP63gA 
提取码:xmqq 

下载链接:https://pan.baidu.com/s/1zZy_tbvuApZW2fsMPGv3Bg 
提取码:u3t3 




添加至镜像仓库

rpm -ivh docker-ce-17.03.2.ce-1.el7.centos.x86_64.rpm
rpm -ivh docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch.rpm



用安装 Docker 和依赖包
 
yum install -y docker-ce-*.rpm 



开机启动 

systemctl enable docker 



启动 docker 服务 

systemctl start docker




20.2、部署 kubelet组件

下载、解压安装包

mkdir -p /server/software/k8s
cd /server/software/k8s
下载链接:https://pan.baidu.com/s/1DXahqP8nXWP1aw5pIunJrw 
提取码:9xfp
tar -xf kubernetes-server-linux-amd64.tar.gz



分发kubelet二进制文件

# 后面kubelet启动文件需要用到kubelet二进制文件路径

mkdir -p /usr/local/kubernetes/bin
cp /server/software/k8s/kubernetes/server/bin/kubelet /usr/local/kubernetes/bin    #这一步很关键



安装kubectl工具

# kubecctl工具和kubelet服务不是一回事。安装包解压后包括 kubectl 工具,所以不需要单独使用kubernetes-server-client-amd64.tar.gz 安装包分发 kubectl 工具

# kubectl作用:当kubelet组件通过bootstrap token 认证后,kubectl 默认会从 ~/.kube/config 文件读取 kube-apiserver 地址、证书、用户名等信息;

# admin.conf = ~/.kube/config,因为复制过来的 ;

cp /server/software/k8s/kubernetes/server/bin/kubectl /usr/local/bin/kubectl



查看 kubectl 版本,

# 出现下面的情况就是正确的。did you specify the right host or port? 这个报错忽略,因为还没有安装kubelet服务

kubectl version

[[email protected] bin]# kubectl version
Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.0", GitCommit:"91e7b4fd31fcd3d5f436da26c980becec37ceefe", GitTreeState:"clean", BuildDate:"2018-06-27T20:17:28Z", GoVersion:"go1.10.2", Compiler:"gc", Platform:"linux/amd64"}
The connection to the server localhost:8080 was refused - did you specify the right host or port?
 
cd $HOME



查看组件状态

kubectl get componentstatuses

[[email protected] ~]# kubectl get componentstatuses
Unable to connect to the server: x509: certificate signed by unknown authority
# 报错原因:经过排查后发现前面操作的几个步骤中 ${KUBE_APISERVER} 这个变量写成了 ${KUBE_  又重做了一遍,正确的结果是下面

[[email protected] ~]# kubectl get componentstatuses
NAME                 STATUS    MESSAGE              ERROR
scheduler            Healthy   ok                   
controller-manager   Healthy   ok                   
etcd-0               Healthy   {"health": "true"}  



拷贝admin.conf

# 从master上复制admin.conf到node节点

scp /etc/kubernetes/admin.conf [email protected]:/etc/kubernetes/



配置 /.kube/config 并且授权

# kubectl 默认从 ~/.kube/config 文件读取 kube-apiserver 地址、证书、用户名等信息,如果没有配置,执行 kubectl 命令时可能会出错:
# /.kube/config 和 admin.conf 内容是一样的,/.kube/config 是 admin.conf 复制过来的,改了下名字而已

rm -rf $HOME/.kube
mkdir -p $HOME/.kube
cp /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config



拷贝bootstrap.conf

#从masters上复制 bootstrap.conf 到node节点

# 后面kubelet服务启动参数中需要使用 kubelet-bootstrap.conf 向 kube-apiserver 发送 CSR 请求,

# 当kubelet服务通过bootstrap token 认证后,kubectl 默认会从 ~/.kube/config 文件读取 kube-apiserver 地址、证书、用户名等信息;

scp /etc/kubernetes/kubelet-bootstrap.conf [email protected]:/etc/kubernetes/



安装cni

# kubelet 的启动参数需要用到

cd /server/software/k8s

下载链接:https://pan.baidu.com/s/1DL1v4cH2SHUfobICjKXm3Q 
提取码:puel
mkdir -p /opt/cni/bin
tar -xf cni-plugins-amd64-v0.7.1.tgz -C /opt/cni/bin
ls -l /opt/cni/bin

cd $HOME




配置启动kubelet

创建数据目录

mkdir -p /data/kubelet



配置kubelet启动文件

cat >/etc/systemd/system/kubelet.service<<EOF
[Unit]
Description=Kubernetes Kubelet Server
Documentation=https://github.com/kubernetes/kubernetes
After=docker.service
Requires=docker.service

[Service]
WorkingDirectory=/data/kubelet
EnvironmentFile=-/etc/kubernetes/config
EnvironmentFile=-/etc/kubernetes/kubelet
ExecStart=/usr/local/kubernetes/bin/kubelet             $KUBE_LOGTOSTDERR             $KUBE_LOG_LEVEL             $KUBELET_CONFIG             $KUBELET_HOSTNAME             $KUBELET_POD_INFRA_CONTAINER             $KUBELET_ARGS
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF



配置参数变量文件

cat >/etc/kubernetes/config<<EOF
KUBE_LOGTOSTDERR="--logtostderr=true"
KUBE_LOG_LEVEL="--v=2"
EOF



配置kubelet文件

# 注意修改相关ip   node节点也配置,node节点改成对应的nodeip

# image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1 这个镜像跟 kubectl get componentstatuses 获取不到资源没有关系,跟docker也没有关系

# 但是网上搜这个镜像,说这个镜像是基础镜像,目前被墙,最好下载放到本地 registry 上

# 注意 kubelet配置文件里面的参数kubelet.conf 是kubelet启动后生成的,不用管,就是这样写的

# 注意 /etc/kubernetes/pki 目录里面只放一个ca.pem 证书,

# 启动kubelet后/etc/kubernetes/pki 目录里面会自动生成四个文件 kubelet.crt、kubelet.key、kubelet-client-current.pem、kubelet-client-2019-01-22-10-07-06.pem

cat >/etc/kubernetes/kubelet<<EOF
KUBELET_HOSTNAME="--hostname-override=192.168.0.92"
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1"
KUBELET_CONFIG="--config=/etc/kubernetes/kubelet-config.yml"
KUBELET_ARGS="--bootstrap-kubeconfig=/etc/kubernetes/kubelet-bootstrap.conf --kubeconfig=/etc/kubernetes/kubelet.conf --cert-dir=/etc/kubernetes/pki --network-plugin=cni --cni-bin-dir=/opt/cni/bin --cni-conf-dir=/etc/cni/net.d"
EOF



拷贝ca.pem 证书

# 从master节点复制复制ca.pem 证书到node节点,下面kubelet-config.yml 文件参数需要用到 ca.pem

scp $HOME/ssl/ca.pem 192.168.0.92:/etc/kubernetes/pki/



配置kubelet-config.yml文件

# 注意修改kubelet-config.yml相关ip,master node2 node3 使用各自ip

cat >/etc/kubernetes/kubelet-config.yml<<EOF
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
address: 192.168.0.92
port: 10250
cgroupDriver: cgroupfs
clusterDNS:
  - 10.96.0.10
clusterDomain: cluster.local.
hairpinMode: promiscuous-bridge
serializeImagePulls: false
authentication:
  x509:
    clientCAFile: /etc/kubernetes/pki/ca.pem
EOF



启动
systemctl daemon-reload
systemctl enable kubelet
systemctl start kubelet
systemctl status kubelet



查看日志

# 启动 kubelet后查看日志会有报错

# 报错原因是因为kubelet配置了network-plugin=cni,但是还没安装网络插件 flannel,所以状态会是NotReady,会报上面的错误,不想看这个报错或者不需要网络,就修改kubelet配置文件,去掉network-plugin=cni 就可以了

[[email protected] ~]# journalctl -u kubelet |tail
network plugin is not ready: cni config uninitialized




20.3、通过csr请求

# 在node节点上操作就可以

# 查看csr

# 如果获取不到csr看日志会出现下面错误:

[[email protected] kubernetes]# journalctl -u kubelet |tail
failed to run Kubelet: cannot create certificate signing request: certificatesigningrequests.certificates.k8s.io is forbidden: User "kubelet-bootstrap" cannot create certificatesigningrequests.certificates.k8s.io at the cluster scope

# 原因是因为没有给kubelet-bootstrap授权,授权即可

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

kubectl get csr



通过csr请求,下面的长字符串填写上一步的结果,

kubectl certificate approve node-csr-Yiiv675wUCvQl3HH11jDr0cC9p3kbrXWrxvG3EjWGoE



查看节点

# 此时节点状态为 NotReady,需要安装flannel网络后才可以ready;

# 但是此时就可以设置集群角色

# 此时查看只有一个节点是因为192.168.0.91上 并没有安装 kubelet 组件,现在还不分master、node,需要后面设置集群角色,

# 如果192.168.0.91 上也安装了kubelet,就可以把192.168.0.91 节点设置为master,因为192.168.0.91上面安装了 kube-apiserver、 kube-controller-manager、kube-scheduler

# 192.168.0.92 节点必须是node节点,因为它没有安装  kube-apiserver、 kube-controller-manager、kube-scheduler 这几个组件

kubectl get nodes

[[email protected] ~]# kubectl get nodes
NAME           STATUS     ROLES     AGE       VERSION
192.168.0.92   NotReady   <none>    9h        v1.11.0



设置集群角色

# 在node节点上操作就可以

# 设置 node2 为 node

kubectl label nodes 192.168.0.92 node-role.kubernetes.io/node=

[[email protected] ~]# kubectl label nodes 192.168.0.92 node-role.kubernetes.io/node=
node/192.168.0.92 labeled



在node节点查看生成的文件

ls -l /etc/kubernetes/kubelet.conf
ls -l /etc/kubernetes/pki/kubelet*

查看结果

[[email protected] ~]# ls -l /etc/kubernetes/kubelet.conf
-rw------- 1 root root 2295 Jan 22 10:07 /etc/kubernetes/kubelet.conf

[[email protected] ~]# ls -l /etc/kubernetes/pki/kubelet*
-rw------- 1 root root 1273 Jan 22 10:07 /etc/kubernetes/pki/kubelet-client-2019-01-22-10-07-06.pem
lrwxrwxrwx 1 root root   58 Jan 22 10:07 /etc/kubernetes/pki/kubelet-client-current.pem -> /etc/kubernetes/pki/kubelet-client-2019-01-22-10-07-06.pem
-rw-r--r-- 1 root root 2181 Jan 22 02:10 /etc/kubernetes/pki/kubelet.crt
-rw------- 1 root root 1675 Jan 22 02:10 /etc/kubernetes/pki/kubelet.key




20.4、配置启动kube-proxy 


下载、解压安装包

mkdir -p /server/software/k8s

cd /server/software/k8s

下载链接:https://pan.baidu.com/s/1DXahqP8nXWP1aw5pIunJrw 
提取码:9xfp

tar -xf kubernetes-server-linux-amd64.tar.gz



分发kube-proxy二进制文件

# 后面kube-proxy.service启动文件需要用到kube-proxy二进制文件路径

mkdir -p /usr/local/kubernetes/bin
cp /server/software/k8s/kubernetes/server/bin/kube-proxy /usr/local/kubernetes/bin    #这一步很关键



安装依赖包

yum install -y conntrack-tools



拷贝kube-proxy.conf文件

把master节点上的kube-proxy.conf复制到/etc/kubernetes/ 目录下,kube-proxy.service启动参数中proxy文件需要用到kube-proxy.conf

scp /etc/kubernetes/kube-proxy.conf 192.168.0.92:/etc/kubernetes/



配置启动文件
cat >/etc/systemd/system/kube-proxy.service<<EOF
[Unit]
Description=Kubernetes Kube-Proxy Server
Documentation=https://github.com/kubernetes/kubernetes
After=network.target

[Service]
EnvironmentFile=-/etc/kubernetes/config
EnvironmentFile=-/etc/kubernetes/proxy
ExecStart=/usr/local/kubernetes/bin/kube-proxy         $KUBE_LOGTOSTDERR         $KUBE_LOG_LEVEL         $KUBECONFIG         $KUBE_PROXY_ARGS
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF



配置参数变量文件

# 前面配置kubelet组件时已经配置过参数变量文件,这里就不需要配置,写在这里这是知道是怎么回事

cat >/etc/kubernetes/config<<EOF
KUBE_LOGTOSTDERR="--logtostderr=true"
KUBE_LOG_LEVEL="--v=2"
EOF



配置proxy文件

# 注意修改相关ip,master node2 node3 使用各自ip

# 看下面proxy文件‘--proxy-mode=iptables‘,由于采用iptables模式,因为 ipvs 模式在centos7上有bug无法正常使用,1.11.0 以后的版本就可使用 ipvs 模式了,

# 本实验采用centos7.5、kubernetes 1.11.0 版本,所以使用iptables模式,



cat >/etc/kubernetes/proxy<<EOF
KUBECONFIG="--kubeconfig=/etc/kubernetes/kube-proxy.conf"
KUBE_PROXY_ARGS="--bind-address=192.168.0.92 --proxy-mode=iptables --hostname-override=192.168.0.92 --cluster-cidr=10.244.0.0/16"
EOF



启动
systemctl daemon-reload
systemctl enable kube-proxy
systemctl start kube-proxy
systemctl status kube-proxy





20.5、安装flannel网络


# 只有在安装了docker的节点上才可以安装flannel

# 注意下面的网卡名称要填写对应的网卡名称



下载配置文件

mkdir flannel && cd flannel

下载链接:https://pan.baidu.com/s/1kdrYzXYzURTzO_tXtv_R1A 
提取码:3kgd 




查看下载的文件

[[email protected] flannel]# ls
kube-flannel.yml



修改ip地址

# flannel网络 分三种网络 node network、service network、flannel network

# node network:承载kubernetes集群中各个“物理”Node(master和node)通信的网络

# 下面Network 就是 node network,因为当前还没有安装 flannel,node节点为 NotReady 状态;安装完 flannel后就是ready状态,

# 此处的Network  ip 要与kube-proxy、kube-controller-manager 里面的 --cluster-cidr 地址保持一致,


[[email protected] flannel]# vi kube-flannel.yml 

  net-conf.json: |
    {
      "Network": "10.244.0.0/16",
      "Backend": {
        "Type": "vxlan"
      }
    }



修改镜像

# 由于自带的镜像被墙,无法下载,所以修改镜像为国内源 image: registry.cn-shanghai.aliyuncs.com/gcr-k8s/flannel:v0.10.0-amd64

# 注意 kube-flannel.yml 文件里面有两个 镜像都需要改


[[email protected] flannel]# vi kube-flannel.yml 
    containers:
      - name: kube-flannel
        image: registry.cn-shanghai.aliyuncs.com/gcr-k8s/flannel:v0.10.0-amd64
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        - --iface=ens33



添加网卡参数

# 注意 - --iface=ens33 这个ens33 是 192.168.0.92 这台虚拟机的网卡信息

# 如果Node有多个网卡的话,参考flannel issues 39701,https://github.com/kubernetes/kubernetes/issues/39701

# 目前需要在kube-flannel.yml中使用--iface参数指定集群主机内网网卡的名称,否则可能会出现dns无法解析。容器无法通信的情况,

# flanneld启动参数加上--iface=<iface-name>


[[email protected] flannel]# vi kube-flannel.yml 
    containers:
      - name: kube-flannel
        image: registry.cn-shanghai.aliyuncs.com/gcr-k8s/flannel:v0.10.0-amd64
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        - --iface=ens33


启动flanel

kubectl apply -f kube-flannel.yml



查看pod

# 如果flanel启动失败,很有可能镜像拉取失败,所以最好还是自建一个镜像仓库

kubectl get pods -n kube-system
kubectl get svc

[[email protected] flannel]# kubectl get pods -n kube-system
NAME                    READY     STATUS    RESTARTS   AGE
kube-flannel-ds-zb2r2   1/1       Running   0          18s

[[email protected] flannel]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   22h



查看节点状态

# 当 flannel pod 全部启动之后,节点状态为 Ready

kubectl get no

[[email protected] flannel]# kubectl get no
NAME           STATUS    ROLES     AGE       VERSION
192.168.0.92   Ready     node      11h       v1.11.0




20.6、安装coredns

# coredns的作用是实现pod里面的DNS解析,就是可以通过域名来访问服务,例如:curl nginx-service ,curl + 服务名

# 注意下面用的 coredns 1.2.0 版本 



安装jq工具

# 之前安装coredns报错找不到jq, 所以配置jq 源,然后用yum安装,安装jq 参照:https://www.cnblogs.com/effortsing/p/10307424.html

yum install jq -y



下载coredns安装文件

cd $HOME && mkdir coredns && cd coredns

下载链接:https://pan.baidu.com/s/1z7x4z2k2w3rdhOt89FDPww 
提取码:7vkf 




查看下载的文件

[[email protected] coredns]# ls
coredns.yaml



修改配置文件

# 把文件里面的ip地址修改为10.96.0.10, 这个地址要和 kubelet组件中kubelet-config.yml 配置文件里面的 clusterDNS ip保持一致

[[email protected] coredns]# vi coredns.yaml 
spec:
  selector:
    k8s-app: kube-dns
  clusterIP: 10.96.0.10
  ports:



启动coredns

kubectl apply -f coredns.yml



查看coredns

# 过几分钟才会running 有点慢

kubectl get pods -n kube-system
kubectl get svc -n kube-system

[[email protected] coredns]# kubectl get pods -n kube-system
NAME                       READY     STATUS    RESTARTS   AGE
coredns-6c65fc5cbb-8ntpv   1/1       Running   0          34m
coredns-6c65fc5cbb-sj65c   1/1       Running   0          34m
kube-flannel-ds-zb2r2      1/1       Running   0          2h

[[email protected] coredns]# kubectl get svc -n kube-system
NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)         AGE
kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP   34m




测试coredns功能

# 启动一个nginx容器,然后进行访问nginx服务进行测试看是否能访问

下载nginx.yaml文件

下载链接:https://pan.baidu.com/s/1PV8jyDhb8yBy6uyaeMMpSQ 
提取码:xt5f 



查看下载的文件

[[email protected] ~]# ls
nginx.yaml



配置nginx镜像

# 用docker 找一个简单的nginx镜像,替换 nginx.yaml 里面的镜像,因为nginx.yaml里面的镜像都已经过时,无法下载

docker search nginx

[[email protected] ~]# docker search nginx
nginxdemos/hello         NGINX webserver that serves a simple page ...   9                    [OK]

[[email protected] ~]# vi nginx.yaml 
    spec:
      containers:
      - name: http-test-con
        image: nginxdemos/hello
        ports:
        - containerPort: 80



创建nginx实例

# 需要过几分钟,拉取镜像很慢的,

kubectl create -f nginx.yaml 



查看pod状态

# 注意:使用kubectl工具养成带上命名空间的习惯,虽然不带default命名空间默认带着的,但是这是个习惯,之前总是因为没有带命名空间出错,排查半天,单耽误时间,

kubectl get deploy -o wide -n default
kubectl get pods -o wide -n default
kubectl get svc -o wide -n default
kubectl describe svc example-service -n default

[[email protected] ~]# kubectl get deploy -o wide -n default
NAME            DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS      IMAGES             SELECTOR
http-test-dm2   1         1         1            1           52m       http-test-con   nginxdemos/hello   name=http-test-dm2


[[email protected] ~]# kubectl get pods -o wide -n default
NAME                             READY     STATUS    RESTARTS   AGE       IP            NODE
http-test-dm2-76d4b58b47-f4pqm   1/1       Running   0          52m       10.244.0.12   192.168.0.92


[[email protected] ~]# kubectl get svc -o wide -n default
NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE       SELECTOR
http-nginx-ser   NodePort    10.103.38.143   <none>        80:31000/TCP   5m        name=http-test-dm2
kubernetes       ClusterIP   10.96.0.1       <none>        443/TCP        1d        <none>


[[email protected] ~]# kubectl describe pod http-test-dm2-76d4b58b47-f4pqm -n default



容器内测试coredns解析 

# 启动一个具有解析功能的 dns 实例,然后进到 pod 里面用 nslookup、curl 工具进行访问测试,下面这个实例启动慢,等几分钟,

# 如果启动失败,通过看日志得知无法拉取镜像 infoblox/dnstools,很可能这个镜像不存在了,那就通过 docker search dns 找一个dns镜像代替infoblox/dnstools镜像

kubectl run -it --rm --image=infoblox/dnstools dns-client
nslookup kubernetes
nslookup nginx-service
curl nginx-service

执行结果:

[[email protected] ~]# kubectl run -it --rm --image=infoblox/dnstools dns-client
If you don‘t see a command prompt, try pressing enter.


dnstools# nslookup kubernetes
Server:        10.96.0.10
Address:    10.96.0.10#53

Name:    kubernetes.default.svc.cluster.local
Address: 10.96.0.1


dnstools# nslookup http-nginx-ser
Server:        10.96.0.10
Address:    10.96.0.10#53

Name:    http-nginx-ser.default.svc.cluster.local
Address: 10.103.38.143


dnstools# curl http-nginx-ser
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>



容器外测试coredns解析

# 注意:是在 node2 节点进行访问访测试的,如果去 master 节点上访问,是无法访问的,因为master 节点上还没有安装 coredns,所以无法解析svc,

# 也可能即使master节点安装了coredns,也不可能在容器外进行访问 svc,需要安装 Traefik-ingress,

# coredns 可能是节点内部域名解析,Traefik-ingress是节点外部域名解析

# 10.103.38.143 是查看svc时获取到的clusterip,svc是service的缩写

curl "10.103.38.143:80"

执行结果:

[[email protected] ~]# curl "10.103.38.143:80" -n default
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>



通过浏览器访问测试

# 31000 是查看svc时获取到的 nodeport

http://192.168.0.92:31000/


清理

kubectl delete -f nginx.yaml

 

以上是关于node节点单独安装的主要内容,如果未能解决你的问题,请参考以下文章

将字符串 XML 片段转换为 Java 中的文档节点

片段“F1”不能在这里传播,因为“节点”类型的对象永远不能是“商店”类型

安装节点 npm v1.0.1

理解 DocumentFragment

节点 JS 不提供静态图像

Cloudlinux NodeJS 要求将应用程序的节点模块存储在名为“node_modules”的符号链接指向的单独文件夹(虚拟环境)中