ubuntu18.04下K8S安装记录

一、系统安装:ubuntu18.04 server

四台机器均安装ubuntu18.04 server

二、系统配置

(以下操作非特殊说明情况下均需要配置)

1.更新源文件

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

2.设置静态IP地址

sudo vi /etc/netplan/50-cloud-init.yaml

network:
    ethernets:
        eno1:
            addresses:
            - 192.168.31.150/24
            dhcp4: false
            gateway4: 192.168.31.1
            nameservers:
                    addresses:
                    - 8.8.8.8
    version: 2

3.设置hosts文件

sudo nano /etc/hosts

#追加到文件尾部
192.168.31.150 master
192.168.31.151 node1
192.168.31.152 node2
192.168.31.153 node3

4.执行命令更新网络

sudo netplan apply

执行如果不报错时,退出当前连接,以新IP地址进行登录

5.更改hostname

hostname需要更改时执行以下命令

sudo hostnamectl set-hostname "k8-master"

6.安装docker

在 /etc/docker/daemon.json 中添加:

"exec-opts": ["native.cgroupdriver=systemd"]
cmpbook@node2:~$ cat /etc/docker/daemon.json
{
  "registry-mirrors": ["https://registry.docker-cn.com"],
  "exec-opts": ["native.cgroupdriver=systemd"]
}

重启 docker:

systemctl restart docker

查看 docker 驱动:

root@master:/# docker info | grep "Cgroup Driver"
Cgroup Driver: systemd

kubernetes 官方建议 docker 驱动采用 systemd,当然可以不修改,只是kubeadm init时会有

warning([WARNING IsDockerSystemdCheck]: detected “cgroupfs” as the Docker cgroup driver. The recommended driver is “systemd”. Please follow the guide at https://kubernetes.io/docs/setup/cri/)

7.关闭防火墙、swap

sudo ufw disable

关闭swap
Kubernetes 1.8开始要求必须禁用Swap,如果不关闭,默认配置下kubelet将无法启动。
/etc/fstab 中的这一行注释掉

/swap.img      none    swap    sw      0       0

执行sudo swapoff -a

8.配置root用户

cmpbook@master:~$ sudo passwd -u root
[sudo] password for cmpbook:
passwd: password expiry information changed.
cmpbook@node2:~$ sudo passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

9.安装kubelet\kubeadm\kubectl

`在`` /etc/apt/sources.list``` 中添加:

deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main

添加源的 key\更新并安装 :

curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -

sudo apt-get update

sudo apt-get install -y kubelet kubeadm kubectl

三.K8S配置

1.master机配置

1.1查看kubernetes所需镜像列表

cmpbook@master:~$ sudo systemctl start kubelet.service
cmpbook@master:~$ kubeadm config images list
k8s.gcr.io/kube-apiserver:v1.20.1
k8s.gcr.io/kube-controller-manager:v1.20.1
k8s.gcr.io/kube-scheduler:v1.20.1
k8s.gcr.io/kube-proxy:v1.20.1
k8s.gcr.io/pause:3.2
k8s.gcr.io/etcd:3.4.13-0
k8s.gcr.io/coredns:1.7.0

1.2master机初始化

root@master:/home/cmpbook# kubeadm init --kubernetes-version=v1.20.1 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=192.168.31.150/24

kubeadm init --kubernetes-version=v1.20.1 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.10.0.0/16 --apiserver-advertise-address=192.168.31.150
 
--apiserver-advertise-address string
API 服务器所公布的其正在监听的 IP 地址。如果未设置,则使用默认网络接口。
 
--pod-network-cidr string
指明 pod 网络可以使用的 IP 地址段。如果设置了这个参数,控制平面将会为每一个节点自动分配 CIDRs。
 
--image-repository string     默认值:"k8s.gcr.io"
选择用于拉取控制平面镜像的容器仓库

[init] Using Kubernetes version: v1.20.1
[preflight] Running pre-flight checks
[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.1   0.1. Latest validated version: 19.03
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local master] and IPs [10.96.0.1 192.168.31.150]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [localhost master] and IPs [192.168.31.150 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost master] and IPs [192.168.31.150 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 24.503584 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.20" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node master as control-plane by adding the labels "node-role.kubernetes.io/master=''" and "node-role.kubernetes.io/control-plane='' (deprecated)"
[mark-control-plane] Marking the node master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: vr8g6z.irmdyxomatp61nc9
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

Your Kubernetes control-plane has initialized successfully!


To start using your cluster, you need to run the following as a regular user:


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


Alternatively, if you are the root user, you can run:


  export KUBECONFIG=/etc/kubernetes/admin.conf


You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/


Then you can join any number of worker nodes by running the following on each as root:
 
#当出现如下内容时**(要保存下来,后边有用)**,执行成功:
kubeadm join 192.168.31.150:6443 --token vr8g6z.irmdyxomatp61nc9 \
    --discovery-token-ca-cert-hash sha256:9bd95573f268ebeda73d5a1be240da7c118bc2cbe6c5cc26251fff02baaaa885
 
#若执行失败,可执行如下命令,清除执行 init 产生的垃圾
kubeadm reset
rm -rf /etc/kubernetes

2.节点机配置

2.1加入k8s集群

cmpbook@node1:~$ su
Password:
root@node1:/home/cmpbook# kubeadm join 192.168.31.150:6443 --token *************** \
>     --discovery-token-ca-cert-hash 
sha256:*****************************************************************
[preflight] Running pre-flight checks
        [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 20.10.1. Latest validated version: 19.03
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...


This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.


Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

3.master机检测所有节点是否上线

cmpbook@master:~$ kubectl get nodes
NAME     STATUS     ROLES                  AGE     VERSION
master   NotReady   control-plane,master   3h39m   v1.20.1
node1    NotReady                    84m     v1.20.1
node2    NotReady                    60m     v1.20.1
node3    NotReady                    11m     v1.20.1

4.检测所有pod状态

cmpbook@master:~$ kubectl get pods --all-namespaces
NAMESPACE     NAME                             READY   STATUS    RESTARTS   AGE
kube-system   coredns-7f89b7bc75-hp729         0/1     Pending   0          3h40m
kube-system   coredns-7f89b7bc75-q6bpt         0/1     Pending   0          3h40m
kube-system   etcd-master                      1/1     Running   0          3h40m
kube-system   kube-apiserver-master            1/1     Running   0          3h40m
kube-system   kube-controller-manager-master   1/1     Running   0          3h40m
kube-system   kube-proxy-b62bc                 1/1     Running   0          61m
kube-system   kube-proxy-k5kj9                 1/1     Running   0          12m
kube-system   kube-proxy-s445f                 1/1     Running   0          84m
kube-system   kube-proxy-wc84m                 1/1     Running   0          3h40m
kube-system   kube-scheduler-master            1/1     Running   0          3h40m

5.非root用户/远程 -管理 k8s集群

客户端需安装kubectl

#当前用户创建.kube文件夹
mkdir -p $HOME/.kube
#复制k8s下admin.conf至.kube文件夹下
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
#修改权限
sudo chown $(id -u):$(id -g) $HOME/.kube/config

四.问题

1.kubernetes coredns 状态为Pod sandbox changed, it will be killed and re-created

安装flannel插件

sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

2.provided port is not in the valid range

修改kubernetes服务 nodeport 类型的端口范围

链接

  • centos8下k8s安装记录

你可能感兴趣的:(ubuntu18.04下K8S安装记录)