Kubernetes 安装配置笔记 - vqiu - 秋郎青鬓春未迟

一、在节点中添加 host 记录
172.16.4.129  server.vqiu.cn
172.16.4.131  node01.vqiu.cn
172.16.4.132  node02.vqiu.cn


二、在 Admin 节点上配置如下# 在 Admin 节点安装 Kubernetes etcd flannel [root@server ~]# yum -y install kubernetes etcd flannel # 生成 key [root@server ~]# openssl genrsa -out /etc/kubernetes/service.key 2048 [root@server ~]# sed -i "s:KUBE_CONTROLLER_MANAGER_ARGS=.*:KUBE_CONTROLLER_MANAGER_ARGS=\"--service_account_private_key_file=/etc/kubernetes/service.key\":" /etc/kubernetes/controller-manager [root@server ~]# sed -i 's:KUBE_API_ADDRESS=.*:KUBE_API_ADDRESS="--address=0.0.0.0":' /etc/kubernetes/apiserver [root@server ~]# sed -i "s/KUBE_ETCD_SERVERS=.*/KUBE_ETCD_SERVERS=\"--etcd_servers=http:\/\/${HOSTNAME}:2379\"/" /etc/kubernetes/apiserver [root@server ~]# sed -i "s:KUBE_SERVICE_ADDRESSES=.*:KUBE_SERVICE_ADDRESSES=\"--service-cluster-ip-range=${IP_RANGE}\":" /etc/kubernetes/apiserver [root@server ~]# echo -en "\nKUBE_API_ARGS=\"--service_account_key_file=/etc/kubernetes/service.key\"" >> /etc/kubernetes/apiserver [root@server ~]# sed -i '7s/#//' /etc/etcd/etcd.conf [root@server ~]# sed -i "s/ETCD_LISTEN_CLIENT_URLS=.*/ETCD_LISTEN_CLIENT_URLS=\"http:\/\/localhost:2379,http:\/\/${HOSTNAME}:2379\"/" /etc/etcd/etcd.conf [root@server ~]# sed -i "s/KUBE_MASTER=.*/KUBE_MASTER=\"--master=http:\/\/${HOSTNAME}:8080\"/" /etc/kubernetes/config # 启动服务,并在实现开机自启动 [root@server ~]# systemctl start etcd kube-apiserver kube-controller-manager kube-scheduler [root@server ~]# systemctl enable etcd kube-apiserver kube-controller-manager kube-scheduler
# 创建一个 文件,内容如下: [root@server ~]# cat flannel-config.json {  "Network":"172.16.0.0/16",  "SubnetLen":24,  "Backend":{    "Type":"vxlan",    "VNI":1  } }

# network 所对应的 IP 为 容器运行网段

[root@server ~]# sed -i "s/FLANNEL_ETCD=.*/FLANNEL_ETCD=\"http:\/\/${HOSTNAME}:2379\"/" /etc/sysconfig/flanneld [root@server ~]# sed -i "s/FLANNEL_ETCD_KEY=.*/FLANNEL_ETCD_KEY=\"\/atomic.io\/network\"/" /etc/sysconfig/flanneld [root@server ~]# etcdctl set atomic.io/network/config < flannel-config.json [root@server ~]# systemctl start flanneld [root@server ~]# systemctl enable flanneld [root@server ~]# echo "flannel running status" [root@server ~]# kubectl cluster-info

 


三、在两个节点上配置如下

MASTER="server.vqiu.cn"          # Master Server Address # 安装系统包[root@node01 ~]# yum -y install kubernetes flannel[root@node01 ~]# sed -i "s/KUBE_MASTER=.*/KUBE_MASTER=\"--master=http:\/\/${MASTER}:8080\"/" /etc/kubernetes/config[root@node01 ~]# sed -i "s/KUBELET_ADDRESS=.*/KUBELET_ADDRESS=\"--address=0.0.0.0\"/" /etc/kubernetes/kubelet[root@node01 ~]# sed -i "s/KUBELET_HOSTNAME=.*/KUBELET_HOSTNAME=\"--hostname_override=${HOST_NAME}\"/" /etc/kubernetes/kubelet[root@node01 ~]# sed -i "s/KUBELET_API_SERVER=.*/KUBELET_API_SERVER=\"--api_servers=http:\/\/${MASTER}:8080\"/" /etc/kubernetes/kubelet[root@node01 ~]# sed -i "s/FLANNEL_ETCD=.*/FLANNEL_ETCD=\"http:\/\/${MASTER}:2379\"/" /etc/sysconfig/flanneld[root@node01 ~]# systemctl start flanneld kube-proxy kubelet[root@node01 ~]# systemctl enable flanneld kube-proxy kubelet [root@node01 ~]# systemctl restart docker

在 Admin 节点上执行下命令[root@server ~]# kubectl get nodes
NAME             LABELS                                  STATUS
node01.vqiu.cn   kubernetes.io/hostname=node01.vqiu.cn   Ready
node02.vqiu.cn   kubernetes.io/hostname=node02.vqiu.cn   Ready



四、小实例
环境配置完了,我们利用 Kubernetes 部署个容器来耍耍

1) 创建一个 web_server 的镜像[在任意 node 节点]
[root@node01 ~]# cat Dockfile

# create new
FROM centos
MAINTAINER Shuhui <shuhui@vqiu.cn>

# Update Yum Repostory
RUN curl -s -L http://mirrors.aliyun.com/repo/Centos-7.repo -o \
/etc/yum.repos.d/CentOS-Base.repo && \
curl -s -L http://mirrors.aliyun.com/repo/epel-7.repo -o \
/etc/yum.repos.d/epel.repo
RUN yum clean all && \
yum makecache fast && \
yum -y update
yum -y install httpd
RUN yum clean all
EXPOSE 80
CMD ["-D","FOREGROUND"]
ENTRYPOINT ["/usr/sbin/httpd"]


[root@node01 ~]# docker build -t web_server .
[root@node01 ~]# docker images
REPOSITORY                       TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
web_server                       latest              5de94d0f03ec        About an hour ago   328.3 MB
docker.io/centos                 latest              60e65a8e4030        6 days ago          196.6 MB

再将此镜像导出来,在另一节点导入,当然也可以直接使用 docker build


在 Admin 节点创建一个 pod

[root@server ~]# cat pod-webserver.yaml
apiVersion: v1
kind: Pod
metadata:
  name: httpd
spec:
  containers:
  - name: httpd
    image: web_server
    ports:
    - containerPort: 80
    volumeMounts:    # 定义一个挂载目录 /var/www/html
    - name: httpd-storage
      # mount point inside Container
      mountPath: /var/www/html
  volumes:
  - name: httpd-storage
    hostPath:      # 把/var/docker/disk01 目录 挂载到窗口中的 /var/www/html
      path: /var/docker/disk01
# 创建
[root@server ~]# kubectl create -f pop-webserver.yaml
pods/httpd


# 使用命令查看创建的状态,状态为Pending[准备中]
[root@server ~]# kubectl get pods
NAME      READY     STATUS    RESTARTS   AGE
httpd     0/1       Pending   0          18s

# pod 已经 Running 状态[已经创建成功]
[root@server ~]# kubectl get pods
NAME      READY     STATUS    RESTARTS   AGE
httpd     0/1      
Running   0          24s

# 查看该容器在哪个节点
[root@server ~]# kubectl get pods -o wide
NAME      READY     STATUS       RESTARTS   AGE       NODE
httpd     1/1       Running      0          29s       node01.vqiu.cn

# 查看该容器的所有状态
[root@server ~]# kubectl get pods httpd -o yaml
apiVersion: v1
kind: Pod
metadata:
 creationTimestamp: 2016-01-11T10:58:49Z
 name: httpd
 namespace: default
 resourceVersion: "1716"
 selfLink: /api/v1/namespaces/default/pods/httpd
 uid: 4b661f54-b852-11e5-bad9-005056b9211a
spec:
 containers:
 - image: web_server
   imagePullPolicy: IfNotPresent
   name: httpd
   ports:
   - containerPort: 80
     protocol: TCP
   resources: {}
   terminationMessagePath: /dev/termination-log
   volumeMounts:
   - mountPath: /var/www/html
     name: httpd-storage
   - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
     name: default-token-tfkup
     readOnly: true
 dnsPolicy: ClusterFirst
 nodeName: node01.vqiu.cn
 restartPolicy: Always
 serviceAccount: default
 serviceAccountName: default
 volumes:
 - hostPath:
     path: /var/docker/disk01
   name: httpd-storage
 - name: default-token-tfkup
   secret:
     secretName: default-token-tfkup
status:
 conditions:
 - status: "True"
   type: Ready
 containerStatuses:
 - containerID: docker://521fa2a7baf4f2821ec11960efcaa8776d9f0ddfc996bb3f4ce4ecbd9ca7688e
   image: web_server
   imageID: docker://5de94d0f03ec3c9fa582b7ab6673502195646a503c2fa8471a5a0ee3c0541dee
   lastState: {}
   name: httpd
   ready: true
   restartCount: 0
   state:
     running:
       startedAt: 2016-01-05T03:53:32Z
 hostIP: 172.16.4.131
 phase: Running
 podIP: 10.1.15.1
 startTime: 2016-01-05T03:53:06Z


# 测试
[root@node01 ~]# echo ${HOSTNAME} > /var/docker/disk01/index.html
[root@node01 ~]# curl http://10.1.15.2
node01.docker.com

# 删除 Pod 
[root@server ~]# kubectl delete pod httpd
pods/httpd