一.k8s

k8s--------->Kubernetes
Kubernetes是容器集群管理系统,是一个开源的平台,可以实现容器集群的自动化部署、自动扩缩容、维护等功能。Kubernetes对计算资源进行了更高层次的抽象,通过将容器进行细致的组合,将最终的应用服务交给用户。
Kubernetes的好处:
(1)隐藏资源管理和错误处理,用户仅需要关注应用的开发。
(2)服务高可用、高可靠。
(3)可将负载运行在由成千上万的机器联合而成的集群中。

二.在每个结点上部署docker

实验环境

master:server4 172.25.13.4
node: server5 172.25.13.5
      server6 172.25.13.6
私有仓库: server7 172.25.13.7

1.安装并启动docker,添加阿里云镜像加速

关闭所有节点的防火墙和selinux

[root@server4 ~]# ls
containerd.io-1.2.13-3.2.el7.x86_64.rpm
container-selinux-2.77-1.el7.noarch.rpm
docker-ce-19.03.12-3.el7.x86_64.rpm
docker-ce-cli-19.03.12-3.el7.x86_64.rpm
[root@server4 ~]# yum install -y *

##启动docker
[root@server4 ~]# systemctl enable --now docker
[root@server4 ~]# cd /etc/docker/
[root@server4 docker]# ls
daemon.json  key.json

## 使用aliyun加速 将cgroup driver参数改为systemd,刷新重启docker
[root@server4 docker]# vim daemon.json 
[root@server4 docker]# scp daemon.json server5:/etc/docker/
root@server5's password: 
daemon.json                                   100%  287   359.7KB/s   00:00  
[root@server4 docker]# scp daemon.json server6:/etc/docker/
root@server6's password: 
daemon.json                                   100%  287   295.9KB/s   00:00    
[root@server4 docker]# systemctl daemon-reload
[root@server4 docker]# systemctl restart docker
[root@server4 docker]# ssh server5 systemctl restart docker
[root@server4 docker]# ssh server6 systemctl restart docker

##查看docker信息,确认修改成功
[root@server4 docker]# docker info

在这里插入图片描述

在这里插入图片描述

2.执行免密操作

[root@server4 docker]# ssh-keygen
[root@server4 docker]# ssh-copy-id server5
[root@server4 docker]# ssh-copy-id server6

3.所有节点上修改docker配置文件,配置桥接

[root@server4 docker]# cd /etc/sysctl.d
[root@server4 sysctl.d]# ls
99-sysctl.conf
[root@server4 sysctl.d]# vim k8s.conf
[root@server4 sysctl.d]# sysctl --system
* Applying /usr/lib/sysctl.d/00-system.conf ...
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
* Applying /usr/lib/sysctl.d/10-default-yama-scope.conf ...
kernel.yama.ptrace_scope = 0
* Applying /usr/lib/sysctl.d/50-default.conf ...
kernel.sysrq = 16
kernel.core_uses_pid = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.conf.all.promote_secondaries = 1
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
* Applying /etc/sysctl.d/99-sysctl.conf ...
* Applying /etc/sysctl.d/k8s.conf ...
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
* Applying /etc/sysctl.conf ...
[root@server4 sysctl.d]# scp k8s.conf server5:/etc/sysctl.d/
k8s.conf                                      100%   79    70.9KB/s   00:00    
[root@server4 sysctl.d]# scp k8s.conf server6:/etc/sysctl.d/
k8s.conf                                      100%   79    97.0KB/s   00:00    
[root@server4 sysctl.d]# ssh server5 sysctl --system
* Applying /etc/sysctl.d/k8s.conf ...
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
* Applying /etc/sysctl.conf ...
[root@server4 sysctl.d]# ssh server6 sysctl --system
* Applying /etc/sysctl.d/k8s.conf ...
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1

在这里插入图片描述

4.所有节点禁用swap分区

[root@server4 sysctl.d]# vim /etc/fstab
[root@server4 sysctl.d]# cd
[root@server4 ~]# swapoff -a
[root@server4 ~]# scp /etc/fstab server5:/etc
fstab                                         100%  466   496.6KB/s   00:00    
[root@server4 ~]# scp /etc/fstab server6:/etc
fstab                                         100%  466   466.8KB/s   00:00    
[root@server4 ~]# ssh server5 swapoff -a
[root@server4 ~]# ssh server6 swapoff -a

在这里插入图片描述

三.k8s部署

1.所有节点配置yum源,安装kubelet kubeadm kubectl,开启kubelet

[root@server4 ~]# cd /etc/yum.repos.d/
[root@server4 yum.repos.d]# vim kubernetes.repo
[k8s@server4 yum.repos.d]$ cat kubernetes.repo 
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
[root@server4 yum.repos.d]# yum repolist
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
kubernetes                                               | 1.4 kB     00:00     
kubernetes/primary                                         |  77 kB   00:00     
kubernetes                                                              557/557
repo id                              repo name                            status
dvd                                  rhel7.6                              5,152
kubernetes                           Kubernetes                             557
repolist: 5,709
[root@server4 yum.repos.d]# scp kubernetes.repo server5:/etc/yum.repos.d/
kubernetes.repo                               100%  129   149.0KB/s   00:00    
[root@server4 yum.repos.d]# scp kubernetes.repo server6:/etc/yum.repos.d/
kubernetes.repo                               100%  129   132.1KB/s   00:00    
[root@server4 ~]# yum install -y kubelet kubeadm kubectl
[root@server4 ~]# ssh server5 yum install -y kubelet kubeadm kubectl
[root@server4 ~]# ssh server6 yum install -y kubelet kubeadm kubectl
[root@server4 ~]# systemctl enable --now kubelet
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
[root@server4 ~]# ssh server5 systemctl enable --now kubelet
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.
[root@server4 ~]# ssh server6 systemctl enable --now kubelet
Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.

查看默认配置信息
[root@server4 ~]# kubeadm config print init-defaults
W0909 11:16:23.646602    7718 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: 1.2.3.4
  bindPort: 6443
nodeRegistration:
  criSocket: /var/run/dockershim.sock
  name: server4
  taints:
  - effect: NoSchedule
    key: node-role.kubernetes.io/master
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
  type: CoreDNS
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: k8s.gcr.io
kind: ClusterConfiguration
kubernetesVersion: v1.19.0
networking:
  dnsDomain: cluster.local
  serviceSubnet: 10.96.0.0/12
scheduler: {}

2.拉取镜像

[root@server4 ~]# kubeadm config images pull --image-repository registry.aliyuncs.com/google_containers

在这里插入图片描述

3.集群初始化

[root@server4 ~]# kubeadm init --pod-network-cidr=10.244.0.0/16 --image-repository registry.aliyuncs.com/google_containers

在这里插入图片描述

4.配置kubectl,创建并使用普通用户

[root@server4 ~]# useradd k8s
[root@server4 ~]# visudo
[root@server4 ~]# su - k8s
[k8s@server4 ~]$ mkdir -p $HOME/.kube
[k8s@server4 ~]$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[k8s@server4 ~]$ sudo chown $(id -u):$(id -g) $HOME/.kube/config
[k8s@server4 ~]$ kubectl get pod --all-namespaces
NAMESPACE     NAME                              READY   STATUS    RESTARTS   AGE
kube-system   coredns-6d56c8448f-7dp8x          0/1     Pending   0          5m22s
kube-system   coredns-6d56c8448f-zxjmm          0/1     Pending   0          5m22s
kube-system   etcd-server4                      1/1     Running   0          5m33s
kube-system   kube-apiserver-server4            1/1     Running   0          5m33s
kube-system   kube-controller-manager-server4   1/1     Running   0          5m33s
kube-system   kube-proxy-tvcgd                  1/1     Running   0          5m22s
kube-system   kube-scheduler-server4            1/1     Running   0          5m33s
[k8s@server4 ~]$ kubectl get node
NAME      STATUS     ROLES    AGE     VERSION
server4   NotReady   master   5m54s   v1.19.0

在这里插入图片描述

5.安装flannel网络组件,使用wget

[k8s@server4 ~]$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

在这里插入图片描述

[k8s@server4 ~]$ sudo yum install -y wget
[k8s@server4 ~]$ wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

在这里插入图片描述

6.查看全部命名空间

[k8s@server4 ~]$ kubectl get pod --all-namespaces

在这里插入图片描述

7.节点扩容(使用master初始化后动态生成的证书)

[root@server5 ~]# kubeadm join --token b3a32e.7cef20447b55261e 172.25.13.4:6443 --discovery-token-ca-cert-hash sha256:bc718df41fdceb0db6c5380c7e27c204589b41dcb5f9a3bc52c254b707377f2f
[root@server6 ~]# kubeadm join --token b3a32e.7cef20447b55261e 172.25.13.4:6443 --discovery-token-ca-cert-hash sha256:bc718df41fdceb0db6c5380c7e27c204589b41dcb5f9a3bc52c254b707377f2f

在这里插入图片描述
在这里插入图片描述

8.master节点查看状态,三台主机状态均为ready

[root@server4 ~]# kubectl get node
[root@server4 ~]# kubectl get pod -n kube-system
[root@server4 ~]# kubectl get pod -n kube-system -o wide

在这里插入图片描述
在这里插入图片描述

Logo

开源、云原生的融合云平台

更多推荐