1. 程式人生 > >記錄《kubernetes權威指南》中單機應用棧例子(1)

記錄《kubernetes權威指南》中單機應用棧例子(1)

kubernetes docker

單機部署一個mysql+tomcat應用棧

前期準備工作

使用centos7虛擬機

  • 關閉centos防火墻
    systemctl stop firewalld
    systemctl disable firewalld
  • 安裝etcd及kubernetes軟件
    yum update -y
    yum install etcd kubernetes -y
  • 安裝好的各軟件版本如下
kubectl: v1.5.2
docker: 1.13.1
etcd: 3.2.15
  • 修復配置文件
    • docker配置文件/etc/sysconfig/docker
修改為OPTIONS=‘--selinux-enabled=false‘
    • kubernetes apiserver配置文件 /etc/kubernetes/apiserver
KUBE_ADMISSION_CONTROL參數中刪除ServiceAccount
  • 使用daocloud加速器

註冊daocloud用戶後,運行daocloud加速器腳本

curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://41379ba5.m.daocloud.io
# 由於網絡原因,最好使用加速器。
# 如果使用daocloud後,出現鏡像下載問題,可以考慮替換為阿裏雲加速器
# 實際上就是編輯了/etc/docker/daemon.json文件
# 如果docker鏡像下載出現問題,優先考慮網絡問題。
  • 啟動服務
systemctl start etcd
systemctl start docker 
systemctl start kube-apiserver
systemctl start kube-controller-manager
systemctl start kube-scheduler
systemctl start kubelet
systemctl start kube-proxy
  • 下載鏡像
# mysql鏡像
docker pull daocloud.io/library/mysql:latest
# tomcat鏡像
docker pull kubeguide/tomcat-app:v2

[root@host1 docker]# docker images
REPOSITORY                       TAG                 IMAGE ID            CREATED             SIZE
daocloud.io/library/mysql        latest              80bbf861367a        3 days ago          445 MB
docker.io/kubeguide/tomcat-app   v2                  00beaa1d956d        21 months ago       358 MB

啟用mysql容器服務

定義rc模板,命名為mysql-rc.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
spec:
  replicas: 1
  #定義RC標簽選擇
  selector:
    app: mysql
  #定義Pod模板
  template:
    metadata:
      labels: 
        app: mysql
    spec:
      #Pod內容器定義
      containers:
        - name: mysql
          # image需要指定docker images輸出中的那個mysql鏡像
          image:  daocloud.io/library/mysql:latest
          ports: 
          - containerPort: 3306
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: "123456"

註意image選擇,是與書中不一致的,因為我們下載的mysql鏡像是daocloud倉庫中的鏡像

創建好文件後,發布到kubernetes集群中

[root@host1 kube-test]# kubectl create -f mysql-rc.yaml
replicationcontroller "mysql" created
[root@host1 kube-test]# kubectl get rc
NAME      DESIRED   CURRENT   READY     AGE
mysql     1         1         0         10s
[root@host1 kube-test]# kubectl get pods
NAME          READY     STATUS              RESTARTS   AGE
mysql-nrcvt   0/1       ContainerCreating   0          27s

這個ContainerCreating狀態是異常狀態,會一直持續,不會進入到running狀態

查看pods狀態時,

kubectl describe pods mysql-nrcvt
......
......
  1m    5s  5   {kubelet 127.0.0.1}     Warning FailedSync  Error syncing pod, skipping: failed to "StartContainer" for "POD" with ImagePullBackOff: "Back-off pulling image \"registry.access.redhat.com/rhel7/pod-infrastructure:latest\""

發現是無法下載這個鏡像,於是我手動pull這個鏡像,發現如下報錯:

[root@host1 kube-test]# docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest
Trying to pull repository registry.access.redhat.com/rhel7/pod-infrastructure ... 
open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such file or directory

搜索這個報錯,發現可以通過yum install rhsm -y解決
運行該命令後,刪除掉之前部署的rc,

[root@host1 kube-test]# kubectl delete rc mysql
replicationcontroller "mysql" deleted
[root@host1 kube-test]# kubectl get pods
No resources found.

重新部署

[root@host1 kube-test]# kubectl create -f mysql-rc.yaml
replicationcontroller "mysql" created
[root@host1 kube-test]# kubectl get rc
NAME      DESIRED   CURRENT   READY     AGE
mysql     1         1         1         7s
[root@host1 kube-test]# kubectl get pods 
NAME          READY     STATUS    RESTARTS   AGE
mysql-nbwvp   1/1       Running   0          13s

mysql正常運行了。
查看正在運行的docker,發現有兩個

[root@host1 kube-test]# docker ps 
CONTAINER ID        IMAGE                                                        COMMAND                  CREATED              STATUS              PORTS               NAMES
f58905ddee64        daocloud.io/library/mysql:latest                             "docker-entrypoint..."   About a minute ago   Up About a minute                       k8s_mysql.9ef925be_mysql-nbwvp_default_30da097b-4ee8-11e8-a311-000c29f71e04_7ac92ba7
99382a3dcbf0        registry.access.redhat.com/rhel7/pod-infrastructure:latest   "/usr/bin/pod"           About a minute ago   Up About a minute                       k8s_POD.1d520ba5_mysql-nbwvp_default_30da097b-4ee8-11e8-a311-000c29f71e04_aad8e14a

記錄《kubernetes權威指南》中單機應用棧例子(1)