本教程將引導(dǎo)你如何安裝CRI-O(基于Open Container Initiative的Kubernetes Container Runtime Interface的實現(xiàn))以及創(chuàng)建運行在Pod中的Redis服務(wù)器储矩。
系統(tǒng)要求
ubntu16.04
安裝
需要安裝的組件:
- crio - 管理pods,實現(xiàn)了kubernetes的CRI
- crictl - CRI的client
- cni - 容器網(wǎng)絡(luò)接口
- runc - 啟動容器的OCI運行時
runc
下載runc
的二進制可執(zhí)行文件
wget https://github.com/opencontainers/runc/releases/download/v1.0.0-rc4/runc.amd64
設(shè)置可執(zhí)行權(quán)限并移動到指定的目錄
chmod +x runc.amd64
sudo mv runc.amd64 /usr/bin/runc
檢查runc的版本
runc --version
runc --version
runc version 1.0.0-rc4
spec: 1.0.0
cri-o
cri-o沒有release的二進制可執(zhí)行文件宠能,所以需要從源碼build。
下載Go安裝包
wget https://dl.google.com/go/go1.10.2.linux-amd64.tar.gz
安裝Go 1.10.2
tar -C /usr/local -zxf go1.10.2.linux-amd64.tar.gz
mkdir -p $HOME/go/src
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
Go應(yīng)該安裝好啦
go version
go version go1.10.2 linux/amd64
下載crictl
go get github.com/kubernetes-incubator/cri-tools/cmd/crictl
cd $GOPATH/src/github.com/kubernetes-incubator/cri-tools
make
make install
編譯可執(zhí)行文件
sudo apt-get update && apt-get install -y libglib2.0-dev \
libseccomp-dev \
libgpgme11-dev \
libdevmapper-dev \
make \
git
go get -d github.com/kubernetes-incubator/cri-o
cd $GOPATH/src/github.com/kubernetes-incubator/cri-o
make install.tools
make
make install
如果你是第一次安裝磁餐,要生成配置文件
make install.config
驗證注冊信息
如果沒有這些配置項,需要自己配置阿弃。
registries = ['registry.access.redhat.com', 'registry.fedoraproject.org', 'docker.io']
啟動進程crio
sudo sh -c 'echo "[Unit]
Description=OCI-based implementation of Kubernetes Container Runtime Interface
Documentation=https://github.com/kubernetes-incubator/cri-o
[Service]
ExecStart=/usr/local/bin/crio --log-level debug
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target" > /etc/systemd/system/crio.service'
systemctl daemon-reload
systemctl enable crio
systemctl start crio
檢查進程是否啟動成功
crictl --runtime-endpoint unix:///var/run/crio/crio.sock version
Version: 0.1.0
RuntimeName: cri-o
RuntimeVersion: 1.10.0-dev
RuntimeApiVersion: v1alpha1
cni 插件安裝
下載cni的源代碼
go get -d github.com/containernetworking/plugins
cd $GOPATH/src/github.com/containernetworking/plugins
編譯CNI
二進制文件
./build.sh
輸出信息
Building plugins
bandwidth
flannel
portmap
tuning
bridge
host-device
ipvlan
loopback
macvlan
ptp
vlan
dhcp
host-local
static
sample
安裝CNI
插件
mkdir -p /opt/cni/bin
cp bin/* /opt/cni/bin/
配置CNI
mkdir -p /etc/cni/net.d
sudo sh -c 'cat >/etc/cni/net.d/10-mynet.conf <<-EOF
{
"cniVersion": "0.2.0",
"name": "mynet",
"type": "bridge",
"bridge": "cni0",
"isGateway": true,
"ipMasq": true,
"ipam": {
"type": "host-local",
"subnet": "10.88.0.0/16",
"routes": [
{ "dst": "0.0.0.0/0" }
]
}
}
EOF'
sudo sh -c 'cat >/etc/cni/net.d/99-loopback.conf <<-EOF
{
"cniVersion": "0.2.0",
"type": "loopback"
}
EOF'
安裝skopeo-containers
sudo add-apt-repository ppa:projectatomic/ppa
sudo apt-get update
sudo apt-get install skopeo-containers -y
重啟cri-o來啟動CNI
systemctl restart crio
現(xiàn)在CNI
安裝并且配置好啦诊霹,使用10.88.0.0/16
的網(wǎng)段來分配給容器。
一切準(zhǔn)備就緒渣淳,現(xiàn)在我們可以創(chuàng)建pods脾还,接下來演示如何在pod中創(chuàng)建一個redis server。
創(chuàng)建一個pod
首先需要設(shè)置pod sandbox的配置
cd $GOPATH/src/github.com/kubernetes-incubator/cri-o
mkdir /etc/containers/
cp test/policy.json /etc/containers
創(chuàng)建pod并且獲得pod的ID
POD_ID=$(sudo crictl runp test/testdata/sandbox_config.json)
用crictl查看pod的信息
sudo crictl inspectp --output table $POD_ID
輸出信息
ID: f66df6126fe9a2e4cf056598afeb1c13a54568c850ead8582924b47b0decc128
Name: podsandbox1
UID: redhat-test-crio
Namespace: redhat.test.crio
Attempt: 1
Status: SANDBOX_READY
Created: 2018-05-17 03:25:19.327406442 +0000 UTC
IP Address: 10.88.0.2
Labels:
group -> test
io.kubernetes.container.name -> POD
Annotations:
owner -> hmeng
security.alpha.kubernetes.io/seccomp/pod -> unconfined
Info: map[version:{"version":"1.11.0-dev"}]
在pod中創(chuàng)建redis容器
用crictl
拉取redis鏡像入愧,根據(jù)配置文件創(chuàng)建redis容器鄙漏,并且附著到之前創(chuàng)建的pod中。
crictl pull quay.io/crio/redis:alpine
CONTAINER_ID=$(sudo crictl create $POD_ID test/testdata/container_redis.json test/testdata/sandbox_config.json)
啟動redis容器
crictl start $CONTAINER_ID
查看容器的日志
crictl inspect $CONTAINER_ID
{
"status": {
"id": "90688b328b0345e682d407ca99472e30bda036d9a0803878328ee43c2b5cf11b",
"metadata": {
"attempt": 0,
"name": "podsandbox1-redis"
},
"state": "CONTAINER_RUNNING",
"createdAt": "2018-05-17T03:26:28.363615962Z",
"startedAt": "2018-05-17T03:26:36.152514904Z",
"finishedAt": "1970-01-01T00:00:00Z",
"exitCode": 0,
"image": {
"image": "quay.io/crio/redis:alpine"
},
"imageRef": "quay.io/crio/redis@sha256:1780b5a5496189974b94eb2595d86731d7a0820e4beb8ea770974298a943ed55",
"reason": "",
"message": "",
"labels": {
"tier": "backend"
},
"annotations": {
"pod": "podsandbox1"
},
"mounts": null,
"logPath": "/var/log/crio/pods/f66df6126fe9a2e4cf056598afeb1c13a54568c850ead8582924b47b0decc128/90688b328b0345e682d407ca99472e30bda036d9a0803878328ee43c2b5cf11b.log"
}
}
測試redis容器
連接容器
telnet 10.88.0.2 6379
Trying 10.88.0.2...
Connected to 10.88.0.2.
Escape character is '^]'.
根據(jù)提示出入MONITOR
Trying 10.88.0.2...
Connected to 10.88.0.2.
Escape character is '^]'.
MONITOR
+OK
輸入ctrl+ 」
和quit
退出連接
^]
telnet> quit
Connection closed.
查看redis的日志
journalctl -u crio --no-pager
停止并且刪除容器
crictl stop $CONTAINER_ID
crictl rm $CONTAINER_ID
crictl stopp $POD_ID
crictl rmp $POD_ID
查看是否刪除
crictl pods
crictl ps