当前位置: 首页 > 工具软件 > Node-limits > 使用案例 >

kubernetes 添加node

韶弘壮
2023-12-01

前置条件

1. 参考  kubernetes 权威指南 从一个简单例子开始 先部署 master

2. 部署 kubernetes 部署本地dns

3. 部署 kubernetes 部署flannel

 

说明

centos上已经部署好了 master ,  虚拟机 ip   192.168.255.130, kubernetes是 yum安装的,版本为 1.5.2

打算在 ubuntu 16.04 上 部署个 node., 虚拟机ip  192.168.255.129

 

安装kubernetes

node 只要 kubelet 和 kube-proxy 2个进程就行了。

把它们 scp 到 ubuntu /usr/bin 目录即可, 参考

/usr/bin # scp kube-proxy  wangjunsheng@192.168.255.129:/home/wangjunsheng/kubernetes/config                                   
/usr/bin # scp kubelet  wangjunsheng@192.168.255.129:/home/wangjunsheng/kubernetes/config 

然后在ubuntu, sudo mv kubelet 和 kube-proxy 到 /usr/bin 目录

 

写给自己的 emacs 下打开文件参考

(opo "/sudo:localhost:/lib/systemd/system/kube-proxy.service")
(opo "/sudo:localhost:/lib/systemd/system/kubelet.service")

新建它们的启动配置文件

/lib/systemd/system/kube-proxy.service

[Unit]
Description=Kubernetes Proxy
After=network.target

[Service]
ExecStart=/usr/bin/kube-proxy  \
  --hostname-override=192.168.255.130 \
  --master=http://192.168.255.130:8080 \
  --logtostderr=true
Restart=on-failure

[Install]
WantedBy=multi-user.target

 

/lib/systemd/system/kubelet.service

[Unit]
Description=Kubernetes Kubelet
After=docker.service
Requires=docker.service

[Service]
WorkingDirectory=/var/lib/kubelet
ExecStart=/usr/bin/kubelet \
  --hostname-override=192.168.255.130 \
  --api-servers=http://192.168.255.130:8080 \
  --cluster-dns=10.254.0.2 \
  --cluster-domain=cluster.local \
  --pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest\
  --logtostderr=true
Restart=on-failure
KillMode=process

[Install]
WantedBy=multi-user.target

 

systemctl daemon-reload
systemctl start kubelet
systemctl start kube-proxy

注意 把 ip 改成适合自己的。  10.254.0.2 是我部署的dns的 集群ip

 

注意:   kube-proxy  和  kubelet 的 --hostname-override=192.168.255.129 必须完全一致,且是正确的虚拟机ip。

我第一次填错了ip,结果 kubectl logs 或 exec 时总是出错,怎么也找不到原因。

可能的报错

kub exec kubia-n8snl env
error: unable to upgrade connection: pod does not exist

pod does not exist 其实是 kubelet 反馈的错误。kube-apiserver 已经找到了然后下发给node,node反馈找不到这个po。当然找不到啦,我的ip配置错了,问了错误的node。我刚好把2个node的 hostname-override 配反了,所以就提示找不到pod。如果不是配反,而是配错,估计就是报类似下面的错误。无法建立连接。

kubectl exec -ti myweb-99lfw -- /bin/bash
Error from server: error dialing backend: dial tcp 192.168.255.129:10250: getsockopt: connection refused

首先确定它们配对了,还是不行的话  kubectl delete no 后,再重启 所有的进程 : master的3个进程和 node的 kube-proxy kubelet

 

 

可以通过 kubectl get svc 查看

~/kubernetes/docker $ kubectl get svc --all-namespaces
NAMESPACE     NAME           TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
default       kubernetes     ClusterIP   10.254.0.1       <none>        443/TCP          18d
kube-system   kube-dns       ClusterIP   10.254.0.2       <none>        53/UDP,53/TCP    12d

 

可以查看到新部署上去的node

kubectl get node
NAME              STATUS    ROLES     AGE       VERSION
127.0.0.1         Ready     <none>    18d       v1.5.2
192.168.255.130   Ready     <none>    18h       v1.5.2

 

安装指定版本的docker

1.5.2 的kubernete 无法驾驭 我已经装好的 18.09的 docker-ce, 查看 centos 下 docker 版本号 (docker -v) 发现是1.13.1的。

我在ubuntu下安装 1.13.1版本的 docker

卸载原先的,我装的 18.09的ce,需要如下卸载

sudo apt-get purge docker-ce

如果你装的不是ce, 则 sudo apt-get remove docker 应该就可以了

 

然后到下面这个链接找到适合k8s的发行版本,我要找 1.13.1 的,翻了一页才找到(下面有个 next 翻页按钮)

https://github.com/moby/moby/releases

Linux 64bits tgz: https://get.docker.com/builds/Linux/x86_64/docker-1.13.1.tgz
wget  https://get.docker.com/builds/Linux/x86_64/docker-1.13.1.tgz
sudo tar --strip-components=1 -xvzf docker-1.13.1.tgz -C /usr/local/bin

 

制作开机启动

写给自己的 emacs 参考
(opo "/sudo:localhost:/lib/systemd/system/docker.service")

 /lib/systemd/system/docker.service

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
EnvironmentFile=-/run/flannel/subnet.env
ExecStart=/usr/local/bin/dockerd -H unix://\
          --selinux-enabled=false \
          --insecure-registry=gcr.io \
          --bip=${FLANNEL_SUBNET} \
          --mtu=${FLANNEL_MTU}
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Comment TasksMax if your systemd version does not supports it.
# Only systemd 226 and above support this option.
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

# kill only the docker process, not all processes in the cgroup
KillMode=process

[Install]
WantedBy=multi-user.target

部署个例子试试

参考 kubernetes 部署redis 集群 guestbook

就用这个例子

kubectl create -f redis-master-rc.yaml -f redis-master-svc.yaml                                                                                        
kubectl create -f redis-slave-rc.yaml -f redis-slave-svc.yaml                                                                                          
kubectl create -f fe-rc.yaml -f fe-svc.yaml            

然后看看 部署情况

~/k8s/demo2 # kubectl get po                                                                                                                           
NAME                 READY     STATUS              RESTARTS   AGE                                                                                      
frontend-g0x36       0/1       ContainerCreating   0          6s                                                                                       
frontend-t2q86       1/1       Running             0          6s                                                                                       
frontend-v86jn       0/1       ContainerCreating   0          6s                                                                                       
redis-master-p23sc   1/1       Running             0          44m                                                                                      
redis-slave-vm5c6    1/1       Running             0          19skubectl create -f redis-master-rc.yaml -f redis-master-svc.yaml 

再看看 pod 的具体情况,   如果部署不成功可以使用 kubectl describe pod  pod_name 来查看详细原因

~/k8s/demo2 # kubectl describe po frontend-v86jn                                                                                                       
Name:           frontend-v86jn                                                                                                                         
Namespace:      default                                                                                                                                
Node:           192.168.255.130/192.168.255.130                                                                                                        
Start Time:     Tue, 15 Jan 2019 10:32:00 +0800                                                                                                        
Labels:         name=frontend                                                                                                                          
Status:         Pending                                                                                                                                
IP:                                                                                                                                                    
Controllers:    ReplicationController/frontend                                                                                                         
Containers:                                                                                                                                            
  php-redis:                                                                                                                                           
    Container ID:                                                                                                                                      
    Image:              kubeguide/guestbook-php-frontend                                                                                               
    Image ID:                                                                                                                                          
    Port:               80/TCP                                                                                                                         
    State:              Waiting                                                                                                                        
      Reason:           ContainerCreating                                                                                                              
    Ready:              False                                                                                                                          
    Restart Count:      0                                                                                                                              
    Volume Mounts:      <none>                                                                                                                         
    Environment Variables:                                                                                                                             
      GET_HOSTS_FROM:   env                                                                                                                            
Conditions:                                                                                                                                            
  Type          Status                                                                                                                                 
  Initialized   True                                                                                                                                   
  Ready         False                                                                                                                                  
  PodScheduled  True                                                                                                                                   
No volumes.                                                                                                                                            
QoS Class:      BestEffort                                                                                                                             
Tolerations:    <none>                                                                                                                                 
Events:                                                                                                                                                
  FirstSeen     LastSeen        Count   From                            SubObjectPath                   Type            Reason          Message        
  ---------     --------        -----   ----                            -------------                   --------        ------          -------        
  3m            3m              1       {default-scheduler }                                            Normal          Scheduled       Successfully assigned frontend-v86jn to 192.168.255.130
  3m            3m              1       {kubelet 192.168.255.130}       spec.containers{php-redis}      Normal          Pulling         pulling image "kubeguide/guestbook-php-frontend"

pull image是比较耗时的,再等等,就都ok了

 

再看看 node 的部署情况,如下可以看到 master 在这个node部署了2个pod : frontend-v86jn 和 redis-master-p23sc

~/kubernetes/docker $ kub describe node 192.168.255.130
Name:               192.168.255.130
Roles:              <none>
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/hostname=192.168.255.130
Annotations:        volumes.kubernetes.io/controller-managed-attach-detach=true
CreationTimestamp:  Mon, 14 Jan 2019 15:13:06 +0800
Taints:             <none>
Unschedulable:      false
Conditions:
  Type             Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message
  ----             ------  -----------------                 ------------------                ------                       -------
  OutOfDisk        False   Tue, 15 Jan 2019 10:46:36 +0800   Mon, 14 Jan 2019 17:53:02 +0800   KubeletHasSufficientDisk     kubelet has sufficient disk space available
  MemoryPressure   False   Tue, 15 Jan 2019 10:46:36 +0800   Mon, 14 Jan 2019 15:13:14 +0800   KubeletHasSufficientMemory   kubelet has sufficient memory available
  DiskPressure     False   Tue, 15 Jan 2019 10:46:36 +0800   Mon, 14 Jan 2019 15:13:14 +0800   KubeletHasNoDiskPressure     kubelet has no disk pressure
  Ready            True    Tue, 15 Jan 2019 10:46:36 +0800   Mon, 14 Jan 2019 17:53:12 +0800   KubeletReady                 kubelet is posting ready status. AppArmor enabled
Addresses:
  LegacyHostIP:  192.168.255.130
  InternalIP:    192.168.255.130
  Hostname:      192.168.255.130
Capacity:
 alpha.kubernetes.io/nvidia-gpu:  0
 cpu:                             3
 memory:                          1513724Ki
 pods:                            110
Allocatable:
 alpha.kubernetes.io/nvidia-gpu:  0
 cpu:                             3
 memory:                          1513724Ki
 pods:                            110
System Info:
 Machine ID:                 c7c3c86052714ab682a8d009e1afb546
 System UUID:                94FE4D56-E298-8159-8525-8C52FFBB9417
 Boot ID:                    112639ee-c30f-4bdb-9891-cc6afd7db108
 Kernel Version:             4.4.0-139-generic
 OS Image:                   Ubuntu 16.04 LTS
 Operating System:           linux
 Architecture:               amd64
 Container Runtime Version:  docker://1.13.1
 Kubelet Version:            v1.5.2
 Kube-Proxy Version:         v1.5.2
ExternalID:                  192.168.255.130
Non-terminated Pods:         (2 in total)
  Namespace                  Name                  CPU Requests  CPU Limits  Memory Requests  Memory Limits
  ---------                  ----                  ------------  ----------  ---------------  -------------
  default                    frontend-v86jn        0 (0%)        0 (0%)      0 (0%)           0 (0%)
  default                    redis-master-p23sc    0 (0%)        0 (0%)      0 (0%)           0 (0%)
Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  CPU Requests  CPU Limits  Memory Requests  Memory Limits
  ------------  ----------  ---------------  -------------
  0 (0%)        0 (0%)      0 (0%)           0 (0%)
Events:         <none>

 

 类似资料: