k8s概念

k8s基本概念


Kubernetes 是Google開源的容器集羣管理系統,基於Docker構建一個容器的調度服務,提供資源調度、均衡容災、服務註冊、動態擴縮容等功能套件,目前最新版本爲1.0.6;

下面是一張簡單的架構

helloWord.png

幾個重要概念:

  • Pod : 在Kubernetes系統中,調度的最小顆粒不是單純的容器,而是抽象成一個Pod,Pod是一個可以被創建、銷燬、調度、管理的最小的部署單元。比如一個或一組容器。

  • Service :Services是真實應用服務的抽象,每一個服務後面都有很多對應的容器來支持,通過Proxy的port和服務selector決定服務請求傳遞給後端提供服務的容器,對外表現爲一個單一訪問接口,外部不需要了解後端如何運行,這給擴展或維護後端帶來很大的好處。使用nat作爲端口轉發;

  • Replication Controllers:Replication Controller確保任何時候Kubernetes集羣中有指定數量的pod副本(replicas)在運行, 如果少於指定數量的pod副本(replicas),Replication Controller會啓動新的Container,反之會殺死多餘的以保證數量不變。

  • Labels:Labels是用於區分Pod、Service、Replication Controller的key/value鍵值對,Pod、Service、 Replication Controller可以有多個label,但是每個label的key只能對應一個value。Labels是Service和Replication Controller運行的基礎,他們正是通過labels來選擇正確的容器。

  • Cluster : Cluster是安裝在物理機或者是虛擬機上用來運行應用的應用的組件;

  • Node : 運行了Kubernetes的Cluster機器被成爲節點;

安裝使用


master上安裝kubernetes

vim /etc/yum.repos.d/virt7-testing.repo

[virt7-testing]
name=virt7-testing
baseurl=http://cbs.centos.org/repos/virt7-testing/x86_64/os/
gpgcheck=0


#注意;這裏etcd使用的是yum中的版本;版本號爲2.1.1;
@使用最新版本時測試不通過;
yum -y install etcd kubernetes

#修改如下文件
cat vim /etc/kubernetes/config  

[root@h0022062 bin]# cat /etc/kubernetes/config
###
# kubernetes system config
#
# The following values are used to configure various aspects of all
# kubernetes services, including
#
#   kube-apiserver.service
#   kube-controller-manager.service
#   kube-scheduler.service
#   kubelet.service
#   kube-proxy.service
# logging to stderr means we get it in the systemd journal
KUBE_LOGTOSTDERR="--logtostderr=true"

# journal message level, 0 is debug
KUBE_LOG_LEVEL="--v=0"

# Should this cluster be allowed to run privileged docker containers
KUBE_ALLOW_PRIV="--allow_privileged=false"

# How the controller-manager, scheduler, and proxy find the apiserver
#KUBE_MASTER="--master=http://127.0.0.1:8080"

KUBE_ETCD_SERVERS="--etcd_servers=http://locate:2379"


[root@h0022062 bin]# cat /etc/kubernetes/apiserver
###
# kubernetes system config
#
# The following values are used to configure the kube-apiserver
#

# The address on the local server to listen to.
KUBE_API_ADDRESS="--address=0.0.0.0"

# The port on the local server to listen on.
KUBE_API_PORT="--port=8080"

# Port minions listen on
KUBELET_PORT="--kubelet_port=10250"

# How the replication controller and scheduler find the kube-apiserver
KUBE_MASTER="--master=http://centos-master:8080"

# Comma separated list of nodes in the etcd cluster
KUBE_ETCD_SERVERS="--etcd_servers=http://localhost:2379"

# Address range to use for services
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"

# default admission control policies
#KUBE_ADMISSION_CONTROL="--admission_control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota"

# Add your own!
KUBE_API_ARGS=""

#啓動服務;
service etcd start
service kube-apiserver start
service kube-controller-manager start
service kube-scheduler start
master上啓動節點;

#修改配置文件
[root@h0022062 server]# cat /etc/kubernetes/kubelet
###
# kubernetes kubelet (minion) config

# The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)
KUBELET_ADDRESS="--address=127.0.0.1"

# The port for the info server to serve on
KUBELET_PORT="--port=10250"

# You may leave this blank to use the actual hostname
KUBELET_HOSTNAME="--hostname_override=127.0.0.1"

# location of the api-server
KUBELET_API_SERVER="--api_servers=http://127.0.0.1:8080"

# Add your own!
KUBELET_ARGS=""

#啓動各個節點;
service kube-proxy start
service kubelet start
service docker start
Offline

在Kubernetes啓動pod的時候;會嘗試下載一些鏡像;由於網絡問題;這些鏡像一般下載不了; 需要事先下載好;以便測試

docker pull gcr.io/google_containers/pause
docker pull gcr.io/google_containers/pause:0.8.0
docker tag gcr.io/google_containers/pause docker.io/kubernetes/pause
HelloWord

#創建pod;
[root@h0022062 server]# kubectl run my-nginx --image=127.0.0.1:5010/centos-nginx --replicas=2 --port=80
CONTROLLER   CONTAINER(S)   IMAGE(S)                      SELECTOR       REPLICAS
my-nginx     my-nginx       127.0.0.1:5010/centos-nginx   run=my-nginx   2
[root@h0022062 server]# 
[root@h0022062 server]# 
#查看已經存在的pod
[root@h0022062 server]# kubectl get pods
NAME             READY     STATUS    RESTARTS   AGE
my-nginx-bnmhj   1/1       Running   0          11s
my-nginx-lqkny   1/1       Running   0          11s

#查看replicationcontroller
[root@h0022062 bin]# kubectl get replicationcontroller
CONTROLLER   CONTAINER(S)   IMAGE(S)   SELECTOR       REPLICAS
my-nginx     my-nginx       nginx      run=my-nginx   2

#停止pods
[root@h0022062 server]# kubectl stop replicationcontroller my-nginx
replicationcontrollers/my-nginx
#確認是否停止成功
[root@h0022062 server]# kubectl get pods
NAME      READY     STATUS    RESTARTS   AGE
HelloWord-實際可以訪問的service

[root@h0022062 server]# kubectl get pods
NAME      READY     STATUS    RESTARTS   AGE
[root@h0022062 server]# kubectl get services
NAME         LABELS                                    SELECTOR   IP(S)         PORT(S)
kubernetes   component=apiserver,provider=kubernetes   <none>     192.168.0.1   443/TCP

[root@h0022062 server]# cat pod.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: mynginx
  labels:
    name: mynginx
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: mynginx
        image: 127.0.0.1:5010/centos-nginx
        ports:
        - containerPort: 80

[root@h0022062 server]# kubectl create -f pod.yaml 
replicationcontrollers/mynginx
[root@h0022062 server]# kubectl get pods
NAME            READY     STATUS    RESTARTS   AGE
mynginx-3sz2i   1/1       Running   0          1m
mynginx-m821h   1/1       Running   0          1m

#添加服務
[root@h0022062 server]# cat service.json 
{
    "kind": "Service",
    "apiVersion": "v1",
    "metadata": {
        "name": "my-service"
    },
    "spec": {
        "selector": {
            "app": "nginx"
        },
        "ports": [
            {
                "protocol": "TCP",
                "port": 80,
                "targetPort": 80
            }
        ]
    }
}

#啓動服務
[root@h0022062 server]# kubectl create -f service.json 

#iptables查看nat的映射表
[root@h0022062 server]# iptables -nvL -t nat
7   420 DNAT       tcp  --  *      *       0.0.0.0/0            10.254.79.222        /* default/my-service: */ tcp dpt:80 to:192.168.77.114:13412

#訪問測試;

helloWord.png

#
[root@h0022062 server]# kubectl stop -f pod.yaml 
pods/mynginx

#有一個pending;可以使用describe命令查看詳情
[root@h0022062 server]# kubectl describe pods/mynginx-3sz2i

常用命令


kubectl create

作用:通過文件創建資源(pod、Replication Controllers、Service)等;支持YAML和JSON格式; 示例:

kubectl create -f ./pod.json
kubectl get

作用:列出資源列表; 示例:

// 顯示所有的pods
$ kubectl get pods

//顯示replicationcontroller
$ kubectl get replicationcontroller
$ kubectl get rc

//顯示service
$ kubectl get service

//顯示所有節點
$ kubectl get node

// 顯示pod web-pod-13je7 的json
$ kubectl get -o json pod web-pod-13je7

// List one or more resources by their type and names.
$ kubectl get rc/web service/frontend pods/web-pod-13je7

kubectl delete

作用:刪除資源;可以使用文件或者是標籤來標記刪除的資源; 示例:

// Delete a pod using the type and name specified in pod.json.
$ kubectl delete -f ./pod.json

// Delete pods and services with label name=myLabel.
$ kubectl delete pods,services -l name=myLabel

// Delete all pods
$ kubectl delete pods --all

kubectl describe

作用:顯示資源的詳情;可以用於顯示pending狀態

示例:

// 顯示nodes名稱爲kubernetes-minion-emt8.c.myproject.internal的詳情
$ kubectl describe nodes kubernetes-minion-emt8.c.myproject.internal

//顯示pods名稱爲nginx的詳情
$ kubectl describe pods/nginx

// 顯示標籤爲 name=myLabel 的pods
$ kubectl describe po -l name=myLabel

kubectl logs

作用:顯示pod內容器的日誌;

示例:

 #如果是pod內只有一個容器;容器名稱可選
 kubectl logs mynginx-24aw5
 kubectl logs mynginx-24aw5 mynginx

kubectl stop

作用:停止一個資源;

示例:

// Shut down foo.
$ kubectl stop replicationcontroller foo

// Stop pods and services with label name=myLabel.
$ kubectl stop pods,services -l name=myLabel

// Shut down the service defined in service.json
$ kubectl stop -f service.json

// Shut down all resources in the path/to/resources directory
$ kubectl stop -f path/to/resources



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章