1. 程式人生 > >kubernetes安裝並啟動mysql示例

kubernetes安裝並啟動mysql示例

kubernetes安裝並啟動mysql示例

  1. 安裝kubernetes
# systemctl disable firewalld
# systemctl stop firewalld
# yum -y install etcd kubernetes
# vim /etc/sysconfig/docker
OPTIONS='--selinux-enabled=false --insecure-registry gcr.io'
# vim /etc/kubernetes/apiserver 

 

刪除--admission-control引數中ServiceAccount

# 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

 

  1. 建立pod時報錯
apiVersion: v1
kind: ReplicationController
metadata: 
  name: mysql
spec:
  replicas: 1
  selector:
    app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql
        ports: 
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"

 

kubectl create -f mysql-rc.yaml

 Error syncing pod a3b95e2a-1a9a-11e8-812e-fa163ee5df35, 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)”

 

# yum install *rhsm* -y

 

依舊報錯

原因是docker映象拉取有問題

 vim /etc/docker/daemon.json 
 ```
 ```
{
   "registry-mirrors": ["https://registry.docker-cn.com"]      
}

 

替換為國內倉庫 
重啟docker

# systemctl daemon-reload
# systemctl restart docker

 

問題解決,pod建立完成 
3. 建立service

apiVersion: v1
kind: Service
metadata: 
  name: mysql1
spec:
  type: NodePort
  ports:
    - port: 3307
      nodePort: 32307
      targetPort: 3306
  selector: 
    app: mysql

 

Kubectl create -f mysql-svc.yaml

檢視狀態

# kubectl get po
NAME          READY     STATUS    RESTARTS   AGE
mysql-7hv93   1/1       Running   0          16m


# kubectl get svc
NAME         CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes   10.254.0.1      <none>        443/TCP          18m
mysql1       10.254.167.91   <nodes>       3307:32307/TCP   16m

# lsof -i:32307
COMMAND     PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
kube-prox 30418 root    9u  IPv6 1166802      0t0  TCP *:32307 (LISTEN)

連線主機ip加32307埠,訪問mysql成功