Kubernetes v1.19.10 安装

Resource

Version

Host OS

Ubuntu 20.04

Kubernetes

v1.19.10

deployment tool

kubeadm

CRI

containerd 1.4.4

cgroup driver

systemd

为开发目的安装软件包

apt update && apt upgrade -yapt install vim htop net-tools build-essential openssh-server axel tmux

容器化

apt-get remove docker docker-engine docker.io containerd runcapt-get updateapt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-commoncurl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -apt-key fingerprint 0EBFCD88add-apt-repository \"deb [arch=amd64] https://download.docker.com/linux/ubuntu \$(lsb_release -cs) stable"apt-get update
CONTAINERD_VER="1.4.4-1"apt-get install -y containerd.io=${CONTAINERD_VER}
apt-mark hold containerd.io

配置容器

https://kubernetes.io/docs/setup/production-environment/container-runtimes/#containerd

cat <
sudo mkdir -p /etc/containerdcontainerd config default | sudo tee /etc/containerd/config.toml

配置 cgroup 驱动程序

vi /etc/containerd/config.toml
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]  ...  [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]    SystemdCgroup = true  # add this

重新启动并在启动时重新加载

sudo systemctl daemon-reloadsudo systemctl restart containerd

使用 kubeadm 安装 Kubernetes

禁用交换

swapoff -a

要永久禁用交换,请编辑 /etc/fstab

桥接流量和 iptables

modprobe br_netfilter
cat <

安装 Kubernetes 包

apt-get update && apt-get install -y apt-transport-https curlcurl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -cat <

配置 kubelet

cat > /etc/systemd/system/kubelet.service.d/10-kubeadm.conf << EOF# Note: This dropin only works with kubeadm and kubelet v1.11+[Service]Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf"Environment="KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml"# This is a file that "kubeadm init" and "kubeadm join" generates at runtime, populating the KUBELET_KUBEADM_ARGS variable dynamicallyEnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env# This is a file that the user can use for overrides of the kubelet args as a last resort. Preferably, the user should use# the .NodeRegistration.KubeletExtraArgs object in the configuration files instead. KUBELET_EXTRA_ARGS should be sourced from this file.EnvironmentFile=-/etc/default/kubeletExecStart=# ExecStart=/usr/bin/kubeletEnvironment="KUBELET_CGROUP_ARGS=--cgroup-driver=systemd"ExecStart=/usr/bin/kubeletEOF

初始化控制平面节点


从 k8s.gcr.io 拉取镜像

K_VER="v1.19.10"
$ kubeadm config images pull \--image-repository="k8s.gcr.io" \--kubernetes-version=${K_VER}W0429 15:48:45.321686   10570 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io][config/images] Pulled k8s.gcr.io/kube-apiserver:v1.19.10[config/images] Pulled k8s.gcr.io/kube-controller-manager:v1.19.10[config/images] Pulled k8s.gcr.io/kube-scheduler:v1.19.10[config/images] Pulled k8s.gcr.io/kube-proxy:v1.19.10[config/images] Pulled k8s.gcr.io/pause:3.2[config/images] Pulled k8s.gcr.io/etcd:3.4.13-0[config/images] Pulled k8s.gcr.io/coredns:1.7.0
kubeadm init \--image-repository=k8s.gcr.io \--kubernetes-version=${K_VER} \--pod-network-cidr=10.244.0.0/16 \--service-cidr=10.96.0.0/12 \--control-plane-endpoint="$(hostname)" \--apiserver-advertise-address=0.0.0.0 \--cri-socket="/run/containerd/containerd.sock"
echo -e "
alias k=kubectl" >> ${HOME}/.bashrcecho "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ${HOME}/.bashrcsource ${HOME}/.bashrc

安装 CNI 插件

为 Pod 网络安装Flannel v0.13.0

wget "https://raw.githubusercontent.com/flannel-io/flannel/v0.13.0/Documentation/kube-flannel.yml"
kubectl apply -f ./kube-flannel.yml
$ kubectl get po -n kube-systemNAME                              READY   STATUS    RESTARTS   AGEcoredns-f9fd979d6-v8dgp           1/1     Running   0          29mcoredns-f9fd979d6-wt88m           1/1     Running   0          29metcd-tom-k8s                      1/1     Running   0          29mkube-apiserver-tom-k8s            1/1     Running   0          29mkube-controller-manager-tom-k8s   1/1     Running   0          29mkube-flannel-ds-5jqww             1/1     Running   0          26mkube-proxy-kdxtr                  1/1     Running   0          29mkube-scheduler-tom-k8s            1/1     Running   0          29m
$ kubectl taint nodes --all node-role.kubernetes.io/master-node/tom-k8s untainted

部署 helloworld 示例应用程序

cat > helloworld.yaml << EOFapiVersion: v1kind: Namespacemetadata:  name: helloworld---apiVersion: networking.k8s.io/v1kind: Ingressmetadata:  name: helloworld  namespace: helloworldspec:  rules:  - http:      paths:      - pathType: Prefix        path: /helloworld        backend:          service:            name: helloworld            port:              number: 8080---apiVersion: apps/v1kind: Deploymentmetadata:  name: helloworld  namespace: helloworldspec:  selector:    matchLabels:      run:  helloworld  replicas: 1  template:    metadata:      labels:         run:  helloworld    spec:      containers:        - name: helloworld          image: gcr.io/google-samples/node-hello:1.0          ports:            - containerPort: 8080              protocol: TCP---apiVersion: v1kind: Servicemetadata:  name: helloworld  namespace: helloworldspec:  ports:  - nodePort: 31215    port: 8080    protocol: TCP    targetPort: 8080  selector:    run: helloworld  type: NodePortEOF

访问服务

kubectl apply -f ./helloworld.yaml$ curl 0.0.0.0:31215Hello Kubernetes!

删除应用程序

kubectl delete -f ./helloworld.yaml

彻底移除 K8s 集群

kubeadm reset -f

Kubernetes

rm -rf ${HOME}/.kubesudo -irm -rf /etc/cni /etc/kubernetes /var/lib/dockershim /var/lib/etcd /var/lib/kubelet /var/run/kubernetesrm -rf ${HOME}/.kubeiptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -Xifconfig cni0 downip link delete cni0ifconfig flannel.1 downip link delete flannel.1rm -rf /var/lib/cni/rm -f /etc/cni/net.d/*
reboot
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章