kubernetes學習第一篇-k8s安裝以及HelloWorld

lonecloud發表於2019-02-17

安裝

1. 關閉防火牆服務

# systemctl disable firewalld
# systemctl stop firewalld  

2. 安裝etcd以及kubernetes軟體

yum install -y etcd kubernetes

3. 修改Docker配置為

vim /etc/sysconfig/docker
OPTIONS='--selinux-enabled=false --insecure-registry grc.io --log-driver=journald --signature-verification=false'

4.按順序啟動服務

systemctl start etcd
systemctl start docker
systemctl start kube-apiserver
systemctl start kube-controller-manager
systemctl start kube-scheduler
systemctl start kubelet
systemctl start kubelet
systemctl start kube-proxy

5. 檢查是否啟動成功

 

HelloWorld

1. 編寫一個配置檔案mysql-rc.yaml

apiVersion: v1
#副本控制器
kind: ReplicationController
metadata:
  name: mysql
spec:
  # 例項的數量
  replicas: 1
  selector:
    # Pod名稱
    app: mysql
  template:
    metadata:
      # Pod副本名稱對應selector
      labels:
        app: mysql
    spec:
      containers:    # Pod內容定義部分
      - name: mysql  # 容器的名稱
        image: mysql # 容器的映象
        ports:
        - containerPort: 3306 ## 暴露埠
        env: ##注入到容器內的環境變數
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"

2. 啟動

kubectl create -f mysql-rc.yaml

3. 檢視狀態

# kubectl get rc
NAME      DESIRED   CURRENT   READY     AGE
mysql     1         1         1         14m

Pod狀態

# kubectl get pods
NAME          READY     STATUS    RESTARTS   AGE
mysql-w3t8z   1/1       Running   0          14m

4. 問題解決

  1. 啟動後發現 Pod狀態一直都是ContainerCreating

    1. 使用kubectl describe po mysql檢視狀態

    2. 如果出現如下錯誤

kubectl describe po  mysql-bwnlk
Name:       mysql-bwnlk
Namespace:  default
Node:       127.0.0.1/127.0.0.1
Start Time: Thu, 04 Jan 2018 10:20:45 +0800
Labels:     app=mysql
Status:     Pending
IP:     
Controllers:    ReplicationController/mysql
Containers:
  mysql:
    Container ID:   
    Image:      daocloud.io/library/mysql
    Image ID:       
    Port:       3306/TCP
    State:      Waiting
      Reason:       ContainerCreating
    Ready:      False
    Restart Count:  0
    Volume Mounts:  <none>
    Environment Variables:
      MYSQL_ROOT_PASSWORD:  123456
Conditions:
  Type      Status
  Initialized   True 
  Ready     False 
  PodScheduled  True 
No volumes.
QoS Class:  BestEffort
Tolerations:    <none>
Events:
  FirstSeen LastSeen    Count   From            SubObjectPath   Type        Reason      Message
  --------- --------    -----   ----            -------------   --------    ------      -------
  3m        3m      1   {default-scheduler }        Normal      Scheduled   Successfully assigned mysql-bwnlk to 127.0.0.1
  3m        20s     5   {kubelet 127.0.0.1}     Warning     FailedSync  Error syncing pod, skipping: failed to "StartContainer" for "POD" with ErrImagePull: "image pull failed for registry.access.redhat.com/rhel7/pod-infrastructure:latest, this may be because there are no credentials on this request.  details: (open /etc/docker/certs.d/registry.access.redhat.com/redhat-ca.crt: no such file or directory)"

  2m    8s  10  {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\""

  解決:

yum install *rhsm*

  安裝服務

#wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm
#rpm2cpio python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm | cpio -iv --to-stdout ./etc/rhsm/ca/redhat-uep.pem | tee /etc/rhsm/ca/redhat-uep.pem

  刪除原先的服務

kubectl delete -f mysql-rc.yaml
kubectl create -f mysql-rc.yaml

  

問題2:docker下載pull失敗問題

  1. 安裝阿里映象加速即可解決https://cr.console.aliyun.com/cn-hangzhou/mirrors

相關文章