Kubeadm 安装

Kubeadm 安装

需要的的机器:

192.168.111.25 master01

192.168.111.21 node01

192.168.111.23 node02

环境准备:

所有节点,关闭防火墙规则,关闭selinux,关闭swap交换

systemctl stop firewalld

systemctl disable firewalld

setenforce 0

sed -i 's/enforcing/disabled/' /etc/selinux/config

iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X

swapoff -a #交换分区必须要关闭

sed -ri 's/.*swap.*/#&/' /etc/fstab #永久关闭swap分区,&符号在sed命令中代表上次匹配的结果

#加载 ip_vs 模块

for i in $(ls /usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs|grep -o "^[^.]*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done

修改主机名

hostnamectl set-hostname master01

hostnamectl set-hostname node01

hostnamectl set-hostname node02

vim /etc/hosts

192.168.111.25 master01

192.168.111.21 node01

192.168.111.23 node02

//调整内核参数

cat > /etc/sysctl.d/kubernetes.conf << EOF

#开启网桥模式,可将网桥的流量传递给iptables链

net.bridge.bridge-nf-call-ip6tables=1

net.bridge.bridge-nf-call-iptables=1

#关闭ipv6协议

net.ipv6.conf.all.disable_ipv6=1

net.ipv4.ip_forward=1

EOF

生效参数

sysctl --system

[root@25 ~]# systemctl stop firewalld

[root@25 ~]# systemctl disable firewalld

[root@25 ~]# setenforce 0

[root@25 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config

[root@25 ~]# iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X

[root@25 ~]# swapoff -a

[root@25 ~]# sed -ri 's/.*swap.*/#&/' /etc/fstab

[root@25 ~]# for i in $(ls /usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs|grep -o "^[^.]*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done

ip_vs_dh

ip_vs_ftp

[root@25 ~]# hostnamectl set-hostname master01

[root@25 ~]# vim /etc/hosts

[root@25 ~]# cat > /etc/sysctl.d/kubernetes.conf << EOF

> #开启网桥模式,可将网桥的流量传递给iptables链

> net.bridge.bridge-nf-call-ip6tables=1

> net.bridge.bridge-nf-call-iptables=1

> #关闭ipv6协议

> net.ipv6.conf.all.disable_ipv6=1

> net.ipv4.ip_forward=1

> EOF

[root@25 ~]# sysctl --system

* Applying /usr/lib/sysctl.d/00-system.conf ...

* Applying /usr/lib/sysctl.d/10-default-yama-scope.conf ...

kernel.yama.ptrace_scope = 0

[root@23 ~]# systemctl enable kubelet.service

Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.

[root@23 ~]# kubeadm join 192.168.111.25:6443 --token agsjqv.418o885psv2uxdgl --discovery-token-ca-cert-hash sha256:fb377a95a613e5f6721ba07bee3dcdaa9a54b71e295501e7d7a5fe6c97c3af0e

[preflight] Running pre-flight checks

[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 23.0.1. Latest validated version: 19.03

[WARNING Hostname]: hostname "node02" c

[root@23 ~]# cd /opt

[root@23 opt]# ls

cni containerd flannel.tar k8s rh

[root@23 opt]# docker load < flannel.tar

777b2c648970: Loading layer 5.848MB/5.848MB

815dff9e0b57: Loading layer 11.42MB/11.42MB

2e16188127c8: Loading layer 2.267MB/2.267MB

eb738177d102: Loading layer 49.34MB/49.34MB

b613d890216c: Loading layer 5.12kB/5.12kB

8a984b390686: Loading layer 9.216kB/9.216kB

814fbd599e1f: Loading layer 7.68kB/7.68kB

Loaded image: quay.io/coreos/flannel:v0.14.0

[root@25~]#yum-config-manager--add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

已加载插件:fastestmirror, langpacks

adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo

repo saved to /etc/yum.repos.d/docker-ce.repo

[root@25 ~]# yum install -y docker-ce docker-ce-cli containerd.io

已加载插件:fastestmirror, langpacks

docker-ce-stable | 3.5 kB 00:00

(1/2): docker-ce-stable/7/x86_64/updateinfo | 55 B 00:00

(2/2): docker-ce-stable/7/x86_64/primary_db | 97 kB 00:06

Loading mirror speeds from cached hostfile

* base: mirrors.ustc.edu.cn

* extras: mirrors.ustc.edu.cn

* updates: mirrors.ustc.edu.cn

正在解决依赖关系

--> 正在检查事务

---> 软件包 containerd.io.x86_64.0.1.6.16-3.1.el7 将被 安装

--> 正在处理依赖关系 container-sel

[root@25 ~]# systemctl restart docker

[root@25 ~]# systemctl enable docker.service

[root@25 ~]# docker info | grep "Cgroup Driver"

Cgroup Driver: systemd

[root@25 ~]# cat > /etc/yum.repos.d/kubernetes.repo << EOF

> [kubernetes]

> name=Kubernetes

> baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64

> enabled=1

> gpgcheck=0

> repo_gpgcheck=0

> gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg

> EOF

[root@25 ~]# yum install -y kubelet-1.20.11 kubeadm-1.20.11 kubectl-1.20.11

已加载插件:fastestmirror, langpacks

kubernetes | 1.4 kB 00:00

kubernetes/primary | 124 kB 00:01

Loading mirror speeds from cached hostfile

* base: mirrors.ustc.edu.cn

* extras: mirrors.ustc.edu.cn

[root@25 ~]# systemctl enable kubelet.service

Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.

[root@25 ~]# cd /opt

[root@25 opt]# unzip v1.20.11.zip -d /opt/k8s

Archive: v1.20.11.zip

creating: /opt/k8s/v1.20.11/

inflating: /opt/k8s/v1.20.11/apiserver.tar

inflating: /opt/k8s/v1.20.11/controller-manager.tar

inflating: /opt/k8s/v1.20.11/coredns.tar

inflating: /opt/k8s/v1.20.11/etcd.tar

inflating: /opt/k8s/v1.20.11/pause.tar

inflating: /opt/k8s/v1.20.11/proxy.tar

inflating: /opt/k8s/v1.20.11/scheduler.tar

[root@25 opt]# cd /opt/k8s/v1.20.11

[root@25 v1.20.11]# for i in $(ls *.tar); do docker load -i $i; done

07363fa84210: Loading layer 3.062MB/3.062MB

[root@25 v1.20.11]# scp -r /opt/k8s root@192.168.111.23:/opt

The authenticity of host '192.168.111.23 (192.168.111.23)' can't be established.

ECDSA key fingerprint is SHA256:5Kq4jP4lwc+9mzHeZLdCPfIjQUK6G9lIDDaW8CC6VWc.

ECDSA key fingerprint is MD5:cd:f1:a4:b5:16:14:74:91:4d:24:d8:1d:67:bd:55:2f.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.111.23' (ECDSA) to the list of known hosts.

root@192.168.111.23's password:

apiserver.tar 100% 117MB 123.9MB/s 00:00

controller-manager.tar 100% 112MB 128.4MB/s 00:00

coredns.tar 100% 43MB 120.6MB/s 00:00

etcd.tar 100% 243MB 132.3MB/s 00:01

pause.tar 100% 677KB 53.6MB/s 00:00

proxy.tar 100% 97MB 133.2MB/s 00:00

scheduler.tar 100% 46MB 133.1MB/s 00:00

[root@25 v1.20.11]# kubeadm init \

> --apiserver-advertise-address=192.168.111.25 \

> --image-repository registry.aliyuncs.com/google_containers \

> --kubernetes-version=v1.20.11 \

> --service-cidr=10.96.0.0/16 \

> --pod-network-cidr=10.244.0.0/16 \

> --token-ttl=0

[init] Using Kubernetes version: v1.20.11

[root@25 ~]# kubectl apply -f kube-flannel.yml

namespace/kube-flannel unchanged

clusterrole.rbac.authorization.k8s.io/flannel unchanged

clusterrolebinding.rbac.authorization.k8s.io/flannel unchanged

serviceaccount/flannel unchanged

configmap/kube-flannel-cfg unchanged

daemonset.apps/kube-flannel-ds unchanged

[root@25 ~]# kubectl get pods -n kube-system

NAME READY STATUS RESTARTS AGE

coredns-7f89b7bc75-bkmvb 1/1 Running 0 76m

coredns-7f89b7bc75-xbf55 1/1 Running 0 76m

etcd-master01 1/1 Running 0 77m

[root@25 ~]# vim /etc/kubernetes/manifests/kube-scheduler.yaml

[root@25 ~]# vim /etc/kubernetes/manifests/kube-controller-manager.yaml

[root@25 ~]# kubectl get pods -A

NAMESPACE NAME READY STATUS RESTARTS AGE

kube-flannel kube-flannel-ds-kfqgs 1/1 Running 0 7m27s

kube-flannel kube-flannel-ds-vsqqk 1/1 Running 0 7m27s

kube-flannel kube-flannel-ds-vt7kp 1/1 Running 0 7m27s

kube-system coredns-7f89b7bc75-bkmvb 1/1 Running 0

[root@25 ~]# kubectl get cs

Warning: v1 ComponentStatus is deprecated in v1.19+

NAME STATUS MESSAGE ERROR

scheduler Healthy ok

controller-manager Healthy ok

etcd-0 Healthy {"health":"true"}

[root@25 ~]# kubectl get nodes

NAME STATUS ROLES AGE VERSION

master01 Ready control-plane,master 81m v1.20.11

node01 Ready node 21m v1.20.11

node02 Ready node 21m v1.20.11

[root@25 ~]# kubectl get pods -A

[root@25 ~]# kubectl create deployment nginx --image=nginx:1.14

deployment.apps/nginx created

[root@25 ~]#

[root@25 ~]# kubectl expose deployment nginx --port=80 --type=NodePort

service/nginx exposed

[root@25 ~]# kubectl get pods

NAME READY STATUS RESTARTS AGE

nginx-5658bdf5d4-jdqjx 1/1 Running 0 42s

[root@25 ~]#

[root@25 ~]# kubectl get svc

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE

kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 82m

nginx NodePort 10.96.189.254 <none> 80:32525/TCP 20s

[root@25 ~]# kubectl scale deployment nginx --replicas=3

deployment.apps/nginx scaled

[root@25 ~]# kubectl get pods -o wide

NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES

nginx-5658bdf5d4-94hff 1/1 Running 0 7s 10.244.1.3 node01 <none> <none>

nginx-5658bdf5d4-jdqjx 1/1 Running 0 34m 10.244.1.2 node01 <none> <none>

nginx-5658bdf5d4-zjwv8 1/1 Running 0 7s 10.244.1.4 node01 <none> <none>

[root@25 k8s]# cd /opt

[root@25 opt]# vim recommended.yaml

[root@25 opt]# kubectl apply -f recommended.yaml

[root@25 opt]# kubectl create serviceaccount dashboard-admin -n kube-system

serviceaccount/dashboard-admin created

[root@25 opt]# kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin

clusterrolebinding.rbac.authorization.k8s.io/dashboard-admin created

[root@25 opt]# kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')

Name: dashboard-admin-token-5gvkv

Namespace: kube-system

Labels: <none>

Annotations: kubernetes.io/service-account.name: dashboard-admin

kubernetes.io/service-account.uid: 5465cdd1-5e00-4ea5-a3f0-e692f4875aa3

[root@25 opt]# kubectl get nodes

NAME STATUS ROLES AGE VERSION

master01 Ready control-plane,master 132m v1.20.11

node01 Ready node 73m v1.20.11

node02 Ready node 72m v1.20.11

-------------------- 安装Harbor私有仓库 --------------------

//修改主机名

hostnamectl set-hostname hub.cj.com

//所有节点加上主机名映射

echo '192.168.111.20 hub.cj.com' >> /etc/hosts

//安装 docker

yum install -y yum-utils device-mapper-persistent-data lvm2

yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

yum install -y docker-ce docker-ce-cli containerd.io

mkdir /etc/docker

cat > /etc/docker/daemon.json <<EOF

{

"registry-mirrors": ["https://6ijb8ubo.mirror.aliyuncs.com"],

"exec-opts": ["native.cgroupdriver=systemd"],

"log-driver": "json-file",

"log-opts": {

"max-size": "100m"

},

"insecure-registries": ["https://hub.kgc.com"]

}

EOF

[root@20 ~]# mkdir /etc/docker

mkdir: 无法创建目录"/etc/docker": 文件已存在

[root@20 ~]# cat > /etc/docker/daemon.json <<EOF

> {

> "registry-mirrors": ["https://6ijb8ubo.mirror.aliyuncs.com"],

> "exec-opts": ["native.cgroupdriver=systemd"],

> "log-driver": "json-file",

> "log-opts": {

> "max-size": "100m"

> },

> "insecure-registries": ["https://hub.kgc.com"]

> }

> EOF

posted @ 2023-02-20 22:46  withfear  阅读(363)  评论(0编辑  收藏  举报
----------------------------------- ©著作权归作者所有:来自51CTO博客作者一品堂_技术学习笔记的原创作品,请联系作者获取转载授权,否则将追究法律责任 博客园随笔中添加目录导航悬浮框博客园随笔中添加目录导航悬浮框 https://blog.51cto.com/ios9/3125785