CentOS7はKubernetes1.16.3をインストールします(2019年11月20日から転載された記事)

記事は2019年11月20日から転載されています。この時期が近ければこの記事を参考にすることができます。

 

1.概要

Kubernetesクラスタを手作業で構築するのは非常に退屈な作業です。これらの操作を簡略化するために、Kubedadm、Kubespray、RKE、その他のコンポーネントなど、多くのインストールツールと構成ツールが生成されています。主に、いくつかの異なるKubernetesバージョンがあるため、公式のKubeadmを最終的に選択しました。違い、Kubeadmのアップデートとサポートが改善されます。Kubeadmは、Kubernetesによって提供されるKubernetesクラスタをすばやくインストールして初期化するためのツールです。現在、まだインキュベーション開発の状態にあります。Kubernetesの新しいバージョンがリリースされるたびに、同期して更新されます。コンポーネントとオブジェクトを理解するには、公式ドキュメントを参照することを強くお勧めします。役割。

https://kubernetes.io/docs/concepts/
https://kubernetes.io/docs/setup/independent/install-kubeadm/
https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/

システム環境

システム カーネル ドッカー ip ホスト名
centos 7.6.1810 3.10.0-957.el7.x86_64 19.03.5 192.168.58.133 k8smaster
centos 7.6.1810 3.10.0-957.el7.x86_64 19.03.5 192.168.58.134 k8snode01

 

 

 

 

2.準備

ファイアウォールをオフにする

各ホストでファイアウォールが有効になっている場合は、Kubernetesの各コンポーネントに必要なポートを開く必要があります。kubeadmのインストールの「必要なポートの確認」セクションを確認できます。簡単にするために、各ノードでファイアウォールを無効にします。

systemctl stop firewalld
systemctl disable firewalld

 

SELINUXを無効にする

# 临时禁用
setenforce 0
# 永久禁用 
vim /etc/selinux/config    # 或者修改/etc/sysconfig/selinux
SELINUX=disabled

 

k8s.confファイルを変更する

cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system

 

スワップを閉じる

# 临时关闭
swapoff -a

 

/etc/fstab ファイルを変更し、  SWAPの自動マウントをコメント化します(スワップを完全に閉じ、再起動後に有効になります)。

# 注释掉以下字段
/dev/mapper/cl-swap     swap                    swap    defaults        0 0

 

Dockerをインストールする

ここでは説明しません。リンクを参照してください:

https://www.cnblogs.com/xiao987334176/p/11771657.html

 

ホスト名を変更する

vi /etc/hostname
k8smaster

注:ホスト名に下線を引くことはできません。下線を引くだけです。
そうしないとk8sのインストール時にエラーが報告されます。

could not convert cfg to an internal cfg: nodeRegistration.name: Invalid value: "k8s_master": a DNS-1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')

 

3、kubeadm、kubelet、kubectlをインストール

各ノードにkubeadm、kubelet、kubectlをインストールします

yumインストールソースを変更する

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

 

ソフトウェアをインストールする

最新バージョンは現在:1.16.3

yum install -y kubelet-1.16.3-0 kubeadm-1.16.3-0 kubectl-1.16.3-0
systemctl enable kubelet && systemctl start kubelet

上記はマスターとノードの両方が動作する必要がある部分です。

 

4番目に、マスターノードを初期化します

初期化コマンドを実行します

kubeadm init --kubernetes-version=1.16.3 \
--apiserver-advertise-address=192.168.58.133 \
--image-repository registry.aliyuncs.com/google_containers \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16

パラメータの説明:

–kubernetes-version: 用于指定k8s版本;
–apiserver-advertise-address:用于指定kube-apiserver监听的ip地址,就是 master本机IP地址。
–pod-network-cidr:用于指定Pod的网络范围; 10.244.0.0/16
–service-cidr:用于指定SVC的网络范围;
–image-repository: 指定阿里云镜像仓库地址

 

この手順は重要です。kubeadmは必要なイメージをデフォルトで公式ウェブサイトk8s.grc.ioからダウンロードするため、中国ではアクセスできないため、–image-repositoryを介してAlibaba Cloudイメージウェアハウスアドレスを指定する必要があります

クラスタが正常に初期化されると、次の情報が返さ
れます。生成されたコンテンツの最後の部分を記録します。これは、他のノードがKubernetesクラスタに参加するときに実行する必要があります。
出力は次のとおりです。

[root@k8smaster /root]$kubeadm init --kubernetes-version=1.16.3 --apiserver-advertise-address=192.168.58.133 --image-repository registry.aliyuncs.com/google_containe
rs --service-cidr=10.1.0.0/16 --pod-network-cidr=10.244.0.0/16
[init] Using Kubernetes version: v1.16.3
[preflight] Running pre-flight checks
        [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 19.03.5. Latest validated version: 18.09
[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] Activating the kubelet service
[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 [k8smaster kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.1.0.1 192.168.58.133]
[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 [k8smaster localhost] and IPs [192.168.58.133 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8smaster localhost] and IPs [192.168.58.133 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"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[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 18.503711 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.16" 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 k8smaster as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node k8smaster as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: zqj4sl.5dlnxyew63pcc7lp
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[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
[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.58.133:6443 --token zqj4sl.5dlnxyew63pcc7lp \
    --discovery-token-ca-cert-hash sha256:fcf730bb86251f0b086a3d43a0c1f6a35b632eee79f69e37554515a2d034d95d 

 

kubectlツールを構成する

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

 

Calicoをインストールする

mkdir k8s
cd k8s
wget https://docs.projectcalico.org/v3.10/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml

## 将192.168.0.0/16修改ip地址为10.244.0.0/16
sed -i 's/192.168.0.0/10.244.0.0/g' calico.yaml

 

キャリコをロード

kubectl apply -f calico.yaml

 

ポッドのステータスを表示する

数分待って、すべてのポッドが実行状態であることを確認します

[root@k8smaster /root/Documents/k8s]$kubectl get pod --all-namespaces -o wide
NAMESPACE     NAME                                       READY   STATUS    RESTARTS   AGE   IP               NODE        NOMINATED NODE   READINESS GATES
kube-system   calico-kube-controllers-6b64bcd855-29mx6   1/1     Running   0          81s   10.244.16.129    k8smaster   <none>           <none>
kube-system   calico-node-b7cc7                          1/1     Running   0          81s   192.168.58.133   k8smaster   <none>           <none>
kube-system   coredns-58cc8c89f4-95fc5                   1/1     Running   0          30m   10.244.16.131    k8smaster   <none>           <none>
kube-system   coredns-58cc8c89f4-cn2lc                   1/1     Running   0          30m   10.244.16.130    k8smaster   <none>           <none>
kube-system   etcd-k8smaster                             1/1     Running   0          30m   192.168.58.133   k8smaster   <none>           <none>
kube-system   kube-apiserver-k8smaster                   1/1     Running   0          30m   192.168.58.133   k8smaster   <none>           <none>
kube-system   kube-controller-manager-k8smaster          1/1     Running   0          30m   192.168.58.133   k8smaster   <none>           <none>
kube-system   kube-proxy-6l7kg                           1/1     Running   0          30m   192.168.58.133   k8smaster   <none>           <none>
kube-system   kube-scheduler-k8smaster                   1/1     Running   0          29m   192.168.58.133   k8smaster   <none>           <none>

calico-kube-controllers IPアドレスが10.244.0.0/16ネットワークセグメントにあることを確認します。

 

ブートを設定

systemctl enable kubelet

 

5、ノードがクラスターに参加

ホスト名を変更する

vi /etc/hostname
k8snode01

ノードに参加

ノードにログインし、dockerとkubeadm、kubelet、kubectlがインストールされていることを確認します

kubeadm join 192.168.58.133:6443 --token zqj4sl.5dlnxyew63pcc7lp \
    --discovery-token-ca-cert-hash sha256:fcf730bb86251f0b086a3d43a0c1f6a35b632eee79f69e37554515a2d034d95d

 

ブートを設定

systemctl enable kubelet

 

ノードを表示

マスターにログインし、コマンドを使用して表示します

[root@k8smaster /root/Documents/k8s]$kubectl get nodes -o wide
NAME        STATUS   ROLES    AGE   VERSION   INTERNAL-IP      EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION          CONTAINER-RUNTIME
k8smaster   Ready    master   64m   v1.16.3   192.168.58.133   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   docker://19.3.5
k8snode01   Ready    <none>   73s   v1.16.3   192.168.58.134   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   docker://19.3.5

6、ポッドを作成する

nginxを作成する

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort

 

ポッドとSVCを表示する

[root@k8smaster /root/Documents/k8s]$kubectl get pod,svc -o wide
NAME                         READY   STATUS    RESTARTS   AGE   IP              NODE        NOMINATED NODE   READINESS GATES
pod/nginx-86c57db685-28qkb   1/1     Running   0          37s   10.244.249.65   k8snode01   <none>           <none>

NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service/kubernetes   ClusterIP   10.1.0.1       <none>        443/TCP        81m   <none>
service/nginx        NodePort    10.1.137.110   <none>        80:31339/TCP   31s   app=nginx

 

外部ネットワークがnodePortにアクセスすることを許可する

iptables -P FORWARD ACCEPT

 

テストアクセス

マスターIP +ノードポートポートを使用してアクセスする

http://192.168.58.133:31339/

 

効果は次のとおりです。

 

コマンド補完

(マスターのみ)

yum install -y bash-completion

source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
source  ~/.bashrc

一度ログアウトしてから再度ログインする必要があります 

 

7、ymlを使用してアプリケーションを公開する

フラスコアプリを例にとります

flaskapp-deployment.yaml

 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: flaskapp-1
spec:
  selector:
    matchLabels:
      run: flaskapp-1
  replicas: 1
  template:
    metadata:
      labels:
        run: flaskapp-1
    spec:
      containers:
      - name: flaskapp-1
        image: jcdemo/flaskapp
        ports:
        - containerPort: 5000

flaskapp-service.yaml

 

apiVersion: v1
kind: Service
metadata:
  name: flaskapp-1
  labels:
    run: flaskapp-1
spec:
  type: NodePort
  ports:
  - port: 5000
    name: flaskapp-port
    targetPort: 5000
    protocol: TCP
    nodePort: 30005
  selector:
    run: flaskapp-1

ymlファイルを読み込む

kubectl apply -f flaskapp-service.yaml 
kubectl apply -f flaskapp-deployment.yaml 

 

訪問ページ

マスターIP +ノードポートを使用してアクセスする

http://192.168.58.133:30005/

効果は次のとおりです。

 

注:ノードnode ip + nodeportを使用してアクセスすることもできます。

 

この記事の参照リンク:
https : //yq.aliyun.com/articles/626118
https://blog.csdn.net/fenglailea/article/details/88745642

69件の元の記事を公開 72のような 240,000以上を訪問

おすすめ

転載: blog.csdn.net/londa/article/details/103417618