kubernetes 1.18安装

一. 系统准备
1.系统版本https://www.cndba.cn/hbhe0316/article/4832

[root@k8s-master home]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)

2./etc/hosts信息https://www.cndba.cn/hbhe0316/article/4832

[root@k8s-master home]#  cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.31.86  k8s-master
192.168.31.128 k8s-node01
192.168.31.166 k8s-node02

3.添加阿里源https://www.cndba.cn/hbhe0316/article/4832

[root@k8s-master ~]# rm -rf /etc/yum.repos.d/*
[root@k8s-master ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo

4.关闭swap,注释swap分区

[root@k8s-master ~]# swapoff -a
[root@k8s-master ~]# vi /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed Apr 15 12:35:57 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=e6d5f326-d177-4503-b7a5-e58dac9d8716 /boot                   xfs     defaults        0 0
#/dev/mapper/centos-swap swap

5.配置内核参数,将桥接的IPv4流量传递到iptables的链

[root@k8s-master ~]# cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system

6.安装常用包

yum install vim bash-completion net-tools gcc -y
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

7.关闭防火墙、关闭selinux

[root@k8s-master home]#  systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@k8s-master home]#  systemctl stop firewalld

7安装docker

[root@k8s-master yum.repos.d]# wget -qO- https://get.docker.com/ | sh 
[root@k8s-master yum.repos.d]# systemctl enable docker.service
[root@k8s-master yum.repos.d]# systemctl restart docker.service
[root@k8s-master yum.repos.d]# systemctl status docker.service

8添加aliyundocker仓库加速器

[root@k8s-master ~]# mkdir -p /etc/docker
[root@k8s-master ~]# tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://fl791z1h.mirror.aliyuncs.com"]
}
EOF
[root@k8s-master ~]# systemctl daemon-reload
[root@k8s-master~]# systemctl restart docker

9.安装kubectl、kubelet、kubeadm
添加阿里kubernetes源https://www.cndba.cn/hbhe0316/article/4832

[root@k8s-master ~]# cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF


[root@k8s-master ~]# yum install -y kubectl kubelet kubeadm
[root@k8s-master ~]# systemctl enable kubelet

10.初始化k8s集群https://www.cndba.cn/hbhe0316/article/4832

kubeadm init --kubernetes-version=1.18.0  /
--apiserver-advertise-address=192.168.31.86   /
--image-repository registry.aliyuncs.com/google_containers  /
--service-cidr=10.10.0.0/16 --pod-network-cidr=10.122.0.0/16

POD的网段为: 10.122.0.0/16, api server地址就是master本机IP。
这一步很关键,由于kubeadm 默认从官网k8s.grc.io下载所需镜像,国内无法访问,因此需要通过–image-repository指定阿里云镜像仓库地址。

[root@k8s-master yum.repos.d]# kubeadm init --kubernetes-version=1.18.0  /
> --apiserver-advertise-address=192.168.31.86   /
> --image-repository registry.aliyuncs.com/google_containers  /
> --service-cidr=10.10.0.0/16 --pod-network-cidr=10.122.0.0/16
W0417 19:33:54.674088    2571 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[init] Using Kubernetes version: v1.18.0
[preflight] Running pre-flight checks
        [WARNING Firewalld]: firewalld is active, please ensure ports [6443 10250] are open or your cluster may not function correctly
        [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/
[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'
[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
[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 [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.10.0.1 192.168.31.86]
[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 [k8s-master localhost] and IPs [192.168.31.86 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.31.86 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
[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"
W0417 19:34:53.129011    2571 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
[control-plane] Creating static Pod manifest for "kube-scheduler"
W0417 19:34:53.130312    2571 manifests.go:225] the default kube-apiserver authorization-mode is "Node,RBAC"; using "Node,RBAC"
[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 19.002477 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.18" 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 k8s-master as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: f9d4gg.y14rly81xjrgzapi
[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

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.86:6443 --token f9d4gg.y14rly81xjrgzapi /
    --discovery-token-ca-cert-hash sha256:c5506eaac27e82df00e1235e14e46338816a86dfccf3bfc35797643f633bc5a7
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

执行下面命令,使kubectl可以自动补充https://www.cndba.cn/hbhe0316/article/4832

source <(kubectl completion bash)

查看节点,podhttps://www.cndba.cn/hbhe0316/article/4832

[root@k8s-master yum.repos.d]# kubectl get node
NAME         STATUS     ROLES    AGE     VERSION
k8s-master   NotReady   master   2m15s   v1.18.1
[root@k8s-master yum.repos.d]# kubectl get pod --all-namespaces
NAMESPACE     NAME                                 READY   STATUS    RESTARTS   AGE
kube-system   coredns-7ff77c879f-8fldx             0/1     Pending   0          2m4s
kube-system   coredns-7ff77c879f-vvbb4             0/1     Pending   0          2m4s
kube-system   etcd-k8s-master                      1/1     Running   0          2m20s
kube-system   kube-apiserver-k8s-master            1/1     Running   0          2m20s
kube-system   kube-controller-manager-k8s-master   1/1     Running   0          2m20s
kube-system   kube-proxy-94mfc                     1/1     Running   0          2m4s
kube-system   kube-scheduler-k8s-master            1/1     Running   0          2m20s

node节点为NotReady,因为corednspod没有启动,缺少网络pod

12. 安装calico网络
[root@k8s-master yum.repos.d]# kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
configmap/calico-config created
customresourcedefinition.apiextensions.k8s.io/bgpconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/bgppeers.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/blockaffinities.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/clusterinformations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/felixconfigurations.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/globalnetworksets.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/hostendpoints.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamblocks.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamconfigs.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ipamhandles.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/ippools.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networkpolicies.crd.projectcalico.org created
customresourcedefinition.apiextensions.k8s.io/networksets.crd.projectcalico.org created
clusterrole.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrolebinding.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrole.rbac.authorization.k8s.io/calico-node created
clusterrolebinding.rbac.authorization.k8s.io/calico-node created
daemonset.apps/calico-node created
serviceaccount/calico-node created
deployment.apps/calico-kube-controllers created
serviceaccount/calico-kube-controllers created


[root@k8s-master yum.repos.d]# kubectl get pod --all-namespaces
NAMESPACE     NAME                                       READY   STATUS    RESTARTS   AGE
kube-system   calico-kube-controllers-555fc8cc5c-sqcx4   1/1     Running   0          102m
kube-system   calico-node-pbdkp                          1/1     Running   0          102m
kube-system   coredns-7ff77c879f-8fldx                   1/1     Running   0          105m
kube-system   coredns-7ff77c879f-vvbb4                   1/1     Running   0          105m
kube-system   etcd-k8s-master                            1/1     Running   0          105m
kube-system   kube-apiserver-k8s-master                  1/1     Running   0          105m
kube-system   kube-controller-manager-k8s-master         1/1     Running   0          105m
kube-system   kube-proxy-94mfc                           1/1     Running   0          105m
kube-system   kube-scheduler-k8s-master                  1/1     Running   0          105m

[root@k8s-master yum.repos.d]# kubectl get node
NAME         STATUS   ROLES    AGE    VERSION
k8s-master   Ready    master   107m   v1.18.1

13.安装dashboard

[root@k8s-master home]# kubectl create -f recommended.yaml
namespace/kubernetes-dashboard created
serviceaccount/kubernetes-dashboard created
service/kubernetes-dashboard created
secret/kubernetes-dashboard-certs created
secret/kubernetes-dashboard-csrf created
secret/kubernetes-dashboard-key-holder created
configmap/kubernetes-dashboard-settings created
role.rbac.authorization.k8s.io/kubernetes-dashboard created
clusterrole.rbac.authorization.k8s.io/kubernetes-dashboard created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
clusterrolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
deployment.apps/kubernetes-dashboard created
service/dashboard-metrics-scraper created
deployment.apps/dashboard-metrics-scraper createdhttps://www.cndba.cn/hbhe0316/article/4832

查看Pods、svc

[root@k8s-master home]# kubectl get pods,svc -n kubernetes-dashboard
NAME                                            READY   STATUS    RESTARTS   AGE
pod/dashboard-metrics-scraper-dc6947fbf-2qdlr   1/1     Running   0          78s
pod/kubernetes-dashboard-5d4dc8b976-snb2j       1/1     Running   0          78s

NAME                                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)         AGE
service/dashboard-metrics-scraper   ClusterIP   10.10.231.53    <none>        8000/TCP        78s
service/kubernetes-dashboard        NodePort    10.10.168.247   <none>        443:30000/TCP   78s

查看token

[root@k8s-master home]# kubectl get secrets -n kubernetes-dashboard
NAME                               TYPE                                  DATA   AGE
default-token-gknpj                kubernetes.io/service-account-token   3      2m20s
kubernetes-dashboard-certs         Opaque                                0      2m20s
kubernetes-dashboard-csrf          Opaque                                1      2m20s
kubernetes-dashboard-key-holder    Opaque                                2      2m20s
kubernetes-dashboard-token-zsx7p   kubernetes.io/service-account-token   3      2m20s

[root@k8s-master home]# kubectl describe secrets -n kubernetes-dashboard kubernetes-dashboard-token-zsx7p | grep token | awk 'NR==3{print $2}'
eyJhbGciOiJSUzI1NiIsImtpZCI6IktYOVdvNlF2TTZvQjVFY0REYWotb2VVSHRtbmRndkkxd3dLM081eTlOWVUifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZC10b2tlbi16c3g3cCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjYzYTI2OWI3LTVkODUtNDk0Ny05YTJmLTM1MmQ0Mjk5YjVmMyIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlcm5ldGVzLWRhc2hib2FyZDprdWJlcm5ldGVzLWRhc2hib2FyZCJ9.CTTqbtHNZe2sWmTWyx1034w94yjAc34ftm3jy3S9cRA_bPCN8etdNqS4KXJStJuYevSfm2WyxTpTAtEoTe2yGpfn-ldMACueEteKz1GqlrzmkM9Hclzq2dnC5oNsdOWTCjbIvEA5KIgLD_jJC-_eAcDE55Rsjed-FrPRJf3WNjtH47d0ujuYdAtI2ooSYgYSHLsQJiqtjiNT5d_Varja_MdTv3-9kKMkwfoX6H_3-TSq0ot59D27ZSqmQlR9UqbvAtWUddHBXt7TGORFxPC6heXv-lagsLgTGv5wqpZHytQNKtdUj8DfdjVxLmLzWeLyprI8IwQ90q-0k7KzBWWT-Q

登录:https://192.168.31.86:30000输入https://www.cndba.cn/hbhe0316/article/4832

eyJhbGciOiJSUzI1NiIsImtpZCI6IktYOVdvNlF2TTZvQjVFY0REYWotb2VVSHRtbmRndkkxd3dLM081eTlOWVUifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZC10b2tlbi16c3g3cCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjYzYTI2OWI3LTVkODUtNDk0Ny05YTJmLTM1MmQ0Mjk5YjVmMyIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlcm5ldGVzLWRhc2hib2FyZDprdWJlcm5ldGVzLWRhc2hib2FyZCJ9.CTTqbtHNZe2sWmTWyx1034w94yjAc34ftm3jy3S9cRA_bPCN8etdNqS4KXJStJuYevSfm2WyxTpTAtEoTe2yGpfn-ldMACueEteKz1GqlrzmkM9Hclzq2dnC5oNsdOWTCjbIvEA5KIgLD_jJC-_eAcDE55Rsjed-FrPRJf3WNjtH47d0ujuYdAtI2ooSYgYSHLsQJiqtjiNT5d_Varja_MdTv3-9kKMkwfoX6H_3-TSq0ot59D27ZSqmQlR9UqbvAtWUddHBXt7TGORFxPC6heXv-lagsLgTGv5wqpZHytQNKtdUj8DfdjVxLmLzWeLyprI8IwQ90q-0k7KzBWWT-Q

14 .将k8s-node01和k8s-node02加入集群,在k8s-node01和k8s-node02上执行命令:https://www.cndba.cn/hbhe0316/article/4832

kubeadm join 192.168.31.86:6443 --token f9d4gg.y14rly81xjrgzapi --discovery-token-ca-cert-hash sha256:c5506eaac27e82df00e1235e14e46338816a86dfccf3bfc35797643f633bc5a7

版权声明:本文为博主原创文章,未经博主允许不得转载。

kuberneter

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值