欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Kubernetes安装

程序员文章站 2023-12-25 15:51:51
...

Kubernetes安装

 

本文使用vultr虚拟主机实现

邀请注册链接:http://www.vultr.com/?ref=6940267

 

ETCD集群安装:http://crabdave.iteye.com/blog/2365488

配置docker 和 flannel:http://crabdave.iteye.com/blog/2365490

 

download kubernetes

kubernetes v1.3.0 大约1.4G 使用东京的vultr实例下载很快

wget https://github.com/kubernetes/kubernetes/releases/download/v1.3.0/kubernetes.tar.gz

 

tar -xvf kubernetes.tar.gz

 

cd kubernetes/server

 

将kubernetes-server-linux-amd64.tar.gz解压

-rw-r--r-- 1 root root 352M 7月   1 2016 kubernetes-server-linux-amd64.tar.gz

 

 tar -xvf kubernetes-server-linux-amd64.tar.gz

 

cd /root/kubernetes/server/kubernetes/server/bin

find ./ -perm 755 | xargs -i cp {} /usr/bin

 

其它两台机器拷贝kubernetes到本机

scp root@10.99.0.10:/root/kubernetes/server/kubernetes-server-linux-amd64.tar.gz ./

 

tar -xvf kubernetes-server-linux-amd64.tar.gz 

 

cd /root/kubernetes/server/bin

cp kube-proxy /usr/bin

cp kubelet /usr/bin

 

---------------------------------

 

mkdir -p /opt/kubernetes/

 

cat <<EOF >/lib/systemd/system/kube-apiserver.service

[Unit]

Description=kube-apiserver

 

[Service]

EnvironmentFile=/etc/sysconfig/kube-apiserver

ExecStart=/usr/bin/kube-apiserver \$ETCD_SERVERS \\

          \$LOG_DIR \\

          \$SERVICE_CLUSTER_IP_RANGE \\

          \$INSECURE_BIND_ADDRESS \\

          \$INSECURE_PORT \\

          \$BIND_ADDRESS \\

          \$SECURE_PORT \\

          \$AUTHORIZATION_MODE \\

          \$AUTHORIZATION_FILE \\

          \$BASIC_AUTH_FILE \\

          \$KUBE_APISERVER_OPTS

Restart=on-failure

EOF

 

cat /lib/systemd/system/kube-apiserver.service

---------------------------------

cat <<EOF >/etc/sysconfig/kube-apiserver

# configure file for kube-apiserver

 

# --etcd-servers

ETCD_SERVERS='--etcd-servers=http://10.99.0.10:2380,http://10.99.0.11:2380,http://10.99.0.12:2380'

# --log-dir

LOG_DIR='/opt/kubernetes/logs'

# --service-cluster-ip-range

SERVICE_CLUSTER_IP_RANGE='--service-cluster-ip-range=172.16.0.0/16'

# --insecure-bind-address

INSECURE_BIND_ADDRESS='--insecure-bind-address=0.0.0.0'

# --insecure-port

INSECURE_PORT='--insecure-port=8080'

# --bind-address

BIND_ADDRESS='--bind-address=0.0.0.0'

# --secure-port

SECURE_PORT='--secure-port=6443'

# --authorization-mode

AUTHORIZATION_MODE='--authorization-mode=ABAC'

# --authorization-policy-file

AUTHORIZATION_FILE='--authorization-policy-file=/opt/kubernetes/authorization'

# --basic-auth-file

BASIC_AUTH_FILE='--basic-auth-file=/opt/kubernetes/authentication.csv'

# other parameters

KUBE_APISERVER_OPTS=''

EOF

 

cat /etc/sysconfig/kube-apiserver

 

---------------------------------

 

 

echo '{"user": "admin"}'>/opt/kubernetes/authorization 

 

(密码,用户名,用户ID):

echo "admin,admin,adminID" > /opt/kubernetes/authentication.csv

---------------------------------

启动kube-apiserver

 

systemctl daemon-reload

systemctl start kube-apiserver

systemctl status -l kube-apiserver

curl -L http://10.99.0.10:8080/healthz

 

tail -n 1000 /var/log/messages

 

----------------

 

启动失败,查看日志

tail -n 1000 /var/log/messages

出现异常

/lib/lsb/init-functions no such file or directory

需要安装

yum install -y redhat-lsb

 

还可以通过如下命令查看kube-apiserver是否正常,正常则返回'ok':

 

curl -L http://10.99.0.10:8080/healthz

 

------------------------------------------------------------------

 

创建、配置和启动kube-controller-manager服务

 

三个组件启动是有顺序,必须等kube-apiserver正常启动之后再启动kube-controller-manager。

 

cat <<EOF >/etc/sysconfig/kube-controller

# configure file for kube-controller-manager

 

# --master

KUBE_MASTER='--master=http://10.99.0.10:8080'

# --log-dir

LOG_DIR='--log-dir=/opt/kubernetes/logs'

# --cloud-provider

CLOUD_PROVIDER='--cloud-provider='

# other parameters

KUBE_CONTROLLER_OPTS=''

EOF

 

cat /etc/sysconfig/kube-controller

 

---------------

cat <<EOF >/lib/systemd/system/kube-controller.service

[Unit]

Description=kube-controller-manager

After=kube-apiserver.service

Wants=kube-apiserver.service

 

[Service]

EnvironmentFile=/etc/sysconfig/kube-controller

ExecStart=/usr/bin/kube-controller-manager \$KUBE_MASTER \\

          \$LOG_DIR \\

          \$CLOUD_PROVIDER \\

          \$KUBE_CONTROLLER_OPTS

Restart=on-failure

EOF

 

cat /lib/systemd/system/kube-controller.service

---------------

启动kube-controller-manager

 

systemctl daemon-reload

systemctl start kube-controller

systemctl status -l kube-controller

------------------------------------------------------------------

 

创建、配置和启动kube-scheduler服务

 

cat <<EOF >/etc/sysconfig/kube-scheduler

# configure file for kube-scheduler

 

# --master

KUBE_MASTER='--master=http://10.99.0.10:8080'

# --log-dir

LOG_DIR='--log-dir=/opt/kubernetes/logs'

# other parameters

KUBE_SCHEDULER_OPTS=''

EOF

 

cat /etc/sysconfig/kube-scheduler

-----------------

cat <<EOF >/lib/systemd/system/kube-scheduler.service

[Unit]

Description=kube-scheduler

After=kube-apiserver.service

Wants=kube-apiserver.service

 

[Service]

EnvironmentFile=/etc/sysconfig/kube-scheduler

ExecStart=/usr/bin/kube-scheduler \$KUBE_MASTER \\

          \$LOG_DIR \\

          \$KUBE_SCHEDULER_OPTS

Restart=on-failure

EOF

 

cat /lib/systemd/system/kube-scheduler.service

------------

 

启动kube-scheduler

 

systemctl daemon-reload

systemctl start kube-scheduler

systemctl status -l kube-scheduler

 

------------------------------------------------------------------

启动k8s-node端,开始配置两台节点机器

mkdir -p  /opt/kubernetes/

 

--------------------------

配置和启动kube-proxy(三台机器,master api server重启一下)

 

cat <<EOF >/etc/sysconfig/kube-proxy

# configure file for kube-proxy

 

# --master

KUBE_MASTER='--master=http://10.99.0.10:8080'

# --proxy-mode

PROXY_MODE='--proxy-mode=iptables'

# --log-dir

LOG_DIR='--log-dir=/opt/kubernetes/logs'

# other parameters

KUBE_PROXY_OPTS=''" > /etc/sysconfig/kube-proxy

EOF

 

cat /etc/sysconfig/kube-proxy

--------------------------

cat <<EOF >/lib/systemd/system/kube-proxy.service

[Unit]

Description=kube-proxy

 

[Service]

EnvironmentFile=/etc/sysconfig/kube-proxy

ExecStart=/usr/bin/kube-proxy \$KUBE_MASTER \\

          \$PROXY_MODE \\

          \$LOG_DIR \\

          \$KUBE_PROXY_OPTS

Restart=on-failure

EOF

 

cat /usr/lib/systemd/system/kube-proxy.service

 

启动kube-proxy

 

systemctl daemon-reload

systemctl start kube-proxy

systemctl status -l kube-proxy

------------------------------------------------------------------

配置和启动kubelet


cat <<EOF >/lib/systemd/system/kubelet.service

[Unit]

Description=kubelet

 

[Service]

EnvironmentFile=/etc/sysconfig/kubelet

ExecStart=/usr/bin/kubelet \$API_SERVERS \\

          \$ADDRESS \\

          \$HOSTNAME_OVERRIDE \\

          \$ALLOW_PRIVILEGED \\

          \$POD_INFRA \\

          \$CLUSTER_DNS \\

          \$CLUSTER_DOMAIN \\

          \$MAX_PODS \\

          \$LOG_DIR \\

          \$KUBELET_OPTS

Restart=on-failure

EOF

 

cat /lib/systemd/system/kubelet.service

-------------------------------

/etc/sysconfig/kubelet配置10.99.0.11机器

cat <<EOF >/etc/sysconfig/kubelet

# configure file for kubelet

 

# --api-servers

API_SERVERS='--api-servers=http://10.99.0.10:8080'

# --address

ADDRESS='--address=0.0.0.0'

# --hostname-override

HOSTNAME_OVERRIDE='vultr.guest-11'

# --allow-privileged

ALLOW_PRIVILEGED='--allow-privileged=false'

# --pod-infra-container-image

#POD_INFRA='--pod-infra-container-image=10.23.23.76:5000/kubernetes/pause:latest'

# --cluster-dns

#CLUSTER_DNS='--cluster-dns=172.16.66.66'

# --cluster-domain

#CLUSTER_DOMAIN='--cluster-domain=cluster.local'

# --max-pods

MAX_PODS='--max-pods=70'

# --log-dir

LOG_DIR='--log-dir=/opt/kubernetes/logs'

# other parameters

KUBELET_OPTS=''

EOF

 

cat /etc/sysconfig/kubelet

 

--------------


/etc/sysconfig/kubelet配置10.99.0.12机器

cat <<EOF >/etc/sysconfig/kubelet

echo "# configure file for kubelet

 

# --api-servers

API_SERVERS='--api-servers=http://10.99.0.10:8080'

# --address

ADDRESS='--address=0.0.0.0'

# --hostname-override

HOSTNAME_OVERRIDE='vultr.guest-12'

# --allow-privileged

ALLOW_PRIVILEGED='--allow-privileged=false'

# --pod-infra-container-image

#POD_INFRA='--pod-infra-container-image=10.23.150.76:5000/kubernetes/pause:latest'

# --cluster-dns

#CLUSTER_DNS='--cluster-dns=172.16.66.66'

# --cluster-domain

#CLUSTER_DOMAIN='--cluster-domain=cluster.local'

# --max-pods

MAX_PODS='--max-pods=70'

# --log-dir

LOG_DIR='--log-dir=/opt/kubernetes/logs'

# other parameters

KUBELET_OPTS=''

EOF

 

cat /etc/sysconfig/kubelet

 

--------------

 

 

启动kubelet

 

systemctl daemon-reload

systemctl start kubelet

systemctl status -l kubelet

------------------------------------------------------------------

回到master机器 10.99.0.10

 

kubectl --server=10.99.0.10:8080 get nodes

 

NAME             STATUS    AGE

vultr.guest-11   Ready     6m

vultr.guest-12   Ready     7m

------------------------------------------------------------------

 

mkdir -p ~/workspace

cd ~/workspace

 

echo "  apiVersion: v1

  kind: ReplicationController

  metadata:

      name: test-1

  spec:

    replicas: 1

    template:

      metadata:

        labels:

          app: test-1

      spec:

        containers:

          - name: nginx-1

            image: docker.io/nginx

        nodeSelector:

          kubernetes.io/hostname: vultr.guest-11" > test-1.yaml

cat test-1.yaml

  

echo "  apiVersion: v1

  kind: ReplicationController

  metadata:

      name: test-2

  spec:

    replicas: 1

    template:

      metadata:

        labels:

          app: test-2

      spec:

        containers:

          - name: nginx-2

            image: docker.io/nginx

        nodeSelector:

          kubernetes.io/hostname: vultr.guest-11" > test-2.yaml

cat test-2.yaml

          

echo "  apiVersion: v1

  kind: ReplicationController

  metadata:

      name: test-3

  spec:

    replicas: 1

    template:

      metadata:

        labels:

          app: test-3

      spec:

        containers:

          - name: nginx-3

            image: docker.io/nginx

        nodeSelector:

          kubernetes.io/hostname: vultr.guest-12" > test-3.yaml

cat test-3.yaml

  

echo "  apiVersion: v1

  kind: ReplicationController

  metadata:

      name: test-4

  spec:

    replicas: 1

    template:

      metadata:

        labels:

          app: test-4

      spec:

        containers:

          - name: nginx-4

            image: docker.io/nginx

        nodeSelector:

          kubernetes.io/hostname: vultr.guest-12" > test-4.yaml

cat test-4.yaml

------------------------------------------------------------------

 

kubectl --server=10.99.0.10:8080 create -f test-1.yaml  

kubectl --server=10.99.0.10:8080 create -f test-2.yaml 

kubectl --server=10.99.0.10:8080 create -f test-3.yaml 

kubectl --server=10.99.0.10:8080 create -f test-4.yaml 

 

kubectl --server=10.99.0.10:8080 get pods

 

NAME           READY     STATUS    RESTARTS   AGE

test-1-p4te6   1/1       Running   0          35s

test-2-7lvz6   1/1       Running   0          34s

test-3-21o5n   1/1       Running   0          34s

test-4-tfkkz   1/1       Running   0          34s

 

查看IP地址及其它相关信息

kubectl --server=10.99.0.10:8080 describe pod test-1-p4te6

kubectl --server=10.99.0.10:8080 describe pod test-2-7lvz6

kubectl --server=10.99.0.10:8080 describe pod test-3-21o5n

kubectl --server=10.99.0.10:8080 describe pod test-4-tfkkz

 

 

------------------------------------------------------------------

上一篇:

下一篇: