Ko o maatau wheako me nga raraunga i roto i te roopu Kubernetes etcd tika (kaore he K8s API)

Kei te piki haere, kei te tono mai nga kaihoko ki a maatau ki te tuku uru ki te roopu Kubernetes kia uru atu ki nga ratonga i roto i te roopu: kia taea te hono tika ki etahi putunga raraunga, ratonga ranei, ki te hono i tetahi tono a-rohe me nga tono kei roto i te roopu...

Ko o maatau wheako me nga raraunga i roto i te roopu Kubernetes etcd tika (kaore he K8s API)

Hei tauira, me hono mai i to miihini rohe ki tetahi ratonga memcached.staging.svc.cluster.local. Ka whakaratohia e matou tenei kaha ma te whakamahi i te VPN i roto i te roopu e hono ana te kiritaki. Ki te mahi i tenei, ka panuitia e matou nga kupenga-roto o nga pene, nga ratonga me te pana i te DNS tautau ki te kiritaki. Na, ka ngana ana te kiritaki ki te hono atu ki te ratonga memcached.staging.svc.cluster.local, ka haere te tono ki te DNS tautau, a, hei whakautu, ka whakawhiwhia te wahitau o tenei ratonga mai i te whatunga ratonga tautau, te wahitau pod ranei.

Ka whirihorahia e matou nga tautau K8s ma te whakamahi i te kubeadm, kei reira te rarangiroto ratonga taunoa 192.168.0.0/16, me te kupenga o nga pākākano ko 10.244.0.0/16. I te nuinga o te wa ka pai nga mea katoa, engari he rua nga waahanga:

  • Rorohiko 192.168.*.* he maha nga wa e whakamahia ana i roto i nga whatunga tari a nga kaihoko, me te maha atu i nga whatunga kaainga kaiwhakawhanake. Na ka puta mai he taupatupatu: ka mahi nga pouara kaainga ki tenei kupenga-roto, ka pana te VPN i enei kupenga-roto mai i te kahui ki te kiritaki.
  • He maha nga tautau (whakaputa, atamira me/ranei etahi tautau dev). Na, ma te taunoa, he rite nga kupenga-roto katoa mo nga poti me nga ratonga, ka puta he uauatanga mo te mahi tukutahi me nga ratonga i roto i nga tautau maha.

Kua roa kee matou ki te whakamahi i nga kupenga-roto rereke mo nga ratonga me nga poti i roto i te kaupapa kotahi - i te nuinga o te waa, kia rereke nga hononga o nga tautau katoa. Heoi, he maha nga tautau e mahi ana kaore au e pai ki te huri mai i te wahanga, na te mea he maha nga ratonga, tono mana, aha atu.

Na ka patai matou ki a matou ano: me pehea te whakarereke i te kupenga-roto i roto i te kahui o naianei?

Te rapu whakatau

Ko te mahi noa ko te hanga ano katoa ratonga me te momo ClusterIP. Hei whiringa, ka taea te tohutohu me tenei:

Ko te tukanga e whai ake nei he raruraru: i muri i nga mea katoa i whirihorahia, ka puta mai nga putea me te IP tawhito hei ingoa ingoa DNS i /etc/resolv.conf.
I te mea kaore ano i kitea e au te otinga, me tautuhi ano ahau i te huinga katoa me te kubeadm tautuhi ka timata ano.

Engari kaore tenei e pai mo te katoa... Anei etahi atu korero whakataki mo ta matou keehi:

  • Ka whakamahia te harakeke;
  • He tautau kei roto i nga kapua me nga taputapu;
  • E hiahia ana ahau ki te karo i te tuku ano i nga ratonga katoa i roto i te roopu;
  • He hiahia ki te mahi i nga mea katoa me te iti rawa o nga raru;
  • Ko te putanga Kubernetes he 1.16.6 (heoi, ka rite etahi atu waahanga mo etahi atu putanga);
  • Ko te mahi matua ko te whakarite kei roto i te roopu ka tukuna ma te whakamahi i te kubeadm me te kupengaroto ratonga 192.168.0.0/16, whakakapi ki te 172.24.0.0/16.

Na ka tupono kua roa matou e hiahia ana ki te kite he aha me pehea te penapena i roto i te Kubernetes i roto i te etcd, he aha te mahi ki reira... Na ka whakaaro matou: "He aha te kore e whakahou noa i nga raraunga i roto i te etcd, ka whakakapi i nga wahitau IP tawhito (roto) me nga mea hou? "

I te rapu mo nga taputapu kua rite mo te mahi me nga raraunga i roto i te etcd, kaore i kitea e matou tetahi mea hei whakaoti i te raru. (Na, mena ka mohio koe mo etahi taputapu mo te mahi me nga raraunga i roto i te etcd, ka mihi matou ki nga hononga.) Heoi, he timatanga pai etcdhelper mai i OpenShift (nga mihi ki ona kaituhi!).

Ka taea e tenei taputapu te hono atu ki te etcd ma te whakamahi i nga tiwhikete me te panui raraunga mai i reira ma te whakamahi i nga whakahau ls, get, dump.

Tāpiri etchelper

Ko te whakaaro e whai ake nei he arorau: "He aha te aukati i a koe ki te taapiri i tenei taputapu ma te taapiri i te kaha ki te tuhi raraunga ki te etcd?"

Ka waiho hei putanga whakarereke o etchelper me nga mahi hou e rua changeServiceCIDR и changePodCIDR. i runga i a ia ka kite koe i te waehere konei.

He aha nga mahi hou? Hātepe changeServiceCIDR:

  • hanga he deserializer;
  • whakahiato he korero auau hei whakakapi i te CIDR;
  • ka haere matou ki nga ratonga katoa me te momo ClusterIP i roto i te roopu:
    • wetewete i te uara mai i etcd ki te ahanoa Haere;
    • mā te whakamahi kīanga auau ka whakakapihia e mātou ngā paita tuatahi e rua o te wāhitau;
    • tautapahia te ratonga he wahitau IP mai i te kupengaroto hou;
    • hanga he serializer, tahuri te ahanoa Haere ki protobuf, tuhituhi raraunga hou ki etcd.

mahi changePodCIDR tino rite changeServiceCIDR - hei utu mo te whakatika i te whakaritenga ratonga, ka mahia e matou mo te node me te whakarereke .spec.PodCIDR ki tetahi kupengaroto hou.

Mahi

Hurihia te ratonga CIDR

Ko te mahere mo te whakatinana i te mahi he tino ngawari, engari ka uru ki te wa heke i te wa o te hanga ano o nga pona katoa i roto i te roopu. Whai muri i te whakamaarama i nga mahi matua, ka tohatoha ano matou i nga whakaaro me pehea, i roto i te ariā, ka taea te whakaiti i tenei wa heke.

Nga mahi whakarite:

  • tāuta i te pūmanawa e tika ana, me te huihui i te etchelper papaki;
  • tārua etcd me /etc/kubernetes.

Mahere mahi poto mo te whakarereke i te ratongaCIDR:

  • te huri i te apiserver me nga whakaaturanga kaiwhakahaere-kaiwhakahaere;
  • te tuku ano i nga tiwhikete;
  • te huri i nga ratonga ClusterIP i etcd;
  • ka whakaara ano i nga pene katoa i roto i te tautau.

E whai ake nei he raupapatanga katoa o nga mahi i roto i nga taipitopito.

1. Tāuta etcd-kiritaki mo te putunga raraunga:

apt install etcd-client

2. Hanga etchelper:

  • Tāuta golang:
    GOPATH=/root/golang
    mkdir -p $GOPATH/local
    curl -sSL https://dl.google.com/go/go1.14.1.linux-amd64.tar.gz | tar -xzvC $GOPATH/local
    echo "export GOPATH="$GOPATH"" >> ~/.bashrc
    echo 'export GOROOT="$GOPATH/local/go"' >> ~/.bashrc
    echo 'export PATH="$PATH:$GOPATH/local/go/bin"' >> ~/.bashrc
  • Ka penapena ma tatou ano etcdhelper.go, tango i nga whakawhirinakitanga, kohikohi:
    wget https://raw.githubusercontent.com/flant/examples/master/2020/04-etcdhelper/etcdhelper.go
    go get go.etcd.io/etcd/clientv3 k8s.io/kubectl/pkg/scheme k8s.io/apimachinery/pkg/runtime
    go build -o etcdhelper etcdhelper.go

3. Hangaia he tārua etcd:

backup_dir=/root/backup
mkdir ${backup_dir}
cp -rL /etc/kubernetes ${backup_dir}
ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/pki/etcd/ca.crt --key=/etc/kubernetes/pki/etcd/server.key --cert=/etc/kubernetes/pki/etcd/server.crt --endpoints https://192.168.199.100:2379 snapshot save ${backup_dir}/etcd.snapshot

4. Hurihia te kupengaroto ratonga i roto i nga whakaaturanga rererangi mana Kubernetes. I roto i nga konae /etc/kubernetes/manifests/kube-apiserver.yaml и /etc/kubernetes/manifests/kube-controller-manager.yaml huri i te tawhā --service-cluster-ip-range ki te kupengaroto hou: 172.24.0.0/16 Tuhinga o mua 192.168.0.0/16.

5. I te mea kei te huri tatou i te kupenga-roto ratonga ka tukuna e te kubeadm nga tiwhikete mo te apiserver (tae atu ki a ia), me tuku ano:

  1. Kia kite ko wai nga rohe me nga wahitau IP kua tukuna te tiwhikete o naianei mo:
    openssl x509 -noout -ext subjectAltName </etc/kubernetes/pki/apiserver.crt
    X509v3 Subject Alternative Name:
        DNS:dev-1-master, DNS:kubernetes, DNS:kubernetes.default, DNS:kubernetes.default.svc, DNS:kubernetes.default.svc.cluster.local, DNS:apiserver, IP Address:192.168.0.1, IP Address:10.0.0.163, IP Address:192.168.199.100
  2. Me whakarite he whirihora iti mo te kubeadm:
    cat kubeadm-config.yaml
    apiVersion: kubeadm.k8s.io/v1beta1
    kind: ClusterConfiguration
    networking:
      podSubnet: "10.244.0.0/16"
      serviceSubnet: "172.24.0.0/16"
    apiServer:
      certSANs:
      - "192.168.199.100" # IP-адрес мастер узла
  3. Me whakakorehia te crt tawhito me te matua, na te mea kaore he tiwhikete hou e kore e tukuna:
    rm /etc/kubernetes/pki/apiserver.{key,crt}
  4. Me whakaputa ano nga tiwhikete mo te tūmau API:
    kubeadm init phase certs apiserver --config=kubeadm-config.yaml
  5. Me titiro kua tukuna te tiwhikete mo te kupengaroto hou:
    openssl x509 -noout -ext subjectAltName </etc/kubernetes/pki/apiserver.crt
    X509v3 Subject Alternative Name:
        DNS:kube-2-master, DNS:kubernetes, DNS:kubernetes.default, DNS:kubernetes.default.svc, DNS:kubernetes.default.svc.cluster.local, IP Address:172.24.0.1, IP Address:10.0.0.163, IP Address:192.168.199.100
  6. Whai muri i te tuku ano i te tiwhikete tūmau API, whakaara ano i tana ipu:
    docker ps | grep k8s_kube-apiserver | awk '{print $1}' | xargs docker restart
  7. Kia whakahouhia te whirihora mo admin.conf:
    kubeadm alpha certs renew admin.conf
  8. Me whakatika nga raraunga ki etcd:
    ./etcdhelper -cacert /etc/kubernetes/pki/etcd/ca.crt -cert /etc/kubernetes/pki/etcd/server.crt -key /etc/kubernetes/pki/etcd/server.key -endpoint https://127.0.0.1:2379 change-service-cidr 172.24.0.0/16 

    Tūpato I tenei wa, ka mutu te mahi a te taumira rohe i roto i te tautau, i te mea kei roto i nga putunga o naianei /etc/resolv.conf kua rehitatia te wahitau CoreDNS tawhito (kube-dns), ka huri te kube-proxy i nga ture iptables mai i te kupengaroto tawhito ki te mea hou. I tua atu i roto i te tuhinga kua tuhia mo nga whiringa ka taea ki te whakaiti i te waatea.

  9. Me whakatika a ConfigMap i te mokowāingoa kube-system:
    kubectl -n kube-system edit cm kubelet-config-1.16

    - whakakapi i konei clusterDNS ki te wāhitau IP hou o te ratonga kube-dns: kubectl -n kube-system get svc kube-dns.

    kubectl -n kube-system edit cm kubeadm-config

    - ma tatou e whakatika data.ClusterConfiguration.networking.serviceSubnet ki tetahi kupengaroto hou.

  10. I te mea kua huri te wahitau kube-dns, he mea tika ki te whakahou i te whirihora kubelet ki nga pona katoa:
    kubeadm upgrade node phase kubelet-config && systemctl restart kubelet
  11. Ko nga mea e toe ana ko te whakaara ano i nga putunga katoa o te tautau:
    kubectl get pods --no-headers=true --all-namespaces |sed -r 's/(S+)s+(S+).*/kubectl --namespace 1 delete pod 2/e'

Whakaitihia te wa heke

Nga whakaaro mo te whakaiti i te wa heke:

  1. Whai muri i te huri i nga whakaaturanga rererangi mana, hanga he ratonga kube-dns hou, hei tauira, me te ingoa kube-dns-tmp me te wahi noho hou 172.24.0.10.
  2. Hanga if i etcdhelper, e kore e whakarereke i te ratonga kube-dns.
  3. Whakakapihia te wahitau ki nga kubelets katoa ClusterDNS ki tetahi mea hou, ka mahi tonu te ratonga tawhito me te mahi hou.
  4. Tatari kia huri noa nga poti me nga tono mo nga take maori, i te wa i whakaaehia ranei.
  5. Mukua te ratonga kube-dns-tmp me te huri serviceSubnetCIDR mo te ratonga kube-dns.

Ma tenei mahere ka taea e koe te whakaiti i te wa heke ki te ~te meneti - mo te roanga o te tango ratonga kube-dns-tmp me te huri i te kupengaroto mo te ratonga kube-dns.

Whakarerekētanga podNetwork

I te wa ano, i whakatau matou ki te titiro me pehea te whakarereke i te podNetwork ma te whakamahi i te etcdhelper hua. Ko te raupapa o nga mahi e whai ake nei:

  • whakatika whirihora i roto kube-system;
  • te whakatika i te whakaaturanga kube-controller-manager;
  • huri tika te podCIDR ki etcd;
  • whakaara anō i ngā kōpuku kāhui katoa.

Inaianei ake mo enei mahi:

1. Whakarerekē ConfigMap's i te mokowāingoa kube-system:

kubectl -n kube-system edit cm kubeadm-config

- whakatika data.ClusterConfiguration.networking.podSubnet ki tetahi kupengaroto hou 10.55.0.0/16.

kubectl -n kube-system edit cm kube-proxy

- whakatika data.config.conf.clusterCIDR: 10.55.0.0/16.

2. Whakakētia te whakaaturanga kaiwhakahaere-whakahaere:

vim /etc/kubernetes/manifests/kube-controller-manager.yaml

- whakatika --cluster-cidr=10.55.0.0/16.

3. Tirohia nga uara o naianei .spec.podCIDR, .spec.podCIDRs, .InternalIP, .status.addresses mō ngā kōpuku kāhui katoa:

kubectl get no -o json | jq '[.items[] | {"name": .metadata.name, "podCIDR": .spec.podCIDR, "podCIDRs": .spec.podCIDRs, "InternalIP": (.status.addresses[] | select(.type == "InternalIP") | .address)}]'

[
  {
    "name": "kube-2-master",
    "podCIDR": "10.244.0.0/24",
    "podCIDRs": [
      "10.244.0.0/24"
    ],
    "InternalIP": "192.168.199.2"
  },
  {
    "name": "kube-2-master",
    "podCIDR": "10.244.0.0/24",
    "podCIDRs": [
      "10.244.0.0/24"
    ],
    "InternalIP": "10.0.1.239"
  },
  {
    "name": "kube-2-worker-01f438cf-579f9fd987-5l657",
    "podCIDR": "10.244.1.0/24",
    "podCIDRs": [
      "10.244.1.0/24"
    ],
    "InternalIP": "192.168.199.222"
  },
  {
    "name": "kube-2-worker-01f438cf-579f9fd987-5l657",
    "podCIDR": "10.244.1.0/24",
    "podCIDRs": [
      "10.244.1.0/24"
    ],
    "InternalIP": "10.0.4.73"
  }
]

4. Whakakapihia te podCIDR ma te huri tika ki etcd:

./etcdhelper -cacert /etc/kubernetes/pki/etcd/ca.crt -cert /etc/kubernetes/pki/etcd/server.crt -key /etc/kubernetes/pki/etcd/server.key -endpoint https://127.0.0.1:2379 change-pod-cidr 10.55.0.0/16

5. Kia tirohia kua tino rerekee te podCIDR:

kubectl get no -o json | jq '[.items[] | {"name": .metadata.name, "podCIDR": .spec.podCIDR, "podCIDRs": .spec.podCIDRs, "InternalIP": (.status.addresses[] | select(.type == "InternalIP") | .address)}]'

[
  {
    "name": "kube-2-master",
    "podCIDR": "10.55.0.0/24",
    "podCIDRs": [
      "10.55.0.0/24"
    ],
    "InternalIP": "192.168.199.2"
  },
  {
    "name": "kube-2-master",
    "podCIDR": "10.55.0.0/24",
    "podCIDRs": [
      "10.55.0.0/24"
    ],
    "InternalIP": "10.0.1.239"
  },
  {
    "name": "kube-2-worker-01f438cf-579f9fd987-5l657",
    "podCIDR": "10.55.1.0/24",
    "podCIDRs": [
      "10.55.1.0/24"
    ],
    "InternalIP": "192.168.199.222"
  },
  {
    "name": "kube-2-worker-01f438cf-579f9fd987-5l657",
    "podCIDR": "10.55.1.0/24",
    "podCIDRs": [
      "10.55.1.0/24"
    ],
    "InternalIP": "10.0.4.73"
  }
]

6. Kia whakaara ano tatou i nga kohinga kohinga katoa.

7. Ki te waiho koe kia kotahi te iti rawa podCIDR tawhito, ka kore e taea e kube-controller-manager te timata, karekau hoki nga poti i roto i te tautau e whakaritea.

Inaa, ka taea te whakarereke i te podCIDR kia ngawari ake (hei tauira, na). Engari i hiahia matou ki te ako me pehea te mahi tika me etcd, na te mea he keehi ka whakatika nga taonga Kubernetes ki etcd - te anake rerekē pea. (Hei tauira, kaore e taea e koe te whakarereke noa i te mara Ratonga me te kore he wa spec.clusterIP.)

Ko te hua

Ko te tuhinga e korero ana mo te mahi me nga raraunga i roto i te etcd tika, i.e. te karo i te Kubernetes API. I etahi wa ka taea e tenei huarahi ki a koe te mahi "nga mahi tinihanga". I whakamatauria e matou nga mahi i tukuna i roto i te tuhinga i runga i nga tautau K8s tūturu. Heoi, ko to raatau mana mo te whakamahi whanui PoC (tohu o te ariā). No reira, ki te hiahia koe ki te whakamahi i tetahi putanga whakarereke o te taputapu etcdhelper i runga i o tautau, mahia i runga i to tupono.

PS

Pānuihia hoki i runga i ta maatau blog:

Source: will.com

Tāpiri i te kōrero